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 // DefaultBranch returns the repository's default branch.
45 DefaultBranch(repo string) (string, error)
46 // SetDefaultBranch sets the default branch for a repository.
47 SetDefaultBranch(repo string, branch string) error
48
49 // Description returns the repo's description.
50 Description(repo string) string
51 // SetDescription sets the repo's description.
52 SetDescription(repo, desc string) error
53 // IsPrivate returns true if the repository is private.
54 IsPrivate(repo string) bool
55 // SetPrivate sets the repository's private status.
56 SetPrivate(repo string, priv bool) error
57
58 // IsCollaborator returns true if the authorized key is a collaborator on the repository.
59 IsCollaborator(pk ssh.PublicKey, repo string) bool
60 // AddCollaborator adds the authorized key as a collaborator on the repository.
61 AddCollaborator(pk ssh.PublicKey, repo string) error
62 // IsAdmin returns true if the authorized key is an admin.
63 IsAdmin(pk ssh.PublicKey) bool
64 // AddAdmin adds the authorized key as an admin.
65 AddAdmin(pk ssh.PublicKey) error
66}
67
68// ParseAuthorizedKey parses an authorized key string into a public key.
69func ParseAuthorizedKey(ak string) (ssh.PublicKey, string, error) {
70 pk, c, _, _, err := ssh.ParseAuthorizedKey([]byte(ak))
71 return pk, c, err
72}
73
74// MarshalAuthorizedKey marshals a public key into an authorized key string.
75//
76// This is the inverse of ParseAuthorizedKey.
77// This function is a copy of ssh.MarshalAuthorizedKey, but without the trailing newline.
78// It returns an empty string if pk is nil.
79func MarshalAuthorizedKey(pk ssh.PublicKey) string {
80 if pk == nil {
81 return ""
82 }
83 return string(bytes.TrimSuffix(ssh.MarshalAuthorizedKey(pk), []byte("\n")))
84}