forked from corestoreio/parrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
86 lines (74 loc) · 2.33 KB
/
token.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
package auth
import (
"fmt"
"github.com/parrot-translate/parrot/parrot-api/model"
jwt "github.com/dgrijalva/jwt-go"
)
// TokenProvider holds the Auth Provider's name and Signing Key.
type TokenProvider struct {
Name string
SigningKey []byte
}
// AuthStore is the interface that an Auth Provider implementation requires to retrieve
// and validate credentials in order to issue a token.
type AuthStore interface {
model.UserStorer
model.ProjectClientStorer
Ping() error
Close() error
}
// CreateToken creates a new token with the provided claims and signs it.
func (p *TokenProvider) CreateToken(claims jwt.Claims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(p.SigningKey)
}
// ParseAndVerifyToken parses, verifies and validates the claims of the token.
// It returns the claims or an error if the token is not valid or if something went wrong.
func (p *TokenProvider) ParseAndVerifyToken(tokenString string) (jwt.MapClaims, error) {
claims, err := p.ParseAndExtractClaims(tokenString)
if err != nil {
return nil, err
}
if err := claims.Valid(); err != nil {
return nil, err
}
return claims, nil
}
// ParseAndExtractClaims parses the claims of the token and its signature without validating the claims.
// It returns the claims or an error.
func (p *TokenProvider) ParseAndExtractClaims(tokenString string) (jwt.MapClaims, error) {
token, err := parseToken(tokenString, p.SigningKey)
if err != nil {
return nil, err
}
claims, err := extractClaims(token)
if err != nil {
return nil, err
}
return claims, nil
}
// parseToken parses and verifies the signature of the token.
// It returns the parsed token or an error.
func parseToken(tokenString string, signingKey []byte) (*jwt.Token, error) {
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method")
}
return signingKey, nil
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, fmt.Errorf("invalid token")
}
return token, nil
}
// extractClaims extracts the claims from the parsed token.
func extractClaims(token *jwt.Token) (jwt.MapClaims, error) {
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, fmt.Errorf("invalid token")
}
return claims, nil
}