Skip to content

Commit

Permalink
fix(auth): use base64 encoding for basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Ryan committed Mar 24, 2017
1 parent 3e26c1c commit 15e4407
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/gigwalk-corp/gigwalk-node#readme",
"dependencies": {
"babel-runtime": "^6.11.6",
"btoa": "^1.1.2",
"json-stable-stringify": "^1.0.1",
"lodash.clonedeep": "^4.4.1"
},
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import cloneDeep from 'lodash.clonedeep';
import btoa from 'btoa';
import GigwalkAxios from './client';
import Authorization from './api/authorization';
import CalendarEvents from './api/calendarEvents';
Expand Down Expand Up @@ -140,8 +141,8 @@ export default class GigwalkAPI {

authenticate(auth: AuthToken | BasicAuth) {
let header: string = '';
if (typeof auth.email === 'string' && typeof auth.password === 'string') {
header = `Basic ${auth.email}:${auth.password}`;
if (typeof auth.username === 'string' && typeof auth.password === 'string') {
header = `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
} else if (typeof auth.token === 'string') {
header = `Token ${auth.token}`;
}
Expand Down
5 changes: 3 additions & 2 deletions test/unit/gigwalkAPI.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sinon from 'sinon';
import btoa from 'btoa';
import GigwalkAPI from '../../src/index';
import GigwalkAxios from '../../src/client';
import Authorization from '../../src/api/authorization';
Expand Down Expand Up @@ -42,8 +43,8 @@ describe('GigwalkAPI', () => {
it('should allow users to authenticate', () => {
const gigwalk = new GigwalkAPI();

gigwalk.authenticate({ email: '[email protected]', password: 'password' });
expect(gigwalk.client).to.have.deep.property('defaults.headers.common.Authorization', 'Basic [email protected]:password');
gigwalk.authenticate({ username: '[email protected]', password: 'password' });
expect(gigwalk.client).to.have.deep.property('defaults.headers.common.Authorization', `Basic ${btoa('[email protected]:password')}`);

gigwalk.authenticate({ token: 'qwerty' });
expect(gigwalk.client).to.have.deep.property('defaults.headers.common.Authorization', 'Token qwerty');
Expand Down

0 comments on commit 15e4407

Please sign in to comment.