@@ -2,17 +2,23 @@ import { ConfidentialClientApplication } from '@azure/msal-node';
22
33import { msalConfiguration } from '../constants' ;
44
5- const resource = 'https://graph.microsoft.com/' ;
5+ const resource = 'https://graph.microsoft.com/.default ' ;
66
77/**
88 * Generate a fully formed uri to use for authentication based on the supplied resource argument
99 * @return {string } a fully formed uri with which authentication can be completed.
1010 */
1111export function getAuthUrl ( ) {
12- return msalConfiguration . authority + '/oauth2/authorize'
13- + '?client_id=' + msalConfiguration . clientID
14- + '&response_type=code'
15- + '&redirect_uri=' + msalConfiguration . redirectUri ;
12+ const authContext = new ConfidentialClientApplication ( {
13+ auth : {
14+ clientId : msalConfiguration . clientID ,
15+ authority : msalConfiguration . authority . replace ( 'common' , msalConfiguration . tenantID ) ,
16+ clientSecret : msalConfiguration . clientSecret
17+ }
18+ } ) ;
19+ return authContext . getAuthCodeUrl ( {
20+ redirectUri : msalConfiguration . redirectUri
21+ } ) ;
1622}
1723
1824/**
@@ -21,34 +27,29 @@ export function getAuthUrl() {
2127 * @param {AcquireTokenCallback } callback The callback function.
2228 */
2329export function getTokenFromCode ( code ) {
24- const authContext = new ConfidentialClientApplication ( msalConfiguration . authority ) ; // TODO replace by onbehalf when available
25- return new Promise ( ( resolve , reject ) => {
26- authContext . acquireTokenWithAuthorizationCode (
27- code ,
28- msalConfiguration . redirectUri ,
29- resource ,
30- msalConfiguration . clientID ,
31- msalConfiguration . clientSecret ,
32- ( err , token ) => {
33- if ( err ) {
34- reject ( err ) ;
35- } else {
36- resolve ( token ) ;
37- }
38- }
39- ) ;
30+ const authContext = new ConfidentialClientApplication ( {
31+ auth : {
32+ clientId : msalConfiguration . clientID ,
33+ authority : msalConfiguration . authority . replace ( 'common' , msalConfiguration . tenantID ) ,
34+ clientSecret : msalConfiguration . clientSecret
35+ }
36+ } ) ;
37+ return authContext . acquireTokenByCode ( {
38+ code : code ,
39+ redirectUri : msalConfiguration . redirectUri ,
40+ scopes : [ resource ]
4041 } ) ;
4142}
4243
4344export function getAppOnlyToken ( ) {
44- const authContext = new ConfidentialClientApplication ( msalConfiguration . authority . replace ( 'common' , msalConfiguration . tenantID ) ) ;
45- return new Promise ( ( resolve , reject ) => { // TODO replace by client credential flow when available
46- authContext . acquireTokenWithClientCredentials ( resource , msalConfiguration . clientID , msalConfiguration . clientSecret , ( err , token ) => {
47- if ( err ) {
48- reject ( err ) ;
49- } else {
50- resolve ( token ) ;
51- }
52- } ) ;
45+ const authContext = new ConfidentialClientApplication ( {
46+ auth : {
47+ clientId : msalConfiguration . clientID ,
48+ authority : msalConfiguration . authority . replace ( 'common' , msalConfiguration . tenantID ) ,
49+ clientSecret : msalConfiguration . clientSecret
50+ }
51+ } ) ;
52+ return authContext . acquireTokenByClientCredential ( {
53+ scopes : [ resource ]
5354 } ) ;
5455}
0 commit comments