-
Notifications
You must be signed in to change notification settings - Fork 14
/
resources.go
94 lines (79 loc) · 3.13 KB
/
resources.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
package keycloak
import (
"context"
"fmt"
"net/http"
)
// ResourcesService handles communication with the resources related methods of the Keycloak API.
type ResourcesService service
// ResourceOwner represents a Keycloak resource owner.
//
// https://github.com/keycloak/keycloak/blob/master/core/src/main/java/org/keycloak/representations/idm/authorization/ResourceOwnerRepresentation.java
type ResourceOwner struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
}
// Resource represents a Keycloak resource.
//
// https://github.com/keycloak/keycloak/blob/master/core/src/main/java/org/keycloak/representations/idm/authorization/ResourceRepresentation.java
type Resource struct {
ID *string `json:"_id,omitempty"`
Scopes []*Scope `json:"scopes,omitempty"`
Attributes *map[string][]string `json:"attributes,omitempty"`
Uris []string `json:"uris,omitempty"`
Name *string `json:"name,omitempty"`
OwnerManagedAccess *bool `json:"ownerManagedAccess,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
Owner *ResourceOwner `json:"owner,omitempty"`
}
// List lists all resources.
func (s *ResourcesService) List(ctx context.Context, realm, clientID string) ([]*Resource, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/clients/%s/authz/resource-server/resource", realm, clientID)
req, err := s.keycloak.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var resources []*Resource
res, err := s.keycloak.Do(ctx, req, &resources)
if err != nil {
return nil, nil, err
}
return resources, res, nil
}
// Create creates a new resource.
func (s *ResourcesService) Create(ctx context.Context, realm, clientID string, resource *Resource) (*Resource, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/clients/%s/authz/resource-server/resource", realm, clientID)
req, err := s.keycloak.NewRequest(http.MethodPost, u, resource)
if err != nil {
return nil, nil, err
}
var created Resource
res, err := s.keycloak.Do(ctx, req, &created)
if err != nil {
return nil, nil, err
}
return &created, res, nil
}
// Get gets a single resource.
func (s *ResourcesService) Get(ctx context.Context, realm, clientID, resourceID string) (*Resource, *http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/clients/%s/authz/resource-server/resource/%s", realm, clientID, resourceID)
req, err := s.keycloak.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var resource Resource
res, err := s.keycloak.Do(ctx, req, &resource)
if err != nil {
return nil, nil, err
}
return &resource, res, nil
}
// Delete deletes a single resource.
func (s *ResourcesService) Delete(ctx context.Context, realm, clientID, resourceID string) (*http.Response, error) {
u := fmt.Sprintf("admin/realms/%s/clients/%s/authz/resource-server/resource/%s", realm, clientID, resourceID)
req, err := s.keycloak.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, err
}
return s.keycloak.Do(ctx, req, nil)
}