-
Notifications
You must be signed in to change notification settings - Fork 3
/
issuer.go
124 lines (112 loc) · 3.02 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package auth
import (
"crypto/rand"
"crypto/rsa"
"os"
"time"
"github.com/LUSHDigital/uuid"
"github.com/dgrijalva/jwt-go"
)
const (
// DefaultValidPeriod is the default amount of minutes a token is valid
DefaultValidPeriod = time.Duration(60 * time.Minute)
)
// IssuerConfig is a set of data to configure an issuer
type IssuerConfig struct {
Name string
ValidPeriod time.Duration
TimeFunc func() time.Time
}
// Issuer represents a set of methods for generating a JWT with a private key
type Issuer struct {
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
name string
validPeriod time.Duration
timeFunc func() time.Time
}
// NewIssuerFromPrivateKeyPEM will take a private key PEM file and return a token issuer
func NewIssuerFromPrivateKeyPEM(cfg IssuerConfig, pem []byte) (*Issuer, error) {
pk, err := jwt.ParseRSAPrivateKeyFromPEM(pem)
if err != nil {
return nil, err
}
return NewIssuer(cfg, pk), nil
}
// NewIssuer returns a new JWT instance
func NewIssuer(cfg IssuerConfig, privateKey *rsa.PrivateKey) *Issuer {
if cfg.ValidPeriod < time.Nanosecond {
cfg.ValidPeriod = DefaultValidPeriod
}
now := cfg.TimeFunc
if now == nil {
now = time.Now
}
return &Issuer{
privateKey: privateKey,
publicKey: &privateKey.PublicKey,
name: cfg.Name,
validPeriod: cfg.ValidPeriod,
timeFunc: now,
}
}
// NewMockIssuer creates a new issuer with a random key pair.
func NewMockIssuer() (*Issuer, error) {
reader := rand.Reader
bitSize := 2048
privateKey, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
return nil, err
}
name, err := os.Hostname()
if err != nil {
return nil, err
}
return NewIssuer(IssuerConfig{
Name: name,
}, privateKey), nil
}
// NewMockIssuerWithTime creates a new issuer with a random key pair.
func NewMockIssuerWithTime(now func() time.Time) (*Issuer, error) {
reader := rand.Reader
bitSize := 2048
privateKey, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
return nil, err
}
name, err := os.Hostname()
if err != nil {
return nil, err
}
return NewIssuer(IssuerConfig{
Name: name,
TimeFunc: now,
}, privateKey), nil
}
// Issue generates and returns a JWT authentication token for a private key
func (i *Issuer) Issue(consumer *Consumer) (string, error) {
id, err := uuid.NewV4()
if err != nil {
return "", err
}
claims := Claims{
Consumer: *consumer,
StandardClaims: jwt.StandardClaims{
ExpiresAt: i.timeFunc().Add(i.validPeriod).Unix(),
IssuedAt: i.timeFunc().Unix(),
NotBefore: i.timeFunc().Unix(),
Issuer: i.name,
Id: id.String(),
},
}
return i.IssueWithClaims(claims)
}
// IssueWithClaims overrides the default claims and issues a JWT token for the a private key
func (i *Issuer) IssueWithClaims(claims Claims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
return token.SignedString(i.privateKey)
}
// Parser returns a parser based on the issuers private key's public counterpart
func (i *Issuer) Parser() *Parser {
return &Parser{publicKey: i.publicKey}
}