-
Notifications
You must be signed in to change notification settings - Fork 7
/
jwt.go
44 lines (35 loc) · 769 Bytes
/
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
package devicecheck
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"time"
jose "github.com/dvsekhvalnov/jose2go"
)
type jwt struct {
issuer string
keyID string
}
func newJWT(issuer, keyID string) jwt {
return jwt{
issuer: issuer,
keyID: keyID,
}
}
func (jwt jwt) generate(key *ecdsa.PrivateKey) (string, error) {
claims := map[string]interface{}{
"iss": jwt.issuer,
"iat": time.Now().UTC().Unix(),
}
// Ignoring error, because json.Marshal never fails.
claimsJSON, _ := json.Marshal(claims)
headers := map[string]interface{}{
"alg": jose.ES256,
"kid": jwt.keyID,
}
token, err := jose.Sign(string(claimsJSON), jose.ES256, key, jose.Headers(headers))
if err != nil {
return "", fmt.Errorf("jose: %w", err)
}
return token, nil
}