@azure/msal-angular 4.0.12 → 4.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/.beachballrc +3 -0
  2. package/.editorconfig +16 -0
  3. package/.eslintrc.json +3 -0
  4. package/CHANGELOG.json +2315 -0
  5. package/CHANGELOG.md +869 -0
  6. package/FAQ.md +182 -0
  7. package/angular.json +51 -0
  8. package/docs/angular-universal.md +64 -0
  9. package/docs/configuration.md +609 -0
  10. package/docs/errors.md +89 -0
  11. package/docs/events.md +262 -0
  12. package/docs/initialization.md +206 -0
  13. package/docs/known-issues.md +19 -0
  14. package/docs/logging.md +47 -0
  15. package/docs/msal-guard.md +208 -0
  16. package/docs/msal-interceptor.md +160 -0
  17. package/docs/multi-tenant.md +90 -0
  18. package/docs/performance.md +50 -0
  19. package/docs/public-apis.md +29 -0
  20. package/docs/redirects.md +257 -0
  21. package/docs/security.md +4 -0
  22. package/docs/ssosilent.md +84 -0
  23. package/docs/v0-v1-upgrade-guide.md +61 -0
  24. package/docs/v1-v2-upgrade-guide.md +70 -0
  25. package/docs/v2-v3-upgrade-guide.md +52 -0
  26. package/docs/v3-v4-upgrade-guide.md +36 -0
  27. package/karma.conf.js +44 -0
  28. package/ng-package.json +7 -0
  29. package/package.json +56 -26
  30. package/src/IMsalService.ts +34 -0
  31. package/src/constants.ts +20 -0
  32. package/src/msal.broadcast.config.ts +8 -0
  33. package/src/msal.broadcast.service.spec.ts +471 -0
  34. package/src/msal.broadcast.service.ts +84 -0
  35. package/src/msal.guard.config.ts +27 -0
  36. package/src/msal.guard.spec.ts +525 -0
  37. package/src/msal.guard.ts +290 -0
  38. package/src/msal.interceptor.config.ts +39 -0
  39. package/src/msal.interceptor.spec.ts +1168 -0
  40. package/src/msal.interceptor.ts +388 -0
  41. package/src/msal.module.ts +51 -0
  42. package/src/msal.navigation.client.spec.ts +96 -0
  43. package/src/msal.navigation.client.ts +59 -0
  44. package/src/msal.redirect.component.spec.ts +64 -0
  45. package/src/msal.redirect.component.ts +26 -0
  46. package/src/msal.service.spec.ts +468 -0
  47. package/src/msal.service.ts +104 -0
  48. package/src/packageMetadata.ts +3 -0
  49. package/{public-api.d.ts → src/public-api.ts} +35 -17
  50. package/src/test.ts +19 -0
  51. package/tsconfig.json +25 -0
  52. package/tsconfig.lib.json +27 -0
  53. package/tsconfig.lib.prod.json +10 -0
  54. package/tsconfig.spec.json +17 -0
  55. package/typedoc.json +10 -0
  56. package/IMsalService.d.ts +0 -17
  57. package/constants.d.ts +0 -5
  58. package/esm2020/IMsalService.mjs +0 -6
  59. package/esm2020/azure-msal-angular.mjs +0 -5
  60. package/esm2020/constants.mjs +0 -10
  61. package/esm2020/msal.broadcast.config.mjs +0 -6
  62. package/esm2020/msal.broadcast.service.mjs +0 -66
  63. package/esm2020/msal.guard.config.mjs +0 -6
  64. package/esm2020/msal.guard.mjs +0 -218
  65. package/esm2020/msal.interceptor.config.mjs +0 -6
  66. package/esm2020/msal.interceptor.mjs +0 -270
  67. package/esm2020/msal.module.mjs +0 -46
  68. package/esm2020/msal.navigation.client.mjs +0 -53
  69. package/esm2020/msal.redirect.component.mjs +0 -31
  70. package/esm2020/msal.service.mjs +0 -88
  71. package/esm2020/packageMetadata.mjs +0 -4
  72. package/esm2020/public-api.mjs +0 -18
  73. package/fesm2015/azure-msal-angular.mjs +0 -762
  74. package/fesm2015/azure-msal-angular.mjs.map +0 -1
  75. package/fesm2020/azure-msal-angular.mjs +0 -758
  76. package/fesm2020/azure-msal-angular.mjs.map +0 -1
  77. package/index.d.ts +0 -5
  78. package/msal.broadcast.config.d.ts +0 -3
  79. package/msal.broadcast.service.d.ts +0 -19
  80. package/msal.guard.config.d.ts +0 -9
  81. package/msal.guard.d.ts +0 -43
  82. package/msal.interceptor.config.d.ts +0 -13
  83. package/msal.interceptor.d.ts +0 -70
  84. package/msal.module.d.ts +0 -13
  85. package/msal.navigation.client.d.ts +0 -19
  86. package/msal.redirect.component.d.ts +0 -15
  87. package/msal.service.d.ts +0 -33
  88. package/packageMetadata.d.ts +0 -2
