2025-02-20 20:45:49 -05:00
|
|
|
package keydirectory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/common-fate/httpsig/verifier"
|
|
|
|
)
|
|
|
|
|
|
|
|
type inMemoryDirectory struct {
|
|
|
|
records map[string]keyEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateMemoryDirectory() inMemoryDirectory {
|
|
|
|
return inMemoryDirectory{
|
|
|
|
records: map[string]keyEntry{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dir inMemoryDirectory) GetKey(ctx context.Context, keyId string, _ string) (verifier.Algorithm, error) {
|
|
|
|
entry, ok := dir.records[keyId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("key not found in directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.toAlg()
|
|
|
|
}
|
|
|
|
|
2025-02-20 22:53:18 -05:00
|
|
|
func (dir inMemoryDirectory) RegisterKey(key crypto.PublicKey, userId string) (string, error) {
|
2025-02-20 20:45:49 -05:00
|
|
|
keyId, err := generateKeyId()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.records[keyId] = keyEntry{
|
|
|
|
PublicKey: key,
|
|
|
|
UserId: userId,
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyId, nil
|
|
|
|
}
|