Skip to content

Commit

Permalink
Fixing BasicAuth by adding a compatibility layer for base64 conversion.
Browse files Browse the repository at this point in the history
btoa is only available on browsers:
nodejs/node#3462
  • Loading branch information
henvic committed Jan 5, 2016
1 parent f85c9e9 commit 386ff67
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/api/Launchpad.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/crypt/Base64.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 386ff67

Please sign in to comment.