forked from auth0/node-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientsManager.js
214 lines (198 loc) · 6.1 KB
/
ClientsManager.js
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
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../Auth0RestClient');
const RetryRestClient = require('../RetryRestClient');
/**
* Auth0 Clients Manager.
*
* {@link https://auth0.com/docs/api/v2#!/Clients Clients} represent
* applications.
* You can learn more about this in the
* {@link https://auth0.com/docs/applications Applications} section of the
* documentation.
*/
class ClientsManager {
/**
* @param {object} options The client options.
* @param {string} options.baseUrl The URL of the API.
* @param {object} [options.headers] Headers to be included in all requests.
* @param {object} [options.retry] Retry Policy Config
*/
constructor(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide client options');
}
if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}
if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}
/**
* Options object for the Rest Client instance.
*
* @type {object}
*/
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false },
};
/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/v2#!/Clients Auth0 Clients endpoint}.
*
* @type {external:RestClient}
*/
const auth0RestClient = new Auth0RestClient(
`${options.baseUrl}/clients/:client_id`,
clientOptions,
options.tokenProvider
);
this.resource = new RetryRestClient(auth0RestClient, options.retry);
/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/management/v2#!/Clients/post_rotate_secret Auth0 Clients Rotate a client secret}.
*
* @type {external:RestClient}
*/
const auth0RotateSecretClient = new Auth0RestClient(
`${options.baseUrl}/clients/:client_id/rotate-secret`,
clientOptions,
options.tokenProvider
);
this.rotateSecretResource = new RetryRestClient(auth0RotateSecretClient, options.retry);
}
/**
* Create an Auth0 client.
*
* @example
* management.clients.create(data, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Client created.
* });
* @param {object} data The client data object.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
create(...args) {
return this.resource.create(...args);
}
/**
* Get all Auth0 clients.
*
* @example <caption>
* This method takes an optional object as first argument that may be used to
* specify pagination settings. If pagination options are not present,
* the first page of a limited number of results will be returned.
* </caption>
*
* // Pagination settings.
* var params = {
* per_page: 10,
* page: 0
* };
*
* management.clients.getAll(params, function (err, clients) {
* console.log(clients.length);
* });
* @param {object} [params] Clients parameters.
* @param {number} [params.per_page] Number of results per page.
* @param {number} [params.page] Page number, zero indexed.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
getAll(...args) {
return this.resource.getAll(...args);
}
/**
* Get an Auth0 client.
*
* @example
* management.clients.get({ client_id: CLIENT_ID }, function (err, client) {
* if (err) {
* // Handle error.
* }
*
* console.log(client);
* });
* @param {object} params Client parameters.
* @param {string} params.client_id Application client ID.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
get(...args) {
return this.resource.get(...args);
}
/**
* Update an Auth0 client.
*
* @example
* var data = { name: 'newClientName' };
* var params = { client_id: CLIENT_ID };
*
* management.clients.update(params, data, function (err, client) {
* if (err) {
* // Handle error.
* }
*
* console.log(client.name); // 'newClientName'
* });
* @param {object} params Client parameters.
* @param {string} params.client_id Application client ID.
* @param {object} data Updated client data.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
update(...args) {
return this.resource.patch(...args);
}
/**
* Delete an Auth0 client.
*
* @example
* management.clients.delete({ client_id: CLIENT_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Client deleted.
* });
* @param {object} params Client parameters.
* @param {string} params.client_id Application client ID.
* @param {Function} [cb] Callback function.
* @returns {Promise|undefined}
*/
delete(...args) {
return this.resource.delete(...args);
}
/**
* Rotate a client secret
*
* @example
* management.clients.rotateClientSecret({ client_id: CLIENT_ID }, function (err, user) {
* if (err) {
* // Handle error.
* }
*
* // Client secret rotated.
* });
* @param {object} params params object
* @param {string} params.client_id Application client ID.
* @returns {Promise|undefined}
*/
rotateClientSecret(params, cb) {
const query = params || {};
// Require a client ID.
if (!params.client_id) {
throw new ArgumentError('The client_id cannot be null or undefined');
}
if (cb && cb instanceof Function) {
return this.rotateSecretResource.create(query, {}, cb);
}
return this.rotateSecretResource.create(query, {});
}
}
module.exports = ClientsManager;