forked from dgrijalva/jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyfunc.go
27 lines (24 loc) · 1.08 KB
/
keyfunc.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
package jwt
import "fmt"
// Keyfunc is the type passed to Parse methods to supply
// the key for verification. The function receives the parsed,
// but unverified Token. This allows you to use properties in the
// Header of the token (such as `kid`) to identify which key to use.
//
// Each signing method accepts a different type of key. See the documentation
// for the signing method you're using to ensure you have the correct key type.
// Examples:
// - SigningMethodRSA ('RS256', etc) takes *rsa.PublicKey
// - SigningMethodHSA ('HS256', etc) takes []byte
type Keyfunc func(*Token) (interface{}, error)
// KnownKeyfunc is a helper for generating a Keyfunc from a known
// signing method and key. If your implementation only supports a single signing method
// and key, this is for you.
func KnownKeyfunc(signingMethod SigningMethod, key interface{}) Keyfunc {
return func(t *Token) (interface{}, error) {
if signingMethod.Alg() != t.Header["alg"] {
return nil, fmt.Errorf("unexpected signing method: %v, expected: %v", t.Header["alg"], signingMethod.Alg())
}
return key, nil
}
}