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