53 lines
1 KiB
Go
53 lines
1 KiB
Go
package sqlite_directory
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"crypto/ed25519"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/common-fate/httpsig/alg_ed25519"
|
|
"github.com/common-fate/httpsig/verifier"
|
|
|
|
"crispbyte.dev/sig-auth/keydirectory"
|
|
)
|
|
|
|
type InMemoryDirectory struct {
|
|
records map[string]keydirectory.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")
|
|
}
|
|
|
|
var alg verifier.Algorithm
|
|
var err error
|
|
|
|
switch entry.Alg {
|
|
case "ed25519":
|
|
alg = alg_ed25519.Ed25519{
|
|
PublicKey: entry.PublicKey.(ed25519.PublicKey),
|
|
Attrs: entry.UserId,
|
|
}
|
|
default:
|
|
err = fmt.Errorf("unknown algoritm: %s", entry.Alg)
|
|
}
|
|
|
|
return alg, err
|
|
}
|
|
|
|
func (dir InMemoryDirectory) RegisterKey(key crypto.PublicKey, alg string, userId string) (string, error) {
|
|
keyId := userId
|
|
|
|
dir.records[keyId] = keydirectory.KeyEntry{
|
|
Alg: alg,
|
|
PublicKey: key,
|
|
UserId: userId,
|
|
}
|
|
|
|
return keyId, nil
|
|
}
|