-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
59 lines (47 loc) · 1.41 KB
/
Copy pathtoken.go
File metadata and controls
59 lines (47 loc) · 1.41 KB
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
package main
import (
"context"
jwt "github.com/golang-jwt/jwt"
"github.com/sirupsen/logrus"
"gopkg.in/oauth2.v4"
cfg "github.com/AVENTER-UG/go-avauth-oauth2/types"
)
type JWTGenerator struct {
SignedKey []byte
}
func (a *JWTGenerator) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) {
logrus.Debug("Token: LoggedIn: ", data)
signingKey := []byte(JwtSignKey)
// Create the Claims
myClaims := &cfg.CustomClaims{
ClientID: data.UserID,
Type: "user",
StandardClaims: jwt.StandardClaims{
Audience: data.Client.GetID(),
Subject: data.UserID,
ExpiresAt: data.TokenInfo.GetAccessCreateAt().Add(data.TokenInfo.GetAccessExpiresIn()).Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, myClaims)
access, err = token.SignedString(signingKey)
if err != nil {
logrus.Debug("newToken = ", err)
}
return
}
// Check if the token is valid
// return true and false and if its true then also the clientID of the token
func validToken(token string) (bool, *cfg.CustomClaims) {
newToken, err := jwt.ParseWithClaims(token, &cfg.CustomClaims{}, func(newToken *jwt.Token) (interface{}, error) {
return []byte(JwtSignKey), nil
})
if err != nil {
logrus.Error("validToken: ", err)
return false, nil
}
if claims, ok := newToken.Claims.(*cfg.CustomClaims); ok && newToken.Valid {
return true, claims
} else {
return false, nil
}
}