Skip to content

Commit f649766

Browse files
authored
Merge pull request #310 from stevezau/custom_domains_support
Custom domains support
2 parents 66cca83 + 0593007 commit f649766

File tree

3 files changed

+703
-0
lines changed

3 files changed

+703
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
var ArgumentError = require('rest-facade').ArgumentError;
2+
var utils = require('../utils');
3+
var Auth0RestClient = require('../Auth0RestClient');
4+
var RetryRestClient = require('../RetryRestClient');
5+
6+
/**
7+
* @class CustomDomainsManager
8+
* Auth0 Custom Domains Manager.
9+
*
10+
* {@link https://auth0.com/docs/api/management/v2#!/Custom_Domains/get_custom_domains CustomDomains} represent
11+
* custom domain names.
12+
* You can learn more about this in the
13+
* {@link https://auth0.com/docs/custom-domains CustomDomains} section of the
14+
* documentation.
15+
* @constructor
16+
* @memberOf module:management
17+
*
18+
* @param {Object} options The client options.
19+
* @param {String} options.baseUrl The URL of the API.
20+
* @param {Object} [options.headers] Headers to be included in all requests.
21+
* @param {Object} [options.retry] Retry Policy Config
22+
*/
23+
var CustomDomainsManager = function(options) {
24+
if (options === null || typeof options !== 'object') {
25+
throw new ArgumentError('Must provide manager options');
26+
}
27+
28+
if (options.baseUrl === null || options.baseUrl === undefined) {
29+
throw new ArgumentError('Must provide a base URL for the API');
30+
}
31+
32+
if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
33+
throw new ArgumentError('The provided base URL is invalid');
34+
}
35+
36+
/**
37+
* Options object for the Rest Client instance.
38+
*
39+
* @type {Object}
40+
*/
41+
var clientOptions = {
42+
errorFormatter: { message: 'message', name: 'error' },
43+
headers: options.headers,
44+
query: { repeatParams: false }
45+
};
46+
47+
/**
48+
* Provides an abstraction layer for consuming the
49+
* {@link https://auth0.com/docs/api/v2#!/Custom_Domains Auth0 Custom Domains endpoint}.
50+
*
51+
* @type {external:RestClient}
52+
*/
53+
var auth0CustomDomainsRestClient = new Auth0RestClient(
54+
options.baseUrl + '/custom-domains/:id',
55+
clientOptions,
56+
options.tokenProvider
57+
);
58+
this.resource = new RetryRestClient(auth0CustomDomainsRestClient, options.retry);
59+
60+
/**
61+
* Provides an abstraction layer for consuming the
62+
* {@link https://auth0.com/docs/api/v2#!/Custom_Domains Auth0 Custom Domains Verify endpoint}.
63+
*
64+
* @type {external:RestClient}
65+
*/
66+
var auth0VerifyRestClient = new Auth0RestClient(
67+
options.baseUrl + '/custom-domains/:id/verify',
68+
clientOptions,
69+
options.tokenProvider
70+
);
71+
this.vefifyResource = new RetryRestClient(auth0VerifyRestClient, options.retry);
72+
};
73+
74+
/**
75+
* Create an Auth0 Custom Domain.
76+
*
77+
* @method create
78+
* @memberOf module:management.CustomDomainsManager.prototype
79+
*
80+
* @example
81+
* management.customDomains.create(data, function (err) {
82+
* if (err) {
83+
* // Handle error.
84+
* }
85+
*
86+
* // CustomDomain created.
87+
* });
88+
*
89+
* @param {Object} data The custom domain data object.
90+
* @param {Function} [cb] Callback function.
91+
*
92+
* @return {Promise|undefined}
93+
*/
94+
utils.wrapPropertyMethod(CustomDomainsManager, 'create', 'resource.create');
95+
96+
/**
97+
* Get all Auth0 CustomDomains.
98+
*
99+
* @method getAll
100+
* @memberOf module:management.CustomDomainsManager.prototype
101+
*
102+
* @example
103+
* management.customDomains.getAll(function (err, customDomains) {
104+
* console.log(customDomains.length);
105+
* });
106+
*
107+
* @return {Promise|undefined}
108+
*/
109+
utils.wrapPropertyMethod(CustomDomainsManager, 'getAll', 'resource.getAll');
110+
111+
/**
112+
* Get a Custom Domain.
113+
*
114+
* @method get
115+
* @memberOf module:management.CustomDomainsManager.prototype
116+
*
117+
* @example
118+
* management.customDomains.get({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) {
119+
* if (err) {
120+
* // Handle error.
121+
* }
122+
*
123+
* console.log(customDomain);
124+
* });
125+
*
126+
* @param {Object} params Custom Domain parameters.
127+
* @param {String} params.id Custom Domain ID.
128+
* @param {Function} [cb] Callback function.
129+
*
130+
* @return {Promise|undefined}
131+
*/
132+
utils.wrapPropertyMethod(CustomDomainsManager, 'get', 'resource.get');
133+
134+
/**
135+
* Verify a Custom Domain.
136+
*
137+
* @method verify
138+
* @memberOf module:management.CustomDomainsManager.prototype
139+
*
140+
* @example
141+
* management.customDomains.verify({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) {
142+
* if (err) {
143+
* // Handle error.
144+
* }
145+
*
146+
* console.log(customDomain);
147+
* });
148+
*
149+
* @param {Object} params Custom Domain parameters.
150+
* @param {String} params.id Custom Domain ID.
151+
* @param {Function} [cb] Callback function.
152+
*
153+
* @return {Promise|undefined}
154+
*/
155+
CustomDomainsManager.prototype.verify = function(params, cb) {
156+
if (!params || !params.id) {
157+
throw new ArgumentError('The custom domain id cannot be null or undefined');
158+
}
159+
160+
if (cb && cb instanceof Function) {
161+
return this.vefifyResource.create(params, {}, cb);
162+
}
163+
164+
return this.vefifyResource.create(params, {});
165+
};
166+
167+
/**
168+
* Delete a Custom Domain.
169+
*
170+
* @method delete
171+
* @memberOf module:management.CustomDomainsManager.prototype
172+
*
173+
* @example
174+
* management.customDomains.delete({ id: CUSTOM_DOMAIN_ID }, function (err) {
175+
* if (err) {
176+
* // Handle error.
177+
* }
178+
*
179+
* // CustomDomain deleted.
180+
* });
181+
*
182+
* @param {Object} params Custom Domain parameters.
183+
* @param {String} params.id Custom Domain ID.
184+
* @param {Function} [cb] Callback function.
185+
*
186+
* @return {Promise|undefined}
187+
*/
188+
utils.wrapPropertyMethod(CustomDomainsManager, 'delete', 'resource.delete');
189+
190+
module.exports = CustomDomainsManager;

