-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbtp-utils.js
More file actions
29 lines (23 loc) · 832 Bytes
/
btp-utils.js
File metadata and controls
29 lines (23 loc) · 832 Bytes
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
const { XsuaaService } = require("@sap/xssec");
const getServiceCredentials = (name) => (cds?.env?.requires[name] || [])?.credentials;
async function getServiceToken(serviceName) {
const srvCredentials = getServiceCredentials(serviceName);
if (!srvCredentials) {
throw new Error(`Missing binding credentials for service "${serviceName}"`);
}
const tenantId = cds.context?.tenant;
const xsuaaService = new XsuaaService(srvCredentials.uaa);
const { access_token: jwt, expires_in } = await xsuaaService.fetchClientCredentialsToken({
...(tenantId && { zid: tenantId }),
});
if (!jwt) {
throw new Error(
`Empty JWT returned from authorization service for bound service "${serviceName}"`,
);
}
return { jwt, expires_in };
}
module.exports = {
getServiceToken,
getServiceCredentials,
};