Skip to content

Commit

Permalink
Merge pull request #232 from fenichelar/master
Browse files Browse the repository at this point in the history
Add support for dynamic header in authentication request
  • Loading branch information
jpadilla authored Jun 7, 2018
2 parents c4d320b + 2d8fd25 commit b0ff5dd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 8 deletions.
5 changes: 3 additions & 2 deletions addon/authenticators/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ export default TokenAuthenticator.extend({
@method authenticate
@param {Object} credentials The credentials to authenticate the session with
@param {Object} headers Optional headers to send with the authentication request
@return {Promise} A promise that resolves when an auth token is
successfully acquired from the server and rejects
otherwise
*/
authenticate(credentials) {
authenticate(credentials, headers) {
return new Promise((resolve, reject) => {
this.makeRequest(this.serverTokenEndpoint, credentials, this.headers).then(response => {
this.makeRequest(this.serverTokenEndpoint, credentials, assign({}, this.headers, headers)).then(response => {
try {
const sessionData = this.handleAuthResponse(response);
return resolve(sessionData);
Expand Down
9 changes: 5 additions & 4 deletions addon/authenticators/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EmberObject from '@ember/object';
import fetch from 'fetch';
import { merge } from '@ember/polyfills';
import { assign } from '@ember/polyfills';
import { Promise, resolve } from 'rsvp';
import { isEmpty } from '@ember/utils';
import Base from 'ember-simple-auth/authenticators/base';
Expand Down Expand Up @@ -62,11 +62,12 @@ export default Base.extend({
@method authenticate
@param {Object} credentials The credentials to authenticate the session with
@param {Object} headers Optional headers to send with the authentication request
@return {Promise} A promise that resolves when an auth token is successfully acquired from the server and rejects otherwise
*/
authenticate(credentials) {
authenticate(credentials, headers) {
return new Promise((resolve, reject) => {
this.makeRequest(this.serverTokenEndpoint, credentials, this.headers).then(response => {
this.makeRequest(this.serverTokenEndpoint, credentials, assign({}, this.headers, headers)).then(response => {
return resolve(response);
}).catch(error => {
return reject(error);
Expand Down Expand Up @@ -95,7 +96,7 @@ export default Base.extend({
return new Promise((resolve, reject) => {
return fetch(url, {
method: 'POST',
headers: merge({
headers: assign({
'Accept': 'application/json',
'Content-Type': 'application/json'
}, headers),
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-simple-auth-token",
"version": "4.0.1",
"version": "4.0.2",
"description": "An authenticator and authorizer for Ember Simple Auth that is compatible with token-based authentication like JWT in Ember CLI applications.",
"directories": {
"doc": "doc",
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/authenticators/jwt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,56 @@ test('#authenticate sends an fetch request with custom headers', assert => {
});
});

test('#authenticate sends an fetch request with dynamic headers', assert => {
assert.expect(2);

const headers = {
'X-API-KEY': '123-abc',
'X-ANOTHER-HEADER': 0,
'Accept': 'application/vnd.api+json'
};

const token = createFakeToken();
const refreshToken = createFakeRefreshToken();
const credentials = createFakeCredentials();
const response = {
[App.authenticator.tokenPropertyName]: token,
[App.authenticator.refreshTokenPropertyName]: refreshToken
};
const refreshResponse = {
[App.authenticator.tokenPropertyName]: token,
[App.authenticator.refreshTokenPropertyName]: refreshToken
};

App.server.respondWith('POST', App.authenticator.serverTokenEndpoint, [
201,
{
'Content-Type': 'application/json'
},
JSON.stringify(response)
]);
App.server.respondWith('POST', App.authenticator.serverTokenRefreshEndpoint, [
201,
{
'Content-Type': 'application/json'
},
JSON.stringify(refreshResponse)
]);

return App.authenticator.authenticate(credentials, headers).then(() => {
const args = fetchWrapper.default.getCall(0).args;
assert.equal(args[0], App.authenticator.serverTokenEndpoint);
assert.deepEqual(args[1], {
method: 'POST',
body: JSON.stringify(credentials),
headers: merge({
'Accept': 'application/json',
'Content-Type': 'application/json'
}, headers)
});
});
});

test('#authenticate rejects with the correct error', assert => {
const credentials = createFakeCredentials();

Expand Down
34 changes: 34 additions & 0 deletions tests/unit/authenticators/token-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,40 @@ test('#authenticate sends an fetch request with custom headers', assert => {
});
});

test('#authenticate sends an fetch request with dynamic headers', assert => {
const response = {
[App.authenticator.tokenPropertyName]: 'secret token!'
};
const credentials = createFakeCredentials();

const headers = {
'X-API-KEY': '123-abc',
'X-ANOTHER-HEADER': 0,
'Accept': 'application/vnd.api+json'
};

App.server.respondWith('POST', App.authenticator.serverTokenEndpoint, [
201,
{
'Content-Type': 'application/json'
},
JSON.stringify(response)
]);

return App.authenticator.authenticate(credentials, headers).then(() => {
const args = fetchWrapper.default.getCall(0).args;
assert.equal(args[0], App.authenticator.serverTokenEndpoint);
assert.deepEqual(args[1], {
method: 'POST',
body: JSON.stringify(credentials),
headers: merge({
'Accept': 'application/json',
'Content-Type': 'application/json'
}, headers)
});
});
});

test('#authenticate rejects with the correct error', assert => {
const credentials = createFakeCredentials();

Expand Down

0 comments on commit b0ff5dd

Please sign in to comment.