-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
1407 lines (1043 loc) · 51.7 KB
/
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// package gokeycloak is a golang keycloak adaptor.
package gokeycloak
import (
"context"
"encoding/base64"
"net/http"
"strings"
"sync"
"time"
"github.com/go-resty/resty/v2"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
)
// GoCloak provides functionalities to talk to Keycloak.
type GoKeycloak struct {
basePath string
certsCache sync.Map
certsLock sync.Mutex
restyClient *resty.Client
Config struct {
CertsInvalidateTime time.Duration
authAdminRealms string
authRealms string
openIDConnect string
attackDetection string
}
}
const (
adminClientID string = "admin-cli"
urlSeparator string = "/"
)
func makeURL(path ...string) string {
return strings.Join(path, urlSeparator)
}
// GetRequest returns a request for calling endpoints.
func (g *GoKeycloak) GetRequest(ctx context.Context) *resty.Request {
var err HTTPErrorResponse
return injectTracingHeaders(
ctx, g.restyClient.R().
SetContext(ctx).
SetError(&err),
)
}
// GetRequestWithBearerAuthNoCache returns a JSON base request configured with an auth token and no-cache header.
func (g *GoKeycloak) GetRequestWithBearerAuthNoCache(ctx context.Context, token string) *resty.Request {
return g.GetRequest(ctx).
SetAuthToken(token).
SetHeader("Content-Type", "application/json").
SetHeader("Cache-Control", "no-cache")
}
// GetRequestWithBearerAuth returns a JSON base request configured with an auth token.
func (g *GoKeycloak) GetRequestWithBearerAuth(ctx context.Context, token string) *resty.Request {
return g.GetRequest(ctx).
SetAuthToken(token).
SetHeader("Content-Type", "application/json")
}
// GetRequestWithBearerAuthXMLHeader returns an XML base request configured with an auth token.
func (g *GoKeycloak) GetRequestWithBearerAuthXMLHeader(ctx context.Context, token string) *resty.Request {
return g.GetRequest(ctx).
SetAuthToken(token).
SetHeader("Content-Type", "application/xml;charset=UTF-8")
}
// GetRequestWithBasicAuth returns a form data base request configured with basic auth.
func (g *GoKeycloak) GetRequestWithBasicAuth(ctx context.Context, clientID, clientSecret string) *resty.Request {
req := g.GetRequest(ctx).
SetHeader("Content-Type", "application/x-www-form-urlencoded")
// Public client doesn't require Basic Auth
if len(clientID) > 0 && len(clientSecret) > 0 {
httpBasicAuth := base64.StdEncoding.EncodeToString([]byte(clientID + ":" + clientSecret))
req.SetHeader("Authorization", "Basic "+httpBasicAuth)
}
return req
}
func getID(resp *resty.Response) string {
header := resp.Header().Get("Location")
splittedPath := strings.Split(header, urlSeparator)
return splittedPath[len(splittedPath)-1]
}
func findUsedKey(usedKeyID string, keys []CertResponseKey) *CertResponseKey {
for _, key := range keys {
if *(key.Kid) == usedKeyID {
return &key
}
}
return nil
}
func injectTracingHeaders(ctx context.Context, req *resty.Request) *resty.Request {
// look for span in context, do nothing if span is not found
span := opentracing.SpanFromContext(ctx)
if span == nil {
return req
}
// look for tracer in context, use global tracer if not found
tracer, ok := ctx.Value(tracerContextKey).(opentracing.Tracer)
if !ok || tracer == nil {
tracer = opentracing.GlobalTracer()
}
// inject tracing header into request
err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if err != nil {
return req
}
return req
}
// ===============
// Keycloak client
// ===============
// NewClient creates a new Client
func NewClient(basePath string, options ...func(*GoKeycloak)) *GoKeycloak {
c := GoKeycloak{
basePath: strings.TrimRight(basePath, urlSeparator),
restyClient: resty.New(),
}
c.Config.CertsInvalidateTime = 10 * time.Minute
c.Config.authAdminRealms = makeURL("admin", "realms")
c.Config.authRealms = makeURL("realms")
c.Config.openIDConnect = makeURL("protocol", "openid-connect")
c.Config.attackDetection = makeURL("attack-detection", "brute-force")
for _, option := range options {
option(&c)
}
return &c
}
// RestyClient returns the internal resty g.
// This can be used to configure the g.
func (g *GoKeycloak) RestyClient() *resty.Client {
return g.restyClient
}
// SetRestyClient overwrites the internal resty g.
func (g *GoKeycloak) SetRestyClient(restyClient *resty.Client) {
g.restyClient = restyClient
}
// ==== Functional Options ===
// SetCertCacheInvalidationTime sets the logout
func SetCertCacheInvalidationTime(duration time.Duration) func(g *GoKeycloak) {
return func(g *GoKeycloak) {
g.Config.CertsInvalidateTime = duration
}
}
// RevokeUserConsents revokes the given user consent.
func (g *GoKeycloak) RevokeUserConsents(ctx context.Context, accessToken, realm, userID, clientID string) (int, error) {
const errMessage = "could not revoke consents"
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
Delete(g.getAdminRealmURL(realm, "users", userID, "consents", clientID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// LogoutUserSession logs out a single sessions of a user given a session id
func (g *GoKeycloak) LogoutUserSession(ctx context.Context, accessToken, realm, session string) (int, error) {
const errMessage = "could not logout"
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
Delete(g.getAdminRealmURL(realm, "sessions", session))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// ExecuteActionsEmail executes an actions email
func (g *GoKeycloak) ExecuteActionsEmail(ctx context.Context, token, realm string, params ExecuteActionsEmail) (int, error) {
const errMessage = "could not execute actions email"
queryParams, err := GetQueryParams(params)
if err != nil {
return http.StatusInternalServerError, errors.Wrap(err, errMessage)
}
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(params.Actions).
SetQueryParams(queryParams).
Put(g.getAdminRealmURL(realm, "users", *(params.UserID), "execute-actions-email"))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// CreateComponent creates the given component.
func (g *GoKeycloak) CreateComponent(ctx context.Context, token, realm string, component Component) (int, string, error) {
const errMessage = "could not create component"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(component).
Post(g.getAdminRealmURL(realm, "components"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), "", err
}
return resp.StatusCode(), getID(resp), nil
}
// CreateClient creates the given g.
func (g *GoKeycloak) CreateClient(ctx context.Context, clientInitialAccessToken, realm string, newClient CreateClientRequest) (int, CreateClientResponse, error) {
const errMessage = "could not create client"
var result CreateClientResponse
// create a client
resp, err := g.GetRequestWithBearerAuth(ctx, clientInitialAccessToken).
SetBody(newClient).SetResult(&result).
Post(g.getRealmURL(realm, "clients-registrations", "openid-connect"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), result, err
}
return resp.StatusCode(), result, nil
}
// CreateClientRepresentation creates a new client representation
func (g *GoKeycloak) CreateClientRepresentation(ctx context.Context, token, realm string, newClient Client) (int, *Client, error) {
const errMessage = "could not create client representation"
var result Client
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
SetBody(newClient).
Post(g.getRealmURL(realm, "clients-registrations", "default"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// CreateClientRole creates a new role for a client
func (g *GoKeycloak) CreateClientRole(ctx context.Context, token, realm, idOfClient string, role Role) (int, string, error) {
const errMessage = "could not create client role"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(role).
Post(g.getAdminRealmURL(realm, "clients", idOfClient, "roles"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), "", err
}
return resp.StatusCode(), getID(resp), nil
}
// CreateClientScope creates a new client scope
func (g *GoKeycloak) CreateClientScope(ctx context.Context, token, realm string, scope ClientScope) (int, string, error) {
const errMessage = "could not create client scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(scope).
Post(g.getAdminRealmURL(realm, "client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), "", err
}
return resp.StatusCode(), getID(resp), nil
}
// CreateClientScopeProtocolMapper creates a new protocolMapper under the given client scope
func (g *GoKeycloak) CreateClientScopeProtocolMapper(ctx context.Context, token, realm, scopeID string, protocolMapper ProtocolMappers) (int, string, error) {
const errMessage = "could not create client scope protocol mapper"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(protocolMapper).
Post(g.getAdminRealmURL(realm, "client-scopes", scopeID, "protocol-mappers", "models"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), "", err
}
return resp.StatusCode(), getID(resp), nil
}
// UpdateClient updates the given Client
func (g *GoKeycloak) UpdateClient(ctx context.Context, token, realm string, updatedClient Client) (int, error) {
const errMessage = "could not update client"
if NilOrEmpty(updatedClient.ID) {
return http.StatusInternalServerError, errors.Wrap(errors.New("ID of a client required"), errMessage)
}
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(updatedClient).
Put(g.getAdminRealmURL(realm, "clients", PString(updatedClient.ID)))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// UpdateClientRepresentation updates the given client representation
func (g *GoKeycloak) UpdateClientRepresentation(ctx context.Context, accessToken, realm string, updatedClient Client) (int, *Client, error) {
const errMessage = "could not update client representation"
if NilOrEmpty(updatedClient.ID) {
return http.StatusInternalServerError, nil, errors.Wrap(errors.New("ID of a client required"), errMessage)
}
var result Client
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
SetResult(&result).
SetBody(updatedClient).
Put(g.getRealmURL(realm, "clients-registrations", "default", PString(updatedClient.ClientID)))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// UpdateRole updates the given role.
func (g *GoKeycloak) UpdateRole(ctx context.Context, token, realm, idOfClient string, role Role) (int, error) {
const errMessage = "could not update role"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(role).
Put(g.getAdminRealmURL(realm, "clients", idOfClient, "roles", PString(role.Name)))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// UpdateClientScope updates the given client scope.
func (g *GoKeycloak) UpdateClientScope(ctx context.Context, token, realm string, scope ClientScope) (int, error) {
const errMessage = "could not update client scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(scope).
Put(g.getAdminRealmURL(realm, "client-scopes", PString(scope.ID)))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// UpdateClientScopeProtocolMapper updates the given protocol mapper for a client scope
func (g *GoKeycloak) UpdateClientScopeProtocolMapper(ctx context.Context, token, realm, scopeID string, protocolMapper ProtocolMappers) (int, error) {
const errMessage = "could not update client scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(protocolMapper).
Put(g.getAdminRealmURL(realm, "client-scopes", scopeID, "protocol-mappers", "models", PString(protocolMapper.ID)))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClient deletes a given client
func (g *GoKeycloak) DeleteClient(ctx context.Context, token, realm, idOfClient string) (int, error) {
const errMessage = "could not delete client"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteComponent deletes the component with the given id.
func (g *GoKeycloak) DeleteComponent(ctx context.Context, token, realm, componentID string) (int, error) {
const errMessage = "could not delete component"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "components", componentID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientRepresentation deletes a given client representation.
func (g *GoKeycloak) DeleteClientRepresentation(ctx context.Context, accessToken, realm, clientID string) (int, error) {
const errMessage = "could not delete client representation"
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
Delete(g.getRealmURL(realm, "clients-registrations", "default", clientID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientRole deletes a given role.
func (g *GoKeycloak) DeleteClientRole(ctx context.Context, token, realm, idOfClient, roleName string) (int, error) {
const errMessage = "could not delete client role"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "roles", roleName))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientScope deletes the scope with the given id.
func (g *GoKeycloak) DeleteClientScope(ctx context.Context, token, realm, scopeID string) (int, error) {
const errMessage = "could not delete client scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "client-scopes", scopeID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientScopeProtocolMapper deletes the given protocol mapper from the client scope
func (g *GoKeycloak) DeleteClientScopeProtocolMapper(ctx context.Context, token, realm, scopeID, protocolMapperID string) (int, error) {
const errMessage = "could not delete client scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "client-scopes", scopeID, "protocol-mappers", "models", protocolMapperID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetClient returns a client
func (g *GoKeycloak) GetClient(ctx context.Context, token, realm, idOfClient string) (int, *Client, error) {
const errMessage = "could not get client"
var result Client
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientRepresentation returns a client representation
func (g *GoKeycloak) GetClientRepresentation(ctx context.Context, accessToken, realm, clientID string) (int, *Client, error) {
const errMessage = "could not get client representation"
var result Client
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
SetResult(&result).
Get(g.getRealmURL(realm, "clients-registrations", "default", clientID))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetAdapterConfiguration returns a adapter configuration
func (g *GoKeycloak) GetAdapterConfiguration(ctx context.Context, accessToken, realm, clientID string) (int, *AdapterConfiguration, error) {
const errMessage = "could not get adapter configuration"
var result AdapterConfiguration
resp, err := g.GetRequestWithBearerAuth(ctx, accessToken).
SetResult(&result).
Get(g.getRealmURL(realm, "clients-registrations", "install", clientID))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientsDefaultScopes returns a list of the client's default scopes
func (g *GoKeycloak) GetClientsDefaultScopes(ctx context.Context, token, realm, idOfClient string) (int, []*ClientScope, error) {
const errMessage = "could not get clients default scopes"
var result []*ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "default-client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// AddDefaultScopeToClient adds a client scope to the list of client's default scopes
func (g *GoKeycloak) AddDefaultScopeToClient(ctx context.Context, token, realm, idOfClient, scopeID string) (int, error) {
const errMessage = "could not add default scope to client"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Put(g.getAdminRealmURL(realm, "clients", idOfClient, "default-client-scopes", scopeID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// RemoveDefaultScopeFromClient removes a client scope from the list of client's default scopes
func (g *GoKeycloak) RemoveDefaultScopeFromClient(ctx context.Context, token, realm, idOfClient, scopeID string) (int, error) {
const errMessage = "could not remove default scope from client"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "default-client-scopes", scopeID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetClientsOptionalScopes returns a list of the client's optional scopes
func (g *GoKeycloak) GetClientsOptionalScopes(ctx context.Context, token, realm, idOfClient string) (int, []*ClientScope, error) {
const errMessage = "could not get clients optional scopes"
var result []*ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "optional-client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// AddOptionalScopeToClient adds a client scope to the list of client's optional scopes
func (g *GoKeycloak) AddOptionalScopeToClient(ctx context.Context, token, realm, idOfClient, scopeID string) (int, error) {
const errMessage = "could not add optional scope to client"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Put(g.getAdminRealmURL(realm, "clients", idOfClient, "optional-client-scopes", scopeID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// RemoveOptionalScopeFromClient deletes a client scope from the list of client's optional scopes
func (g *GoKeycloak) RemoveOptionalScopeFromClient(ctx context.Context, token, realm, idOfClient, scopeID string) (int, error) {
const errMessage = "could not remove optional scope from client"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "optional-client-scopes", scopeID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetDefaultOptionalClientScopes returns a list of default realm optional scopes
func (g *GoKeycloak) GetDefaultOptionalClientScopes(ctx context.Context, token, realm string) (int, []*ClientScope, error) {
const errMessage = "could not get default optional client scopes"
var result []*ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "default-optional-client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetDefaultDefaultClientScopes returns a list of default realm default scopes
func (g *GoKeycloak) GetDefaultDefaultClientScopes(ctx context.Context, token, realm string) (int, []*ClientScope, error) {
const errMessage = "could not get default client scopes"
var result []*ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "default-default-client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScope returns a clientscope
func (g *GoKeycloak) GetClientScope(ctx context.Context, token, realm, scopeID string) (int, *ClientScope, error) {
const errMessage = "could not get client scope"
var result ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "client-scopes", scopeID))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientScopes returns all client scopes
func (g *GoKeycloak) GetClientScopes(ctx context.Context, token, realm string) (int, []*ClientScope, error) {
const errMessage = "could not get client scopes"
var result []*ClientScope
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "client-scopes"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeProtocolMappers returns all protocol mappers of a client scope
func (g *GoKeycloak) GetClientScopeProtocolMappers(ctx context.Context, token, realm, scopeID string) (int, []*ProtocolMappers, error) {
const errMessage = "could not get client scope protocol mappers"
var result []*ProtocolMappers
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "client-scopes", scopeID, "protocol-mappers", "models"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeProtocolMapper returns a protocol mapper of a client scope
func (g *GoKeycloak) GetClientScopeProtocolMapper(ctx context.Context, token, realm, scopeID, protocolMapperID string) (int, *ProtocolMappers, error) {
const errMessage = "could not get client scope protocol mappers"
var result *ProtocolMappers
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "client-scopes", scopeID, "protocol-mappers", "models", protocolMapperID))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeMappings returns all scope mappings for the client
func (g *GoKeycloak) GetClientScopeMappings(ctx context.Context, token, realm, idOfClient string) (int, *MappingsRepresentation, error) {
const errMessage = "could not get all scope mappings for the client"
var result *MappingsRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeMappingsRealmRoles returns realm-level roles associated with the client’s scope
func (g *GoKeycloak) GetClientScopeMappingsRealmRoles(ctx context.Context, token, realm, idOfClient string) (int, []*Role, error) {
const errMessage = "could not get realm-level roles with the client’s scope"
var result []*Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "realm"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeMappingsRealmRolesAvailable returns realm-level roles that are available to attach to this client’s scope
func (g *GoKeycloak) GetClientScopeMappingsRealmRolesAvailable(ctx context.Context, token, realm, idOfClient string) (int, []*Role, error) {
const errMessage = "could not get available realm-level roles with the client’s scope"
var result []*Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "realm", "available"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// CreateClientScopeMappingsRealmRoles create realm-level roles to the client’s scope
func (g *GoKeycloak) CreateClientScopeMappingsRealmRoles(ctx context.Context, token, realm, idOfClient string, roles []Role) (int, error) {
const errMessage = "could not create realm-level roles to the client’s scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(roles).
Post(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "realm"))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientScopeMappingsRealmRoles deletes realm-level roles from the client’s scope
func (g *GoKeycloak) DeleteClientScopeMappingsRealmRoles(ctx context.Context, token, realm, idOfClient string, roles []Role) (int, error) {
const errMessage = "could not delete realm-level roles from the client’s scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(roles).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "realm"))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetClientScopeMappingsClientRoles returns roles associated with a client’s scope
func (g *GoKeycloak) GetClientScopeMappingsClientRoles(ctx context.Context, token, realm, idOfClient, idOfSelectedClient string) (int, []*Role, error) {
const errMessage = "could not get roles associated with a client’s scope"
var result []*Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "clients", idOfSelectedClient))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientScopeMappingsClientRolesAvailable returns available roles associated with a client’s scope
func (g *GoKeycloak) GetClientScopeMappingsClientRolesAvailable(ctx context.Context, token, realm, idOfClient, idOfSelectedClient string) (int, []*Role, error) {
const errMessage = "could not get available roles associated with a client’s scope"
var result []*Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "clients", idOfSelectedClient, "available"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// CreateClientScopeMappingsClientRoles creates client-level roles from the client’s scope
func (g *GoKeycloak) CreateClientScopeMappingsClientRoles(ctx context.Context, token, realm, idOfClient, idOfSelectedClient string, roles []Role) (int, error) {
const errMessage = "could not create client-level roles from the client’s scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(roles).
Post(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "clients", idOfSelectedClient))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientScopeMappingsClientRoles deletes client-level roles from the client’s scope
func (g *GoKeycloak) DeleteClientScopeMappingsClientRoles(ctx context.Context, token, realm, idOfClient, idOfSelectedClient string, roles []Role) (int, error) {
const errMessage = "could not delete client-level roles from the client’s scope"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(roles).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "scope-mappings", "clients", idOfSelectedClient))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetClientSecret returns a client's secret
func (g *GoKeycloak) GetClientSecret(ctx context.Context, token, realm, idOfClient string) (int, *CredentialRepresentation, error) {
const errMessage = "could not get client secret"
var result CredentialRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "client-secret"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientServiceAccount retrieves the service account "user" for a client if enabled
func (g *GoKeycloak) GetClientServiceAccount(ctx context.Context, token, realm, idOfClient string) (int, *User, error) {
const errMessage = "could not get client service account"
var result User
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "service-account-user"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// RegenerateClientSecret triggers the creation of the new client secret.
func (g *GoKeycloak) RegenerateClientSecret(ctx context.Context, token, realm, idOfClient string) (int, *CredentialRepresentation, error) {
const errMessage = "could not regenerate client secret"
var result CredentialRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Post(g.getAdminRealmURL(realm, "clients", idOfClient, "client-secret"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientOfflineSessions returns offline sessions associated with the client
func (g *GoKeycloak) GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string) (int, []*UserSessionRepresentation, error) {
const errMessage = "could not get client offline sessions"
var res []*UserSessionRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&res).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "offline-sessions"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), res, nil
}
// GetClientUserSessions returns user sessions associated with the client
func (g *GoKeycloak) GetClientUserSessions(ctx context.Context, token, realm, idOfClient string) (int, []*UserSessionRepresentation, error) {
const errMessage = "could not get client user sessions"
var res []*UserSessionRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&res).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "user-sessions"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), res, nil
}
// CreateClientProtocolMapper creates a protocol mapper in client scope
func (g *GoKeycloak) CreateClientProtocolMapper(ctx context.Context, token, realm, idOfClient string, mapper ProtocolMapperRepresentation) (int, string, error) {
const errMessage = "could not create client protocol mapper"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(mapper).
Post(g.getAdminRealmURL(realm, "clients", idOfClient, "protocol-mappers", "models"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), "", err
}
return resp.StatusCode(), getID(resp), nil
}
// UpdateClientProtocolMapper updates a protocol mapper in client scope
func (g *GoKeycloak) UpdateClientProtocolMapper(ctx context.Context, token, realm, idOfClient, mapperID string, mapper ProtocolMapperRepresentation) (int, error) {
const errMessage = "could not update client protocol mapper"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetBody(mapper).
Put(g.getAdminRealmURL(realm, "clients", idOfClient, "protocol-mappers", "models", mapperID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// DeleteClientProtocolMapper deletes a protocol mapper in client scope
func (g *GoKeycloak) DeleteClientProtocolMapper(ctx context.Context, token, realm, idOfClient, mapperID string) (int, error) {
const errMessage = "could not delete client protocol mapper"
resp, err := g.GetRequestWithBearerAuth(ctx, token).
Delete(g.getAdminRealmURL(realm, "clients", idOfClient, "protocol-mappers", "models", mapperID))
return resp.StatusCode(), checkForError(resp, err, errMessage)
}
// GetKeyStoreConfig get keystoreconfig of the realm
func (g *GoKeycloak) GetKeyStoreConfig(ctx context.Context, token, realm string) (int, *KeyStoreConfig, error) {
const errMessage = "could not get key store config"
var result KeyStoreConfig
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "keys"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
func (g *GoKeycloak) getRoleMappings(ctx context.Context, token, realm, path, objectID string) (int, *MappingsRepresentation, error) {
const errMessage = "could not get role mappings"
var result MappingsRepresentation
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, path, objectID, "role-mappings"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetRoleMappingByGroupID gets the role mappings by group
func (g *GoKeycloak) GetRoleMappingByGroupID(ctx context.Context, token, realm, groupID string) (int, *MappingsRepresentation, error) {
return g.getRoleMappings(ctx, token, realm, "groups", groupID)
}
// GetRoleMappingByUserID gets the role mappings by user
func (g *GoKeycloak) GetRoleMappingByUserID(ctx context.Context, token, realm, userID string) (int, *MappingsRepresentation, error) {
return g.getRoleMappings(ctx, token, realm, "users", userID)
}
// GetClientRoles get all roles for the given client in realm
func (g *GoKeycloak) GetClientRoles(ctx context.Context, token, realm, idOfClient string, params GetRoleParams) (int, []*Role, error) {
const errMessage = "could not get client roles"
var result []*Role
queryParams, err := GetQueryParams(params)
if err != nil {
return http.StatusInternalServerError, nil, errors.Wrap(err, errMessage)
}
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
SetQueryParams(queryParams).
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "roles"))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}
// GetClientRoleByID gets role for the given client in realm using role ID
func (g *GoKeycloak) GetClientRoleByID(ctx context.Context, token, realm, roleID string) (int, *Role, error) {
const errMessage = "could not get client role"
var result Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "roles-by-id", roleID))
if err := checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), &result, nil
}
// GetClientRolesByUserID returns all client roles assigned to the given user
func (g *GoKeycloak) GetClientRolesByUserID(ctx context.Context, token, realm, idOfClient, userID string) (int, []*Role, error) {
const errMessage = "could not client roles by user id"
var result []*Role
resp, err := g.GetRequestWithBearerAuth(ctx, token).
SetResult(&result).
Get(g.getAdminRealmURL(realm, "users", userID, "role-mappings", "clients", idOfClient))
if err = checkForError(resp, err, errMessage); err != nil {
return resp.StatusCode(), nil, err
}
return resp.StatusCode(), result, nil
}