Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…tion-library-for-js into msal-react-logger
  • Loading branch information
tnorling committed Dec 17, 2020
2 parents ef655f7 + 02a34c0 commit b7f7b19
Show file tree
Hide file tree
Showing 45 changed files with 17,735 additions and 14,917 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "@azure/msal-angular",
"email": "[email protected]",
"dependentChangeType": "none",
"date": "2020-11-25T16:26:49.257Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "package-lock update",
"packageName": "@azure/msal-angular",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Update docs and samples (#2764)",
"packageName": "@azure/msal-angular",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "@azure/msal-angularjs",
"email": "[email protected]",
"dependentChangeType": "none",
"date": "2020-11-25T16:27:01.359Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "none",
"date": "2020-11-25T16:27:16.921Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Update msal-angular link in msal-browser readme (#2751)",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Add internal in-memory storage to BrowserCacheManager (#2765)",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Adding an active account API to PublicClientApplication (#2728)",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Update event docs in msal-browser (#2764)",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "Adding device code timeout to the device code request(#2656)",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-11-25T16:28:12.416Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Adding account info equality check function (#2728)",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-11-25T16:29:04.442Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "package-lock changes",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "@azure/msal-react",
"email": "[email protected]",
"dependentChangeType": "none",
"date": "2020-11-25T16:29:14.020Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix redirect loop when service returns error (#2762)",
"packageName": "@azure/msal-react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "package-lock changes",
"packageName": "@azure/msal-react",
"email": "[email protected]",
"dependentChangeType": "none"
}
8 changes: 8 additions & 0 deletions change/msal-2020-11-25-19-29-14-device-code-timeout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "package.lock change",
"packageName": "msal",
"email": "[email protected]",
"dependentChangeType": "none",
"date": "2020-11-25T16:28:26.323Z"
}
7 changes: 7 additions & 0 deletions change/msal-305ab0cb-39c5-4629-b574-6150b2370199.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "package-lock changes",
"packageName": "msal",
"email": "[email protected]",
"dependentChangeType": "none"
}
44 changes: 43 additions & 1 deletion lib/msal-angular/docs/v2-docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,50 @@ export class AppComponent implements OnInit, OnDestroy {
```


For the full example of using events, please see the sample [here](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/samples/msal-angular-v2-samples/angular10-sample-app/src/app/app.component.ts#L29).
For the full example of using events, please see our sample [here](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/samples/msal-angular-v2-samples/angular10-sample-app/src/app/app.component.ts#L29).

## Table of events

For more information about the `EventMessage` object, including the full table of events currently emitted by `@azure/msal-browser` (including descriptions and related payloads), please see the documentation [here](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/events.md).

## Handling errors with events

As the `EventError` in `EventMessage` is defined as `AuthError | Error | null`, an error should be validated as the correct type before accessing specific properties on it.

See the example below of how an error can be cast to `AuthError` to avoid TypeScript errors:

```javascript
import { MsalBroadcastService } from '@azure/msal-angular';
import { EventMessage, EventType } from '@azure/msal-browser';

export class AppComponent implements OnInit, OnDestroy {
private readonly _destroying$ = new Subject<void>();

constructor(
//...
private msalBroadcastService: MsalBroadcastService
) {}

ngOnInit(): void {
this.msalBroadcastService.msalSubject$
.pipe(
// Optional filtering of events
filter((msg: EventMessage) => msg.eventType === EventType.LOGIN_FAILURE),
takeUntil(this._destroying$)
)
.subscribe((result: EventMessage) => {
if (result.error instanceof AuthError) {
// Do something with the error
}
});
}

ngOnDestroy(): void {
this._destroying$.next(null);
this._destroying$.complete();
}
}
```

An example of error handling can also be found on our [MSAL Angular v2 B2C Sample App](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/4d79b8ebacd7e4d9acf80fd69d602346dee6bf3c/samples/msal-angular-v2-samples/angular11-b2c-sample/src/app/app.component.ts#L68).

18 changes: 0 additions & 18 deletions lib/msal-angular/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/msal-browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ npm run test:coverage:only

If you are using a framework such as Angular or React you may be interested in using one of our wrapper libraries:

- Angular: [@azure/msal-angular v2 (Alpha)](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/msal-angular-v2/lib/msal-angular)
- Angular: [@azure/msal-angular v2 (Alpha)](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-angular)
- React: [@azure/msal-react (Alpha)](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-react)

## Implicit Flow vs Authorization Code Flow with PKCE
Expand Down
23 changes: 23 additions & 0 deletions lib/msal-browser/docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export type EventMessage = {
};
```

The payload and error in `EventMessage` are defined as follows:
```javascript
export type EventPayload = PopupRequest | RedirectRequest | SilentRequest | SsoSilentRequest | EndSessionRequest | AuthenticationResult | null;

export type EventError = AuthError | Error | null;
```

## How events are emitted in msal-browser
Msal-browser has a protected function `emitEvent`, and emits events in major APIs. For the list of currently emitted events, see the table below.

Expand Down Expand Up @@ -42,6 +49,22 @@ Adding an event callback will return an id. This id can be used to remove the ca
msalInstance.removeEventCallback(callbackId);
```

### Handling errors
Due to the way `EventError` is defined, handling errors emitted with an event may require validating that the error is of the correct type before accessing specific properties on the emitted error. The error can be cast to `AuthError` or checked that it is an instance of `AuthError`.

Here is an example of consuming an emitted event and casting the error:

```javascript
const callbackId = msalInstance.addEventCallback((message: EventMessage) => {
// Update UI or interact with EventMessage here
if (message.eventType === EventType.LOGIN_FAILURE) {
if (message.error instanceof AuthError) {
// Do something with the error
}
}
});
```

## Table of events
These are the events currently emitted by msal-browser.

Expand Down
31 changes: 29 additions & 2 deletions lib/msal-browser/src/app/ClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { CryptoOps } from "../crypto/CryptoOps";
import { Authority, TrustedAuthority, StringUtils, UrlString, ServerAuthorizationCodeResponse, AuthorizationCodeRequest, AuthorizationUrlRequest, AuthorizationCodeClient, PromptValue, ServerError, InteractionRequiredAuthError, AccountInfo, AuthorityFactory, ServerTelemetryManager, SilentFlowClient, ClientConfiguration, BaseAuthRequest, ServerTelemetryRequest, PersistentCacheKeys, IdToken, ProtocolUtils, ResponseMode, Constants, INetworkModule, AuthenticationResult, Logger, ThrottlingUtils, RefreshTokenClient, AuthenticationScheme, SilentFlowRequest, EndSessionRequest as CommonEndSessionRequest } from "@azure/msal-common";
import { Authority, TrustedAuthority, StringUtils, UrlString, ServerAuthorizationCodeResponse, AuthorizationCodeRequest, AuthorizationUrlRequest, AuthorizationCodeClient, PromptValue, ServerError, InteractionRequiredAuthError, AccountInfo, AuthorityFactory, ServerTelemetryManager, SilentFlowClient, ClientConfiguration, BaseAuthRequest, ServerTelemetryRequest, PersistentCacheKeys, IdToken, ProtocolUtils, ResponseMode, Constants, INetworkModule, AuthenticationResult, Logger, ThrottlingUtils, RefreshTokenClient, AuthenticationScheme, SilentFlowRequest, EndSessionRequest as CommonEndSessionRequest, AccountEntity } from "@azure/msal-common";
import { BrowserCacheManager } from "../cache/BrowserCacheManager";
import { buildConfiguration, Configuration } from "../config/Configuration";
import { TemporaryCacheKeys, InteractionType, ApiId, BrowserConstants, BrowserCacheLocation } from "../utils/BrowserConstants";
Expand Down Expand Up @@ -49,6 +49,9 @@ export abstract class ClientApplication {
// Flag to indicate if in browser environment
protected isBrowserEnvironment: boolean;

// Sets the account to use if no account info is given
private activeLocalAccountId: string | null;

// Callback for subscribing to events
private eventCallbacks: Map<string, EventCallbackFunction>;

Expand Down Expand Up @@ -106,6 +109,7 @@ export abstract class ClientApplication {
TrustedAuthority.setTrustedAuthoritiesFromConfig(this.config.auth.knownAuthorities, this.config.auth.cloudDiscoveryMetadata);

this.defaultAuthority = null;
this.activeLocalAccountId = null;
}

// #region Redirect Flow
Expand Down Expand Up @@ -565,6 +569,10 @@ export abstract class ClientApplication {
const logoutUri: string = authClient.getLogoutUri(validLogoutRequest);
this.emitEvent(EventType.LOGOUT_SUCCESS, InteractionType.Redirect, validLogoutRequest);

if (!validLogoutRequest.account || AccountEntity.accountInfoIsEqual(validLogoutRequest.account, this.getActiveAccount())) {
this.setActiveAccount(null);
}

// Check if onRedirectNavigate is implemented, and invoke it if so
if (logoutRequest && typeof logoutRequest.onRedirectNavigate === "function") {
const navigate = logoutRequest.onRedirectNavigate(logoutUri);
Expand Down Expand Up @@ -644,6 +652,25 @@ export abstract class ClientApplication {
}
}

/**
* Sets the account to use as the active account. If no account is passed to the acquireToken APIs, then MSAL will use this active account.
* @param account
*/
setActiveAccount(account: AccountInfo | null): void {
this.activeLocalAccountId = account ? account.localAccountId : null;
}

/**
* Gets the currently active account
*/
getActiveAccount(): AccountInfo | null {
if (!this.activeLocalAccountId) {
return null;
}

return this.getAccountByLocalId(this.activeLocalAccountId);
}

// #endregion

// #region Helpers
Expand Down Expand Up @@ -830,7 +857,6 @@ export abstract class ClientApplication {
* @param request
*/
protected initializeAuthorizationRequest(request: RedirectRequest|PopupRequest|SsoSilentRequest, interactionType: InteractionType): AuthorizationUrlRequest {

const redirectUri = this.getRedirectUri(request.redirectUri);
const browserState: BrowserStateObject = {
interactionType: interactionType
Expand All @@ -856,6 +882,7 @@ export abstract class ClientApplication {
nonce: nonce,
responseMode: ResponseMode.FRAGMENT,
authenticationScheme: authenticationScheme,
account: request.account || this.getActiveAccount()
};

// Check for ADAL SSO
Expand Down
8 changes: 8 additions & 0 deletions lib/msal-browser/src/app/IPublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface IPublicClientApplication {
ssoSilent(request: SsoSilentRequest): Promise<AuthenticationResult>;
getLogger(): Logger;
setLogger(logger: Logger): void;
setActiveAccount(account: AccountInfo | null): void;
getActiveAccount(): AccountInfo | null;
}

export const stubbedPublicClientApplication: IPublicClientApplication = {
Expand Down Expand Up @@ -78,5 +80,11 @@ export const stubbedPublicClientApplication: IPublicClientApplication = {
},
setLogger: () => {
return;
},
setActiveAccount: () => {
return;
},
getActiveAccount: () => {
return null;
}
};
1 change: 1 addition & 0 deletions lib/msal-browser/src/app/PublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class PublicClientApplication extends ClientApplication implements IPubli
const silentRequest: SilentFlowRequest = {
...request,
...this.initializeBaseRequest(request),
account: request.account || this.getActiveAccount(),
forceRefresh: request.forceRefresh || false
};
this.emitEvent(EventType.ACQUIRE_TOKEN_START, InteractionType.Silent, request);
Expand Down
3 changes: 3 additions & 0 deletions lib/msal-browser/src/cache/BrowserCacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class BrowserCacheManager extends CacheManager {
private cacheConfig: CacheOptions;
// Window storage object (either local or sessionStorage)
private browserStorage: IWindowStorage;
// Internal in-memory storage object used for data used by msal that does not need to persist across page loads
private internalStorage: MemoryStorage;
// Client id of application. Used in cache keys to partition cache correctly in the case of multiple instances of MSAL.
private logger: Logger;

Expand All @@ -37,6 +39,7 @@ export class BrowserCacheManager extends CacheManager {
this.logger = logger;

this.browserStorage = this.setupBrowserStorage(cacheConfig.cacheLocation);
this.internalStorage = new MemoryStorage();

// Migrate any cache entries from older versions of MSAL.
this.migrateCacheEntries();
Expand Down
Loading

0 comments on commit b7f7b19

Please sign in to comment.