-
Notifications
You must be signed in to change notification settings - Fork 14
/
keycloak.go
158 lines (130 loc) · 3.48 KB
/
keycloak.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
package keycloak
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
"github.com/google/go-querystring/query"
)
// Keycloak ...
type Keycloak struct {
client *http.Client
BaseURL *url.URL
common service
Clients *ClientsService
ClientRoles *ClientRolesService
ClientScopes *ClientScopesService
Groups *GroupsService
Permissions *PermissionsService
Policies *PoliciesService
Realms *RealmsService
RealmRoles *RealmRolesService
Resources *ResourcesService
Scopes *ScopesService
Users *UsersService
}
type service struct {
keycloak *Keycloak
}
// addOptions adds the parameters in opts as URL query parameters to s. opts
// must be a struct whose fields may contain "url" tags.
func addOptions(s string, opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opts)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}
// NewKeycloak ...
func NewKeycloak(httpClient *http.Client, baseURL string) (*Keycloak, error) {
if httpClient == nil {
httpClient = &http.Client{}
}
uri, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
k := &Keycloak{
client: httpClient,
BaseURL: uri,
}
k.common.keycloak = k
k.Clients = (*ClientsService)(&k.common)
k.ClientRoles = (*ClientRolesService)(&k.common)
k.ClientScopes = (*ClientScopesService)(&k.common)
k.Groups = (*GroupsService)(&k.common)
k.Permissions = (*PermissionsService)(&k.common)
k.Policies = (*PoliciesService)(&k.common)
k.Realms = (*RealmsService)(&k.common)
k.RealmRoles = (*RealmRolesService)(&k.common)
k.Resources = (*ResourcesService)(&k.common)
k.Scopes = (*ScopesService)(&k.common)
k.Users = (*UsersService)(&k.common)
return k, nil
}
// NewRequest ...
func (k *Keycloak) NewRequest(method string, url string, body interface{}) (*http.Request, error) {
if !strings.HasSuffix(k.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", k.BaseURL)
}
u, err := k.BaseURL.Parse(url)
if err != nil {
return nil, err
}
var b io.ReadWriter
if body != nil {
b = &bytes.Buffer{}
if err := json.NewEncoder(b).Encode(body); err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), b)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}
// Do ...
func (k *Keycloak) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
req = req.WithContext(ctx)
res, err := k.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if v != nil {
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
return nil, err
}
}
return res, err
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// // Int is a helper routine that allocates a new int value
// // to store v and returns a pointer to it.
// func Int(v int) *int { return &v }
// // Int64 is a helper routine that allocates a new int64 value
// // to store v and returns a pointer to it.
// func Int64(v int64) *int64 { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }