-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuer.go
50 lines (41 loc) · 1.08 KB
/
issuer.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
package issuer
import (
"time"
"github.com/gotopia/watcher/keychain"
"github.com/dgrijalva/jwt-go"
)
const defaultDuration = 7 * 24 * time.Hour
// Issuer holds the issuer's internal state.
type Issuer struct {
uri string
keychain *keychain.Keychain
duration time.Duration
}
// New returns a new issuer.
func New(uri string, k *keychain.Keychain) *Issuer {
return &Issuer{
uri: uri,
keychain: k,
duration: defaultDuration,
}
}
// Sign a JSON web token for user.
func (i *Issuer) Sign(subject string) string {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
ExpiresAt: time.Now().Add(i.duration).Unix(),
Issuer: i.uri,
Subject: subject,
})
privateKey := i.keychain.RandKey()
token.Header["kid"] = privateKey.Kid
tokenString, _ := token.SignedString(privateKey.Key)
return tokenString
}
// SetDuration sets the valid duration of token.
func (i *Issuer) SetDuration(duration time.Duration) {
i.duration = duration
}
// Keychain returns the keychain used by the issuer.
func (i *Issuer) Keychain() *keychain.Keychain {
return i.keychain
}