forked from karalabe/go-bluesky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
45 lines (39 loc) · 1.17 KB
/
jwt.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
package bluesky
import (
"github.com/ureeves/jwt-go-secp256k1"
"github.com/golang-jwt/jwt/v5"
)
type es256k struct{}
// Verify verifies a signature generated by Sign.
//
// signingString is the concatenated header, payload, and separator.
// sig is the signature.
// key is the ECDSA private key in the PEM format.
//
// Returns an error if the signature cannot be verified.
func (e *es256k) Verify(signingString string, sig []byte, key interface{}) error {
return secp256k1.SigningMethodES256K.Verify(signingString, string(sig), key)
}
// Sign signs a signingString with a key.
//
// signingString is the concatenated header, payload, and separator.
// key is the ECDSA private key in the PEM format.
//
// Returns the signature as a byte slice, or an error if there is a problem
// signing.
func (e *es256k) Sign(signingString string, key interface{}) ([]byte, error) {
sig, err := secp256k1.SigningMethodES256K.Sign(signingString, key)
if err != nil {
return nil, err
}
return []byte(sig), nil
}
func (e *es256k) Alg() string {
return "ES256K"
}
func init() {
// Register ES256K signing method
jwt.RegisterSigningMethod("ES256K", func() jwt.SigningMethod {
return &es256k{}
})
}