-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth_info.go
executable file
·225 lines (197 loc) · 6.17 KB
/
auth_info.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package auth
import (
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient"
"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient/o_auth2_0"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/constant"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/factory"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/repository"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
)
type Session struct {
Token repository.TokenRepository
Config repository.ConfigRepository
Refresh repository.RefreshTokenRepository
}
var Once utils.Once
// AuthInfoWriter called by the existing security from the wrapper
func AuthInfoWriter(s Session, outerValues [][]string, key string) runtime.ClientAuthInfoWriter {
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
var errors []string
for _, innerValue := range outerValues {
success := true
for _, value := range innerValue {
switch value {
case constant.BearerAuth:
err := TokenRepo(s).AuthenticateRequest(r, nil)
if err != nil {
errors = append(errors, err.Error())
success = false
break
}
case constant.BasicAuth:
err := ConfigRepo(s).AuthenticateRequest(r, nil)
if err != nil {
errors = append(errors, err.Error())
success = false
break
}
case constant.CookieAuth:
err := Cookie(s, key).AuthenticateRequest(r, nil)
if err != nil {
errors = append(errors, err.Error())
success = false
break
}
}
}
if success {
return nil
}
}
aggregatedErr := fmt.Errorf(strings.Join(errors, ""))
return fmt.Errorf("failed to find an existing authorization. %s", aggregatedErr)
})
}
func ConfigRepo(s Session) runtime.ClientAuthInfoWriter {
clientID := s.Config.GetClientId()
if clientID == "" {
return Error(fmt.Errorf("empty clientID"))
}
clientSecret := s.Config.GetClientSecret()
return Basic(clientID, clientSecret)
}
func Basic(username, password string) runtime.ClientAuthInfoWriter {
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
encoded := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
return r.SetHeaderParam(constant.Authorization, "Basic "+encoded)
})
}
func TokenRepo(s Session) runtime.ClientAuthInfoWriter {
getToken, err := s.Token.GetToken()
if err != nil {
return Error(err)
}
if getToken.AccessToken == nil {
return Error(fmt.Errorf("access token is nil. please do login first"))
}
return Bearer(*getToken.AccessToken)
}
func Bearer(token string) runtime.ClientAuthInfoWriter {
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
return r.SetHeaderParam(constant.Authorization, "Bearer "+token)
})
}
func CookieValue(key, value string) runtime.ClientAuthInfoWriter {
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
return r.SetHeaderParam(constant.CookieAuth, key+"="+value)
})
}
func Cookie(s Session, key string) runtime.ClientAuthInfoWriter {
getToken, err := s.Token.GetToken()
if err != nil {
return Error(err)
}
if getToken.AccessToken == nil {
return Error(fmt.Errorf("access token is nil"))
}
return CookieValue(key, *getToken.AccessToken)
}
func Error(err error) runtime.ClientAuthInfoWriter {
return runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
return err
})
}
func RefreshTokenScheduler(session Session, loginType string) {
Once.Do(func() {
getToken, _ := session.Token.GetToken()
refreshRate := session.Refresh.GetRefreshRate()
switch loginType {
case "user": // user token have a refreshToken
if getToken.RefreshToken != "" {
go func() {
for {
token, _ := session.Token.GetToken()
if token == nil {
return // Logout
}
if repository.HasTokenExpired(session.Token, refreshRate) {
if !repository.HasRefreshTokenExpired(session.Token, refreshRate) {
session.Refresh.SetRefreshIsRunningInBackground(true)
UserTokenRefresher(session)
session.Refresh.SetRefreshIsRunningInBackground(false)
}
}
time.Sleep(1 * time.Second)
}
}()
}
case "client":
go func() {
for {
token, _ := session.Token.GetToken()
if token == nil {
return // Logout
}
if repository.HasTokenExpired(session.Token, refreshRate) {
session.Refresh.SetRefreshIsRunningInBackground(true)
ClientTokenRefresher(session)
session.Refresh.SetRefreshIsRunningInBackground(false)
}
time.Sleep(1 * time.Second)
}
}()
}
})
}
type OAuth20RefreshService struct {
Client *iamclient.JusticeIamService
ConfigRepository repository.ConfigRepository
Token repository.TokenRepository
}
func UserTokenRefresher(s Session) {
token, _ := s.Token.GetToken()
p := &o_auth2_0.TokenGrantV3Params{
GrantType: o_auth2_0.TokenGrantV3RefreshTokenConstant,
RefreshToken: &token.RefreshToken,
}
service := OAuth20RefreshService{
Client: factory.NewIamClient(s.Config),
ConfigRepository: s.Config,
Token: s.Token,
}
newToken, errLogin := service.Client.OAuth20.TokenGrantV3Short(p, Basic(s.Config.GetClientId(), s.Config.GetClientSecret()))
if errLogin != nil {
return
}
err := s.Token.Store(*newToken.Payload)
if err != nil {
return
}
}
func ClientTokenRefresher(s Session) {
p := &o_auth2_0.TokenGrantV3Params{
GrantType: o_auth2_0.TokenGrantV3ClientCredentialsConstant,
}
service := OAuth20RefreshService{
Client: factory.NewIamClient(s.Config),
ConfigRepository: s.Config,
Token: s.Token,
}
newToken, errLogin := service.Client.OAuth20.TokenGrantV3Short(p, Basic(s.Config.GetClientId(), s.Config.GetClientSecret()))
if errLogin != nil {
return
}
err := s.Token.Store(*newToken.Payload)
if err != nil {
return
}
}