46 lines
820 B
Go
46 lines
820 B
Go
|
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()
|
||
|
}
|
||
|
|
||
|
func (dir inMemoryDirectory) RegisterKey(key crypto.PublicKey, alg string, userId string) (string, error) {
|
||
|
keyId, err := generateKeyId()
|
||
|
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
dir.records[keyId] = keyEntry{
|
||
|
Alg: alg,
|
||
|
PublicKey: key,
|
||
|
UserId: userId,
|
||
|
}
|
||
|
|
||
|
return keyId, nil
|
||
|
}
|