-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
43 lines (35 loc) · 826 Bytes
/
signer.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
package gothic
import (
"github.com/dgrijalva/jwt-go"
"time"
)
type Signer struct {
Store KeyStore
Policy *Policy
}
func (signer Signer) Sign(id string, c interface{}) (string, error) {
t := jwt.New(jwt.GetSigningMethod(signer.Store.GetAlgorithm()))
key, err := signer.Store.GetKey(signer.Policy.Issuer)
if err != nil {
return "", err
}
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return "", err
}
context, err := toJSON(c)
if err != nil {
return "", err
}
t.Claims = &TokenClaims{
Context: context,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(signer.Policy.ExpiresIn).Unix(),
Issuer: signer.Policy.Issuer,
Subject: signer.Policy.Subject,
Audience: signer.Policy.Audience,
Id: id,
},
}
return t.SignedString(signKey)
}