Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AD-7291 cert passphrase #15

Open
wants to merge 5 commits into
base: env-sandbox
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6
18
2 changes: 2 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"softwareStatementId": "",
"clientScopes": "",
"keyId": "",
"signingCert_passphrase": null,
"transportCert_passphrase": null,
"tokenUrl": "https://matls-sso.openbankingtest.org.uk/as/token.oauth2",
"tppTestUrl": "https://matls-api.openbankingtest.org.uk/scim/v2/participants/",
"aud": "https://matls-sso.openbankingtest.org.uk/as/token.oauth2"
Expand Down
157 changes: 88 additions & 69 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,76 @@
const
fs = require('fs'),
nJwt = require('njwt'),
qs = require('qs'),
request = require('axios'),
https = require('https'),
chalk = require('chalk'),
boxen = require('boxen');

console.log(chalk.bold.green(boxen("Open Banking Directory Access Token Acquisition Tool", {
margin: 1,
padding: 1,
style: 'double'
})));
const fs = require('fs'),
nJwt = require('njwt'),
qs = require('qs'),
request = require('axios'),
https = require('https'),
chalk = require('chalk'),
boxen = require('boxen');

console.log(
chalk.bold.green(
boxen('Open Banking Directory Access Token Acquisition Tool', {
margin: 1,
padding: 1,
style: 'double',
})
)
);
console.log();

// Load Private Key and config from files
const config = JSON.parse(fs.readFileSync(`${__dirname}/config/config.json`));
const signingCert = fs.readFileSync(`${__dirname}/config/certSigning.pem`);
const signingKey = fs.readFileSync(`${__dirname}/config/privateKeySigning.key`); // ES512
const transportCert = fs.readFileSync(`${__dirname}/config/certTransport.pem`);
const transportKey = fs.readFileSync(`${__dirname}/config/privateKeyTransport.key`); // ES512
const transportKey = fs.readFileSync(
`${__dirname}/config/privateKeyTransport.key`
); // ES512

// Node doesn't support concatenated CAs in a single PEM
// Read both files into the globalAgents file one at a time.
const trustedCa = [
`${__dirname}/config/root.pem`,
`${__dirname}/config/issuingca.pem`,
`${__dirname}/config/signingca.pem`
`${__dirname}/config/signingca.pem`,
];

const claims = {
iss: config.softwareStatementId,
sub: config.softwareStatementId,
scope: config.clientScopes,
aud: config.aud
iss: config.softwareStatementId,
sub: config.softwareStatementId,
scope: config.clientScopes,
aud: config.aud,
};

const created_jwt = nJwt.create(claims, signingKey, 'RS256');
const created_jwt = nJwt.create(
claims,
{
key: signingKey,
passphrase:
config.signingCert_passphrase === null
? undefined
: config.signingCert_passphrase,
},
'RS256'
);
created_jwt.setHeader('kid', config.keyId);
const compacted_jwt = created_jwt.compact();

console.log(chalk.bold.blue("Created JWT:"), compacted_jwt);
console.log(chalk.bold.blue('Created JWT:'), compacted_jwt);
console.log();


//Blank the CA list and load only the MA ones
var httpsAgent = https.globalAgent;
https.globalAgent.options.ca = [];
for (const ca of trustedCa) {
https.globalAgent.options.ca.push(fs.readFileSync(ca));
httpsAgent = new https.Agent({
cert: transportCert,
key: transportKey,
ca: https.globalAgent.options.ca,
rejectUnauthorized: true
cert: transportCert,
key: transportKey,
passphrase:
config.transportCert_passphrase === null
? undefined
: config.transportCert_passphrase,
ca: https.globalAgent.options.ca,
rejectUnauthorized: true,
});
}

Expand All @@ -63,53 +80,55 @@ const tokenRequestSpec = {
httpsAgent: httpsAgent,
method: 'POST',
data: qs.stringify({
'client_assertion_type': 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
'grant_type': 'client_credentials',
'client_id': config.softwareStatementId,
'client_assertion': compacted_jwt,
'scope': config.clientScopes
})
client_assertion_type:
'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
grant_type: 'client_credentials',
client_id: config.softwareStatementId,
client_assertion: compacted_jwt,
scope: config.clientScopes,
}),
};

console.log(chalk.bold.blue("Requesting Access Token..."));
console.log(chalk.bold.blue('Requesting Access Token...'));
console.log();

const errorHandler = function(error) {
const errorHandler = function (error) {
console.log(chalk.red.bold(error));
console.log(chalk.blue.bold("Response body:"));
console.log(error.response.data);
console.log(error.response.status);
console.log(chalk.blue.bold('Response body:'));
console.log(error.response?.data);
console.log(error.response?.status);
};

// Send request to get the token
request(tokenRequestSpec)
.then((response) => {
console.log(chalk.bold.blue("Token acquired:"), response.data.access_token);
console.log();

// Configure the request for test endpoint - list of Participants
const tppRequestSpec = {
url: config.tppTestUrl,
httpsAgent: httpsAgent,
method: "GET",
headers: {
"Authorization": `Bearer ${response.data.access_token}`
}
};

return request(tppRequestSpec);

})
.then((response) => {

// Test request to get the list of Participants
response.data.Resources.forEach((participant) => {
const org = participant['urn:openbanking:organisation:1.0'];
const auth = participant['urn:trustframework:competentauthorityclaims:1.1'];
const authorisation =participant['urn:trustframework:competentauthorityclaims:1.1.Authorisations'];

console.log("-", org.OrganisationCommonName, "-", auth.Authorisations);
});

})
.catch(errorHandler);
.then((response) => {
console.log(chalk.bold.blue('Token acquired:'), response.data.access_token);
console.log();

// Configure the request for test endpoint - list of Participants
const tppRequestSpec = {
url: config.tppTestUrl,
httpsAgent: httpsAgent,
method: 'GET',
headers: {
Authorization: `Bearer ${response.data.access_token}`,
},
};

return request(tppRequestSpec);
})
.then((response) => {
// Test request to get the list of Participants
response.data.Resources.forEach((participant) => {
const org = participant['urn:openbanking:organisation:1.0'];
const auth =
participant['urn:trustframework:competentauthorityclaims:1.1'];
const authorisation =
participant[
'urn:trustframework:competentauthorityclaims:1.1.Authorisations'
];

console.log('-', org.OrganisationCommonName, '-', auth.Authorisations);
});
})
.catch(errorHandler);