# idcs-auth-library

> [![npm version](https://img.shields.io/npm/v/idcs-auth-library.svg?style=flat-square)](https://www.npmjs.com/package/idcs-auth-library) An Angular wrapper around IDCS **Identity Provider** which supports single sign-on using OpenID standard

Latest version **0.0.6** (published 2019-05-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install idcs-auth-library
pnpm add idcs-auth-library
yarn add idcs-auth-library
bun add idcs-auth-library
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2019-05-20 |
| First published | 2019-05-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 17 |
| Unpacked size | 251.9 KB |
| Known vulnerabilities | 0 (+37 in 6 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Rajanikanth.karupakula |
| Maintainers | rajanikanth.karupakula |

## Links

- npm: https://www.npmjs.com/package/idcs-auth-library
- Repository: https://github.com/Rajanikanth16/idcs-auth-library
- Homepage: https://github.com/Rajanikanth16/idcs-auth-library#readme
- Issues: https://github.com/Rajanikanth16/idcs-auth-library/issues
- npm.io page: https://npm.io/package/idcs-auth-library

## Dependencies (17)

- [tar](https://npm.io/package/tar.md) ^4.4.8
- [rxjs](https://npm.io/package/rxjs.md) ~6.2.0
- [tslib](https://npm.io/package/tslib.md) ^1.9.0
- [core-js](https://npm.io/package/core-js.md) ^2.5.4
- [zone.js](https://npm.io/package/zone.js.md) ~0.8.26
- [auth0-js](https://npm.io/package/auth0-js.md) ^9.10.2
- [crypto-js](https://npm.io/package/crypto-js.md) ^3.1.9-1
- [@angular/core](https://npm.io/package/@angular/core.md) ^6.1.0
- [@angular/http](https://npm.io/package/@angular/http.md) ^6.1.0
- [@angular/forms](https://npm.io/package/@angular/forms.md) ^6.1.0
- [@angular/common](https://npm.io/package/@angular/common.md) ^6.1.0
- [@angular/router](https://npm.io/package/@angular/router.md) ^6.1.0
- [@angular/compiler](https://npm.io/package/@angular/compiler.md) ^6.1.0
- [@auth0/angular-jwt](https://npm.io/package/@auth0/angular-jwt.md) ^2.1.0
- [@angular/animations](https://npm.io/package/@angular/animations.md) ^6.1.0
- [@angular/platform-browser](https://npm.io/package/@angular/platform-browser.md) ^6.1.0
- [@angular/platform-browser-dynamic](https://npm.io/package/@angular/platform-browser-dynamic.md) ^6.1.0

## Recent versions

- 0.0.6 (latest) — 2019-05-20
- 0.0.3 (beta) — 2019-05-15
- 0.0.5 — 2019-05-20
- 0.0.4 — 2019-05-15
- 0.0.1 — 2019-05-10

## README

# IDCS Auth Library SDK 
[![npm version](https://img.shields.io/npm/v/idcs-auth-library.svg?style=flat-square)](https://www.npmjs.com/package/idcs-auth-library)
An Angular wrapper around IDCS **Identity Provider** which supports single sign-on using OpenID standard

This library currently supports three-legged authentication (usually known as most secure) with PKCE (Proof of key code exchange) is used and implemented by both client and authorization server.

This library is tested against the latest version of Angular (currently 6), and is currently known to be compatible with Angular 4, 5, and 6.

`idcs-auth-library` works directly with [`@angular/router`](https://angular.io/guide/router). 

## Getting Started

-   First You need to register your application with IDCS to obtain client id and client secret which can be further used for authentication.
-   You can register an application by following this [link](https://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/idcs/idcs_authn_api_obe/authn-api.html)

## Installation

This library is available through [npm](https://www.npmjs.com/package/idcs-auth-library). To install it, simply add it to your project:

```bash
# npm
npm install --save idcs-auth-library

```

## Usage

Add [`AuthModule`](#authmodule) to your module's imports.
Then Add [`AuthGuard`](#authguard),[`AuthService`](#authservice),[`ApplicationConfigurationService`] to your module's imports.
Create a configuration object in your local then, import that and provide as [`AUTH_CONFIG`](#auth_config).
Now add [`AuthGuard`](#authguard),[`AuthService`](#authservice),[`ApplicationConfigurationService`] and  [`AUTH_CONFIG`](#auth_config) to the providers.


```typescript
// app.module.ts

import {
  AuthModule,
  AuthGuard, 
  AuthService, 
  ApplicationConfigurationService
} from 'idcs-auth-library';
import { AUTH_CONFIG } from  './auth.constants';

@NgModule({
  imports: [
    ...
    AuthModule.initAuth(AUTH_CONFIG),
    AppRoutingModule
  ],
   providers: [AuthGuard, AuthService,
     {
	    provide:  ApplicationConfigurationService,
		useValue:  AUTH_CONFIG,
     }
  ],
})
export class MyAppModule { }
```

### `AUTH_CONFIG`

```typescript
// auth.constants.ts

export  const  AUTH_CONFIG  = {
basePath:  '',
idcsInstance:  '',
spaOAuthClientId:  '',
oauthTokenScope:  '',
redirectUrl:  '',
};
```
Replace the empty string with your respected values.

An Angular InjectionToken used to configure the AuthService. This value must be provided by your own application. It is initialized by a plain object which can have the following properties:
- `basePath` : this is a prefix for urls used for authcode and access token.
- `idcsInstance` : This is the domain name of the `Identity Provider`.
- `spaOAuthClientId` : ClientId of the app registered in the identityprovider.
- `oauthTokenScope` : is mandatory to get access on all permitted admin APIs.
- `redirectUrl` : URL given while registering application in `Redirect_Url` field.

### `AuthModule`

The top-level Angular module which provides these components and services:

- [`AuthGuard`](#authguard) - A navigation guard using [CanActivate](https://angular.io/api/router/CanActivate) to grant access to a page only after successful authentication.
- [`AuthService`](#authservice) - Highest-level service containing the `idcs-auth-library` public methods.

### `AuthGuard`

Routes are protected by the `AuthGuard`, which verifies there is a valid `accessToken` stored. To ensure the user has been authenticated before accessing your route, add the `canActivate` guard to one of your routes:

```typescript
// app.routing.module.ts

import {
  AuthGuard,
  ...
} from 'idcs-auth-library';

const appRoutes: Routes = [
  {
    path: 'protected',
    component: MyProtectedComponent,
    canActivate: [ AuthGuard ]
  },
  ...
]
```

If a user does not have a valid session, they will be redirected to the IDCS Login Page for authentication. Once authenticated, they will be redirected back to your application's **protected** page.

### `AuthService`

In your components, your can take advantage of all of `idcs-auth-library`'s features by importing the `AuthService`. The example below shows how to  **logout**:

```typescript
// sample.component.ts

import { AuthService } from 'idcs-auth-library';

@Component({
  selector: 'app-component',
  template: `
    <button *ngIf="isAuthenticated" (click)="logout()">Logout</button>

    <router-outlet></router-outlet>
  `,
})
export class MyComponent {
  constructor(public authService: AuthService) {

  logout() {
    this.authService.logout();
  }
}
```

#### `authService.getIdentityProviderDomain()`

Returns domain name of Identity Provider .

#### `authService.getSpaOAuthClientId()`

Returns the ClientId of the app registered in the Identity Provider.

#### `authService.logout()`

Terminates the user's session in IDCS and clears all stored tokens.

---
_Source: https://npm.io/package/idcs-auth-library · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
