Coder Social home page Coder Social logo

alx-andru / ng-oidc-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from source-inc/ng-oidc-client

0.0 2.0 1.0 3.89 MB

A package for managing OpenID-Connect authentication in Angular apps, wrapping oidc-client-js

License: MIT License

JavaScript 4.40% TypeScript 83.98% CSS 1.81% HTML 9.82%

ng-oidc-client's Introduction

NG OIDC Client

npm npm Maintenance

An Angular 6+ package wrapping oidc-client-js to manage authentication with OIDC and OAuth2 in a reactive way using NgRx.

OIDC State in Redux DevTools

Getting started πŸš€

The configuration for the examples are based on running IdentityServer4 on localhost. A ready-to-go reference implementation for testing purposes can be found at ng-oidc-client-server.

Install the package

npm install -s ng-oidc-client

if not already the case, install the necessary peer dependencies

npm install -s @ngrx/store 
npm install -s @ngrx/effects 
npm install -s oidc-client

Add the NgOidcClientModule to your AppModule

export interface State {
  router: RouterReducerState;
}
export const rootStore: ActionReducerMap<State> = {
  router: routerReducer
};

@NgModule({
  declarations: [AppComponent, ProtectedComponent, HomeComponent, LoginComponent],
  imports: [
    BrowserModule,
    StoreModule.forRoot(rootStore),
    EffectsModule.forRoot([]),
+    NgOidcClientModule.forRoot({
+     oidc_config: {
+       authority: 'https://localhost:5001',
+       client_id: 'ng-oidc-client-identity',
+       redirect_uri: 'http://localhost:4200/callback.html',
+       response_type: 'id_token token',
+       scope: 'openid profile offline_access api1',
+       post_logout_redirect_uri: 'http://localhost:4200/signout-callback.html',
+       silent_redirect_uri: 'http://localhost:4200/renew-callback.html',
+       accessTokenExpiringNotificationTime: 10,
+       automaticSilentRenew: true,
+       userStore: new WebStorageStateStore({ store: window.localStorage })
+     },
+     log: {
+       logger: console,
+       level: Log.NONE
+     }
+   })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

For a complete list of configuration options please see the official oidc-client-js documentation.

Coming soon: Example configuration Using ng-oidc-client with Auth0 and Okta

Inject the OidcFacade in your Component

...
export class HomeComponent {
  constructor(private oidcFacade: OidcFacade) {}

  loginPopup() {
    this.oidcFacade.signinPopup();
  }

  logoutPopup() {
    this.oidcFacade.signoutPopup();
  }
}

Alternatively you can you also use .signinRedirect(); or .signoutRedirect();.

Create a new directory called static below the src folder, to serve the static callback HTML sites.

src/static
β”œβ”€β”€ callback.html
β”œβ”€β”€ renew-callback.html
└── signout-callback.html

Example for a callback.html

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Callback</title>
  <link rel="icon"
        type="image/x-icon"
        href="favicon.png">
  <script src="oidc-client.min.js"
          type="application/javascript"></script>
</head>

<body>
  <script>
    var Oidc = window.Oidc;

    var config = {
      userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
    }

    if ((Oidc && Oidc.Log && Oidc.Log.logger)) {
      Oidc.Log.logger = console;
    }
    var isPopupCallback = JSON.parse(window.localStorage.getItem('ngoidc:isPopupCallback'));

    if (isPopupCallback) {
      new Oidc.UserManager(config).signinPopupCallback();
    } else {
      new Oidc.UserManager(config).signinRedirectCallback().then(t => {
        window.location.href = '/';
      });
    }
  </script>
</body>

</html>

Example for a renew-callback.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>Renew Callback</title>
    <link rel="icon"
          type="image/x-icon"
          href="favicon.png">
</head>

<body>
    <script src="oidc-client.min.js"></script>
    <script>
        var config = {
            userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
        }
        new Oidc.UserManager(config).signinSilentCallback().catch(function (e) {
            console.error(e);
        });
    </script>
</body>

</html>

Example for a signout-callback.html

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Signout Callback</title>
  <link rel="icon"
        type="image/x-icon"
        href="favicon.png">
  <script src="oidc-client.min.js"
          type="application/javascript"></script>
</head>

<body>
  <script>
    var Oidc = window.Oidc;

    var config = {
      userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
    }

    if ((Oidc && Oidc.Log && Oidc.Log.logger)) {
      Oidc.Log.logger = console;
    }

    var isPopupCallback = JSON.parse(window.localStorage.getItem('ngoidc:isPopupCallback'));

    if (isPopupCallback) {
      new Oidc.UserManager(config).signoutPopupCallback();
    } else {
      new Oidc.UserManager(config).signoutRedirectCallback().then(test => {
        window.location.href = '/';
      });
    }
  </script>
</body>

</html>

Modify your angular.json to include the static assets and oidc-client

...
"assets": [
    "src/favicon.ico",
    "src/assets",
+   {
+     "glob": "**/*",
+     "input": "src/static",
+     "output": "/"
+   },
+   {
+     "glob": "oidc-client.min.js",
+     "input": "node_modules/oidc-client/dist",
+     "output": "/"
+   }
  ],
...

You will be able to authenticate against your configurated identity provider and the obtained user will be accessible in through the created state.

Protecting Routes using an AuthGuard πŸ’‚

Create a new Service and implement the AuthGuard interface

export class OidcGuardService implements CanActivate {
  constructor(private router: Router, private oidcFacade: OidcFacade) {}

  public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
    return this.oidcFacade.identity$.pipe(
      take(1),
      switchMap(user => {
        console.log('Auth Guard - Checking if user exists', user);
        console.log('Auth Guard - Checking if user is expired:', user && user.expired);
        if (user && !user.expired) {
          return of(true);
        } else {
          this.router.navigate(['/login']);
          return of(false);
        }
      })
    );
  }
}

The guard needs to inject the OidcFacade to check if the user exists and is not expired. It is up to the application flow if the unauthenticated user is redirected to a login route or not.

Add the Guard to the list of providers in your AppModule

...
providers: [
+   OidcGuardService,
  ],
...

Using HTTP Interceptor to add the Bearer token to API calls 🐻

In case an API requires a valid access token to interact with it, an HTTP Interceptor can be used to add the required Bearer token to each call.

Create a new Service and implement the HttpInterceptor interface

export class OidcInterceptorService implements HttpInterceptor {
  static OidcInterceptorService: any;
  constructor(private oidcFacade: OidcFacade) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return this.oidcFacade.identity$.pipe(
      switchMap(user => {
        if (user && !user.expired && user.access_token) {
          req = req.clone({
            setHeaders: {
              Authorization: `Bearer ${user.access_token}`
            }
          });
        }
        return next.handle(req);
      })
    );
  }
}

The interceptor needs to inject the OidcFacade to check if the user exists, is not expired and has an access token. The outgoing request will be cloned and an Authorization Header will be added to the request. Additionally, the requests should be filtered to only add the Bearer token where it is needed.

Add the Interceptor to the list of providers in your AppModule

...
providers: [
+   {
+     provide: HTTP_INTERCEPTORS,
+     useClass: OidcInterceptorService,
+     multi: true
+   }
  ],
...

Docs

You'll find a more detailed Documentation in our Wiki

Articles

Examples

Coming soon

Licence

This software is licensed under the MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.