1package backend
2
3import (
4 "bytes"
5
6 "golang.org/x/crypto/ssh"
7)
8
9// Backend is an interface that handles repositories management and any
10// non-Git related operations.
11type Backend interface {
12 // ServerName returns the server's name.
13 ServerName() string
14 // SetServerName sets the server's name.
15 SetServerName(name string) error
16 // ServerHost returns the server's host.
17 ServerHost() string
18 // SetServerHost sets the server's host.
19 SetServerHost(host string) error
20 // ServerPort returns the server's port.
21 ServerPort() string
22 // SetServerPort sets the server's port.
23 SetServerPort(port string) error
24
25 // AnonAccess returns the access level for anonymous users.
26 AnonAccess() AccessLevel
27 // SetAnonAccess sets the access level for anonymous users.
28 SetAnonAccess(level AccessLevel) error
29 // AllowKeyless returns true if keyless access is allowed.
30 AllowKeyless() bool
31 // SetAllowKeyless sets whether or not keyless access is allowed.
32 SetAllowKeyless(allow bool) error
33
34 // Repository finds the given repository.
35 Repository(repo string) (Repository, error)
36 // Repositories returns a list of all repositories.
37 Repositories() ([]Repository, error)
38 // CreateRepository creates a new repository.
39 CreateRepository(name string, private bool) (Repository, error)
40 // DeleteRepository deletes a repository.
41 DeleteRepository(name string) error
42 // RenameRepository renames a repository.
43 RenameRepository(oldName, newName string) error
44
45 // Description returns the repo's description.
46 Description(repo string) string
47 // SetDescription sets the repo's description.
48 SetDescription(repo, desc string) error
49 // IsPrivate returns true if the repository is private.
50 IsPrivate(repo string) bool
51 // SetPrivate sets the repository's private status.
52 SetPrivate(repo string, priv bool) error
53
54 // IsCollaborator returns true if the authorized key is a collaborator on the repository.
55 IsCollaborator(pk ssh.PublicKey, repo string) bool
56 // AddCollaborator adds the authorized key as a collaborator on the repository.
57 AddCollaborator(pk ssh.PublicKey, repo string) error
58 // IsAdmin returns true if the authorized key is an admin.
59 IsAdmin(pk ssh.PublicKey) bool
60 // AddAdmin adds the authorized key as an admin.
61 AddAdmin(pk ssh.PublicKey) error
62}
63
64// ParseAuthorizedKey parses an authorized key string into a public key.
65func ParseAuthorizedKey(ak string) (ssh.PublicKey, string, error) {
66 pk, c, _, _, err := ssh.ParseAuthorizedKey([]byte(ak))
67 return pk, c, err
68}
69
70// MarshalAuthorizedKey marshals a public key into an authorized key string.
71//
72// This is the inverse of ParseAuthorizedKey.
73// This function is a copy of ssh.MarshalAuthorizedKey, but without the trailing newline.
74// It returns an empty string if pk is nil.
75func MarshalAuthorizedKey(pk ssh.PublicKey) string {
76 if pk == nil {
77 return ""
78 }
79 return string(bytes.TrimSuffix(ssh.MarshalAuthorizedKey(pk), []byte("\n")))
80}