Skip to content

Commit

Permalink
Change Buffer constructor usage to Buffer.from function (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryaningl3 authored and tngan committed Jan 25, 2020
1 parent 0c0bc33 commit 16ded23
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async function redirectFlow(options) {
}

// put the below two assignemnts into verifyMessageSignature function
const base64Signature = new Buffer(decodeURIComponent(signature), 'base64');
const base64Signature = Buffer.from(decodeURIComponent(signature), 'base64');
const decodeSigAlg = decodeURIComponent(sigAlg);

const verified = libsaml.verifyMessageSignature(targetEntityMetadata, octetString, base64Signature, sigAlg);
Expand Down
4 changes: 2 additions & 2 deletions src/libsaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ const libSaml = () => {
if (sourceEntitySetting.isAssertionEncrypted) {
xmlenc.encrypt(assertions[0].toString(), {
// use xml-encryption module
rsa_pub: new Buffer(utility.getPublicKeyPemFromCertificate(targetEntityMetadata.getX509Certificate(certUse.encrypt)).replace(/\r?\n|\r/g, '')), // public key from certificate
pem: new Buffer('-----BEGIN CERTIFICATE-----' + targetEntityMetadata.getX509Certificate(certUse.encrypt) + '-----END CERTIFICATE-----'),
rsa_pub: Buffer.from(utility.getPublicKeyPemFromCertificate(targetEntityMetadata.getX509Certificate(certUse.encrypt)).replace(/\r?\n|\r/g, '')), // public key from certificate
pem: Buffer.from('-----BEGIN CERTIFICATE-----' + targetEntityMetadata.getX509Certificate(certUse.encrypt) + '-----END CERTIFICATE-----'),
encryptionAlgorithm: sourceEntitySetting.dataEncryptionAlgorithm,
keyEncryptionAlgorighm: sourceEntitySetting.keyEncryptionAlgorithm,
}, (err, res) => {
Expand Down
6 changes: 3 additions & 3 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function isString(input: any) {
* @return {string} base64 encoded string
*/
function base64Encode(message: string | number[]) {
return new Buffer(message as string).toString(BASE64_STR);
return Buffer.from(message as string).toString(BASE64_STR);
}
/**
* @desc Decode string from base64 format
Expand All @@ -79,7 +79,7 @@ function base64Encode(message: string | number[]) {
* @return {bytes/string} decoded bytes/string depends on isBytes, default is {string}
*/
export function base64Decode(base64Message: string, isBytes?: boolean): string | Buffer {
const bytes = new Buffer(base64Message, BASE64_STR);
const bytes = Buffer.from(base64Message, BASE64_STR);
return Boolean(isBytes) ? bytes : bytes.toString();
}
/**
Expand All @@ -97,7 +97,7 @@ function deflateString(message: string): number[] {
* @return {string} decompressed string
*/
export function inflateString(compressedString: string): string {
const inputBuffer = new Buffer(compressedString, BASE64_STR);
const inputBuffer = Buffer.from(compressedString, BASE64_STR);
const input = Array.prototype.map.call(inputBuffer.toString('binary'), char => char.charCodeAt(0));
return Array.from(inflate(input, { raw: true }))
.map(byte => String.fromCharCode(byte))
Expand Down
8 changes: 4 additions & 4 deletions test/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ test('should reject signature wrapped response - case 1', async t => {
const user = { email: '[email protected]' };
const { id, context: SAMLResponse } = await idpNoEncrypt.createLoginResponse(sp, sampleRequestInfo, 'post', user, createTemplateCallback(idpNoEncrypt, sp, user));
//Decode
const buffer = new Buffer(SAMLResponse, 'base64');
const buffer = Buffer.from(SAMLResponse, 'base64');
const xml = buffer.toString();
//Create version of response without signature
const stripped = xml
Expand All @@ -634,7 +634,7 @@ test('should reject signature wrapped response - case 1', async t => {
.replace('[email protected]', '[email protected]');
//Put stripped version under SubjectConfirmationData of modified version
const xmlWrapped = outer.replace(/<saml:SubjectConfirmationData[^>]*\/>/, '<saml:SubjectConfirmationData>' + stripped.replace('<?xml version="1.0" encoding="UTF-8"?>', '') + '</saml:SubjectConfirmationData>');
const wrappedResponse = new Buffer(xmlWrapped).toString('base64');
const wrappedResponse = Buffer.from(xmlWrapped).toString('base64');
try {
await sp.parseLoginResponse(idpNoEncrypt, 'post', { body: { SAMLResponse: wrappedResponse } });
} catch (e) {
Expand All @@ -647,7 +647,7 @@ test('should reject signature wrapped response - case 2', async t => {
const user = { email: '[email protected]' };
const { id, context: SAMLResponse } = await idpNoEncrypt.createLoginResponse(sp, sampleRequestInfo, 'post', user, createTemplateCallback(idpNoEncrypt, sp, user));
//Decode
const buffer = new Buffer(SAMLResponse, 'base64');
const buffer = Buffer.from(SAMLResponse, 'base64');
const xml = buffer.toString();
//Create version of response without signature
const stripped = xml
Expand All @@ -658,7 +658,7 @@ test('should reject signature wrapped response - case 2', async t => {
.replace('[email protected]', '[email protected]');
//Put stripped version under SubjectConfirmationData of modified version
const xmlWrapped = outer.replace(/<\/saml:Conditions>/, '</saml:Conditions><saml:Advice>' + stripped.replace('<?xml version="1.0" encoding="UTF-8"?>', '') + '</saml:Advice>');
const wrappedResponse = new Buffer(xmlWrapped).toString('base64');
const wrappedResponse = Buffer.from(xmlWrapped).toString('base64');
try {
const result = await sp.parseLoginResponse(idpNoEncrypt, 'post', { body: { SAMLResponse: wrappedResponse } });
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ test('getAssertionConsumerService with two bindings', t => {
});
test('verify stringified SAML message signed with RSA-SHA1', t => {
const signature = libsaml.constructMessageSignature(octetString, _spPrivPem, _spPrivKeyPass);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetString, new Buffer(signature, 'base64')), true);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetString, Buffer.from(signature, 'base64')), true);
});
test('verify stringified SAML message signed with RSA-SHA256', t => {
const signature = libsaml.constructMessageSignature(octetStringSHA256, _spPrivPem, _spPrivKeyPass);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetStringSHA256, new Buffer(signature, 'base64')), true);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetStringSHA256, Buffer.from(signature, 'base64')), true);
});
test('verify stringified SAML message signed with RSA-SHA512', t => {
const signature = libsaml.constructMessageSignature(octetStringSHA512, _spPrivPem, _spPrivKeyPass);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetStringSHA512, new Buffer(signature, 'base64')), true);
t.is(libsaml.verifyMessageSignature(SPMetadata, octetStringSHA512, Buffer.from(signature, 'base64')), true);
});
test('construct signature with RSA-SHA1', t => {
t.is(libsaml.constructSAMLSignature({
Expand Down

0 comments on commit 16ded23

Please sign in to comment.