@emeraldpay/api-node
Version:
Node client for Emerald gRPC APIs
81 lines • 2.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialsContext = exports.emeraldCredentials = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
const Auth_1 = require("./wrapped/Auth");
const api_1 = require("@emeraldpay/api");
const api_2 = require("@emeraldpay/api");
const Factory_1 = require("./wrapped/Factory");
const { version: clientVersion } = require('../package.json');
/**
* Use this function to create a new CredentialsContext for the Emerald API
*
* @param url
* @param agents
* @param secretToken
*/
function emeraldCredentials(url, agents, secretToken) {
return new CredentialsContext(url, agents, secretToken);
}
exports.emeraldCredentials = emeraldCredentials;
///
/// ------------- Internal implementation details -------------
///
class NodeAuthClient {
constructor(client) {
this.convert = new api_2.ConvertAuth(Factory_1.classFactory);
this.client = client;
}
authenticate(req) {
return this.client.authenticate(this.convert.authRequest(req))
.then(this.convert.authResponse);
}
refresh(req) {
return this.client.refresh(this.convert.refreshRequest(req))
.then(this.convert.authResponse);
}
}
class CredentialsContext {
constructor(address, agents, secretToken) {
this.address = address;
this.agents = [...agents, `emerald-client-node/${clientVersion}`];
if (!(0, api_1.isSecretToken)(secretToken)) {
throw new Error('Invalid secret token');
}
this.secretToken = secretToken;
this.ssl = grpc_js_1.credentials.createSsl();
let authClient = new Auth_1.AuthClient(this.address, this.ssl, this.agents);
this.signer = new api_1.StandardSigner(new NodeAuthClient(authClient), this.secretToken, this.agents);
const ssl = this.getSsl();
const callCredentials = grpc_js_1.credentials.createFromMetadataGenerator((params, callback) => this.signer.getAuth()
.then((auth) => {
const meta = new grpc_js_1.Metadata();
try {
auth.applyAuth(meta);
}
catch (exception) {
callback(exception);
return;
}
callback(null, meta);
})
.catch(() => {
callback(new Error('Unable to get token'));
}));
this.channelCredentials = grpc_js_1.credentials.combineChannelCredentials(ssl, callCredentials);
}
getChannelCredentials() {
return this.channelCredentials;
}
setAuthentication(authentication) {
this.signer.setAuthentication(authentication);
}
setListener(listener) {
this.signer.setListener(listener);
}
getSsl() {
return this.ssl;
}
}
exports.CredentialsContext = CredentialsContext;
//# sourceMappingURL=credentials.js.map
;