Skip to content

Commit 41e2d15

Browse files
alann-maulanadplewis
authored andcommitted
Microsoft Graph Authentication (parse-community#6051)
* add microsoft graph auth * change mail to id * add graph user id and email * add microsoft graph auth test case * remove validating auth data using mail * add test case to AuthenticationAdapters * fix indentation * fix httpsRequest and fakeClaim not found * add newline eof last * fix test in auth adapter * fix unhandled promise rejection
1 parent 4993470 commit 41e2d15

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const responses = {
1515
weibo: { uid: 'userId' },
1616
qq: 'callback( {"openid":"userId"} );', // yes it's like that, run eval in the client :P
1717
phantauth: { sub: 'userId' },
18+
microsoft: { id: 'userId', mail: 'userMail' },
1819
};
1920

2021
describe('AuthenticationProviders', function() {
@@ -37,7 +38,8 @@ describe('AuthenticationProviders', function() {
3738
'wechat',
3839
'weibo',
3940
'phantauth',
40-
].map(function(providerName) {
41+
'microsoft',
42+
].map(function (providerName) {
4143
it('Should validate structure of ' + providerName, done => {
4244
const provider = require('../lib/Adapters/Auth/' + providerName);
4345
jequal(typeof provider.validateAuthData, 'function');
@@ -1194,3 +1196,17 @@ describe('phant auth adapter', () => {
11941196
}
11951197
});
11961198
});
1199+
1200+
describe('microsoft graph auth adapter', () => {
1201+
const microsoft = require('../lib/Adapters/Auth/microsoft');
1202+
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
1203+
1204+
it('should use access_token for validation is passed and responds with id and mail', async () => {
1205+
spyOn(httpsRequest, 'get').and.callFake(() => {
1206+
return Promise.resolve({ id: 'userId', mail: 'userMail' });
1207+
});
1208+
await microsoft.validateAuthData(
1209+
{ id: 'userId', access_token: 'the_token' }
1210+
);
1211+
});
1212+
});

spec/MicrosoftAuth.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const microsoft = require('../lib/Adapters/Auth/microsoft');
2+
3+
describe('Microsoft Auth', () => {
4+
it('should fail to validate Microsoft Graph auth with bad token', done => {
5+
const authData = {
6+
id: 'fake-id',
7+
8+
access_token: 'very.long.bad.token',
9+
};
10+
microsoft.validateAuthData(authData).then(done.fail, err => {
11+
expect(err.code).toBe(101);
12+
expect(err.message).toBe(
13+
'Microsoft Graph auth is invalid for this user.'
14+
);
15+
done();
16+
});
17+
});
18+
});

src/Adapters/Auth/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const wechat = require('./wechat');
2020
const weibo = require('./weibo');
2121
const oauth2 = require('./oauth2');
2222
const phantauth = require('./phantauth');
23+
const microsoft = require('./microsoft');
2324

2425
const anonymous = {
2526
validateAuthData: () => {
@@ -51,6 +52,7 @@ const providers = {
5152
wechat,
5253
weibo,
5354
phantauth,
55+
microsoft,
5456
};
5557

5658
function authDataValidator(adapter, appIds, options) {

src/Adapters/Auth/microsoft.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Helper functions for accessing the microsoft graph API.
2+
var Parse = require('parse/node').Parse;
3+
const httpsRequest = require('./httpsRequest');
4+
5+
// Returns a promise that fulfills if this user mail is valid.
6+
function validateAuthData(authData) {
7+
return request('me', authData.access_token).then(
8+
response => {
9+
if (response && response.id && response.id == authData.id) {
10+
return;
11+
}
12+
throw new Parse.Error(
13+
Parse.Error.OBJECT_NOT_FOUND,
14+
'Microsoft Graph auth is invalid for this user.'
15+
);
16+
}
17+
);
18+
}
19+
20+
// Returns a promise that fulfills if this app id is valid.
21+
function validateAppId() {
22+
return Promise.resolve();
23+
}
24+
25+
// A promisey wrapper for api requests
26+
function request(path, access_token) {
27+
return httpsRequest.get({
28+
host: 'graph.microsoft.com',
29+
path: '/v1.0/' + path,
30+
headers: {
31+
Authorization: 'Bearer ' + access_token,
32+
},
33+
});
34+
}
35+
36+
module.exports = {
37+
validateAppId: validateAppId,
38+
validateAuthData: validateAuthData,
39+
};

0 commit comments

Comments
 (0)