/
ecdsa.go
60 lines (48 loc) · 1.47 KB
/
ecdsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package auth
import (
"crypto/ecdsa"
"crypto/ed25519"
"github.com/golang-jwt/jwt/v5"
)
type ECDSA struct {
*jwt.SigningMethodECDSA
privateKeyFunc func(string) (*ecdsa.PrivateKey, error)
publicKeyFunc func(string) (*ecdsa.PublicKey, error)
}
func NewECDSA(method *jwt.SigningMethodECDSA, s func(string) (*ecdsa.PrivateKey, error), v func(string) (*ecdsa.PublicKey, error)) *ECDSA {
return &ECDSA{
SigningMethodECDSA: method,
privateKeyFunc: s,
publicKeyFunc: v,
}
}
func (h *ECDSA) SecretKeyFunc(uid string) (*ecdsa.PrivateKey, error) {
return h.privateKeyFunc(uid)
}
func (h *ECDSA) VerifyKeyFunc(uid string) (*ecdsa.PublicKey, error) {
return h.publicKeyFunc(uid)
}
func (h *ECDSA) SigningMethod() *jwt.SigningMethodECDSA {
return h.SigningMethodECDSA
}
type Ed25519 struct {
*jwt.SigningMethodEd25519
privateKeyFunc func(string) (ed25519.PrivateKey, error)
publicKeyFunc func(string) (ed25519.PublicKey, error)
}
func NewEd25519(s func(string) (ed25519.PrivateKey, error), v func(string) (ed25519.PublicKey, error)) *Ed25519 {
return &Ed25519{
SigningMethodEd25519: jwt.SigningMethodEdDSA,
privateKeyFunc: s,
publicKeyFunc: v,
}
}
func (h *Ed25519) SecretKeyFunc(uid string) (ed25519.PrivateKey, error) {
return h.privateKeyFunc(uid)
}
func (h *Ed25519) VerifyKeyFunc(uid string) (ed25519.PublicKey, error) {
return h.publicKeyFunc(uid)
}
func (h *Ed25519) SigningMethod() *jwt.SigningMethodEd25519 {
return h.SigningMethodEd25519
}