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

fix: exporting SAML connections base64 encode the certificate #1008

Merged
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
13 changes: 13 additions & 0 deletions src/context/directory/handlers/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
sanitize,
ensureProp,
mapClientID2NameSorted,
encodeCertStringToBase64,
} from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
Expand Down Expand Up @@ -88,6 +89,18 @@ async function dump(context: DirectoryContext): Promise<void> {
dumpedConnection.options.email.body = `./${connectionName}.html`;
}

if (dumpedConnection.strategy === 'samlp' && dumpedConnection.options) {
if ('cert' in dumpedConnection.options) {
dumpedConnection.options.cert = encodeCertStringToBase64(dumpedConnection.options.cert);
}

if ('signingCert' in dumpedConnection.options) {
dumpedConnection.options.signingCert = encodeCertStringToBase64(
dumpedConnection.options.signingCert
);
}
}

const connectionFile = path.join(connectionsFolder, `${connectionName}.json`);
dumpJSON(connectionFile, dumpedConnection);
});
Expand Down
12 changes: 12 additions & 0 deletions src/context/yaml/handlers/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ensureProp,
convertClientIdToName,
mapClientID2NameSorted,
encodeCertStringToBase64,
} from '../../../utils';
import { YAMLHandler } from '.';
import YAMLContext from '..';
Expand Down Expand Up @@ -90,6 +91,17 @@ async function dump(context: YAMLContext): Promise<ParsedConnections> {
dumpedConnection.options.email.body = `./${connectionName}.html`;
}

if (dumpedConnection.strategy === 'samlp' && dumpedConnection.options) {
if ('cert' in dumpedConnection.options) {
dumpedConnection.options.cert = encodeCertStringToBase64(dumpedConnection.options.cert);
}

if ('signingCert' in dumpedConnection.options) {
dumpedConnection.options.signingCert = encodeCertStringToBase64(
dumpedConnection.options.signingCert
);
}
}
return dumpedConnection;
}),
};
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,16 @@ export const findKeyPathWithValue = (obj: any, findKey: string, parentPath: stri

return results;
};

/**
* Encodes a certificate string to Base64 format if it starts with '-----BEGIN CERTIFICATE-----'.
*
* @param cert - The certificate string to be encoded.
* @returns The Base64 encoded certificate string if the input starts with '-----BEGIN CERTIFICATE-----', otherwise returns the original string.
*/
export const encodeCertStringToBase64 = (cert: string) => {
if (cert?.startsWith('-----BEGIN CERTIFICATE-----')) {
return Buffer.from(cert).toString('base64');
}
return cert;
};
20 changes: 20 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
sanitize,
stripIdentifiers,
toConfigFn,
encodeCertStringToBase64,
} from '../src/utils';

const mockConfigFn = () => {};
Expand Down Expand Up @@ -269,4 +270,23 @@ describe('#utils', function () {
expect(mapClientID2NameSorted(null, null)).deep.equal([]);
});
});

describe('encodeCertStringToBase64', () => {
it('should encode certificate string to Base64', () => {
const cert =
'-----BEGIN CERTIFICATE-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7\n-----END CERTIFICATE-----';
const expectedBase64 = Buffer.from(cert).toString('base64');
expect(encodeCertStringToBase64(cert)).to.equal(expectedBase64);
});

it('should return the original string if it does not start with "-----BEGIN CERTIFICATE-----"', () => {
const nonCertString = 'This is not a certificate';
expect(encodeCertStringToBase64(nonCertString)).to.equal(nonCertString);
});

it('should return the original string if it is null or undefined', () => {
expect(encodeCertStringToBase64(null)).to.equal(null);
expect(encodeCertStringToBase64(undefined)).to.equal(undefined);
});
});
});
Loading