Skip to content

Commit

Permalink
Merge pull request #30 from uphold/feature/reset-2fa-email
Browse files Browse the repository at this point in the history
Add scope and otp header to storage
  • Loading branch information
pgom authored Feb 21, 2020
2 parents 473d706 + 51d11bc commit 84f10f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/core/errors/otp-required.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BaseError } from './base';
import { includes } from 'lodash';

export class OTPRequiredError extends BaseError {
static hasError({ headers } = {}) {
if (!headers || !headers['otp-token']) {
return false;
}

return headers['otp-token'].toUpperCase() === 'REQUIRED';
return includes(['OPTIONAL', 'REQUIRED'], headers['otp-token'].toUpperCase());
}

constructor() {
Expand Down
12 changes: 9 additions & 3 deletions src/core/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as actions from './actions';
import { AuthorizationRequiredError, UnauthorizedError } from './errors';
import { OAuthClient, Paginator } from './services';
import { buildBearerAuthorizationHeader, buildUrl } from './utils';
import { get } from 'lodash';

export default class SDK {
constructor(options) {
Expand All @@ -21,7 +22,9 @@ export default class SDK {
accessTokenKey: 'uphold.access_token',
baseUrl: 'https://api.uphold.com',
itemsPerPage: 10,
otpTokenStatus: 'uphold.otp_token_status',
refreshTokenKey: 'uphold.refresh_token',
scope: 'uphold.scope',
version: 'v0'
};

Expand Down Expand Up @@ -122,19 +125,22 @@ export default class SDK {
]);
}

setToken(token) {
setToken(token, headers = {}) {
return this.storage.setItem(this.options.accessTokenKey, token.access_token)
.then(() => {
this.storage.setItem(this.options.scope, get(token, 'scope', ''));
this.storage.setItem(this.options.otpTokenStatus, get(headers, 'otp-token', ''));

if (token.refresh_token) {
return this.storage.setItem(this.options.refreshTokenKey, token.refresh_token);
this.storage.setItem(this.options.refreshTokenKey, token.refresh_token);
}
})
.then(() => token);
}

_authenticationRequest({ body, headers, url }) {
return this.client.request(url, 'post', body, headers)
.then(({ body }) => this.setToken(body));
.then(({ body, headers }) => this.setToken(body, headers));
}

_refreshToken(url, method, body, headers, options) { // eslint-disable-line max-params
Expand Down
8 changes: 5 additions & 3 deletions test/core/sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,19 @@ describe('SDK', () => {
sdk.storage = { setItem: jest.fn(() => Promise.resolve()) };
});

it('should store both access and refresh tokens', () => {
return sdk.setToken({ access_token: 'foo', refresh_token: 'bar' })
it('should store both access and refresh tokens, otp-token header and scope', () => {
return sdk.setToken({ access_token: 'foo', refresh_token: 'bar', scope: 'read' }, { 'otp-token': 'required' })
.then(() => {
expect(sdk.storage.setItem.mock.calls).toEqual([
['uphold.access_token', 'foo'],
['uphold.scope', 'read'],
['uphold.otp_token_status', 'required'],
['uphold.refresh_token', 'bar']
]);
});
});

it('should not set refresh_token if not provided', () => {
it('should not set refresh_token, scope or otp-token header if not provided', () => {
return sdk.setToken({ access_token: 'foo' })
.then(() => {
expect(sdk.storage.setItem).toBeCalledWith('uphold.access_token', 'foo');
Expand Down

0 comments on commit 84f10f3

Please sign in to comment.