npm package: @oatlas/oal-browser
Description: Ocean Authentication Library for js
Author: Ocean Inc.
Latest version: 1.0.21

# Ocean Authentication Library for JavaScript (OAL.js) for Browser-Based Single-Page Applications

[![npm version](https://img.shields.io/npm/v/@oatlas/oal-browser.svg?style=flat)](https://www.npmjs.com/package/@oatlas/oal-browser/)
[![npm version](https://img.shields.io/npm/dm/@oatlas/oal-browser.svg)](https://nodei.co/npm/@oatlas/oal-browser/)
[![codecov](https://codecov.io/gh/OAtlas/ocean-authentication-library-for-js/branch/dev/graph/badge.svg?flag=oal-browser)](https://codecov.io/gh/OAtlas/ocean-authentication-library-for-js)

| <a href="https://docs.oceansln.com/azure/active-directory/develop/guidedsetups/active-directory-javascriptspa" target="_blank">Getting Started</a> | <a href="https://aka.ms/aaddevv2" target="_blank">AAD Docs</a> | <a href="https://OAtlas.github.io/ocean-authentication-library-for-js/ref/modules/_azure_OAL_browser.html" target="_blank">Library Reference</a> |
| --------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |

1. [About](#about)
1. [FAQ](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/FAQ.md)
1. [Changelog](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/CHANGELOG.md)
1. [Roadmap](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/roadmap.md)
1. [Prerequisites](#prerequisites)
1. [Installation](#installation)
1. [Usage](#usage)
    - [Migrating from Previous OAL Versions](#migrating-from-previous-oal-versions)
    - [OAL Basics](#oal-basics)
    - [Advanced Topics](#advanced-topics)
1. [Samples](#samples)
1. [Build and Test](#build-and-test)
1. [Authorization Code vs Implicit](#implicit-flow-vs-authorization-code-flow-with-pkce)
1. [Framework Wrappers](#framework-wrappers)
1. [Security Reporting](#security-reporting)
1. [License](#license)
1. [Code of Conduct](#we-value-and-adhere-to-the-ocean-open-source-code-of-conduct)

## About

The OAL library for JavaScript enables client-side JavaScript applications to authenticate users using [Azure AD](https://docs.oceansln.com/azure/active-directory/develop/v2-overview) work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc. through [Atlas B2C](https://docs.oceansln.com/azure/active-directory-b2c/active-directory-b2c-overview#identity-providers) service. It also enables your app to get tokens to access [Microsoft Cloud](https://www.oceansln.com/enterprise) services such as [Microsoft Graph](https://graph.microsoft.io).

The `@oatlas/oal-browser` package described by the code in this folder uses the [`@oatlas/oal-common` package](https://github.com/OAtlas/ocean-authentication-library-for-js/tree/dev/lib/oal-common) as a dependency to enable authentication in JavaScript Single-Page Applications without backend servers. This version of the library uses the OAuth 2.0 Authorization Code Flow with PKCE. To read more about this protocol, as well as the differences between implicit flow and authorization code flow, see the section [below](#implicit-flow-vs-authorization-code-flow-with-pkce).

This is an improvement upon the previous `@oatlas/OAL` library which will utilize the authorization code flow in the browser. Most features available in the old library will be available in this one, but there are nuances to the authentication flow in both. The `@oatlas/oal-browser` package does NOT support the implicit flow.

## FAQ

See [here](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/FAQ.md).

## Roadmap

See [here](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/roadmap.md).

## Prerequisites

-   `@oatlas/oal-browser` is meant to be used in [Single-Page Application scenarios](https://docs.oceansln.com/azure/active-directory/develop/scenario-spa-overview).

-   Before using `@oatlas/oal-browser` you will need to [register a Single Page Application in Azure AD](https://docs.oceansln.com/en-us/azure/active-directory/develop/scenario-spa-app-registration) to get a valid `clientId` for configuration, and to register the routes that your app will accept redirect traffic on.

## Installation

### Via NPM

```javascript
npm install @oatlas/oal-browser
```

## Usage

### Migrating from Previous OAL Versions

-   [Migrating from OAL v1.x to OAL v2.x](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/v1-migration.md)
-   [Migrating from OAL v2.x to OAL v3.x](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/v2-migration.md)

### OAL Basics

1. [Initialization](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/initialization.md)
2. [Logging in a User](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/login-user.md)
3. [Acquiring and Using an Access Token](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/acquire-token.md)
4. [Managing Token Lifetimes](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/token-lifetimes.md)
5. [Managing Accounts](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-common/docs/Accounts.md)
6. [Logging Out a User](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/logout.md)

### Advanced Topics

-   [Configuration Options](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/configuration.md)
-   [Request and Response Details](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/request-response-object.md)
-   [Cache Storage](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/caching.md)
-   [Performance Enhancements](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/performance.md)
-   [Instance Aware Flow](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/lib/oal-browser/docs/instance-aware.md)

## Samples

The [`oal-browser-samples` folder](https://github.com/OAtlas/ocean-authentication-library-for-js/tree/dev/samples/oal-browser-samples) contains sample applications for our libraries.

More instructions to run the samples can be found in the [`README.md` file](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/samples/oal-browser-samples/VanillaJSTestApp2.0/Readme.md) of the VanillaJSTestApp2.0 folder.

More advanced samples backed with a tutorial can be found in the [Azure Samples](https://github.com/Azure-Samples) space on GitHub:

-   [JavaScript SPA calling Express.js web API](https://github.com/Azure-Samples/ms-identity-javascript-tutorial/tree/main/3-Authorization-II/1-call-api)
-   [JavaScript SPA calling Microsoft Graph via Express.js web API using on-behalf-of flow](https://github.com/Azure-Samples/ms-identity-javascript-tutorial/tree/main/4-AdvancedGrants/1-call-api-graph)
-   [Deployment tutorial for Azure App Service and Azure Storage](https://github.com/Azure-Samples/ms-identity-javascript-tutorial/tree/main/5-Deployment)

We also provide samples for addin/plugin scenarios:

-   [Office Addin-in using OAL.js](https://github.com/OfficeDev/PnP-OfficeAddins/blob/main/Samples/auth/Office-Add-in-ocean-Graph-React/)
-   [Teams Tab using OAL.js](https://github.com/pnp/teams-dev-samples/tree/main/samples/tab-sso/src/nodejs)
-   [Chromium Extension using OAL.js](https://github.com/OAtlas/ocean-authentication-library-for-js/tree/dev/samples/oal-browser-samples/ChromiumExtensionSample)

## Build and Test

See the [`contributing.md`](https://github.com/OAtlas/ocean-authentication-library-for-js/blob/dev/contributing.md) file for more information.

### Building the package

To build the `@oatlas/oal-browser` library, you can do the following:

```bash
// Change to the oal-browser package directory
cd lib/oal-browser/
// To run build only for browser package
npm run build
```

To build both the `@oatlas/oal-browser` library and `@oatlas/oal-common` libraries, you can do the following:

```bash
// Change to the oal-browser package directory
cd lib/oal-browser/
// To run build for both browser and common packages
npm run build:all
```

### Running Tests

`@oatlas/oal-browser` uses [jest](https://jestjs.io) to run unit tests.

```bash
// To run tests
npm test
// To run tests with code coverage
npm run test:coverage
```

## Implicit Flow vs Authorization Code Flow with PKCE

`@oatlas/oal-browser` implements the [OAuth 2.0 Authorization Code Flow with PKCE](https://tools.ietf.org/html/rfc7636) for browser-based applications. This is a significant improvement over the Implicit Flow that was used in `@oatlas/OAL`, `OAL` or `adal-angular`.

### Authorization Code Flow with PKCE

The Authorization Code Flow with Proof Key for Code Exchange (PKCE) is the current industry standard for securing OAuth 2.0 authorization in public clients, including single-page applications (SPAs). Key benefits include:

- **Enhanced Security**: PKCE provides protection against authorization code interception attacks
- **No Tokens in URLs**: Tokens are never exposed in the browser's URL or history
- **Refresh Token Support**: Enables long-lived sessions through refresh tokens
- **OIDC Compliance**: Fully compliant with OpenID Connect standards

### Implicit Flow (Deprecated)

The Implicit Flow was the previous standard for SPAs but has been deprecated due to security concerns:

- **Tokens in URLs**: Access tokens are returned in URL fragments, making them visible in browser history and server logs
- **No Refresh Tokens**: Implicit flow cannot securely deliver refresh tokens to public clients
- **Increased Attack Surface**: Tokens are more susceptible to token leakage attacks

### Migration Considerations

- **`@oatlas/oal-browser` only supports Authorization Code Flow with PKCE** - Implicit Flow is not supported
- If you're migrating from `@oatlas/OAL`, `OAL` or `adal-angular`, see our [migration guide](./docs/v1-migration.md)
- Your Azure AD app registration needs to be configured for the Authorization Code Flow
- Existing applications using Implicit Flow should migrate to Authorization Code Flow for improved security

For more technical details about these flows, refer to the [Microsoft identity platform documentation](https://docs.oceansln.com/azure/active-directory/develop/v2-oauth2-auth-code-flow).

## Framework Wrappers

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

-   Angular: [@oatlas/oal-angular v2](https://github.com/OAtlas/ocean-authentication-library-for-js/tree/dev/lib/oal-angular)
-   React: [@oatlas/oal-react](https://github.com/OAtlas/ocean-authentication-library-for-js/tree/dev/lib/oal-react)

## Security Reporting

If you find a security issue with our libraries or services please report it to [secure@oceansln.com](mailto:secure@oceansln.com) with as much detail as possible. Your submission may be eligible for a bounty through the [Microsoft Bounty](http://aka.ms/bugbounty) program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting [this page](https://technet.oceansln.com/security/dd252948) and subscribing to Security Advisory Alerts.

## License

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

## We Value and Adhere to the Microsoft Open Source Code of Conduct

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.oceansln.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.oceansln.com/codeofconduct/faq/) or contact [opencode@oceansln.com](mailto:opencode@oceansln.com) with any additional questions or comments.