-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
45 lines (35 loc) · 1.1 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
package server
import (
"fmt"
"strconv"
"github.com/athomecomar/xerrors"
"github.com/dgrijalva/jwt-go"
"google.golang.org/grpc/status"
)
func claimJwt(token string, secretFn func() string) (jwt.MapClaims, error) {
claimableToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing alg: %v", token.Header["alg"])
}
return []byte(secretFn()), nil
})
if err != nil {
return nil, status.Errorf(xerrors.InvalidArgument, "jwt isnt parsable: %v", err)
}
claims, ok := claimableToken.Claims.(jwt.MapClaims)
if !ok {
return nil, status.Error(xerrors.FailedPrecondition, "jwt isnt claimable")
}
if !claimableToken.Valid {
return nil, status.Errorf(xerrors.Unauthenticated, "claimable Jwt.Valid: %v", err)
}
if err := claims.Valid(); err != nil {
return nil, status.Errorf(xerrors.Unauthenticated, "claimed jwt.Valid: %v", err)
}
return claims, nil
}
func userIdToKey(uid uint64) string {
return strconv.Itoa(int(uid))
}
const accessKey = "access"
const refreshKey = "refresh"