# Introduction

[![NPM](https://img.shields.io/npm/v/cbp-js.svg)](https://www.npmjs.com/package/cbp-js) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

cbp-js is a Javascript library for cbp. It provides support of OIDC and Oauth 2.0 protocol and other useful functions.

The ```AuthenticationClient``` class provides a higher level api for signing in, signing out, monitoring session, event for access token expiring and access token expired.

# Install

```
npm install i cbp-js
```

# Getting Started

## AuthenticationClient

## Configuration

The ``AuthenticationClient`` constructor requires a settings object. These settings are these:

### Required Settings

*   <b>authorization_server</b> - The URL of the authorization server.
*   <b>authentication_endpoint</b> - The URL for authentication.
*   <b>token_endpoint</b> - The URL for getting token.
*   <b>client_id</b> - The client identifier of the application.
*   <b>redirect_uri</b> - The redirect url of the application. This URL must be registered when registering the application.
*   <b>storage</b> - The storage. use WebStorage.
*   <b>response_type</b> - The response type. Currently supported response_type ``code``.

### Optional Settings

*   <b>check_session_iframe</b> - The URL of authorization server for checking session.
*   <b>userinfo_endpoint</b> - The URL of userinfo for getting the user information.
*   <b>end_session_endpoint</b> - The URL of authorization for ending the session.
*   <b>post_logout_redirect_uri</b> - The redirect url of the application. This URL must be registered when registering the application.
*   <b>monitor_session</b> - Enable session monitoring.
*   <b>silent_renew</b> - Set to true to enable silent renew
*   <b>silent_redirect_uri</b> Set the silent redirect uri
*   <b>state</b> - Add state parameter.
*   <b>nonce</b> - Add nonce parameter.
*   <b>scope</b> - Add a scope. Default ```openid```.
*   <b>prompt</b> - Add prompt parameter. Default ```login```. Available options ```login``` and ```consent```.

### Properties

*   <b>options</b> Returns the options provided.
*   <b>event</b> Returns various events raised by AuthenticationClient.
*   <b>oidc</b> Returns various methods for authorization/authentication.

### Oidc

These are the available methods under ``oidc``:

*   <b>signInCallback</b> - This method redirects the user to authorization server authentication endpoint.
*   <b>signInRedirectCallback</b> - This method handle the redirection if successful exchange the code for token and store it.
*   <b>logoutCallback</b> - This method redirects the user to the end session endpoint.
*   <b>getUser</b> - This method get the user information.
*   <b>silentRenew</b> - This method create a hidden iframe for renewing of token.
*   <b>signinSilentCallback</b> This method handle the new token.

### Event

These are the available methods under ``event``:

*   <b>signOutEvent</b> - Raised when the user logout from the OP.
*   <b>accessTokenExpiringEvent</b> - Raised when access token is expiring.
*   <b>accessTokenExpiredEvent</b> - Raised when access token is expired.

### Other classes

*   <b>Util</b> - Helper class.
*   <b>TokenManager</b> - Class for token management.
*   <b>WebStorage</b> - Specify storage.
 
### Usage

Example usage:

Initialization

```
import { AuthenticationClient , WebStorage } from 'cbp-js/cbp-lib.es';

export const auth = new AuthenticationClient({
  storage: new WebStorage({
    store: window.localStorage
  }),
  authorization_server: 'https://AUTH_DOMAIN',
  authentication_endpoint: 'https://AUTH_DOMAIN/oauth/v1/authorize',
  token_endpoint: 'https://AUTH_DOMAIN/oauth/v1/token',
  client_id: 'YOUR_CLIENTID',
  response_type: "code",
  redirect_uri: 'https://REDIRECT_URI',
  scope: "openid user:read",
  monitor_session: true,
  check_session_iframe: 'https://AUTH_DOMAINT/oauth/v1/checkSession"
});
```

Calling the method

```
// Signing user
auth.oidc.signInCallback().catch(error => console.log(error));

// Handling callback
auth.oidc.signInRedirectCallback().then(token => {})
    .catch(error => console.log(error));

// Logging out
auth.oidc.logoutCallback().catch(error => console.log(error));


// Checking user signout
auth.oidc.event.signOutEvent.subscribe(session => {})
    .catch(error => console.log(error));

// Checking access token expiring
auth.oidc.event.accessTokenExpiringEvent.subscribe(expiring => {})
    .catch(error => console.log(error));

// Checking access token expired
auth.oidc.event.accessTokenExpiredEvent.subscribe(expired => {})
    .catch(error => console.log(error));
```

# License

MIT 