-
Notifications
You must be signed in to change notification settings - Fork 303
/
DeviceCredentialsManager.js
125 lines (109 loc) · 3.22 KB
/
DeviceCredentialsManager.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
var RestClient = require('rest-facade').Client;
var ArgumentError = require('rest-facade').ArgumentError;
var utils = require('../utils');
/**
* Simple facade for consuming a REST API endpoint.
* @external RestClient
* @see https://github.com/ngonzalvez/rest-facade
*/
/**
* @class DeviceCredentialsManager
* Manages Auth0 Device Credentials.
* @constructor
* @memberOf module:management
*
* @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.
*/
var DeviceCredentialsManager = function (options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide manager 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 RestClient instance.
*
* @type {Object}
*/
var 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#!/Device_Credentials
* Auth0 DeviceCredentialsManagers endpoint}.
*
* @type {external:RestDeviceCredentialsManager}
*/
this.resource = new RestClient(options.baseUrl + '/device-credentials/:id', clientOptions);
};
/**
* Create an Auth0 credential.
*
* @method create
* @memberOf module:management.DeviceCredentialsManager.prototype
*
* @example
* management.deviceCredentials.create(data, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Credential created.
* });
*
* @param {Object} data The device credential data object.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(DeviceCredentialsManager, 'createPublicKey', 'resource.create');
/**
* Get all Auth0 credentials.
*
* @method getAll
* @memberOf module:management.DeviceCredentialsManager.prototype
*
* @example
* management.deviceCredentials.getAll(function (err, credentials) {
* console.log(credentials.length);
* });
*
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(DeviceCredentialsManager, 'getAll', 'resource.getAll');
/**
* Delete an Auth0 device credential.
*
* @method delete
* @memberOf module:management.DeviceCredentialsManager.prototype
*
* @example
* var params = { id: CREDENTIAL_ID };
*
* management.deviceCredentials.delete(params, function (err) {
* if (err) {
* // Handle error.
* }
*
* // Credential deleted.
* });
*
* @param {Object} params Credential parameters.
* @param {String} params.id Device credential ID.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(DeviceCredentialsManager, 'delete', 'resource.delete');
module.exports = DeviceCredentialsManager;