-
Notifications
You must be signed in to change notification settings - Fork 1
/
usertoken.go
41 lines (33 loc) · 1.01 KB
/
usertoken.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
package usertoken
import (
jwt "github.com/dgrijalva/jwt-go"
"github.com/hashicorp/errwrap"
)
// JSONWebToken for the user
type JSONWebToken struct {
Email string `json:"email"`
UID string `json:"sub"`
}
type jsonWebToken JSONWebToken
// Valid function for the JWT token
func (j jsonWebToken) Valid() error {
return nil
}
// ParseUnsignedJSONWebToken to retrieve an user info without checking signature
func ParseUnsignedJSONWebToken(accessToken string) (JSONWebToken, error) {
var claims = jsonWebToken{}
_, err := jwt.ParseWithClaims(accessToken, &claims, keyFunc)
err = filterInvalidSignatureError(err)
return JSONWebToken(claims), err
}
func keyFunc(token *jwt.Token) (interface{}, error) {
return []byte{}, nil
}
func filterInvalidSignatureError(err error) error {
// if only the bitmask for the 'signature invalid' is detected, ignore
ev, ok := err.(*jwt.ValidationError)
if ok && ev.Errors == jwt.ValidationErrorSignatureInvalid {
return nil
}
return errwrap.Wrapf("Error parsing token: {{err}}", err)
}