Skip to content

Commit

Permalink
🐛 fixed response data as JSON (#140)
Browse files Browse the repository at this point in the history
* fixed issue invalid json response body at https://appleid.apple.com/auth/revoke

* fixed issue invalid json response body at https://appleid.apple.com/auth/revoke

* fixed issue invalid json response body at https://appleid.apple.com/auth/revoke

* populate response as json if can be

* - populate response data as json if not empty
- add test case for each functions

Co-authored-by: Anh Nguyen <anh.nguyen@kilo.vn>
  • Loading branch information
dauden and Anh Nguyen committed Jun 23, 2022
1 parent 339e124 commit 95f75bf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
55 changes: 55 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,58 @@ describe('appleSignin test', () => {
expect(_getApplePublicKeys).toBeTruthy();
});
});

describe('test for each functions', () => {
describe('test revokeAuthorizationToken functions', () => {
const token = 'test token';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.revokeAuthorizationToken(token, option);
expect(result).toEqual('');
});
});

describe('test getAuthorizationToken functions', () => {
const code = 'test code';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.getAuthorizationToken(code, option);
expect(result).toEqual({
error: 'invalid_client',
});
});
});

describe('test refreshAuthorizationToken functions', () => {
const refreshToken = 'test refreshToken';
const option = {
clientID: 'clientID',
clientSecret: 'clientSecret',
tokenHintType: 'refresh_token',
};
it('Should not throw error when response is empty', async () => {
const result = await appleSignin.refreshAuthorizationToken(
refreshToken,
option,
);
expect(result).toEqual({
error: 'invalid_client',
});
});
});

describe('test _getApplePublicKeys functions', () => {
it('Should not throw error when response is empty', async () => {
const result = await appleSignin._getApplePublicKeys();
expect(result).not.toBeNull();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apple-signin-auth",
"version": "1.7.2",
"version": "1.7.3",
"description": " Apple signin for node.",
"author": {
"name": "Ahmed Tarek",
Expand Down
21 changes: 14 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ const getClientSecret = (
*
* populate response as json if can be
*/
const _populateDResAsJson = (res) =>
typeof res?.json === 'function' ? res.json() : res;
const _populateResAsJson = async (res) => {
const data = await res.text();
if (!data) {
return data;
}
return JSON.parse(data);
};

/** Gets an Apple authorization token */
const getAuthorizationToken = async (
Expand Down Expand Up @@ -228,7 +233,7 @@ const getAuthorizationToken = async (
return fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateDResAsJson(res));
}).then((res) => _populateResAsJson(res));
};

/** Refreshes an Apple authorization token */
Expand Down Expand Up @@ -258,7 +263,7 @@ const refreshAuthorizationToken = async (
return fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateDResAsJson(res));
}).then((res) => _populateResAsJson(res));
};

/** Revoke Apple authorization token */
Expand Down Expand Up @@ -286,10 +291,12 @@ const revokeAuthorizationToken = async (
params.append('token', token);
params.append('token_hint_type', options.tokenHintType);

return fetch(url.toString(), {
const result = await fetch(url.toString(), {
method: 'POST',
body: params,
}).then((res) => _populateDResAsJson(res));
});

return _populateResAsJson(result);
};

/** Gets an Array of Apple Public Keys that can be used to decode Apple's id tokens */
Expand All @@ -305,7 +312,7 @@ const _getApplePublicKeys = async ({
headers: {
'Content-Type': 'application/json',
},
}).then((res) => _populateDResAsJson(res));
}).then((res) => _populateResAsJson(res));

// Reset cache - will be refilled below
APPLE_KEYS_CACHE = {};
Expand Down

0 comments on commit 95f75bf

Please sign in to comment.