@adobe/pdftools-extract-node-sdk
Version:
The Document Services PDF Tools Extract Node.js SDK provides APIs for extracting elements and renditions from PDF
128 lines (111 loc) • 3.71 kB
JavaScript
/*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
const ServiceApiError = require('../../error/service-api-error'),
jwt = require('jsonwebtoken'),
Httprequest = require('../http/http-request'),
uuid = require('uuid'),
DefaultHeaders = require('../http/default-dc-request-options'),
DefaultConfig = require('./../config/dc-services-default-config');
class JwtAuthenticator {
constructor(serviceAccountCredentials) {
Object.defineProperty(this, 'config', {
value: serviceAccountCredentials,
writable: false
});
this.token = null;
Object.defineProperty(this, 'audience', {
value: `${this.config.getImsBaseUri() || DefaultConfig.imsBaseUri}/${DefaultConfig.jwt.audienceSuffix}${this.config.getClientId()}`,
writable: false,
enumerable: true
});
Object.defineProperty(this, 'endpoint', {
value: `${this.config.getImsBaseUri() || DefaultConfig.imsBaseUri}/${DefaultConfig.jwt.uriSuffix}`,
writable: false,
enumerable: true
});
}
getPrivateKeyPromise() {
const claims = {};
return new Promise((resolve, reject) => {
claims[this.config.getClaim()] = true;
try {
resolve(jwt.sign(claims, this.config.getPrivateKey(), {
algorithm: 'RS256',
issuer: this.config.getOrganizationId(),
subject: this.config.getAccountId(),
audience: this.audience,
// internally adds 24 hours to the current time
expiresIn: 60 * 60 * 24 // 24 hours in seconds
}));
} catch (err) {
reject(err);
}
});
}
refreshSessionToken() {
return this.getPrivateKeyPromise()
.then(privateToken => {
const options = { headers: {} },
content = /\/jwt\//.test(this.endpoint) ? [
`client_id=${this.config.getClientId()}`,
`client_secret=${this.config.getClientSecret()}`,
`jwt_token=${privateToken}`
].join('&') : [
'grant_type=authorization_code',
`client_id=${this.config.getClientId()}`,
`client_secret=${this.config.getClientSecret()}`,
`code=${privateToken}`
].join('&');
options.headers[DefaultHeaders.SESSION_TOKEN_REQUEST_ID_HEADER_KEY] = uuid.v4();
options.headers['x-api-app-info'] = DefaultConfig.appInfo;
options.requestConfig = {
method: 'POST',
uri: this.endpoint
};
const request = new Httprequest(options).withBodyContent(content);
return request.call()
.then(result => {
if (result.status === 200) {
result.expiresAt = (new Date().getTime() + result.content.expires_in) - (60 * 1000);
const token = {
access_token: result.content.access_token,
expiresAt: result.expiresAt
};
return Promise.resolve(token);
}
return Promise.reject(new ServiceApiError(
result.content.error,
result.headers[DefaultHeaders.SESSION_TOKEN_REQUEST_ID_HEADER_KEY],
result.status
));
})
.catch(err => Promise.reject(err));
}).catch(err=> Promise.reject(err));
}
setToken(token) {
this.token = token;
}
getSessionToken(forced) {
if (this.token && !forced) {
if (this.token.expiresAt &&
new Date().getTime() <= this.token.expiresAt) {
return Promise.resolve(this.token);
}
}
return this.refreshSessionToken()
.then(token => {
this.setToken(token);
return Promise.resolve(token);
})
.catch(err => Promise.reject(err));
}
}
module.exports = JwtAuthenticator;