Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back4app4.1.0 apple auth #51

Open
wants to merge 5 commits into
base: back4app4.1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 56 additions & 56 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@parse/push-adapter": "3.2.0",
"@parse/s3-files-adapter": "1.4.0",
"@parse/simple-mailgun-adapter": "1.1.0",
"apollo-server-express": "2.10.1",
"apollo-server-express": "2.11.0",
"bcryptjs": "2.4.3",
"body-parser": "1.19.0",
"commander": "4.1.1",
Expand Down Expand Up @@ -73,7 +73,7 @@
"apollo-utilities": "1.3.3",
"babel-eslint": "10.1.0",
"bcrypt-nodejs": "0.0.3",
"cross-env": "7.0.0",
"cross-env": "7.0.1",
"deep-diff": "1.0.2",
"eslint": "6.8.0",
"eslint-plugin-flowtype": "4.5.0",
Expand Down
119 changes: 119 additions & 0 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ describe('oauth2 auth adapter', () => {
describe('apple signin auth adapter', () => {
const apple = require('../lib/Adapters/Auth/apple');
const jwt = require('jsonwebtoken');
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');

it('should throw error with missing id_token', async () => {
try {
Expand All @@ -1146,7 +1147,89 @@ describe('apple signin auth adapter', () => {
}
});

it('should not decode invalid id_token', async () => {
try {
await apple.validateAuthData(
{ id: 'the_user_id', token: 'the_token' },
{ client_id: 'secret' }
);
fail();
} catch (e) {
expect(e.message).toBe('provided token does not decode as JWT');
}
});

it('should throw error if public key used to encode token is not available', async () => {
try {
const fakeDecodedToken = { header: { kid: '789', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);

await apple.validateAuthData(
{ id: 'the_user_id', token: 'the_token' },
{ client_id: 'secret' }
);
fail();
} catch (e) {
expect(e.message).toBe(
'Public key with matching key ID to token not found'
);
}
});

it('should use algorithm from key header to verify id_token', async () => {
const fakeClaim = {
iss: 'https://appleid.apple.com',
aud: 'secret',
exp: Date.now(),
sub: 'the_user_id',
};
const fakeDecodedToken = { header: { kid: '123', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);
spyOn(jwt, 'verify').and.callFake(() => fakeClaim);

const result = await apple.validateAuthData(
{ id: 'the_user_id', token: 'the_token' },
{ client_id: 'secret' }
);
expect(result).toEqual(fakeClaim);
expect(jwt.verify.calls.first().args[2].algorithms).toEqual(
fakeDecodedToken.header.alg
);
});

it('should not verify invalid id_token', async () => {
const fakeDecodedToken = { header: { kid: '123', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);
try {
await apple.validateAuthData(
{ id: 'the_user_id', token: 'the_token' },
Expand All @@ -1165,6 +1248,18 @@ describe('apple signin auth adapter', () => {
exp: Date.now(),
sub: 'the_user_id',
};
const fakeDecodedToken = { header: { kid: '123', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);
spyOn(jwt, 'verify').and.callFake(() => fakeClaim);

const result = await apple.validateAuthData(
Expand All @@ -1179,6 +1274,18 @@ describe('apple signin auth adapter', () => {
iss: 'https://not.apple.com',
sub: 'the_user_id',
};
const fakeDecodedToken = { header: { kid: '123', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);
spyOn(jwt, 'verify').and.callFake(() => fakeClaim);

try {
Expand All @@ -1200,6 +1307,18 @@ describe('apple signin auth adapter', () => {
aud: 'invalid_client_id',
sub: 'the_user_id',
};
const fakeDecodedToken = { header: { kid: '123', alg: 'RS256' } };
const fakeKeys = {
keys: [
{
e: 'AQAB',
kid: '123',
n: 'ABCDEFGH',
},
],
};
spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
spyOn(httpsRequest, 'get').and.callFake(() => fakeKeys);
spyOn(jwt, 'verify').and.callFake(() => fakeClaim);

try {
Expand Down
Loading