Skip to content

Commit cbca443

Browse files
szkibadplewis
authored andcommitted
support PhantAuth authentication (parse-community#5850)
* support PhantAuth authentication * fix spelling issues * Add test case
1 parent 26030ce commit cbca443

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const responses = {
1313
wechat: { errcode: 0 },
1414
weibo: { uid: 'userId' },
1515
qq: 'callback( {"openid":"userId"} );', // yes it's like that, run eval in the client :P
16+
phantauth: { sub: 'userId' },
1617
};
1718

1819
describe('AuthenticationProviders', function() {
@@ -33,6 +34,7 @@ describe('AuthenticationProviders', function() {
3334
'spotify',
3435
'wechat',
3536
'weibo',
37+
'phantauth',
3638
].map(function(providerName) {
3739
it('Should validate structure of ' + providerName, done => {
3840
const provider = require('../lib/Adapters/Auth/' + providerName);
@@ -1165,3 +1167,23 @@ describe('apple signin auth adapter', () => {
11651167
}
11661168
});
11671169
});
1170+
1171+
describe('phant auth adapter', () => {
1172+
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
1173+
1174+
it('validateAuthData should throw for invalid auth', async () => {
1175+
const authData = {
1176+
id: 'fakeid',
1177+
access_token: 'sometoken',
1178+
};
1179+
const { adapter } = authenticationLoader.loadAuthAdapter('phantauth', {});
1180+
1181+
spyOn(httpsRequest, 'get').and.callFake(() => Promise.resolve({ sub: 'invalidID' }));
1182+
try {
1183+
await adapter.validateAuthData(authData);
1184+
fail();
1185+
} catch (e) {
1186+
expect(e.message).toBe('PhantAuth auth is invalid for this user.');
1187+
}
1188+
});
1189+
});

src/Adapters/Auth/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const qq = require('./qq');
1818
const wechat = require('./wechat');
1919
const weibo = require('./weibo');
2020
const oauth2 = require('./oauth2');
21+
const phantauth = require('./phantauth');
2122

2223
const anonymous = {
2324
validateAuthData: () => {
@@ -47,6 +48,7 @@ const providers = {
4748
qq,
4849
wechat,
4950
weibo,
51+
phantauth,
5052
};
5153

5254
function authDataValidator(adapter, appIds, options) {

src/Adapters/Auth/phantauth.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* PhantAuth was designed to simplify testing for applications using OpenID Connect
3+
* authentication by making use of random generated users.
4+
*
5+
* To learn more, please go to: https://www.phantauth.net
6+
*/
7+
8+
const { Parse } = require('parse/node');
9+
const httpsRequest = require('./httpsRequest');
10+
11+
// Returns a promise that fulfills if this user id is valid.
12+
function validateAuthData(authData) {
13+
return request('auth/userinfo', authData.access_token).then(data => {
14+
if (data && data.sub == authData.id) {
15+
return;
16+
}
17+
throw new Parse.Error(
18+
Parse.Error.OBJECT_NOT_FOUND,
19+
'PhantAuth auth is invalid for this user.'
20+
);
21+
});
22+
}
23+
24+
// Returns a promise that fulfills if this app id is valid.
25+
function validateAppId() {
26+
return Promise.resolve();
27+
}
28+
29+
// A promisey wrapper for api requests
30+
function request(path, access_token) {
31+
return httpsRequest.get({
32+
host: 'phantauth.net',
33+
path: '/' + path,
34+
headers: {
35+
Authorization: 'bearer ' + access_token,
36+
'User-Agent': 'parse-server',
37+
},
38+
});
39+
}
40+
41+
module.exports = {
42+
validateAppId: validateAppId,
43+
validateAuthData: validateAuthData,
44+
};

0 commit comments

Comments
 (0)