-
Notifications
You must be signed in to change notification settings - Fork 3
/
issuer.go
60 lines (52 loc) · 1.53 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
package auth
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"time"
"github.com/dgrijalva/jwt-go"
)
// Issuer represents a set of methods for generating a JWT with a private key
type Issuer struct {
method jwt.SigningMethod
private crypto.PrivateKey
name string
valid time.Duration
}
type encodingFunc func(der []byte) (interface{}, error)
// NewIssuerFromPEM will take a private key PEM and derive the private key from it.
func NewIssuerFromPEM(key []byte, method jwt.SigningMethod) (*Issuer, error) {
private, err := PrivateKeyFromPEM(key)
if err != nil {
return nil, err
}
return NewIssuer(private, method), nil
}
// NewIssuerFromPEMWithPassword will take a private key PEM with a password and derive the private key from it.
func NewIssuerFromPEMWithPassword(key []byte, password string, method jwt.SigningMethod) (*Issuer, error) {
private, err := PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return nil, err
}
return NewIssuer(private, method), nil
}
// NewIssuer creates a new issuer.
func NewIssuer(private crypto.PrivateKey, method jwt.SigningMethod) *Issuer {
if method == nil {
switch private.(type) {
case *rsa.PrivateKey:
method = jwt.SigningMethodRS256
case *ecdsa.PrivateKey:
method = jwt.SigningMethodES256
}
}
return &Issuer{
method: method,
private: private,
}
}
// Issue will sign a JWT and return its string representation.
func (i *Issuer) Issue(claims jwt.Claims) (string, error) {
token := jwt.NewWithClaims(i.method, claims)
return token.SignedString(i.private)
}