-
Notifications
You must be signed in to change notification settings - Fork 4
/
option.go
363 lines (317 loc) · 8.96 KB
/
option.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2023 LY Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authorizerd
import (
"net/http"
"time"
urlutil "github.com/AthenZ/athenz-authorizer/v5/internal/url"
)
const (
defaultHTTPClientTimeout = 30
)
var (
defaultOptions = []Option{
WithAthenzURL("athenz.io/zts/v1"),
WithHTTPClient(nil),
WithCacheExp(time.Minute),
WithEnablePubkeyd(),
WithEnablePolicyd(),
WithEnableJwkd(),
WithAccessTokenParam(NewAccessTokenParam(true, true, "1h", "1h", false, nil)),
WithEnableRoleToken(),
WithRoleAuthHeader("Athenz-Role-Auth"),
WithEnableRoleCert(),
WithRoleCertURIPrefix("athenz://role/"),
}
)
type AccessTokenParam struct {
enable bool
verifyCertThumbprint bool
certBackdateDur string
certOffsetDur string
verifyClientID bool
authorizedClientIDs map[string][]string
}
// Option represents a functional option
type Option func(*authority) error
// WithAthenzURL returns an AthenzURL functional option
func WithAthenzURL(url string) Option {
return func(authz *authority) error {
u := urlutil.TrimHTTPScheme(url)
if urlutil.HasScheme(u) {
return urlutil.ErrUnsupportedScheme
}
authz.athenzURL = u
return nil
}
}
// WithAthenzDomains returns an AthenzDomains functional option
func WithAthenzDomains(domains ...string) Option {
return func(authz *authority) error {
authz.athenzDomains = domains
return nil
}
}
// WithHTTPClient returns a HTTPClient functional option
func WithHTTPClient(c *http.Client) Option {
return func(authz *authority) error {
if c == nil {
authz.client = &http.Client{
Timeout: defaultHTTPClientTimeout * time.Second,
}
return nil
}
authz.client = c
return nil
}
}
// WithCacheExp returns a CacheExp functional option
func WithCacheExp(exp time.Duration) Option {
return func(authz *authority) error {
authz.cache.SetDefaultExpire(exp)
authz.cacheExp = exp
return nil
}
}
/*
pubkeyd parameters
*/
// WithEnablePubkeyd returns an EnablePubkeyd functional option
func WithEnablePubkeyd() Option {
return func(authz *authority) error {
authz.disablePubkeyd = false
return nil
}
}
// WithDisablePubkeyd returns a DisablePubkeyd functional option
func WithDisablePubkeyd() Option {
return func(authz *authority) error {
authz.disablePubkeyd = true
return nil
}
}
// WithPubkeyRefreshPeriod returns a PubkeyRefreshPeriod functional option
func WithPubkeyRefreshPeriod(t string) Option {
return func(authz *authority) error {
authz.pubkeyRefreshPeriod = t
return nil
}
}
// WithPubkeyRetryDelay returns a PubkeyRetryDelay functional option
func WithPubkeyRetryDelay(i string) Option {
return func(authz *authority) error {
authz.pubkeyRetryDelay = i
return nil
}
}
// WithPubkeySysAuthDomain returns a PubkeySysAuthDomain functional option
func WithPubkeySysAuthDomain(domain string) Option {
return func(authz *authority) error {
authz.pubkeySysAuthDomain = domain
return nil
}
}
// WithPubkeyETagExpiry returns a PubkeyETagExpiry functional option
func WithPubkeyETagExpiry(t string) Option {
return func(authz *authority) error {
authz.pubkeyETagExpiry = t
return nil
}
}
// WithPubkeyETagPurgePeriod returns a PubkeyETagPurgePeriod functional option
func WithPubkeyETagPurgePeriod(t string) Option {
return func(authz *authority) error {
authz.pubkeyETagPurgePeriod = t
return nil
}
}
/*
policyd parameters
*/
// WithEnablePolicyd returns an EnablePolicyd functional option
func WithEnablePolicyd() Option {
return func(authz *authority) error {
authz.disablePolicyd = false
return nil
}
}
// WithDisablePolicyd returns a DisablePolicyd functional option
func WithDisablePolicyd() Option {
return func(authz *authority) error {
authz.disablePolicyd = true
return nil
}
}
// WithPolicyRefreshPeriod returns a PolicyRefreshPeriod functional option
func WithPolicyRefreshPeriod(t string) Option {
return func(authz *authority) error {
authz.policyRefreshPeriod = t
return nil
}
}
// WithPolicyExpiryMargin returns a PolicyExpiryMargin functional option
func WithPolicyExpiryMargin(t string) Option {
return func(authz *authority) error {
authz.policyExpiryMargin = t
return nil
}
}
// WithPolicyPurgePeriod returns a PolicyPurgePeriod functional option
func WithPolicyPurgePeriod(t string) Option {
return func(authz *authority) error {
authz.policyPurgePeriod = t
return nil
}
}
// WithPolicyRetryDelay returns a PolicyRetryDelay functional option
func WithPolicyRetryDelay(i string) Option {
return func(authz *authority) error {
authz.policyRetryDelay = i
return nil
}
}
// WithPolicyRetryAttempts returns a PolicyRetryAttempts functional option
func WithPolicyRetryAttempts(c int) Option {
return func(authz *authority) error {
authz.policyRetryAttempts = c
return nil
}
}
/*
jwkd parameters
*/
// WithEnableJwkd returns an EnableJwkd functional option
func WithEnableJwkd() Option {
return func(authz *authority) error {
authz.disableJwkd = false
return nil
}
}
// WithDisableJwkd returns a DisableJwkd functional option
func WithDisableJwkd() Option {
return func(authz *authority) error {
authz.disableJwkd = true
return nil
}
}
// WithJwkRefreshPeriod returns a JwkRefreshPeriod functional option
func WithJwkRefreshPeriod(t string) Option {
return func(authz *authority) error {
authz.jwkRefreshPeriod = t
return nil
}
}
// WithJwkRetryDelay returns a JwkRetryDelay functional option
func WithJwkRetryDelay(i string) Option {
return func(authz *authority) error {
authz.jwkRetryDelay = i
return nil
}
}
// WithJwkURLs returns a JwkURLs functional option
func WithJwkURLs(urls []string) Option {
return func(authz *authority) error {
authz.jwkURLs = urls
return nil
}
}
/*
access token parameters
*/
// NewAccessTokenParam returns a new access token parameter
func NewAccessTokenParam(enable bool, verifyCertThumbprint bool, certBackdateDur, certOffsetDur string, verifyClientID bool, authorizedClientIDs map[string][]string) AccessTokenParam {
return AccessTokenParam{
// Flag to enable verify of access token
enable: enable,
// The client certificate Thumbprint hash and access token cnf checks are enabled. (Certificate-Bound Access Tokens)
verifyCertThumbprint: verifyCertThumbprint,
// If the time of issuance of the certificate is intentionally earlier, specify that time.
certBackdateDur: certBackdateDur,
// If the certificate and token have not been bound, specify the time to determine that the certificate has been updated.
certOffsetDur: certOffsetDur,
// The client certificate common name and client_id verification.
verifyClientID: verifyClientID,
// The list of authorized client_id and common name.
authorizedClientIDs: authorizedClientIDs,
}
}
// WithAccessTokenParam returns a functional option that new access token parameter
func WithAccessTokenParam(accessTokenParam AccessTokenParam) Option {
return func(authz *authority) error {
authz.accessTokenParam = accessTokenParam
return nil
}
}
/*
role token parameters
*/
// WithEnableRoleToken returns a enable roletoken functional option
func WithEnableRoleToken() Option {
return func(authz *authority) error {
authz.enableRoleToken = true
return nil
}
}
// WithDisableRoleToken returns a disable roletoken functional option
func WithDisableRoleToken() Option {
return func(authz *authority) error {
authz.enableRoleToken = false
return nil
}
}
// WithRoleAuthHeader returns a RoleAuthHeader functional option
func WithRoleAuthHeader(h string) Option {
return func(authz *authority) error {
authz.roleAuthHeader = h
return nil
}
}
/*
role certificate parameters
*/
// WithEnableRoleCert returns a enable rolecert functional option
func WithEnableRoleCert() Option {
return func(authz *authority) error {
authz.enableRoleCert = true
return nil
}
}
// WithDisableRoleCert returns a disable rolecert functional option
func WithDisableRoleCert() Option {
return func(authz *authority) error {
authz.enableRoleCert = false
return nil
}
}
// WithRoleCertURIPrefix returns a RoleCertURIPrefix functional option
func WithRoleCertURIPrefix(t string) Option {
return func(authz *authority) error {
authz.roleCertURIPrefix = t
return nil
}
}
// WithTranslator returns a Translator functional option
func WithTranslator(t Translator) Option {
return func(authz *authority) error {
authz.translator = t
return nil
}
}
// WithResourcePrefix returns a ResourcePrefix functional option
func WithResourcePrefix(p string) Option {
return func(authz *authority) error {
authz.resourcePrefix = p
return nil
}
}