@@ -4,25 +4,28 @@ const CoreError = require('./../CoreError');
4
4
const Token = require ( './token' ) ;
5
5
6
6
/**
7
- * Cayenne Core Auth Class
7
+ * Auth provides the client credential token handling for the Cayenne Core services
8
8
*/
9
9
class Auth {
10
10
/**
11
11
* Constructs the Cayenne Core authenticator
12
12
*
13
- * @param {String } clientId
14
- * @param {String } clientSecret
15
- * @param {String } [authorizationUrl]
16
- * @param {Object } options
17
- * @param {Function | Object | Boolean } [options.logger]
18
- * @param {Boolean } [options.isOffline=true]
13
+ * @param {String } clientId Cayenne Core client id
14
+ * @param {String } clientSecret Cayenne Core client secret
15
+ * @param {String } [authorizationUrl] Authorization URL to authenticate against
16
+ * @param {Object } [options={}] Additional options
17
+ * @param {Function | Object | Boolean } [options.logger] Debug, info, and error logger to use
18
+ * @param {Boolean } [options.isOffline=true] Provides a session that can be used indefinitely by refreshing
19
19
*/
20
- constructor ( clientId , clientSecret , authorizationUrl , options ) {
20
+ constructor ( clientId , clientSecret , authorizationUrl , options = { } ) {
21
21
/**
22
22
* Constants
23
23
*/
24
+ /** @private @contant */
24
25
this . OFFLINE_SCOPE = 'offline_access' ;
26
+ /** @private @contant */
25
27
this . MAX_RETRIES = 1 ;
28
+ /** @private @contant */
26
29
this . DEFAULT_AUTHORIZATION_URL =
27
30
'https://accounts.mydevices.com/auth/realms/cayenne-core/protocol/openid-connect/token' ;
28
31
@@ -38,18 +41,19 @@ class Auth {
38
41
this . logger = options . logger ;
39
42
this . isOffline = options . isOffline ;
40
43
44
+ /** @type CoreError */
41
45
this . invalidCredentials ;
42
46
this . access = new Token ( ) ;
43
47
}
44
48
45
49
/**
46
- * @function
47
- * @private
48
- *
49
50
* Get authorization token
50
51
*
52
+ * @private
53
+ *
51
54
* @param {Number } [retry] Current retry count
52
- * @returns {Promise } Valid access token
55
+ *
56
+ * @returns {Promise<String> } Valid access token
53
57
*/
54
58
async getApplicationToken ( retry = 0 ) {
55
59
if ( retry > this . MAX_RETRIES ) {
@@ -115,20 +119,23 @@ class Auth {
115
119
}
116
120
117
121
/**
118
- * @function
119
- * @public
120
- *
121
122
* Send a request to Core service
122
123
*
123
- * @param {String } service Core service being requested
124
- * @param {String } method ReST method to use
125
- * @param {String } url Endpoint
126
- * @param {Object } opts Additional options
127
- * @param {Object } [opts.headers={}] Headers to send
128
- * @param {Object } [opts.query] Query to send
129
- * @param {Object } [opts.payload] Payload to send
124
+ * @public
125
+ *
126
+ * @param {String } service Core service being requested
127
+ * @param {String } method ReST method to use
128
+ * @param {String } url Full cayenne core path
129
+ * @param {Object } opts Additional options
130
+ * @param {Object } [opts.headers={}] Headers to send
131
+ * @param {Object } [opts.query] Query to send
132
+ * @param {Object } [opts.payload] Payload to send
130
133
* @param {boolean } [opts.isPublic=false] If this request should include authorization
131
- * @param {Number } retry Allow retries in case of expired token
134
+ * @param {Number } retry Allow retries in case of expired token
135
+ *
136
+ * @returns {Promise<{} | Array<{}>> }
137
+ *
138
+ * @throws {CoreError } Returns erroror max authenticated request limit reached
132
139
*/
133
140
async send ( service , method , url , opts , retry = 0 ) {
134
141
if ( this . invalidCredentials ) {
@@ -190,17 +197,18 @@ class Auth {
190
197
}
191
198
192
199
/**
193
- * @function
200
+ * Create the Core Error and log it
201
+ *
194
202
* @private
195
203
*
196
- * Create the Core Error and log it
204
+ * @param {typeof Error } error Error returned
205
+ * @param {String } url Endpoint
206
+ * @param {String } service Core service requested
207
+ * @param {String } method REST method used
208
+ * @param {Number } tDelta Request time in ms
209
+ * @param {Object } query The query sent
197
210
*
198
- * @param {typeof Error } error Error returned
199
- * @param {String } url Endpoint
200
- * @param {String } service Core service requested
201
- * @param {String } method REST method used
202
- * @param {Number } tDelta Request time in ms
203
- * @param {Object } query The query sent
211
+ * @returns {CoreError }
204
212
*/
205
213
getError ( error , url , service , method , tDelta , query ) {
206
214
const log = ( ) => {
@@ -229,22 +237,23 @@ class Auth {
229
237
}
230
238
231
239
/**
232
- * @function
233
- * @private
234
- *
235
240
* Returns timedelta in ms
236
241
*
242
+ * @private
243
+ *
237
244
* @param {Number } begin ms
245
+ *
246
+ * @returns {Number }
238
247
*/
239
248
timeDelta ( begin ) {
240
249
return new Date ( ) . getTime ( ) - begin ;
241
250
}
242
251
243
252
/**
244
- * @function
253
+ * Log the message if enabled and with applicable log level
254
+ *
245
255
* @public
246
256
*
247
- * Log the message if enabled and with applicable log level
248
257
* @param {String } message
249
258
* @param {String } level
250
259
*/
0 commit comments