@@ -0,0 +1,290 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Injectable, Inject } from "@angular/core";
7
+ import { Location } from "@angular/common";
8
+ import {
9
+ ActivatedRouteSnapshot,
10
+ RouterStateSnapshot,
11
+ UrlTree,
12
+ Router,
13
+ } from "@angular/router";
14
+ import {
15
+ InteractionType,
16
+ BrowserConfigurationAuthError,
17
+ BrowserUtils,
18
+ UrlString,
19
+ PopupRequest,
20
+ RedirectRequest,
21
+ AuthenticationResult,
22
+ } from "@azure/msal-browser";
23
+ import { Observable, of } from "rxjs";
24
+ import { concatMap, catchError, map } from "rxjs/operators";
25
+ import { MsalService } from "./msal.service";
26
+ import { MsalGuardConfiguration } from "./msal.guard.config";
27
+ import { MsalBroadcastService } from "./msal.broadcast.service";
28
+ import { MSAL_GUARD_CONFIG } from "./constants";
29
+
30
+ @Injectable()
31
+ export class MsalGuard {
32
+ private loginFailedRoute?: UrlTree;
33
+
34
+ constructor(
35
+ @Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration,
36
+ private msalBroadcastService: MsalBroadcastService,
37
+ private authService: MsalService,
38
+ private location: Location,
39
+ private router: Router
40
+ ) {
41
+ // Subscribing so events in MsalGuard will set inProgress$ observable
42
+ this.msalBroadcastService.inProgress$.subscribe();
43
+ }
44
+
45
+ /**
46
+ * Parses url string to UrlTree
47
+ * @param url
48
+ */
49
+ parseUrl(url: string): UrlTree {
50
+ return this.router.parseUrl(url);
51
+ }
52
+
53
+ /**
54
+ * Builds the absolute url for the destination page
55
+ * @param path Relative path of requested page
56
+ * @returns Full destination url
57
+ */
58
+ getDestinationUrl(path: string): string {
59
+ this.authService.getLogger().verbose("Guard - getting destination url");
60
+ // Absolute base url for the application (default to origin if base element not present)
61
+ const baseElements = document.getElementsByTagName("base");
62
+ const baseUrl = this.location.normalize(
63
+ baseElements.length ? baseElements[0].href : window.location.origin
64
+ );
65
+
66
+ // Path of page (including hash, if using hash routing)
67
+ const pathUrl = this.location.prepareExternalUrl(path);
68
+
69
+ // Hash location strategy
70
+ if (pathUrl.startsWith("#")) {
71
+ this.authService
72
+ .getLogger()
73
+ .verbose("Guard - destination by hash routing");
74
+ return `${baseUrl}/${pathUrl}`;
75
+ }
76
+
77
+ /*
78
+ * If using path location strategy, pathUrl will include the relative portion of the base path (e.g. /base/page).
79
+ * Since baseUrl also includes /base, can just concatentate baseUrl + path
80
+ */
81
+ return `${baseUrl}${path}`;
82
+ }
83
+
84
+ /**
85
+ * Interactively prompt the user to login
86
+ * @param url Path of the requested page
87
+ */
88
+ private loginInteractively(state: RouterStateSnapshot): Observable<boolean> {
89
+ const authRequest =
90
+ typeof this.msalGuardConfig.authRequest === "function"
91
+ ? this.msalGuardConfig.authRequest(this.authService, state)
92
+ : { ...this.msalGuardConfig.authRequest };
93
+ if (this.msalGuardConfig.interactionType === InteractionType.Popup) {
94
+ this.authService.getLogger().verbose("Guard - logging in by popup");
95
+ return this.authService.loginPopup(authRequest as PopupRequest).pipe(
96
+ map((response: AuthenticationResult) => {
97
+ this.authService
98
+ .getLogger()
99
+ .verbose(
100
+ "Guard - login by popup successful, can activate, setting active account"
101
+ );
102
+ this.authService.instance.setActiveAccount(response.account);
103
+ return true;
104
+ })
105
+ );
106
+ }
107
+
108
+ this.authService.getLogger().verbose("Guard - logging in by redirect");
109
+ const redirectStartPage = this.getDestinationUrl(state.url);
110
+ return this.authService
111
+ .loginRedirect({
112
+ redirectStartPage,
113
+ ...authRequest,
114
+ } as RedirectRequest)
115
+ .pipe(map(() => false));
116
+ }
117
+
118
+ /**
119
+ * Helper which checks for the correct interaction type, prevents page with Guard to be set as redirect, and calls handleRedirectObservable
120
+ * @param state
121
+ */
122
+ private activateHelper(
123
+ state?: RouterStateSnapshot
124
+ ): Observable<boolean | UrlTree> {
125
+ if (
126
+ this.msalGuardConfig.interactionType !== InteractionType.Popup &&
127
+ this.msalGuardConfig.interactionType !== InteractionType.Redirect
128
+ ) {
129
+ throw new BrowserConfigurationAuthError(
130
+ "invalid_interaction_type",
131
+ "Invalid interaction type provided to MSAL Guard. InteractionType.Popup or InteractionType.Redirect must be provided in the MsalGuardConfiguration"
132
+ );
133
+ }
134
+ this.authService.getLogger().verbose("MSAL Guard activated");
135
+
136
+ /*
137
+ * If a page with MSAL Guard is set as the redirect for acquireTokenSilent,
138
+ * short-circuit to prevent redirecting or popups.
139
+ */
140
+ if (typeof window !== "undefined") {
141
+ if (
142
+ UrlString.hashContainsKnownProperties(window.location.hash) &&
143
+ BrowserUtils.isInIframe() &&
144
+ !this.authService.instance.getConfiguration().system
145
+ .allowRedirectInIframe
146
+ ) {
147
+ this.authService
148
+ .getLogger()
149
+ .warning(
150
+ "Guard - redirectUri set to page with MSAL Guard. It is recommended to not set redirectUri to a page that requires authentication."
151
+ );
152
+ return of(false);
153
+ }
154
+ } else {
155
+ this.authService
156
+ .getLogger()
157
+ .info(
158
+ "Guard - window is undefined, MSAL does not support server-side token acquisition"
159
+ );
160
+ return of(true);
161
+ }
162
+
163
+ /**
164
+ * If a loginFailedRoute is set in the config, set this as the loginFailedRoute
165
+ */
166
+ if (this.msalGuardConfig.loginFailedRoute) {
167
+ this.loginFailedRoute = this.parseUrl(
168
+ this.msalGuardConfig.loginFailedRoute
169
+ );
170
+ }
171
+
172
+ // Capture current path before it gets changed by handleRedirectObservable
173
+ const currentPath = this.location.path(true);
174
+
175
+ return this.authService.initialize().pipe(
176
+ concatMap(() => {
177
+ return this.authService.handleRedirectObservable();
178
+ }),
179
+ concatMap(() => {
180
+ if (!this.authService.instance.getAllAccounts().length) {
181
+ if (state) {
182
+ this.authService
183
+ .getLogger()
184
+ .verbose(
185
+ "Guard - no accounts retrieved, log in required to activate"
186
+ );
187
+ return this.loginInteractively(state);
188
+ }
189
+ this.authService
190
+ .getLogger()
191
+ .verbose("Guard - no accounts retrieved, no state, cannot load");
192
+ return of(false);
193
+ }
194
+
195
+ this.authService
196
+ .getLogger()
197
+ .verbose("Guard - at least 1 account exists, can activate or load");
198
+
199
+ // Prevent navigating the app to /#code= or /code=
200
+ if (state) {
201
+ /*
202
+ * Path routing:
203
+ * state.url: /#code=...
204
+ * state.root.fragment: code=...
205
+ */
206
+
207
+ /*
208
+ * Hash routing:
209
+ * state.url: /code
210
+ * state.root.fragment: null
211
+ */
212
+ const urlContainsCode: boolean = this.includesCode(state.url);
213
+ const fragmentContainsCode: boolean =
214
+ !!state.root &&
215
+ !!state.root.fragment &&
216
+ this.includesCode(`#${state.root.fragment}`);
217
+ const hashRouting: boolean =
218
+ this.location.prepareExternalUrl(state.url).indexOf("#") === 0;
219
+
220
+ // Ensure code parameter is in fragment (and not in query parameter), or that hash hash routing is used
221
+ if (urlContainsCode && (fragmentContainsCode || hashRouting)) {
222
+ this.authService
223
+ .getLogger()
224
+ .info(
225
+ "Guard - Hash contains known code response, stopping navigation."
226
+ );
227
+
228
+ // Path routing (navigate to current path without hash)
229
+ if (currentPath.indexOf("#") > -1) {
230
+ return of(this.parseUrl(this.location.path()));
231
+ }
232
+
233
+ // Hash routing (navigate to root path)
234
+ return of(this.parseUrl(""));
235
+ }
236
+ }
237
+
238
+ return of(true);
239
+ }),
240
+ catchError((error: Error) => {
241
+ this.authService
242
+ .getLogger()
243
+ .error("Guard - error while logging in, unable to activate");
244
+ this.authService
245
+ .getLogger()
246
+ .errorPii(`Guard - error: ${error.message}`);
247
+ /**
248
+ * If a loginFailedRoute is set, checks to see if state is passed before returning route
249
+ */
250
+ if (this.loginFailedRoute && state) {
251
+ this.authService
252
+ .getLogger()
253
+ .verbose("Guard - loginFailedRoute set, redirecting");
254
+ return of(this.loginFailedRoute);
255
+ }
256
+ return of(false);
257
+ })
258
+ );
259
+ }
260
+
261
+ includesCode(path: string): boolean {
262
+ return (
263
+ (path.lastIndexOf("/code") > -1 &&
264
+ path.lastIndexOf("/code") === path.length - "/code".length) || // path.endsWith("/code")
265
+ path.indexOf("#code=") > -1 ||
266
+ path.indexOf("&code=") > -1
267
+ );
268
+ }
269
+
270
+ canActivate(
271
+ route: ActivatedRouteSnapshot,
272
+ state: RouterStateSnapshot
273
+ ): Observable<boolean | UrlTree> {
274
+ this.authService.getLogger().verbose("Guard - canActivate");
275
+ return this.activateHelper(state);
276
+ }
277
+
278
+ canActivateChild(
279
+ route: ActivatedRouteSnapshot,
280
+ state: RouterStateSnapshot
281
+ ): Observable<boolean | UrlTree> {
282
+ this.authService.getLogger().verbose("Guard - canActivateChild");
283
+ return this.activateHelper(state);
284
+ }
285
+
286
+ canMatch(): Observable<boolean | UrlTree> {
287
+ this.authService.getLogger().verbose("Guard - canLoad");
288
+ return this.activateHelper();
289
+ }
290
+ }
@@ -0,0 +1,39 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ // eslint-disable-next-line import/no-unresolved
7
+ import { HttpRequest } from "@angular/common/http";
8
+ import {
9
+ PopupRequest,
10
+ RedirectRequest,
11
+ InteractionType,
12
+ SilentRequest,
13
+ } from "@azure/msal-browser";
14
+ import { MsalService } from "./msal.service";
15
+
16
+ export declare type MsalInterceptorAuthRequest =
17
+ | Omit<PopupRequest, "scopes">
18
+ | Omit<RedirectRequest, "scopes">
19
+ | Omit<SilentRequest, "scopes">;
20
+
21
+ export type MsalInterceptorConfiguration = {
22
+ interactionType: InteractionType.Popup | InteractionType.Redirect;
23
+ protectedResourceMap: Map<
24
+ string,
25
+ Array<string | ProtectedResourceScopes> | null
26
+ >;
27
+ authRequest?:
28
+ | MsalInterceptorAuthRequest
29
+ | ((
30
+ msalService: MsalService,
31
+ req: HttpRequest<unknown>,
32
+ originalAuthRequest: MsalInterceptorAuthRequest
33
+ ) => MsalInterceptorAuthRequest);
34
+ };
35
+
36
+ export type ProtectedResourceScopes = {
37
+ httpMethod: string;
38
+ scopes: Array<string> | null;
39
+ };