auth.go

 1package auth
 2
 3import (
 4	"context"
 5	"fmt"
 6
 7	"github.com/charmbracelet/soft-serve/server/sshutils"
 8	"golang.org/x/crypto/ssh"
 9)
10
11// Auth is an interface that represents a auth store.
12type Auth interface {
13	// Authenticate returns the user for the given auth method.
14	Authenticate(ctx context.Context, method AuthMethod) (User, error)
15}
16
17// AuthMethod is an interface that represents a auth method.
18type AuthMethod interface {
19	fmt.Stringer
20
21	// Name returns the name of the auth method.
22	Name() string
23}
24
25// PublicKey is a public-key auth method.
26type PublicKey struct {
27	ssh.PublicKey
28}
29
30// NewPublicKey returns a new PublicKey auth method from ssh.PublicKey.
31func NewPublicKey(pk ssh.PublicKey) PublicKey {
32	return PublicKey{PublicKey: pk}
33}
34
35var _ AuthMethod = PublicKey{}
36
37// String implements AuthMethod.
38func (pk PublicKey) String() string {
39	return sshutils.MarshalAuthorizedKey(pk.PublicKey)
40}
41
42// Name implements AuthMethod.
43func (PublicKey) Name() string {
44	return "ssh-public-key"
45}