src/management/index.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var ManagementTokenProvider = require('./ManagementTokenProvider');
2727
var RulesConfigsManager = require('./RulesConfigsManager');
2828
var EmailTemplatesManager = require('./EmailTemplatesManager');
2929
var GuardianManager = require('./GuardianManager');
30+
var CustomDomainsManager = require('./CustomDomainsManager');
3031

3132
var BASE_URL_FORMAT = 'https://%s/api/v2';
3233
var MANAGEMENT_API_AUD_FORMAT = 'https://%s/api/v2/';
@@ -164,6 +165,14 @@ var ManagementClient = function(options) {
164165
*/
165166
this.guardian = new GuardianManager(managerOptions);
166167

168+
/**
169+
* Simple abstraction for performing CRUD operations on the
170+
* custom domains endpoint.
171+
*
172+
* @type {CustomDomainsManager}
173+
*/
174+
this.customDomains = new CustomDomainsManager(managerOptions);
175+
167176
/**
168177
* Simple abstraction for performing CRUD operations on the
169178
* connections endpoint.
@@ -1929,6 +1938,112 @@ utils.wrapPropertyMethod(ManagementClient, 'getRulesConfigs', 'rulesConfigs.getA
19291938
*/
19301939
utils.wrapPropertyMethod(ManagementClient, 'deleteRulesConfig', 'rulesConfigs.delete');
19311940

1941+
/**
1942+
* Create an Auth0 Custom Domain.
1943+
*
1944+
* @method create
1945+
* @memberOf module:management.CustomDomainsManager.prototype
1946+
*
1947+
* @example
1948+
* management.createCustomDomain(data, function (err) {
1949+
* if (err) {
1950+
* // Handle error.
1951+
* }
1952+
*
1953+
* // CustomDomain created.
1954+
* });
1955+
*
1956+
* @param {Object} data The custom domain data object.
1957+
* @param {Function} [cb] Callback function.
1958+
*
1959+
* @return {Promise|undefined}
1960+
*/
1961+
utils.wrapPropertyMethod(ManagementClient, 'createCustomDomain', 'customDomains.create');
1962+
1963+
/**
1964+
* Get all Auth0 CustomDomains.
1965+
*
1966+
* @method getAll
1967+
* @memberOf module:management.CustomDomainsManager.prototype
1968+
*
1969+
* @example
1970+
* management.getCustomDomains(function (err, customDomains) {
1971+
* console.log(customDomains.length);
1972+
* });
1973+
*
1974+
* @return {Promise|undefined}
1975+
*/
1976+
utils.wrapPropertyMethod(ManagementClient, 'getCustomDomains', 'customDomains.getAll');
1977+
1978+
/**
1979+
* Get a Custom Domain.
1980+
*
1981+
* @method get
1982+
* @memberOf module:management.CustomDomainsManager.prototype
1983+
*
1984+
* @example
1985+
* management.getCustomDomain({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) {
1986+
* if (err) {
1987+
* // Handle error.
1988+
* }
1989+
*
1990+
* console.log(customDomain);
1991+
* });
1992+
*
1993+
* @param {Object} params Custom Domain parameters.
1994+
* @param {String} params.id Custom Domain ID.
1995+
* @param {Function} [cb] Callback function.
1996+
*
1997+
* @return {Promise|undefined}
1998+
*/
1999+
utils.wrapPropertyMethod(ManagementClient, 'getCustomDomain', 'customDomains.get');
2000+
2001+
/**
2002+
* Verify a Custom Domain.
2003+
*
2004+
* @method verify
2005+
* @memberOf module:management.CustomDomainsManager.prototype
2006+
*
2007+
* @example
2008+
* management.verifyCustomDomain({ id: CUSTOM_DOMAIN_ID }, function (err, customDomain) {
2009+
* if (err) {
2010+
* // Handle error.
2011+
* }
2012+
*
2013+
* console.log(customDomain);
2014+
* });
2015+
*
2016+
* @param {Object} params Custom Domain parameters.
2017+
* @param {String} params.id Custom Domain ID.
2018+
* @param {Function} [cb] Callback function.
2019+
*
2020+
* @return {Promise|undefined}
2021+
*/
2022+
utils.wrapPropertyMethod(ManagementClient, 'verifyCustomDomain', 'customDomains.verify');
2023+
2024+
/**
2025+
* Delete a Custom Domain.
2026+
*
2027+
* @method delete
2028+
* @memberOf module:management.CustomDomainsManager.prototype
2029+
*
2030+
* @example
2031+
* management.deleteCustomDomain({ id: CUSTOM_DOMAIN_ID }, function (err) {
2032+
* if (err) {
2033+
* // Handle error.
2034+
* }
2035+
*
2036+
* // CustomDomain deleted.
2037+
* });
2038+
*
2039+
* @param {Object} params Custom Domain parameters.
2040+
* @param {String} params.id Custom Domain ID.
2041+
* @param {Function} [cb] Callback function.
2042+
*
2043+
* @return {Promise|undefined}
2044+
*/
2045+
utils.wrapPropertyMethod(ManagementClient, 'deleteCustomDomain', 'customDomains.delete');
2046+
19322047
/**
19332048
* Create a Guardian enrollment ticket.
19342049
*

0 commit comments

Comments
 (0)