-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cAuthJWT.js
More file actions
76 lines (57 loc) · 2.44 KB
/
_b2cAuthJWT.js
File metadata and controls
76 lines (57 loc) · 2.44 KB
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
'use strict';
// Initialize constants
const config = require('config');
// Include Account Manager clientCredential grant via JWT function
const jwtClientCredentialsGrant = require('../apis/sfcc/ocapi/auth/_amClientCredentialsGrantJWT');
/**
* @function _b2cAuthJWT
* @description Attempts to authenticate against the B2C Commerce Account Manager leveraging JWT.
*
* @param {Object} environmentDef Represents the already-validated environment
* details to use when performing the actions
* @returns {Promise} Returns the results of the JWT authentication event
*/
module.exports = environmentDef => new Promise(async (resolve, reject) => {
// Initialize the output
const output = {
apiCalls: {
authenticate: {}
},
outputDisplay: [],
errorOutputDisplay: []
};
try {
// Execute the BM User authentication attempt
output.apiCalls.authenticate = await jwtClientCredentialsGrant(environmentDef);
// Was an authentication error caught?
if (output.apiCalls.authenticate.status !== 200) {
// Capture the error message
output.success = false;
// Build the output display
output.errorOutputDisplay = [
['errorType', output.apiCalls.authenticate.data.error],
['errorMessage', output.apiCalls.authenticate.data.error_description]
];
} else {
// Capture the authToken and build out the display details
output.authToken = output.apiCalls.authenticate.data.access_token;
output.success = true;
// Build the output display
output.outputDisplay = [
['authToken', output.apiCalls.authenticate.data.access_token]
];
}
// Append the remaining common output attributes
output.outputDisplay = output.outputDisplay.concat([
['requestUrl', output.apiCalls.authenticate.jwt.amUrl],
['audienceUrl', output.apiCalls.authenticate.jwt.audienceUrl],
['jwtHeader', JSON.stringify(output.apiCalls.authenticate.jwt.jwt.header, null, 4)],
['jwtOptions', JSON.stringify(output.apiCalls.authenticate.jwt.jwt.options, null, 4)]
]);
// Resolve the request successfully
return resolve(output);
} catch (e) {
// Otherwise, throw an error
return reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
}
});