2025-02-17 19:55:53 -05:00
|
|
|
package keydirectory
|
|
|
|
|
2025-02-20 20:45:49 -05:00
|
|
|
import (
|
|
|
|
"crypto"
|
2025-02-20 22:53:18 -05:00
|
|
|
"crypto/ecdsa"
|
2025-02-20 20:45:49 -05:00
|
|
|
"crypto/ed25519"
|
2025-02-20 22:53:18 -05:00
|
|
|
"crypto/rsa"
|
2025-02-20 20:45:49 -05:00
|
|
|
"fmt"
|
2025-02-20 22:53:18 -05:00
|
|
|
"reflect"
|
2025-02-17 19:55:53 -05:00
|
|
|
|
2025-02-20 22:53:18 -05:00
|
|
|
"github.com/common-fate/httpsig/alg_ecdsa"
|
2025-02-20 20:45:49 -05:00
|
|
|
"github.com/common-fate/httpsig/alg_ed25519"
|
2025-02-20 22:53:18 -05:00
|
|
|
"github.com/common-fate/httpsig/alg_rsa"
|
2025-02-20 20:45:49 -05:00
|
|
|
"github.com/common-fate/httpsig/verifier"
|
|
|
|
)
|
|
|
|
|
|
|
|
type keyEntry struct {
|
2025-02-17 19:55:53 -05:00
|
|
|
PublicKey crypto.PublicKey
|
|
|
|
UserId string
|
|
|
|
}
|
2025-02-20 20:45:49 -05:00
|
|
|
|
|
|
|
func (k keyEntry) toAlg() (verifier.Algorithm, error) {
|
|
|
|
var alg verifier.Algorithm
|
|
|
|
var err error
|
|
|
|
|
2025-02-20 22:53:18 -05:00
|
|
|
switch k.PublicKey.(type) {
|
|
|
|
case ed25519.PublicKey:
|
2025-02-20 20:45:49 -05:00
|
|
|
alg = alg_ed25519.Ed25519{
|
|
|
|
PublicKey: k.PublicKey.(ed25519.PublicKey),
|
|
|
|
Attrs: k.UserId,
|
|
|
|
}
|
2025-02-20 22:53:18 -05:00
|
|
|
case *rsa.PublicKey:
|
|
|
|
alg = alg_rsa.RSAPKCS256{
|
|
|
|
PublicKey: k.PublicKey.(*rsa.PublicKey),
|
|
|
|
Attrs: k.UserId,
|
|
|
|
}
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
alg = alg_ecdsa.P256{
|
|
|
|
PublicKey: k.PublicKey.(*ecdsa.PublicKey),
|
|
|
|
Attrs: k.UserId,
|
|
|
|
}
|
2025-02-20 20:45:49 -05:00
|
|
|
default:
|
2025-02-20 22:53:18 -05:00
|
|
|
err = fmt.Errorf("unknown key type: %s", reflect.TypeOf(k.PublicKey))
|
2025-02-20 20:45:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return alg, err
|
|
|
|
}
|