-
Notifications
You must be signed in to change notification settings - Fork 5
/
token.go
70 lines (59 loc) · 2.46 KB
/
token.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
63
64
65
66
67
68
69
70
package data
import (
"time"
"github.com/google/uuid"
"github.com/wissance/Ferrum/utils/jsontools"
)
// RawUserInfo is a type that is using for place all public user data (in Keycloak - "info":{...} struct) into JWT encoded token
type RawUserInfo interface{}
// JwtCommonInfo - struct with all field for representing token in JWT format
type JwtCommonInfo struct {
IssuedAt time.Time `json:"iat"`
ExpiredAt time.Time `json:"exp"`
JwtId uuid.UUID `json:"jti"`
Type string `json:"typ"`
Issuer string `json:"iss"`
Audience string `json:"aud"`
Subject uuid.UUID `json:"sub"`
SessionState uuid.UUID `json:"session_state"`
SessionId uuid.UUID `json:"sid"`
Scope string `json:"scope"`
}
// TokenRefreshData is a JWT token with embedded just a common data (JwtCommonInfo)
type TokenRefreshData struct {
JwtCommonInfo
}
// AccessTokenData is a struct that stores data for build JWT access token (jwtCommonInfo, rawUserInfo) and result (ResultData, ResultJsonStr)
// this token = jwtCommonInfo + rawUserInfo
type AccessTokenData struct {
jwtCommonInfo JwtCommonInfo
rawUserInfo RawUserInfo
ResultData map[string]interface{}
ResultJsonStr string
}
// CreateAccessToken creates new AccessToken from common token data and public user info
func CreateAccessToken(commonData *JwtCommonInfo, userData User) *AccessTokenData {
token := AccessTokenData{jwtCommonInfo: *commonData, rawUserInfo: userData.GetUserInfo()}
token.Init()
return &token
}
// Valid is using for checking token fields values contains proper values, temporarily doesn't do anything
func (token *AccessTokenData) Valid() error {
// just pass formally, we don't have anything to validate, maybe in future
return nil
}
// CreateRefreshToken creates Refresh token
func CreateRefreshToken(commonData *JwtCommonInfo) *TokenRefreshData {
return &TokenRefreshData{JwtCommonInfo: *commonData}
}
// Valid is using for checking token fields values contains proper values, temporarily doesn't do anything
func (token *TokenRefreshData) Valid() error {
// just pass formally, we don't have anything to validate, maybe in future
return nil
}
// Init - combines 2 fields into map (ResultJsonStr) and simultaneously in a marshalled string ResultJsonStr
func (token *AccessTokenData) Init() {
data, str := jsontools.MergeNonIntersect[JwtCommonInfo, RawUserInfo](&token.jwtCommonInfo, &token.rawUserInfo)
token.ResultData = data.(map[string]interface{})
token.ResultJsonStr = str
}