-
Notifications
You must be signed in to change notification settings - Fork 361
/
token.go
57 lines (50 loc) · 1.58 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
package auth
import (
"fmt"
"time"
"github.com/golang-jwt/jwt"
"github.com/rs/xid"
)
const (
ResetPasswordAudience = "reset_password"
)
func generateJWT(claims *jwt.StandardClaims, secret []byte) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(secret)
}
// GenerateJWTResetPassword creates a jwt token with the field subject set the email passed.
func GenerateJWTResetPassword(secret []byte, email string, issuedAt, expiresAt time.Time) (string, error) {
claims := &jwt.StandardClaims{
// xid is k-sorted which is important functionality to ensure
// easy deletion of tokens from the kv-store.
Id: xid.New().String(),
Audience: ResetPasswordAudience,
Subject: email,
IssuedAt: issuedAt.Unix(),
ExpiresAt: expiresAt.Unix(),
}
return generateJWT(claims, secret)
}
func VerifyToken(secret []byte, tokenString string) (*jwt.StandardClaims, error) {
claims := &jwt.StandardClaims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("%w: %s", ErrUnexpectedSigningMethod, token.Header["alg"])
}
return secret, nil
})
if err != nil || !token.Valid {
return nil, ErrInvalidToken
}
return claims, nil
}
func VerifyTokenWithAudience(secret []byte, token, audience string) (*jwt.StandardClaims, error) {
claims, err := VerifyToken(secret, token)
if err != nil {
return nil, err
}
if !claims.VerifyAudience(audience, true) {
return nil, ErrInvalidToken
}
return claims, nil
}