7.0.0 • Published 5 years ago

esta-webjs-extensions v7.0.0

Weekly downloads
18
License
Apache-2.0
Repository
github
Last release
5 years ago

Deprecation Notice :warning:

This repository is being deprecated.
The Authentication code has been moved to https://github.com/SchweizerischeBundesbahnen/sbb-angular/tree/master/projects/sbb-esta/angular-keycloak
It is now part of the sbb-angular monorepo.
The new npm package can be found at @sbb-esta/angular-keycloak.
At a later date the old npm package will be deprecated and this repository removed.

Esta Web JS Exstentions

This project contains all extensions for esta-webjs-2. Currently we offer the following extensions:

  • Authentication module

Getting started

To use esta-webjs-extensions you need to have node and npm installed. You can then install esta-webjs-extensions with the following command:

npm install --save esta-webjs-extensions

Authentication Module

The authentication module provides functionality for SSO with Keycloak. It provides an authentication service that you can use to handle all your authentication tasks.

It also provides an optional interceptor (Angular Interceptor). It is not contained in the authentication module but can be used by adding AUTH_INTERCEPTOR to the AppModule.

How to use the authentication module

After the redirect from the authentication server, keycloak needs to be notified even before Angular has started. This is achieved by using Angular App Initializers.

You can import the AuthModule in your app module or in your core module. Use the forRoot method to provide the configuration.

The keycloak configuration has to match the following interface:

interface KeycloakConfig {
  url?: string;
  realm?: string;
  clientId?: string;
}
 ...
import { AuthModule, AUTH_INTERCEPTOR } from 'esta-webjs-extensions';

import { environment } from '../environment/environment'; // Your Angular CLI Environment config

@NgModule({
  imports: [
    AuthModule.forRoot(environment.authConfig)
  ],
  declarations: [...],
  providers: [
    AUTH_INTERCEPTOR // Optional
  ]
})
export class CoreModule {
}

By importing the AuthModule the AuthService is now available over dependency injection inside your application.

import { AuthService } from 'esta-webjs-extensions';

@Component({
    selector: ...,
    templateUrl: ...
})
export class SampleComponent{

    constructor(private authService: AuthService) {
    }
}

Authentication Module

The Authentication Module provides the authentication service. It exposes the static forRoot method to configure authentication.

AuthModule.forRoot(
  config: string | KeycloakConfig,
  options?: KeycloakInitOptions,
  loginOptions?: KeycloakLoginOptions)
ParameterDescription
configRequired Either a configuration object or an url where the configuration is provided in json format
optionsOptional Options object (default is { onLoad: 'check-sso', flow: 'implicit' })
loginOptionsOptional Login options object, which will be used on AuthService.login (default is { idpHint: 'adfs_sbb_prod' })

Authentication Service

The Authentication Service provides the necessary API to interact with the authentication module.

MethodDescription
login: PromiseWhen you call this method you are redirected to the authentication server where you need to enter your credentials. After a successful login you are then redirect to your app. The AuthModule then internally stores the authorization token. This token is stored persistent. It is also available after a window refresh. You can get the token by calling the getToken() method of the auth service.
getToken: stringThis method returns the stored token. Notice that it only returns the token and not the complete authHeader. To get the authHeader you can use the getAuthHeader() method on the authService.
getAuthHeader: anyThis method returns an auth header object. This auth header object has an authorization property that contains Bearer + token as value.
refreshToken: PromiseThis method allows you to refresh the token. It returns a promise that indicates if the refresh has been successful or not. Don't forget to call getToken() again to get the refreshed token.
getUserInfo: ObservableThis method returns you an Observable who streams the user profile. This user profile has the following structure. - id?: string - username?: string - email?: string - firstName?: string - lastName?: string - enabled?: boolean - emailVerified?: boolean - totp?: boolean - createdTimestamp?: number
authenticated: booleanReturns a boolean that indicates if the user is authenticated or not.
logout: Promiselogout: voidThis method will logout the current user and remove the token from the auth module.
7.0.0

5 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.0.0

7 years ago