Reog and implement sqlite
This commit is contained in:
parent
30cdf2c7e7
commit
61aa1be730
9 changed files with 223 additions and 76 deletions
45
keydirectory/in_memory_directory.go
Normal file
45
keydirectory/in_memory_directory.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
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
|
||||
}
|
15
keydirectory/key_ids.go
Normal file
15
keydirectory/key_ids.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package keydirectory
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
func generateKeyId() (string, error) {
|
||||
uuid, err := uuid.NewRandom()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
keyId := uuid.String()
|
||||
|
||||
return keyId, nil
|
||||
}
|
|
@ -1,9 +1,33 @@
|
|||
package keydirectory
|
||||
|
||||
import "crypto"
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
|
||||
type KeyEntry struct {
|
||||
"github.com/common-fate/httpsig/alg_ed25519"
|
||||
"github.com/common-fate/httpsig/verifier"
|
||||
)
|
||||
|
||||
type keyEntry struct {
|
||||
Alg string
|
||||
PublicKey crypto.PublicKey
|
||||
UserId string
|
||||
}
|
||||
|
||||
func (k keyEntry) toAlg() (verifier.Algorithm, error) {
|
||||
var alg verifier.Algorithm
|
||||
var err error
|
||||
|
||||
switch k.Alg {
|
||||
case "ed25519":
|
||||
alg = alg_ed25519.Ed25519{
|
||||
PublicKey: k.PublicKey.(ed25519.PublicKey),
|
||||
Attrs: k.UserId,
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown algoritm: %s", k.Alg)
|
||||
}
|
||||
|
||||
return alg, err
|
||||
}
|
||||
|
|
110
keydirectory/sqlite.go
Normal file
110
keydirectory/sqlite.go
Normal file
|
@ -0,0 +1,110 @@
|
|||
package keydirectory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/common-fate/httpsig/verifier"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type dbWrapper struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func InitSqlite(dbPath string) (*dbWrapper, error) {
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
|
||||
if err != nil {
|
||||
return &dbWrapper{}, err
|
||||
}
|
||||
|
||||
createStmt := `
|
||||
create table
|
||||
if not exists
|
||||
keys(keyId text not null primary key, userId text, alg text, publicKey blob)
|
||||
`
|
||||
|
||||
_, err = db.Exec(createStmt)
|
||||
|
||||
if err != nil {
|
||||
return &dbWrapper{}, err
|
||||
}
|
||||
|
||||
return &dbWrapper{db}, nil
|
||||
}
|
||||
|
||||
func (dir *dbWrapper) GetKey(ctx context.Context, keyId string, _ string) (verifier.Algorithm, error) {
|
||||
db := dir.db
|
||||
|
||||
query := "select userId, alg, publicKey from keys where keyId = ?"
|
||||
|
||||
stmt, err := db.Prepare(query)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer stmt.Close()
|
||||
|
||||
var userId string
|
||||
var alg string
|
||||
var keyBytes []byte
|
||||
|
||||
row := stmt.QueryRow(keyId)
|
||||
|
||||
err = row.Scan(&userId, &alg, &keyBytes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var publicKey crypto.PublicKey
|
||||
|
||||
switch alg {
|
||||
case "ed25519":
|
||||
publicKey = ed25519.PublicKey(keyBytes)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown algorithm: %s", alg)
|
||||
}
|
||||
|
||||
keyEntry := keyEntry{
|
||||
Alg: alg,
|
||||
UserId: userId,
|
||||
PublicKey: publicKey,
|
||||
}
|
||||
|
||||
return keyEntry.toAlg()
|
||||
}
|
||||
|
||||
func (dir *dbWrapper) RegisterKey(key crypto.PublicKey, alg string, userId string) (string, error) {
|
||||
db := dir.db
|
||||
|
||||
keyId, err := generateKeyId()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stmt := "insert into keys(keyId, userId, alg, publicKey) values (?, ?, ?, ?)"
|
||||
|
||||
var keyBytes []byte
|
||||
|
||||
switch alg {
|
||||
case "ed25519":
|
||||
keyBytes = []byte(key.(ed25519.PublicKey))
|
||||
default:
|
||||
return "", fmt.Errorf("unknown algorithm: %s", alg)
|
||||
}
|
||||
|
||||
_, err = db.Exec(stmt, keyId, userId, alg, keyBytes)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return keyId, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue