-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrosoft_server.js
104 lines (96 loc) · 2.87 KB
/
microsoft_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
MicrosoftGraph = require('@microsoft/microsoft-graph-client')
Microsoft = {};
OAuth.registerService('microsoft', 2, null, function(query) {
var respData = getAccessToken(query);
var c = MicrosoftGraph.Client.init({
authProvider: function(done) {
return done(null, respData.access_token)
}
})
// FIXME: we could just read it from the jwt passed in id_token
var me = Promise.await(c.api('/me').get())
return {
serviceData: {
id: me.id,
accessToken: OAuth.sealSecret(respData.access_token),
email: me.mail,
provider: respData.provider,
tokenData: {
id_token: respData.id_token,
refresh_token: respData.refresh_token,
expires_at: new Date().getTime() + 1000*respData.expires_in,
scope:respData.scope,
},
name: me.displayName
},
options: {
profile: { email: respData.email_address, name: me.displayName }
}
};
});
// http://developer.github.com/v3/#user-agent-required
var userAgent = 'Meteor';
if (Meteor.release) userAgent += '/' + Meteor.release;
var getIdentity = function(namespace, accessToken) {
try {
return HTTP.get('https://graph.microsoft.com/v1.0/me', {
headers: {
Accept: 'application/json',
'User-Agent': userAgent
}, // http://developer.github.com/v3/#user-agent-required
auth: accessToken + ':'
}).data;
} catch (err) {
throw _.extend(
new Error('Failed to fetch identity from Microsoft. ' + err.message),
{ response: err.response }
);
}
};
var getAccessToken = function(query) {
var config = ServiceConfiguration.configurations.findOne({
service: 'microsoft'
});
if (!config) throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
'https://login.microsoftonline.com/common/oauth2/token',
//'https://login.microsoftonline.com/common/oauth2/v2.0/token',
{
headers: {
Accept: 'application/json',
'User-Agent': userAgent
},
params: {
resource: 'https://graph.microsoft.com',
code: query.code,
grant_type: 'authorization_code',
client_id: config.clientId,
client_secret: config.secret,
redirect_uri: config.redirect_uri
}
}
);
} catch (err) {
throw _.extend(
new Error(
'Failed to complete OAuth handshake with Microsoft. ' + err.message
),
{ response: err.response }
);
}
if (response.data.error) {
// if the http response was a json object with an error attribute
throw new Error(
'Failed to complete OAuth handshake with Microsoft. ' +
response.data.reason
);
} else {
return response.data
}
};
Microsoft.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};