-
Notifications
You must be signed in to change notification settings - Fork 5
/
tokenRepository.go
110 lines (88 loc) · 2.84 KB
/
tokenRepository.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2021 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package repository
import (
"encoding/json"
"time"
"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
)
var token iamclientmodels.OauthmodelTokenResponseV3
type TokenRepository interface {
Store(accessToken interface{}) error
GetToken() (*iamclientmodels.OauthmodelTokenResponseV3, error)
RemoveToken() error
TokenIssuedTimeUTC() time.Time
}
func GetRefreshExpiresIn(repository TokenRepository) (int32, error) {
getToken, errGet := repository.GetToken()
if errGet != nil {
return 0, errGet
}
return getToken.RefreshExpiresIn, nil
}
func GetExpiresIn(repository TokenRepository) (*int32, error) {
getToken, errGet := repository.GetToken()
if errGet != nil {
return nil, errGet
}
token = *getToken
return token.ExpiresIn, nil
}
func GetRefreshToken(repository TokenRepository) (string, error) {
getToken, errGet := repository.GetToken()
if errGet != nil {
return "", errGet
}
return getToken.RefreshToken, nil
}
func GetSecondsTillExpiry(repository TokenRepository, refreshRate float64) time.Duration {
if !HasToken(repository) {
return 0
}
get, err := GetExpiresIn(repository)
if err != nil {
return 0
}
withRefreshRate := float64(*get) * refreshRate
expiresAt := repository.TokenIssuedTimeUTC().Add(time.Duration(withRefreshRate) * time.Second)
timeNow := time.Now().UTC()
secondsTillExpiry := expiresAt.Sub(timeNow) // in seconds ex 1m6.995968173s
return secondsTillExpiry
}
func HasToken(repository TokenRepository) bool {
_, err := repository.GetToken()
return err == nil
}
func HasTokenExpired(repository TokenRepository, refreshRate float64) bool {
x := GetSecondsTillExpiry(repository, refreshRate) <= 0
return x
}
func GetSecondsTillExpiryRefresh(repository TokenRepository, refreshRate float64) time.Duration {
if !HasToken(repository) {
return 0
}
get, err := GetRefreshExpiresIn(repository)
if err != nil {
return 0
}
withRefreshRate := float64(get) * refreshRate
expiresAt := repository.TokenIssuedTimeUTC().Add(time.Duration(withRefreshRate) * time.Second)
secondsTillExpiry := expiresAt.Sub(time.Now().UTC()) // in seconds ex 1m6.995968173s
return secondsTillExpiry
}
func HasRefreshTokenExpired(repository TokenRepository, refreshRate float64) bool {
return GetSecondsTillExpiryRefresh(repository, refreshRate) <= 0
}
func ConvertInterfaceToModel(tokenInterface interface{}) (*iamclientmodels.OauthmodelTokenResponseV3, error) {
tmpToken, errMarshal := json.Marshal(tokenInterface)
if errMarshal != nil {
return nil, errMarshal
}
token := &iamclientmodels.OauthmodelTokenResponseV3{}
errUnmarshal := json.Unmarshal(tmpToken, &token)
if errUnmarshal != nil {
return nil, errUnmarshal
}
return token, nil
}