-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_client.go
553 lines (486 loc) · 16.1 KB
/
api_client.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package keycloak
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/dgrijalva/jwt-go/v4"
"github.com/google/go-querystring/query"
"github.com/hashicorp/go-cleanhttp"
)
const (
HTTPpHeaderAuthorization = "Authorization"
// grant type values
GrantTypeCode = "code"
GrantTypeUMA2Ticket = "urn:ietf:params:oauth:grant-type:uma-ticket"
GrantTypeClientCredentials = "client_credentials"
GrantTypeRefreshToken = "refresh_token"
// token type hint values
TokenTypeHintRequestingPartyToken = "requesting_party_token"
// response modes
UMA2ResponseModeDecision = "decision"
UMA2ResponseModePermissions = "permissions"
DecisionStrategyUnanimous = "UNANIMOUS"
DecisionStrategyAffirmative = "AFFIRMATIVE"
DecisionStrategyPositive = "POSITIVE"
PermissionTypeResource = "resource"
PermissionTypeRole = "role"
PolicyTypeRole = "role"
PolicyTypeJavascript = "js"
PolicyTypeTime = "time"
LogicPositive = "POSITIVE"
LogicNegative = "NEGATIVE"
// DefaultTokenExpirationMargin will be used if you do not specify your own ExpiryMargin key in the config
DefaultTokenExpirationMargin = 2 * time.Second
)
const (
// cache stuff
pkKeyPrefix = "pk"
pkKeyFormat = pkKeyPrefix + "\n%s\n%s\n%s"
reKeyPrefix = "re"
reKeyFormat = reKeyPrefix + "\n%s\n%s"
// common
httpHeaderAccept = "Accept"
httpHeaderContentType = "Content-Type"
httpHeaderValueJSON = "application/json"
httpHeaderValueFormURLEncoded = "application/x-www-form-urlencoded"
httpHeaderAuthorizationBearerPrefix = "Bearer"
httpHeaderAuthorizationBasicPrefix = "Basic"
httpHeaderAuthValueFormat = "%s %s"
httpHeaderLocationKey = "Location"
// permissions params
paramTokenTypeHint = "token_type_hint"
paramTypeToken = "token"
// url structures
authServerURLFormat = "%s://%s/%s"
apiPathFormat = "%s/%s"
// ks api paths
kcURLPathRealmsFormat = "%s/realms/%s/%s"
kcURLPathAdminRealmsFormat = "%s/admin/realms/%s/%s"
// well known stuff
kcPathOIDC = ".well-known/openid-configuration"
kcPathUMA2C = ".well-known/uma2-configuration"
// individual api call path slugs
kcPathPartAssociatedPolicies = "associatedPolicies"
kcPathPartAuthz = "authz"
kcPathPartAvailable = "available"
kcPathPartChildren = "children"
kcPathPartClients = "clients"
kcPathPartClientScopes = "client-scopes"
kcPathPartComposites = "composites"
kcPathPartCount = "count"
kcPathPartDependentPolicies = "dependentPolicies"
kcPathPartEntitlement = "entitlement"
kcPathPartGroups = "groups"
kcPathPartMembers = "members"
kcPathPartJS = "js"
kcPathPartPermission = "permission"
kcPathPartPermissions = "permissions"
kcPathPartPolicy = "policy"
kcPathPartProviders = "providers"
kcPathPartRealm = "realm"
kcPathPartResource = "resource"
kcPathPartResourceServer = "resource-server"
kcPathPartRoleMappings = "role-mappings"
kcPathPartRole = "role"
kcPathPartRoles = "roles"
kcPathPartRolesByID = "roles-by-id"
kcPathPartSearch = "search"
kcPathPartScope = "scope"
kcPathPartScopes = "scopes"
kcPathPartTime = "time"
kcPathPartUsers = "users"
)
var ErrTokenExpired = errors.New("token has expired")
func IsTokenExpiredErr(err error) bool {
for err != nil {
if errors.Is(err, ErrTokenExpired) {
return true
}
err = errors.Unwrap(err)
}
return false
}
// DebugConfig
//
// This type contains configuration options that provide additional utility during testing or development, but should
// not be configured when in production use.
type DebugConfig struct {
// BaseRequestMutators [optional]
//
// Optional list of request mutators that will always be run before any other mutators
BaseRequestMutators []APIRequestMutator
// FinalRequestMutators [optional]
//
// Optional list of request mutators that will always be run after any other mutators
FinalRequestMutators []APIRequestMutator
}
// APIClientConfig
//
// This is the configuration container for a APIClient. See individual comments on fields for more details.
type APIClientConfig struct {
// AuthServerURLProvider [required]
//
// This is called once during client initialization to determine the target keycloak instance
AuthServerURLProvider AuthServerURLProvider
// CacheBackend [optional]
//
// Optionally provide your own cache implementation. This cache is used, by default, for realm environment and
// parsed public key data.
CacheBackend CacheBackend
// HTTPClient [optional]
//
// Set if you wish to use a specific http client configuration. Otherwise, one will be created using
// cleanhttp.DefaultClient()
HTTPClient *http.Client
// Debug [optional]
//
// Optional configurations aimed to ease debugging
Debug *DebugConfig
}
func DefaultAPIClientConfig() *APIClientConfig {
c := APIClientConfig{
AuthServerURLProvider: defaultAuthServerURLProvider(),
CacheBackend: globalCache,
HTTPClient: cleanhttp.DefaultClient(),
Debug: new(DebugConfig),
}
return &c
}
// APIClient
//
// This is the base client for interacting with a Keycloak instance
type APIClient struct {
authServerURL string
cache CacheBackend
mr requestMutatorRunner
hc *http.Client
}
// NewAPIClient will attempt to construct and return a APIClient to you
func NewAPIClient(config *APIClientConfig, mutators ...ConfigMutator) (*APIClient, error) {
var (
cc *APIClientConfig
err error
cl = new(APIClient)
)
cc = CompileAPIClientConfig(config, mutators...)
// set and cleanup auth server url
if cl.authServerURL, err = cc.AuthServerURLProvider.AuthServerURL(); err != nil {
return nil, err
}
cl.authServerURL = strings.TrimRight(cl.authServerURL, "/")
cl.cache = cc.CacheBackend
cl.hc = cc.HTTPClient
cl.mr = buildRequestMutatorRunner(cc.Debug)
return cl, nil
}
// AuthServerURL will return the address of the issuer this client is targeting
func (c *APIClient) AuthServerURL() string {
return c.authServerURL
}
func (c *APIClient) CacheBackend() CacheBackend {
return c.cache
}
func (c *APIClient) Do(ctx context.Context, req *APIRequest, mutators ...APIRequestMutator) (*http.Response, error) {
var (
httpRequest *http.Request
httpResponse *http.Response
err error
)
// apply any mutators
if i, err := c.mr(req, mutators...); err != nil {
return nil, fmt.Errorf("mutator %d returned error: %w", i, err)
}
// construct http request
if httpRequest, err = req.ToHTTP(ctx); err != nil {
return nil, err
}
// execute
httpResponse, err = c.hc.Do(httpRequest)
return httpResponse, err
}
// Call is a helper method that wraps the creation of an *APIRequest type and executes it.
func (c *APIClient) Call(ctx context.Context, ap AuthenticationProvider, method, requestURL string, body interface{}, mutators ...APIRequestMutator) (*http.Response, error) {
var (
req *APIRequest
err error
)
// ensure we've got an actual slice here
if mutators == nil {
mutators = make([]APIRequestMutator, 0)
}
if ap != nil {
var (
am []APIRequestMutator
err error
)
if am, err = ap.RequestMutators(ctx, c); err != nil {
return nil, err
}
mutators = requestMutators(mutators, am...)
}
req = NewAPIRequest(method, requestURL)
if err = req.SetBody(body); err != nil {
return nil, err
}
return c.Do(ctx, req, mutators...)
}
func (c *APIClient) RealmEnvironment(ctx context.Context, realmName string) (*RealmEnvironment, error) {
return GetRealmEnvironment(ctx, c, realmName)
}
// RealmIssuerConfiguration returns metadata about the keycloak realm instance being connected to, such as the public
// key for token signing.
func (c *APIClient) RealmIssuerConfiguration(ctx context.Context, realmName string, mutators ...APIRequestMutator) (*RealmIssuerConfiguration, error) {
var (
resp *http.Response
ic *RealmIssuerConfiguration
err error
)
resp, err = c.Call(
ctx,
nil,
http.MethodGet,
c.realmsURL(realmName),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
ic = new(RealmIssuerConfiguration)
if err = handleResponse(resp, http.StatusOK, ic, err); err != nil {
return nil, err
}
return ic, nil
}
// OpenIDConfiguration returns well-known open-id configuration values for the provided realm
func (c *APIClient) OpenIDConfiguration(ctx context.Context, realmName string, mutators ...APIRequestMutator) (*OpenIDConfiguration, error) {
var (
resp *http.Response
oidc *OpenIDConfiguration
err error
)
resp, err = c.Call(
ctx,
nil,
http.MethodGet,
c.realmsURL(realmName, kcPathOIDC),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
oidc = new(OpenIDConfiguration)
if err = handleResponse(resp, http.StatusOK, oidc, err); err != nil {
return nil, err
}
return oidc, nil
}
// UMA2Configuration returns well-known uma2 configuration values for the provided realm, assuming you are running
// keycloak > 3.4
func (c *APIClient) UMA2Configuration(ctx context.Context, realmName string, mutators ...APIRequestMutator) (*UMA2Configuration, error) {
var (
resp *http.Response
uma2 *UMA2Configuration
err error
)
resp, err = c.Call(
ctx,
nil,
http.MethodGet,
c.realmsURL(realmName, kcPathUMA2C),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
uma2 = new(UMA2Configuration)
if err = handleResponse(resp, http.StatusOK, uma2, err); err != nil {
return nil, err
}
return uma2, nil
}
func (c *APIClient) JSONWebKeys(ctx context.Context, realmName string, mutators ...APIRequestMutator) (*JSONWebKeySet, error) {
var (
resp *http.Response
jwks *JSONWebKeySet
env *RealmEnvironment
err error
)
if env, err = c.RealmEnvironment(ctx, realmName); err != nil {
return nil, err
}
resp, err = c.Call(
ctx,
nil,
http.MethodGet,
env.JSONWebKeysEndpoint(),
nil, requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
jwks = new(JSONWebKeySet)
if err = handleResponse(resp, http.StatusOK, jwks, err); err != nil {
return nil, err
}
return jwks, nil
}
func (c *APIClient) Login(ctx context.Context, req *OpenIDConnectTokenRequest, realmName string, mutators ...APIRequestMutator) (*OpenIDConnectToken, error) {
var (
res interface{}
token *OpenIDConnectToken
ok bool
err error
)
req.ResponseMode = nil
res, err = c.openIDConnectToken(ctx, realmName, nil, req, requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...)
if err != nil {
return nil, err
}
if token, ok = res.(*OpenIDConnectToken); !ok {
return nil, fmt.Errorf("expected response to be %T, saw %T", token, res)
}
return token, nil
}
// ParseRequestToken attempts to extract the encoded bearer token from the provided request and parse it into a modeled
// access token type
func (c *APIClient) ParseRequestToken(ctx context.Context, request *http.Request, claimsType jwt.Claims, parserOpts ...jwt.ParserOption) (*jwt.Token, error) {
if bt, ok := RequestBearerToken(request); ok {
return c.ParseToken(ctx, bt, claimsType, parserOpts...)
}
return nil, errors.New("bearer token not found in request")
}
// ParseToken will attempt to parse and validate a raw token into a modeled type. If this method does not return
// an error, you can safely assume the provided raw token is safe for use.
func (c *APIClient) ParseToken(ctx context.Context, rawToken string, claimsType jwt.Claims, opts ...jwt.ParserOption) (*jwt.Token, error) {
var (
jwtToken *jwt.Token
err error
)
if claimsType == nil {
claimsType = new(jwt.StandardClaims)
}
if jwtToken, err = jwt.ParseWithClaims(rawToken, claimsType, c.keyFunc(ctx), opts...); err != nil {
return nil, fmt.Errorf("error parsing raw token into %T: %w", claimsType, err)
}
return jwtToken, nil
}
func (c *APIClient) keyFunc(ctx context.Context) jwt.Keyfunc {
return func(token *jwt.Token) (interface{}, error) {
var (
tp TokenParser
ok bool
)
if tp, ok = GetTokenParser(token.Method.Alg()); !ok {
return nil, fmt.Errorf("no token parser registered to handle %q", token.Method.Alg())
}
return tp.Parse(ctx, c, token)
}
}
func (c *APIClient) openIDConnectToken(ctx context.Context, realmName string, ap AuthenticationProvider, req *OpenIDConnectTokenRequest, mutators ...APIRequestMutator) (interface{}, error) {
var (
body url.Values
resp *http.Response
env *RealmEnvironment
model interface{}
err error
)
if env, err = c.RealmEnvironment(ctx, realmName); err != nil {
return nil, err
}
if body, err = query.Values(req); err != nil {
return nil, fmt.Errorf("error encoding request: %w", err)
}
resp, err = c.Call(
ctx,
ap,
http.MethodPost,
env.TokenEndpoint(),
body,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
HeaderMutator(httpHeaderContentType, httpHeaderValueFormURLEncoded, true),
)...,
)
if req.ResponseMode == nil {
model = new(OpenIDConnectToken)
} else {
switch *req.ResponseMode {
case UMA2ResponseModeDecision:
model = new(PermissionDecisionResponse)
case UMA2ResponseModePermissions:
model = make(EvaluatedPermissions, 0)
default:
model = new(OpenIDConnectToken)
}
}
if err = handleResponse(resp, http.StatusOK, model, err); err != nil {
return nil, err
}
return model, nil
}
// realmsURL builds a request path under the /realms/{realm}/... path
func (c *APIClient) realmsURL(realmName string, bits ...string) string {
return fmt.Sprintf(kcURLPathRealmsFormat, c.authServerURL, realmName, path.Join(bits...))
}
func (c *APIClient) callRealms(ctx context.Context, realmName string, ap AuthenticationProvider, method, requestPath string, body interface{}, mutators ...APIRequestMutator) (*http.Response, error) {
return c.Call(ctx, ap, method, c.realmsURL(realmName, requestPath), body, mutators...)
}
// AdminAPIClient is a simple extension of the base APIClient, adding /admin api calls
type AdminAPIClient struct {
*APIClient
realmName string
ap AuthenticationProvider
}
func NewAdminAPIClient(config *APIClientConfig, realmName string, ap AuthenticationProvider, mutators ...ConfigMutator) (*AdminAPIClient, error) {
var (
c *APIClient
err error
)
if c, err = NewAPIClient(config, mutators...); err != nil {
return nil, err
}
return c.AdminClient(realmName, ap), nil
}
func NewAdminAPIClientWithProvider(cp CombinedProvider, realmName string, mutators ...ConfigMutator) (*AdminAPIClient, error) {
conf := DefaultAPIClientConfig()
conf.AuthServerURLProvider = cp
c, err := NewAPIClient(conf, mutators...)
if err != nil {
return nil, err
}
return c.AdminClient(realmName, cp), nil
}
func NewAdminAPIClientWithInstallDocument(id *InstallDocument, realmName string, mutators ...ConfigMutator) (*AdminAPIClient, error) {
// todo: support ID's for things other than a confidential client
ctp, err := NewClientSecretAuthenticationProvider(NewClientSecretConfigWithInstallDocument(id))
if err != nil {
return nil, err
}
return NewAdminAPIClientWithProvider(ctp, realmName, mutators...)
}
// AdminClient returns a new AdminAPIClient for the provided realm (does not have to be the same as the auth'd realm)
func (c *APIClient) AdminClient(realmName string, ap AuthenticationProvider) *AdminAPIClient {
return &AdminAPIClient{APIClient: c, realmName: realmName, ap: ap}
}
func (c *AdminAPIClient) AuthProvider() AuthenticationProvider {
return c.ap
}
// adminRealmsURL builds a request path under the /admin/realms/{realm}/... path
func (c *AdminAPIClient) adminRealmsURL(bits ...string) string {
return fmt.Sprintf(kcURLPathAdminRealmsFormat, c.authServerURL, c.realmName, path.Join(bits...))
}
func (c *AdminAPIClient) callAdminRealms(ctx context.Context, method, requestPath string, body interface{}, mutators ...APIRequestMutator) (*http.Response, error) {
return c.Call(ctx, c.ap, method, c.adminRealmsURL(requestPath), body, mutators...)
}