-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
74 lines (62 loc) · 1.7 KB
/
jwt.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
package jwt
import (
"errors"
"time"
"github.com/dgrijalva/jwt-go"
)
var (
Expired = errors.New("token expired")
Invalid = errors.New("token is invalid")
)
type Claims struct {
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
LastName string `json:"lastname,omitempty"`
jwt.StandardClaims
}
func NewClaims(name, lastname, email string, expiration time.Duration) *Claims {
claims := &Claims{
Name: name,
LastName: lastname,
Email: email,
StandardClaims: jwt.StandardClaims{
// In JWT, the expiry time is expressed as unix milliseconds
ExpiresAt: time.Now().Add(expiration).Unix(),
},
}
return claims
}
func GenerateToken(name, lastname, email string, expirationTime time.Duration, privateKey string) (string, error) {
pKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKey))
if err != nil {
return "", err
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, NewClaims(name, lastname, email, expirationTime))
return token.SignedString(pKey)
}
func VerifyToken(token string, pubKey string) (*jwt.Token, error) {
publicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(pubKey))
if err != nil {
return nil, err
}
parser := jwt.Parser{
ValidMethods: []string{"RS256"},
}
parsedToken, err := parser.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) { return publicKey, nil })
switch err.(type) {
case nil: // no error
if !parsedToken.Valid {
return nil, Invalid
}
return parsedToken, nil
case *jwt.ValidationError:
vErr := err.(*jwt.ValidationError)
switch vErr.Errors {
case jwt.ValidationErrorExpired:
return parsedToken, Expired
}
return nil, Invalid
default:
return nil, Invalid
}
}