/
AuthAction.js
99 lines (87 loc) · 3.43 KB
/
AuthAction.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
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { Core } = require('@adobe/aio-sdk');
const QueryStringAddon = require('wretch/addons/queryString');
const { ImsClient } = require('./ImsClient.js');
const { wretch } = require('./Network.js');
const logger = Core.Logger('AuthAction');
async function isValidToken(endpoint, clientId, token) {
try {
const response = await wretch(`${endpoint}/ims/validate_token/v1`)
.addon(QueryStringAddon).query({
client_id: clientId,
type: 'access_token',
})
.headers({
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
})
.get()
.json();
const { valid } = response;
return valid;
} catch (error) {
logger.error(error);
return false;
}
}
async function checkForProductContext(endpoint, clientId, org, token, productContext) {
try {
const response = await wretch(`${endpoint}/ims/profile/v1`)
.addon(QueryStringAddon).query({
client_id: clientId,
})
.headers({
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
})
.get()
.json();
if (Array.isArray(response.projectedProductContext)) {
const filteredProductContext = response.projectedProductContext
.filter((obj) => obj.prodCtx.serviceCode === productContext);
// For each entry in filteredProductContext check that
// there is at least one entry where imsOrg matches the owningEntity property
// otherwise, if no match, the user is not authorized
return filteredProductContext.some((obj) => obj.prodCtx.owningEntity === org);
}
return false;
} catch (error) {
logger.error(error);
return false;
}
}
function asAuthAction(action) {
return async (params) => {
const imsEndpoint = params.IMS_ENDPOINT;
const clientId = params.IMS_CLIENT_ID;
const serviceClientId = params.IMS_SERVICE_CLIENT_ID;
const clientSecret = params.IMS_SERVICE_CLIENT_SECRET;
const permAuthCode = params.IMS_SERVICE_PERM_AUTH_CODE;
const productContext = params.IMS_PRODUCT_CONTEXT;
// Extract the token from the params
const { imsOrg, accessToken } = params;
// Validate the access token
if (!await isValidToken(imsEndpoint, clientId, accessToken)) {
throw new Error('Access token is invalid');
}
// Check that the profile has the expected product context
if (!await checkForProductContext(imsEndpoint, clientId, imsOrg, accessToken, productContext)) {
throw new Error('Profile does not have the required product context');
}
// If everything is okay, generate a service token
const imsClient = new ImsClient(imsEndpoint, serviceClientId, clientSecret, permAuthCode);
const serviceToken = await imsClient.getServiceToken();
return action({ ...params, imsOrg, serviceToken });
};
}
module.exports = { asAuthAction };