Skip to content

Commit 6745519

Browse files
committed
tmp
1 parent 0406347 commit 6745519

File tree

3 files changed

+177
-20
lines changed

3 files changed

+177
-20
lines changed

package-lock.json

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"webpack-merge": "^5.2.0"
6464
},
6565
"dependencies": {
66+
"@azure/msal-browser": "^2.19.0",
6667
"@paperbits/azure": "0.1.356-hotfix",
6768
"@paperbits/common": "0.1.396",
6869
"@paperbits/core": "0.1.396",

src/authentication/armAuthenticator.ts

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,135 @@
1-
import * as Msal from "msal";
1+
import * as Msal from "@azure/msal-browser";
22
import { Utils } from "../utils";
33
import { IAuthenticator, AccessToken } from ".";
44

55

6-
const aadClientId = "73a510c3-9946-46dd-b5ae-a8f0ae68fd04"; // test app
6+
const aadClientId = "a962e1ed-5694-4abe-9e9b-d08d35877efc"; // test app
77
const scopes = ["https://management.azure.com/user_impersonation"];
8+
const loginRequest = { scopes: ["openid", "profile", "https://management.azure.com/user_impersonation"] };
9+
810

911
export class ArmAuthenticator implements IAuthenticator {
1012
private accessToken: AccessToken;
11-
private msalInstance: Msal.UserAgentApplication;
13+
private msalInstance: Msal.PublicClientApplication;
1214

1315
constructor() {
1416
const msalConfig: Msal.Configuration = {
1517
auth: {
1618
clientId: aadClientId,
1719
authority: "https://login.microsoftonline.com/common",
1820
redirectUri: "https://apimanagement-cors-proxy-df.azure-api.net/portal/signin-aad",
21+
},
22+
cache: {
23+
cacheLocation: "sessionStorage", // This configures where your cache will be stored
24+
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
1925
}
2026
};
2127

22-
this.msalInstance = new Msal.UserAgentApplication(msalConfig);
28+
this.msalInstance = new Msal.PublicClientApplication(msalConfig);
29+
this.checkCallbacks();
30+
}
31+
32+
public async checkCallbacks(): Promise<void> {
33+
try {
34+
const response = await this.msalInstance.handleRedirectPromise();
35+
36+
debugger;
37+
38+
if (response !== null) {
39+
// sessionStorage[tokenKey] = response.idToken;
40+
// this.onLogin.next(true);
41+
42+
// this.checkToken();
43+
}
44+
}
45+
catch (error) {
46+
console.error(error);
47+
}
48+
}
49+
50+
51+
52+
private async getTokenRedirect(request): Promise<Msal.AuthenticationResult> {
53+
const account = this.getAccount();
54+
55+
if (!account) {
56+
await this.msalInstance.acquireTokenRedirect(request);
57+
return;
58+
}
59+
60+
request.account = account;
61+
62+
try {
63+
return await this.msalInstance.acquireTokenSilent(request);
64+
}
65+
catch (error) {
66+
console.warn("silent token acquisition fails. acquiring token using redirect");
67+
68+
if (error instanceof Msal.InteractionRequiredAuthError) {
69+
// fallback to interaction when silent call fails
70+
await this.msalInstance.acquireTokenRedirect(request);
71+
}
72+
else {
73+
console.warn(error);
74+
}
75+
}
76+
}
77+
78+
private getAccount(): Msal.AccountInfo {
79+
const accounts = this.msalInstance.getAllAccounts();
80+
81+
if (accounts.length === 0) {
82+
return null;
83+
}
84+
85+
return accounts[0];
2386
}
2487

25-
private async tryAcquireToken(): Promise<AccessToken> {
26-
let response: Msal.AuthResponse;
27-
const loginRequest: Msal.AuthenticationParameters = { scopes: scopes };
88+
private async tryAcquireToken(request: any): Promise<any> {
89+
const account = this.getAccount();
90+
91+
if (!account) {
92+
await this.msalInstance.acquireTokenRedirect(request);
93+
return;
94+
}
2895

29-
console.log("1");
96+
request.account = account;
3097

31-
if (this.msalInstance.getAccount()) {
32-
response = await this.msalInstance.acquireTokenSilent(loginRequest);
98+
try {
99+
const result = await this.msalInstance.acquireTokenSilent(request);
100+
debugger;
33101
}
34-
else {
35-
response = await this.msalInstance.loginPopup(loginRequest);
102+
catch (error) {
103+
console.warn("silent token acquisition fails. acquiring token using redirect");
104+
105+
if (error instanceof Msal.InteractionRequiredAuthError) {
106+
// fallback to interaction when silent call fails
107+
await this.msalInstance.acquireTokenRedirect(request);
108+
}
109+
else {
110+
console.warn(error);
111+
}
36112
}
37113

38114

39115
console.log("2");
40116
// await Utils.delay(1);
41117

42-
if (!response.accessToken) {
43-
throw new Error(`Unable to acquire ARM token.`);
44-
}
118+
// if (!response.accessToken) {
119+
// throw new Error(`Unable to acquire ARM token.`);
120+
// }
45121

46-
const accessToken = AccessToken.parse(`Bearer ${response.accessToken}`);
47-
this.setAccessToken(accessToken);
122+
// const accessToken = AccessToken.parse(`Bearer ${response.accessToken}`);
123+
// this.setAccessToken(accessToken);
48124

49-
setTimeout(this.tryAcquireToken.bind(this), 30 * 60 * 1000); // scheduling token refresh in 30 min
50125

51-
return accessToken;
126+
127+
// setTimeout(this.tryAcquireToken.bind(this), 30 * 60 * 1000); // scheduling token refresh in 30 min
128+
129+
// return accessToken;
130+
131+
132+
return null;
52133
}
53134

54135
public async getAccessToken(): Promise<AccessToken> {
@@ -66,7 +147,7 @@ export class ArmAuthenticator implements IAuthenticator {
66147
}
67148
}
68149

69-
const accessToken = await this.tryAcquireToken();
150+
const accessToken = await this.tryAcquireToken(loginRequest);
70151
return accessToken;
71152
}
72153

0 commit comments

Comments
 (0)