-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.go
65 lines (52 loc) · 1.29 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package git
import (
"encoding/base64"
"fmt"
"log/slog"
"path/filepath"
"github.com/charmbracelet/soft-serve/pkg/utils"
"github.com/charmbracelet/ssh"
gossh "golang.org/x/crypto/ssh"
)
type Backend struct {
Logger *slog.Logger
DB *DB
Cfg *GitCfg
}
func (be *Backend) ReposDir() string {
return filepath.Join(be.Cfg.DataDir, "repos")
}
func (be *Backend) RepoName(id string) string {
return utils.SanitizeRepo(id)
}
func (be *Backend) RepoID(name string) string {
return name + ".git"
}
func (be *Backend) Pubkey(pk ssh.PublicKey) string {
return be.KeyForKeyText(pk)
}
func (be *Backend) KeyForFingerprint(pk ssh.PublicKey) string {
return gossh.FingerprintSHA256(pk)
}
func (be *Backend) PubkeyToPublicKey(pubkey string) (ssh.PublicKey, error) {
kk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
return kk, err
}
func (be *Backend) KeyForKeyText(pk ssh.PublicKey) string {
kb := base64.StdEncoding.EncodeToString(pk.Marshal())
return fmt.Sprintf("%s %s", pk.Type(), kb)
}
func (be *Backend) KeysEqual(pka, pkb string) bool {
return pka == pkb
}
func (be *Backend) IsAdmin(pk ssh.PublicKey) bool {
for _, apk := range be.Cfg.Admins {
if ssh.KeysEqual(pk, apk) {
return true
}
}
return false
}
func (be *Backend) IsPrOwner(pka, pkb int64) bool {
return pka == pkb
}