UNPKG

@adobe/pdftools-extract-node-sdk

Version:

The Document Services PDF Tools Extract Node.js SDK provides APIs for extracting elements and renditions from PDF

262 lines (232 loc) 8.31 kB
/* * 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 Schema = require('validate'), fs = require('fs'), path = require('path'), DefaultConfig = require('../internal/config/dc-services-default-config'); /** * Service Account credentials allow your application to call PDF Tools Extract API on behalf of the application itself, * or on behalf of an enterprise organization. For getting the credentials, * <a href="https://www.adobe.io/apis/documentcloud/dcsdk/extractbetaform.html">Click Here</a> */ class ServiceAccountCredentials { /** * @hideconstructor */ constructor(ims_base_uri, client_id, client_secret, private_key, claim, organization_id, account_id){ this.imsBaseUri = ims_base_uri || DefaultConfig.imsBaseUri; this.clientId = client_id; this.clientSecret = client_secret; this.privateKey = private_key; this.claim = claim || (this.imsBaseUri + DefaultConfig.claim); this.organizationId = organization_id; this.accountId = account_id; this.validate(); } /** * Client Id (API Key) */ getClientId() { return this.clientId; } /** * Client Secret */ getClientSecret() { return this.clientSecret; } /** * Content of the Private Key (PEM format) */ getPrivateKey() { return this.privateKey; } /** * Identifies the Service for which Authorization(Access) Token will be issued */ getClaim() { return this.claim; } /** * Identifies the organization(format: org_ident@AdobeOrg) that has been configured for access to PDF Tools API. */ getOrganizationId() { return this.organizationId; } /** * Account ID(format: id@techacct.adobe.com) */ getAccountId() { return this.accountId; } /** * IMS Base URI of the API call to retrieve Authorization(Access) Token */ getImsBaseUri() { return this.imsBaseUri; } static get Builder() { /** * * Builds a {@link ServiceAccountCredentials} instance. */ class ServiceAccountCredentialsBuilder { /** * @hideconstructor */ constructor() { } /** * Set Client ID (API Key) * @param {!String} clientId - Client Id (API Key) * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ withClientId(clientId) { this.clientId = clientId; return this; } /** * Set Client Secret * @param {!String} clientSecret - Client Secret * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ withClientSecret(clientSecret) { this.clientSecret = clientSecret; return this; } /** * Set private key * @param {!String} privateKey - Content of the Private Key (PEM format) * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ withPrivateKey(privateKey) { this.privateKey = privateKey; return this; } /** * Set Organization Id (format: org_ident@AdobeOrg) that has been configured for access to PDF Tools API * @param {!String} organizationId - Organization ID (format: org_ident@AdobeOrg) * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ withOrganizationId(organizationId) { this.organizationId = organizationId; return this; } /** * Set Account Id (format: id@techacct.adobe.com) * @param {!String} accountId - Account ID (format: id@techacct.adobe.com) * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ withAccountId(accountId) { this.accountId = accountId; return this; } /** * Sets Service Account Credentials using the JSON credentials file path. All the keys in the JSON * structure are optional. * <p> * JSON structure: * <pre> * { * "client_credentials": { * "client_id": "CLIENT_ID", * "client_secret": "CLIENT_SECRET" * }, * "service_account_credentials": { * "organization_id": "org_ident@AdobeOrg", * "account_id": "id@techacct.adobe.com", * "private_key_file": "private.key" * } * } * </pre> * private_key_file is the path of private key file. It will be looked up in the classpath and the * directory of JSON credentials file. * @param {!String} credentialsFilePath - JSON credentials file path * @returns {ServiceAccountCredentialsBuilder} This Builder instance to add any additional parameters. */ fromFile(credentialsFilePath){ const credentialsConfig = JSON.parse(fs.readFileSync(credentialsFilePath)); const clientCredentials = credentialsConfig.client_credentials; const serviceAccountCredentials = credentialsConfig.service_account_credentials; if (clientCredentials) { this.clientId = (clientCredentials.client_id) ? clientCredentials.client_id : this.clientId; this.clientSecret = (clientCredentials.client_secret) ? clientCredentials.client_secret : this.clientSecret; } if(serviceAccountCredentials){ this.organizationId = (serviceAccountCredentials.organization_id) ? serviceAccountCredentials.organization_id : this.organizationId; this.accountId = (serviceAccountCredentials.account_id) ? serviceAccountCredentials.account_id : this.accountId; this.imsBaseUri = (serviceAccountCredentials.ims_base_uri) ? serviceAccountCredentials.ims_base_uri : this.imsBaseUri; this.claim = (serviceAccountCredentials.claim) ? serviceAccountCredentials.claim : this.claim; const privateKeyFilePath = serviceAccountCredentials.private_key_file; if (privateKeyFilePath!=null){ if(fs.existsSync(privateKeyFilePath)) { this.privateKey = fs.readFileSync(privateKeyFilePath).toString(); } else{ this.privateKey = fs.readFileSync(path.dirname(credentialsFilePath)+path.sep+privateKeyFilePath).toString(); } } } return this; } /** * Returns a new {@link ServiceAccountCredentials} instance built from the current state of this builder. * @returns {ServiceAccountCredentials} A ServiceAccountCredentials instance. */ build() { let serviceAccountCredentials =new ServiceAccountCredentials(this.imsBaseUri, this.clientId, this.clientSecret, this.privateKey, this.claim, this.organizationId, this.accountId); Object.freeze(serviceAccountCredentials); return serviceAccountCredentials; } } return ServiceAccountCredentialsBuilder; } validate(){ const ServiceAccountValidator = new Schema({ 'clientId': { required: true, message: 'client_id must not be blank' }, 'clientSecret': { required: true, message: 'client_secret must not be blank' }, 'privateKey': { required: true, message: 'private_key must not be blank' }, 'claim': { required: true, message: 'claim must not be blank' }, 'organizationId': { required: true, message: 'organisation_id must not be blank' }, 'accountId': { required: true, message: 'account_id must not be blank' }, }); const config = Object.assign({}, {clientId:this.clientId,clientSecret:this.clientSecret,privateKey:this.privateKey,claim: this.claim,organizationId:this.organizationId,accountId:this.accountId}); const errors = ServiceAccountValidator.validate(config); if (errors.length > 0) { const messages = []; errors.forEach(err => messages.push(err.message)); throw new Error(messages.join('; ')); } } } module.exports = ServiceAccountCredentials;