Skip to content

Commit 83d31b9

Browse files
authoredJan 7, 2025··
fix: exporting SAML connections base64 encode the certificate (#1008)
* Add Base64 encoding for SAML certificate options in connections * unit test added for encodeCertStringToBase64
·
v8.9.0v8.4.2
1 parent 6e98caa commit 83d31b9

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
 

‎src/context/directory/handlers/connections.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
sanitize,
1313
ensureProp,
1414
mapClientID2NameSorted,
15+
encodeCertStringToBase64,
1516
} from '../../../utils';
1617
import { DirectoryHandler } from '.';
1718
import DirectoryContext from '..';
@@ -88,6 +89,18 @@ async function dump(context: DirectoryContext): Promise<void> {
8889
dumpedConnection.options.email.body = `./${connectionName}.html`;
8990
}
9091

92+
if (dumpedConnection.strategy === 'samlp' && dumpedConnection.options) {
93+
if ('cert' in dumpedConnection.options) {
94+
dumpedConnection.options.cert = encodeCertStringToBase64(dumpedConnection.options.cert);
95+
}
96+
97+
if ('signingCert' in dumpedConnection.options) {
98+
dumpedConnection.options.signingCert = encodeCertStringToBase64(
99+
dumpedConnection.options.signingCert
100+
);
101+
}
102+
}
103+
91104
const connectionFile = path.join(connectionsFolder, `${connectionName}.json`);
92105
dumpJSON(connectionFile, dumpedConnection);
93106
});

‎src/context/yaml/handlers/connections.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ensureProp,
1010
convertClientIdToName,
1111
mapClientID2NameSorted,
12+
encodeCertStringToBase64,
1213
} from '../../../utils';
1314
import { YAMLHandler } from '.';
1415
import YAMLContext from '..';
@@ -90,6 +91,17 @@ async function dump(context: YAMLContext): Promise<ParsedConnections> {
9091
dumpedConnection.options.email.body = `./${connectionName}.html`;
9192
}
9293

94+
if (dumpedConnection.strategy === 'samlp' && dumpedConnection.options) {
95+
if ('cert' in dumpedConnection.options) {
96+
dumpedConnection.options.cert = encodeCertStringToBase64(dumpedConnection.options.cert);
97+
}
98+
99+
if ('signingCert' in dumpedConnection.options) {
100+
dumpedConnection.options.signingCert = encodeCertStringToBase64(
101+
dumpedConnection.options.signingCert
102+
);
103+
}
104+
}
93105
return dumpedConnection;
94106
}),
95107
};

‎src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,16 @@ export const findKeyPathWithValue = (obj: any, findKey: string, parentPath: stri
259259

260260
return results;
261261
};
262+
263+
/**
264+
* Encodes a certificate string to Base64 format if it starts with '-----BEGIN CERTIFICATE-----'.
265+
*
266+
* @param cert - The certificate string to be encoded.
267+
* @returns The Base64 encoded certificate string if the input starts with '-----BEGIN CERTIFICATE-----', otherwise returns the original string.
268+
*/
269+
export const encodeCertStringToBase64 = (cert: string) => {
270+
if (cert?.startsWith('-----BEGIN CERTIFICATE-----')) {
271+
return Buffer.from(cert).toString('base64');
272+
}
273+
return cert;
274+
};

‎test/utils.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
sanitize,
2020
stripIdentifiers,
2121
toConfigFn,
22+
encodeCertStringToBase64,
2223
} from '../src/utils';
2324

2425
const mockConfigFn = () => {};
@@ -269,4 +270,23 @@ describe('#utils', function () {
269270
expect(mapClientID2NameSorted(null, null)).deep.equal([]);
270271
});
271272
});
273+
274+
describe('encodeCertStringToBase64', () => {
275+
it('should encode certificate string to Base64', () => {
276+
const cert =
277+
'-----BEGIN CERTIFICATE-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7\n-----END CERTIFICATE-----';
278+
const expectedBase64 = Buffer.from(cert).toString('base64');
279+
expect(encodeCertStringToBase64(cert)).to.equal(expectedBase64);
280+
});
281+
282+
it('should return the original string if it does not start with "-----BEGIN CERTIFICATE-----"', () => {
283+
const nonCertString = 'This is not a certificate';
284+
expect(encodeCertStringToBase64(nonCertString)).to.equal(nonCertString);
285+
});
286+
287+
it('should return the original string if it is null or undefined', () => {
288+
expect(encodeCertStringToBase64(null)).to.equal(null);
289+
expect(encodeCertStringToBase64(undefined)).to.equal(undefined);
290+
});
291+
});
272292
});

0 commit comments

Comments
 (0)
Please sign in to comment.