|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE.txt in the project root for license information. |
2 | | -var credentials = { |
| 2 | +const credentials = { |
3 | 3 | client: { |
4 | | - id: 'YOUR APP ID HERE', |
5 | | - secret: 'YOUR APP PASSWORD HERE', |
| 4 | + id: 'YOUR APP ID HERE', |
| 5 | + secret: 'YOUR APP PASSWORD HERE', |
6 | 6 | }, |
7 | 7 | auth: { |
8 | 8 | tokenHost: 'https://login.microsoftonline.com', |
9 | 9 | authorizePath: 'common/oauth2/v2.0/authorize', |
10 | 10 | tokenPath: 'common/oauth2/v2.0/token' |
11 | 11 | } |
12 | 12 | }; |
13 | | -var oauth2 = require('simple-oauth2').create(credentials); |
| 13 | +const oauth2 = require('simple-oauth2').create(credentials); |
14 | 14 |
|
15 | | -var redirectUri = 'http://localhost:8000/authorize'; |
| 15 | +const redirectUri = 'http://localhost:8000/authorize'; |
16 | 16 |
|
17 | 17 | // The scopes the app requires |
18 | | -var scopes = [ 'openid', |
| 18 | +const scopes = [ 'openid', |
19 | 19 | 'offline_access', |
20 | 20 | 'User.Read', |
21 | 21 | 'Mail.Read', |
22 | 22 | 'Calendars.Read', |
23 | 23 | 'Contacts.Read' ]; |
24 | 24 |
|
25 | 25 | function getAuthUrl() { |
26 | | - var returnVal = oauth2.authorizationCode.authorizeURL({ |
| 26 | + const returnVal = oauth2.authorizationCode.authorizeURL({ |
27 | 27 | redirect_uri: redirectUri, |
28 | 28 | scope: scopes.join(' ') |
29 | 29 | }); |
30 | 30 | console.log('Generated auth url: ' + returnVal); |
31 | 31 | return returnVal; |
32 | 32 | } |
33 | 33 |
|
34 | | -function getTokenFromCode(auth_code, callback, response) { |
35 | | - var token; |
36 | | - oauth2.authorizationCode.getToken({ |
37 | | - code: auth_code, |
38 | | - redirect_uri: redirectUri, |
39 | | - scope: scopes.join(' ') |
40 | | - }, function (error, result) { |
41 | | - if (error) { |
42 | | - console.log('Access token error: ', error.message); |
43 | | - callback(response, error, null); |
44 | | - } else { |
45 | | - token = oauth2.accessToken.create(result); |
46 | | - console.log('Token created: ', token.token); |
47 | | - callback(response, null, token); |
48 | | - } |
49 | | - }); |
| 34 | +async function getTokenFromCode(auth_code) { |
| 35 | + let result = await oauth2.authorizationCode.getToken({ |
| 36 | + code: auth_code, |
| 37 | + redirect_uri: redirectUri, |
| 38 | + scope: scopes.join(' ') |
| 39 | + }); |
| 40 | + |
| 41 | + const token = oauth2.accessToken.create(result); |
| 42 | + console.log('Token created: ', token.token); |
| 43 | + return token; |
50 | 44 | } |
51 | 45 |
|
52 | | -function refreshAccessToken(refreshToken, callback) { |
53 | | - var tokenObj = oauth2.accessToken.create({refresh_token: refreshToken}); |
54 | | - tokenObj.refresh(callback); |
| 46 | +function refreshAccessToken(refreshToken) { |
| 47 | + return oauth2.accessToken.create({refresh_token: refreshToken}).refresh(); |
55 | 48 | } |
56 | 49 |
|
57 | 50 | exports.getAuthUrl = getAuthUrl; |
|
0 commit comments