From 386ff675e8dc7cad3a62b713f6f8ae3f2570c536 Mon Sep 17 00:00:00 2001 From: Henrique Vicente Date: Mon, 30 Nov 2015 02:30:05 -0300 Subject: [PATCH] Fixing BasicAuth by adding a compatibility layer for base64 conversion. btoa is only available on browsers: https://github.com/nodejs/node/issues/3462 --- src/api/Launchpad.js | 5 +++-- src/crypt/Base64.js | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/crypt/Base64.js diff --git a/src/api/Launchpad.js b/src/api/Launchpad.js index d1931ec..9366d92 100644 --- a/src/api/Launchpad.js +++ b/src/api/Launchpad.js @@ -1,7 +1,8 @@ 'use strict'; import core from 'bower:metal/src/core'; -import Auth from '../api/Auth'; +import Auth from './Auth'; +import Base64 from '../crypt/Base64'; import Embodied from '../api-query/Embodied'; import Filter from '../api-query/Filter'; import Query from '../api-query/Query'; @@ -397,7 +398,7 @@ class Launchpad { clientRequest.header('Authorization', 'Bearer ' + this.auth_.token()); } else { var credentials = this.auth_.username() + ':' + this.auth_.password(); - clientRequest.header('Authorization', 'Basic ' + btoa(credentials)); + clientRequest.header('Authorization', 'Basic ' + Base64.encodeString(credentials)); } } diff --git a/src/crypt/Base64.js b/src/crypt/Base64.js new file mode 100644 index 0000000..245d45a --- /dev/null +++ b/src/crypt/Base64.js @@ -0,0 +1,23 @@ +'use strict'; + +/** + * Abstraction layer for string to base64 conversion + * reference: https://github.com/nodejs/node/issues/3462 + */ +class Base64 { + /** + * Creates a base-64 encoded ASCII string from a "string" of binary data. + * @param {string} string to be encoded. + * @return {string} + * @static + */ + static encodeString(string) { + if (typeof btoa === 'function') { + return btoa(string); + } + + return new Buffer(string.toString(), 'binary'); + } +} + +export default Base64;