8
8
/**
9
9
* This sample is referenced from - https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/manual/authorizationCodeSample.ts
10
10
*/
11
+ import "isomorphic-fetch" ;
12
+
11
13
import { AuthorizationCodeCredential } from "@azure/identity" ;
14
+ import { Client } from "@microsoft/microsoft-graph-client" ;
15
+ import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials" ;
12
16
import express from "express" ;
13
17
import { Server } from "http" ;
14
- import "isomorphic-fetch" ;
15
18
import open from "open" ;
16
19
import qs from "qs" ;
17
20
18
- import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials" ;
19
- import { Client } from "@microsoft/microsoft-graph-client" ;
20
- import { port , tenantId , clientSecret , clientId , scopes , authorityHost , redirectUri } from "./secrets" ;
21
+ import { authorityHost , clientId , clientSecret , port , redirectUri , scopes , tenantId } from "./secrets" ;
21
22
22
23
if ( tenantId === undefined || clientId === undefined ) {
23
24
console . error ( "AZURE_TENANT_ID and AZURE_CLIENT_ID environment variables must be set" ) ;
@@ -40,8 +41,7 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
40
41
// authentication redirect to be sent to the local redirect URI.
41
42
const authCodePromise = new Promise < string > ( ( resolve , reject ) => {
42
43
const app = express ( ) ;
43
- let server : Server | undefined = undefined ;
44
-
44
+ const server : Server = app . listen ( port , ( ) => console . log ( `Authorization code redirect server listening on port ${ port } ` ) ) ;
45
45
app . get ( "/authresponse" , ( req , res ) => {
46
46
// Close the temporary server once we've received the redirect.
47
47
res . sendStatus ( 200 ) ;
@@ -57,14 +57,12 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
57
57
reject ( new Error ( `Authentication Error "${ req . query [ "error" ] } ":\n\n${ req . query [ "error_description" ] } ` ) ) ;
58
58
}
59
59
} ) ;
60
-
61
- server = app . listen ( port , ( ) => console . log ( `Authorization code redirect server listening on port ${ port } ` ) ) ;
62
60
} ) ;
63
61
64
62
// Direct the user to the authentication URI either by opening a
65
63
// browser (desktop and mobile apps) or redirecting their browser
66
64
// using a Location header (web apps and APIs).
67
- const authorizeUrl = getAuthorizeUrl ( tenantId ! , clientId ! , scopes ) ;
65
+ const authorizeUrl = getAuthorizeUrl ( tenantId , clientId , scopes ) ;
68
66
console . log ( "Opening user's browser to URL:" , authorizeUrl ) ;
69
67
await open ( authorizeUrl ) ;
70
68
@@ -77,8 +75,8 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
77
75
// refreshing the access token from this point forward.
78
76
if ( clientSecret ) {
79
77
return new AuthorizationCodeCredential (
80
- tenantId ! ,
81
- clientId ! ,
78
+ tenantId ,
79
+ clientId ,
82
80
clientSecret ,
83
81
authorizationCode ,
84
82
redirectUri ,
@@ -92,8 +90,8 @@ async function getCredential(): Promise<AuthorizationCodeCredential> {
92
90
// NOTE: If there is no client secret, we can construct an auth code credential
93
91
// using this method.
94
92
return new AuthorizationCodeCredential (
95
- tenantId ! ,
96
- clientId ! ,
93
+ tenantId ,
94
+ clientId ,
97
95
authorizationCode ,
98
96
redirectUri ,
99
97
// NOTE: It is not necessary to explicitly pass the authorityHost when using
0 commit comments