1package backend
2
3import (
4 "bytes"
5 "context"
6
7 "github.com/charmbracelet/ssh"
8 gossh "golang.org/x/crypto/ssh"
9)
10
11// Backend is an interface that handles repositories management and any
12// non-Git related operations.
13type Backend interface {
14 SettingsBackend
15 RepositoryStore
16 RepositoryMetadata
17 RepositoryAccess
18 UserStore
19 UserAccess
20 Hooks
21
22 // WithContext returns a copy Backend with the given context.
23 WithContext(ctx context.Context) Backend
24}
25
26// ParseAuthorizedKey parses an authorized key string into a public key.
27func ParseAuthorizedKey(ak string) (gossh.PublicKey, string, error) {
28 pk, c, _, _, err := gossh.ParseAuthorizedKey([]byte(ak))
29 return pk, c, err
30}
31
32// MarshalAuthorizedKey marshals a public key into an authorized key string.
33//
34// This is the inverse of ParseAuthorizedKey.
35// This function is a copy of ssh.MarshalAuthorizedKey, but without the trailing newline.
36// It returns an empty string if pk is nil.
37func MarshalAuthorizedKey(pk gossh.PublicKey) string {
38 if pk == nil {
39 return ""
40 }
41 return string(bytes.TrimSuffix(gossh.MarshalAuthorizedKey(pk), []byte("\n")))
42}
43
44// KeysEqual returns whether the two public keys are equal.
45func KeysEqual(a, b gossh.PublicKey) bool {
46 return ssh.KeysEqual(a, b)
47}