-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt_methods.go
46 lines (40 loc) · 1019 Bytes
/
jwt_methods.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
package jwt
import (
"context"
"fmt"
"time"
"github.com/golang-jwt/jwt"
)
func (i jwtImpl) NewToken(jwtSecret, issuer, subject string, duration time.Duration) (string, error) {
now := time.Now()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Id: i.entity.NewID(),
Issuer: issuer,
Subject: subject,
IssuedAt: now.Unix(),
NotBefore: now.Unix(),
ExpiresAt: now.Add(duration).Unix(),
})
return token.SignedString([]byte(jwtSecret))
}
func (i jwtImpl) GetUserID(ctx context.Context) string {
claims, ok := ctx.Value("claims").(jwt.MapClaims)
if !ok {
return ""
}
return fmt.Sprintf("%s", claims["iss"])
}
func (i jwtImpl) GetUserEmail(ctx context.Context) string {
claims, ok := ctx.Value("claims").(jwt.MapClaims)
if !ok {
return ""
}
return fmt.Sprintf("%s", claims["sub"])
}
func (i jwtImpl) GetUserClaims(ctx context.Context) map[string]interface{} {
claims, ok := ctx.Value("claims").(jwt.MapClaims)
if !ok {
return nil
}
return claims
}