collabs.go

  1package sqlite
  2
  3import (
  4	"net/mail"
  5	"strconv"
  6
  7	"github.com/charmbracelet/soft-serve/proto"
  8	"github.com/charmbracelet/soft-serve/server/db/types"
  9	"golang.org/x/crypto/ssh"
 10)
 11
 12var _ proto.CollaboratorService = &Sqlite{}
 13
 14// AddCollaborator adds a collaborator to a repository.
 15func (d *Sqlite) AddCollaborator(repo string, collab proto.Collaborator) error {
 16	r, err := d.GetRepo(repo)
 17	if err != nil {
 18		return err
 19	}
 20	switch c := collab.(type) {
 21	}
 22}
 23
 24// RemoveCollaborator removes a collaborator from a repository.
 25func (d *Sqlite) RemoveCollaborator(repo string, collab proto.Collaborator) error {
 26	return nil
 27}
 28
 29// ListCollaborators lists the collaborators of a repository.
 30func (d *Sqlite) ListCollaborators(repo string) ([]proto.Collaborator, error) {
 31	return nil, nil
 32}
 33
 34type publicKey struct {
 35	key *types.PublicKey
 36}
 37
 38// PublicKey returns the collaborator's public key.
 39func (k publicKey) PublicKey() ssh.PublicKey {
 40	pk, err := ssh.ParsePublicKey([]byte(k.key.PublicKey))
 41	if err != nil {
 42		return nil
 43	}
 44	return pk
 45}
 46
 47var _ proto.PublicKeyCollaborator = &collaborator{}
 48var _ proto.UserLoginCollaborator = &collaborator{}
 49var _ proto.EmailCollaborator = &collaborator{}
 50
 51type collaborator struct {
 52	user *types.User
 53	keys []*types.PublicKey
 54	db   *Sqlite
 55}
 56
 57func (c *collaborator) init() {
 58	if c.keys != nil || len(c.keys) > 0 {
 59		return
 60	}
 61	ks, err := c.db.GetUserPublicKeys(c.user)
 62	if err != nil {
 63		return
 64	}
 65	c.keys = ks
 66}
 67
 68// Identifier returns the collaborator's identifier.
 69func (c *collaborator) Identifier() string {
 70	return strconv.Itoa(c.user.ID)
 71}
 72
 73// Name returns the collaborator's name.
 74func (c *collaborator) Name() string {
 75	return c.user.Name
 76}
 77
 78// String returns the collaborator's username.
 79func (c *collaborator) String() string {
 80	return c.user.Name
 81}
 82
 83// Marshal implements proto.PublicKeyCollaborator
 84func (c *collaborator) Marshal() []byte {
 85	c.init()
 86	pk := c.keys[0]
 87	return pk.Marshal()
 88}
 89
 90// Type implements proto.PublicKeyCollaborator
 91func (c *collaborator) Type() string {
 92	c.init()
 93	pk := c.keys[0]
 94	return pk.Type()
 95}
 96
 97// Verify implements proto.PublicKeyCollaborator
 98func (c *collaborator) Verify(data []byte, sig *ssh.Signature) error {
 99	c.init()
100	pk := c.keys[0]
101	return pk.Verify(data, sig)
102}
103
104// Login implements proto.UserLoginCollaborator
105func (c *collaborator) Login() string {
106	var login string
107	if c.user.Login != nil {
108		login = *c.user.Login
109	}
110	return login
111}
112
113// Address implements proto.EmailCollaborator
114func (c *collaborator) Address() mail.Address {
115	var addr string
116	if c.user.Email != nil {
117		addr = *c.user.Email
118	}
119	return mail.Address{
120		Name:    c.user.Name,
121		Address: addr,
122	}
123}
124
125// Password implements proto.UserLoginCollaborator
126func (c *collaborator) Password() string {
127	var pwd string
128	if c.user.Password != nil {
129		pwd = *c.user.Password
130	}
131	return pwd
132}