-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
49 lines (41 loc) · 1.12 KB
/
auth.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
package auth
import (
"fmt"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
// GenerateToken generates a token with the username stored.
func GenerateToken(username string, secret string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
c := jwt.MapClaims{
"username": username,
"exp": time.Now().Add(time.Hour * 24 * 7),
}
token.Claims = c
r, err := token.SignedString([]byte(secret))
if err != nil {
return "", err
}
return r, nil
}
// ValidateToken checks whether the token is valid and returns the username.
func ValidateToken(tokenString, secret string) (string, error) {
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
_, ok := t.Method.(*jwt.SigningMethodHMAC)
if !ok {
return nil, fmt.Errorf("auth: unexpected signing method: %v", t.Header["alg"])
}
return []byte(secret), nil
})
if err != nil {
return "", err
}
if !token.Valid {
return "", fmt.Errorf("auth: token is invalid")
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return "", fmt.Errorf("auth: could not extract claims from token")
}
return claims["username"].(string), nil
}