-
Notifications
You must be signed in to change notification settings - Fork 3
/
authenticate.go
62 lines (39 loc) · 1.13 KB
/
authenticate.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
package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/romana/rlog"
"github.com/dgrijalva/jwt-go"
)
func authenticate(tokenString string) bool {
rlog.Debug("authenticate() function invoked for token: " + tokenString)
var isValid bool
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
secret := []byte(JWTSecret)
return secret, nil
})
if err != nil {
return false
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
if REDISCLIENT.Get(tokenString).Err() == redis.Nil {
REDISCLIENT.Set(tokenString, claims["cxs"], 0)
}
if REDISCLIENT.Get(tokenString+"_rxt").Err() == redis.Nil {
REDISCLIENT.Set(tokenString+"_rxt", claims["rxt"], 0)
}
isValid = true
} else {
if REDISCLIENT.Get(tokenString).Err() != redis.Nil {
REDISCLIENT.Del(tokenString)
}
if REDISCLIENT.Get(tokenString+"_rxt").Err() != redis.Nil {
REDISCLIENT.Del(tokenString + "_rxt")
}
isValid = false
}
return isValid
}