-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
39 lines (32 loc) · 1007 Bytes
/
parser.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
package auth
import (
"crypto"
"github.com/dgrijalva/jwt-go"
)
// Parser represents a set of methods for parsing and validating a JWT against a public key
type Parser struct {
public crypto.PublicKey
fn PublicKeyFunc
}
// NewParser returns a new parser with a public key.
func NewParser(pk crypto.PublicKey, fn PublicKeyFunc) *Parser {
return &Parser{public: pk, fn: fn}
}
// NewParserFromPEM will take a PEM and derive the public key from it and instantiate a parser.
func NewParserFromPEM(key []byte, fn PublicKeyFunc) (*Parser, error) {
public, err := PublicKeyFromPEM(key)
if err != nil {
return nil, err
}
return NewParser(public, fn), nil
}
// PublicKeyFunc is used to parse tokens using a public key.
type PublicKeyFunc func(crypto.PublicKey) jwt.Keyfunc
// Parse takes a string and returns a valid jwt token
func (p *Parser) Parse(raw string, claims jwt.Claims) error {
_, err := jwt.ParseWithClaims(raw, claims, p.fn(p.public))
if err != nil {
return err
}
return nil
}