forked from go-auth0/auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management.go
284 lines (229 loc) · 6.3 KB
/
management.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
package management
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// Management is an Auth0 management client used to interact with the Auth0
// Management API v2.
//
type Management struct {
// Client manages Auth0 Client (also known as Application) resources.
Client *ClientManager
// ClientGrant manages Auth0 ClientGrant resources.
ClientGrant *ClientGrantManager
// ResourceServer manages Auth0 Resource Server (also known as API)
// resources.
ResourceServer *ResourceServerManager
// Connection manages Auth0 Connection resources.
Connection *ConnectionManager
// CustomDomain manages Auth0 Custom Domains.
CustomDomain *CustomDomainManager
// Grant manages Auth0 Grants.
Grant *GrantManager
// Log reads Auth0 Logs.
Log *LogManager
// RuleManager manages Auth0 Rules.
Rule *RuleManager
// RuleManager manages Auth0 Rule Configurations.
RuleConfig *RuleConfigManager
// Email manages Auth0 Email Providers.
Email *EmailManager
// EmailTemplate manages Auth0 Email Templates.
EmailTemplate *EmailTemplateManager
// User manages Auth0 User resources.
User *UserManager
// Job manages Auth0 jobs.
Job *JobManager
// Tenant manages your Auth0 Tenant.
Tenant *TenantManager
// Ticket creates verify email or change password tickets.
Ticket *TicketManager
// Stat is used to retrieve usage statistics.
Stat *StatManager
domain string
basePath string
timeout time.Duration
debug bool
http *http.Client
}
// New creates a new Auth0 Management client by authenticating using the
// supplied client id and secret.
func New(domain, clientID, clientSecret string, options ...apiOption) (*Management, error) {
m := &Management{
domain: domain,
basePath: "api/v2",
timeout: 1 * time.Minute,
debug: false,
}
for _, option := range options {
option(m)
}
m.http = newClient(domain, clientID, clientSecret, m.debug)
m.Client = NewClientManager(m)
m.ClientGrant = NewClientGrantManager(m)
m.Connection = NewConnectionManager(m)
m.CustomDomain = NewCustomDomainManager(m)
m.Grant = NewGrantManager(m)
m.Log = NewLogManager(m)
m.ResourceServer = NewResourceServerManager(m)
m.Rule = NewRuleManager(m)
m.RuleConfig = NewRuleConfigManager(m)
m.EmailTemplate = NewEmailTemplateManager(m)
m.Email = NewEmailManager(m)
m.User = NewUserManager(m)
m.Job = NewJobManager(m)
m.Tenant = NewTenantManager(m)
m.Ticket = NewTicketManager(m)
m.Stat = NewStatManager(m)
return m, nil
}
func (m *Management) uri(path ...string) string {
return (&url.URL{
Scheme: "https",
Host: m.domain,
Path: m.basePath + "/" + strings.Join(path, "/"),
}).String()
}
func (m *Management) q(options []reqOption) string {
if len(options) == 0 {
return ""
}
v := make(url.Values)
for _, option := range options {
option(v)
}
return "?" + v.Encode()
}
func (m *Management) request(method, uri string, v interface{}) error {
var payload bytes.Buffer
if v != nil {
json.NewEncoder(&payload).Encode(v)
}
req, _ := http.NewRequest(method, uri, &payload)
req.Header.Add("Content-Type", "application/json")
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
defer cancel()
if m.http == nil {
m.http = http.DefaultClient
}
res, err := m.http.Do(req.WithContext(ctx))
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
return err
}
}
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
return newError(res.Body)
}
if res.StatusCode != http.StatusNoContent {
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(v)
}
return nil
}
func (m *Management) get(uri string, v interface{}) error {
return m.request("GET", uri, v)
}
func (m *Management) post(uri string, v interface{}) error {
return m.request("POST", uri, v)
}
func (m *Management) put(uri string, v interface{}) error {
return m.request("PUT", uri, v)
}
func (m *Management) patch(uri string, v interface{}) error {
return m.request("PATCH", uri, v)
}
func (m *Management) delete(uri string) error {
return m.request("DELETE", uri, nil)
}
type apiOption func(*Management)
// WithTimeout configures the management client with a request timeout.
func WithTimeout(t time.Duration) apiOption {
return func(m *Management) {
m.timeout = t
}
}
// WithDebug configures the management client to dump http requests and
// responses to stdout.
func WithDebug(d bool) apiOption {
return func(m *Management) {
m.debug = d
}
}
type Error interface {
Status() int
error
}
type managementError struct {
StatusCode int `json:"statusCode"`
Err string `json:"error"`
Message string `json:"message"`
}
func newError(r io.Reader) error {
m := &managementError{}
err := json.NewDecoder(r).Decode(m)
if err != nil {
return err
}
return m
}
func (m *managementError) Error() string {
return fmt.Sprintf("%d %s: %s", m.StatusCode, m.Err, m.Message)
}
func (m *managementError) Status() int {
return m.StatusCode
}
// reqOption configures a call (typically to retrieve a resource) to Auth0 with
// query parameters.
type reqOption func(url.Values)
// WithFields configures a call to include the desired fields.
func WithFields(fields ...string) reqOption {
return func(v url.Values) {
v.Set("fields", strings.Join(fields, ","))
v.Set("include_fields", "true")
}
}
// WithoutFields configures a call to exclude the desired fields.
func WithoutFields(fields ...string) reqOption {
return func(v url.Values) {
v.Set("fields", strings.Join(fields, ","))
v.Set("include_fields", "false")
}
}
// Page configures a call to receive a specific page, if the results where
// concatenated.
func Page(page int) reqOption {
return func(v url.Values) {
v.Set("page", strconv.FormatInt(int64(page), 10))
}
}
// PerPage configures a call to limit the amount of items in the result.
func PerPage(items int) reqOption {
return func(v url.Values) {
v.Set("per_page", strconv.FormatInt(int64(items), 10))
}
}
// IncludeTotals configures a call to include totals.
func IncludeTotals(include bool) reqOption {
return func(v url.Values) {
v.Set("include_totals", strconv.FormatBool(include))
}
}
// Parameter is a generic configuration to add arbitrary query parameters to
// calls made to Auth0.
func Parameter(key, value string) reqOption {
return func(v url.Values) {
v.Set(key, value)
}
}