Skip to content

Commit

Permalink
added x509data to AuthnRequest (#97)
Browse files Browse the repository at this point in the history
* added x509data to AuthnRequest

* TS fix

* TS fix

* workflows cleanup
  • Loading branch information
deepakprabhakara authored Feb 19, 2022
1 parent fe80252 commit ce1a9e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ jobs:
uses: actions/download-artifact@v2
with:
name: npm_sbom.cyclonedx
- name: Remove temp files & pull latest code
run: rm results.sarif && git pull || true
- name: Remove older SBOMs
run: rm -rf ./npm/sbom*.* || true
- name: Move SPDX Report
Expand Down
1 change: 1 addition & 0 deletions npm/src/controller/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class OAuthController implements IOAuthController {
entityID: this.opts.samlAudience!,
callbackUrl: this.opts.externalUrl + this.opts.samlPath,
signingKey: samlConfig.certs.privateKey,
publicKey: samlConfig.certs.publicKey,
});

const sessionId = crypto.randomBytes(16).toString('hex');
Expand Down
35 changes: 33 additions & 2 deletions npm/src/saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,36 @@ const authnXPath =
'/*[local-name(.)="AuthnRequest" and namespace-uri(.)="urn:oasis:names:tc:SAML:2.0:protocol"]';
const issuerXPath = '/*[local-name(.)="Issuer" and namespace-uri(.)="urn:oasis:names:tc:SAML:2.0:assertion"]';

const signRequest = (xml: string, signingKey: string) => {
export const stripCertHeaderAndFooter = (cert: string): string => {
cert = cert.replace(/-+BEGIN CERTIFICATE-+\r?\n?/, '');
cert = cert.replace(/-+END CERTIFICATE-+\r?\n?/, '');
cert = cert.replace(/\r\n/g, '\n');
return cert;
};

function PubKeyInfo(this: any, pubKey: string) {
this.pubKey = stripCertHeaderAndFooter(pubKey);

this.getKeyInfo = function (_key, prefix) {
prefix = prefix || '';
prefix = prefix ? prefix + ':' : prefix;
return (
'<' +
prefix +
'X509Data><' +
prefix +
'X509Certificate>' +
this.pubKey +
'</' +
prefix +
'X509Certificate></' +
prefix +
'X509Data>'
);
};
}

const signRequest = (xml: string, signingKey: string, publicKey: string) => {
if (!xml) {
throw new Error('Please specify xml');
}
Expand All @@ -23,6 +52,7 @@ const signRequest = (xml: string, signingKey: string) => {

const sig = new xmlcrypto.SignedXml();
sig.signatureAlgorithm = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256';
sig.keyInfoProvider = new PubKeyInfo(publicKey);
sig.signingKey = signingKey;
sig.addReference(
authnXPath,
Expand All @@ -45,6 +75,7 @@ const request = ({
identifierFormat = 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
providerName = 'BoxyHQ',
signingKey,
publicKey,
}: SAMLReq): { id: string; request: string } => {
const id = idPrefix + crypto.randomBytes(10).toString('hex');
const date = new Date().toISOString();
Expand Down Expand Up @@ -85,7 +116,7 @@ const request = ({

let xml = xmlbuilder.create(samlReq).end({});
if (signingKey) {
xml = signRequest(xml, signingKey);
xml = signRequest(xml, signingKey, publicKey);
}

return {
Expand Down
1 change: 1 addition & 0 deletions npm/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface SAMLReq {
identifierFormat?: 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress';
providerName?: 'BoxyHQ';
signingKey: string;
publicKey: string;
}

export interface SAMLProfile {
Expand Down

0 comments on commit ce1a9e9

Please sign in to comment.