sig-auth.git

git clone https://git.crispbyte.dev/sig-auth.git

sig-auth.git / keydirectory
cheddar  ·  2025-02-21

in_memory_directory.go

 1package keydirectory
 2
 3import (
 4	"context"
 5	"crypto"
 6	"errors"
 7
 8	"github.com/common-fate/httpsig/verifier"
 9)
10
11type inMemoryDirectory struct {
12	records map[string]keyEntry
13}
14
15func CreateMemoryDirectory() inMemoryDirectory {
16	return inMemoryDirectory{
17		records: map[string]keyEntry{},
18	}
19}
20
21func (dir inMemoryDirectory) GetKey(ctx context.Context, keyId string, _ string) (verifier.Algorithm, error) {
22	entry, ok := dir.records[keyId]
23
24	if !ok {
25		return nil, errors.New("key not found in directory")
26	}
27
28	return entry.toAlg()
29}
30
31func (dir inMemoryDirectory) RegisterKey(key crypto.PublicKey, userId string) (string, error) {
32	keyId, err := generateKeyId()
33
34	if err != nil {
35		return "", err
36	}
37
38	dir.records[keyId] = keyEntry{
39		PublicKey: key,
40		UserId:    userId,
41	}
42
43	return keyId, nil
44}