Support for OAuth 2 and OpenId Connect (OIDC) in Angular.
Installations
npm install angular-oauth2-oidc
Developer
manfredsteyer
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
No
Node Version
20.11.1
NPM Version
10.2.4
Statistics
1,901 Stars
627 Commits
688 Forks
54 Watching
23 Branches
109 Contributors
Updated on 19 Nov 2024
Bundle Size
54.15 kB
Minified
13.95 kB
Minified + Gzipped
Languages
TypeScript (82.88%)
JavaScript (10.33%)
HTML (6.46%)
CSS (0.29%)
Batchfile (0.03%)
Total Downloads
Cumulative downloads
Total Downloads
31,635,730
Last day
1.5%
37,987
Compared to previous day
Last week
6.1%
188,119
Compared to previous week
Last month
7.8%
762,428
Compared to previous month
Last year
14.2%
8,563,720
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
2
angular-oauth2-oidc
Support for OAuth 2 and OpenId Connect (OIDC) in Angular. Already prepared for the upcoming OAuth 2.1.
Credits
- jsrsasign for validating token signature and for hashing
- Identity Server for testing with an .NET/.NET Core Backend
- Keycloak (Redhat) for testing with Java
- Auth0
Resources
- Sources and Sample: https://github.com/manfredsteyer/angular-oauth2-oidc
- Source Code Documentation: https://manfredsteyer.github.io/angular-oauth2-oidc/docs
- Community-provided sample implementation: https://github.com/jeroenheijmans/sample-angular-oauth2-oidc-with-auth-guards/
Tested Environment
Successfully tested with Angular 4.3 to Angular 16 and its Router, PathLocationStrategy as well as HashLocationStrategy and CommonJS-Bundling via webpack.
At server side we've used IdentityServer (.NET / .NET Core), Redhat's Keycloak (Java), and Auth0 (Auth0 is officially supported since version 10 of this lib). For Auth0, please have a look into the respective documentation page here.
For using this library with Azure Active Directory (Azure AD), we recommend an additional look to this blog post and the example linked at the end of this blog post.
Also, the Okta community created some guidelines on how to use this lib with Okta. See the links at the end of this page for more information.
Angular 17: Use 17.x versions of this library (should also work with older Angular versions!).
Angular 16: Use 16.x versions of this library (should also work with older Angular versions!).
Angular 15: Use 15.x versions of this library (should also work with older Angular versions!).
Angular 14: Use 14.x versions of this library (should also work with older Angular versions!).
Angular 13: Use 13.x versions of this library (should also work with older Angular versions!).
Angular 12: Use 12.x versions of this library (should also work with older Angular versions!).
Angular 11: Use 10.x versions of this library (should also work with older Angular versions!).
Angular 10: Use 10.x versions of this library (should also work with older Angular versions!).
Angular 9: Use 9.x versions of this library (should also work with older Angular versions!).
Angular 8: Use 8.x versions of this library.
Angular 7: Use 7.x versions of this library.
Angular 6: Use Version 4.x of this library. Version 4.x was tested with Angular 6. You can also try the newer version 5.x of this library which has a much smaller bundle size.
Angular 5.x or 4.3: If you need support for Angular < 6 (4.3 to 5.x) you can download the former version 3.1.4 (npm i angular-oauth2-oidc@^3 --save).
Release Cycle
- We plan one major release for each Angular version
- Will contain new features
- Will contain bug fixes and PRs
- Critical bugfixes on demand
Contributions
-
Feel free to file pull requests
-
The issues contain some ideas for PRs and enhancements (see labels)
-
If you want to contribute to the docs, you can do so in the
docs-src
folder. Make sure you updatesummary.json
as well. Then generate the docs with the following commands:1npm install -g @compodoc/compodoc 2npm run docs
Features
- Logging in via Code Flow + PKCE
- Hence, you are safe for the upcoming OAuth 2.1
- Logging in via Implicit Flow (where a user is redirected to Identity Provider)
- "Logging in" via Password Flow (where a user enters their password into the client)
- Token Refresh for all supported flows
- Automatically refreshing a token when/some time before it expires
- Querying Userinfo Endpoint
- Querying Discovery Document to ease configuration
- Validating claims of the id_token regarding the specs
- Hook for further custom validations
- Single-Sign-Out by redirecting to the auth-server's logout-endpoint
- Tested with all modern browsers and IE
- Token Revocation according to RFC 7009
Sample-Auth-Server
You can use the OIDC-Sample-Server used in our examples. It assumes, that your Web-App runs on http://localhost:4200
Username/Password:
- max/geheim
- bob/bob
- alice/alice
clientIds:
- spa (Code Flow + PKCE)
- implicit (implicit flow)
redirectUris:
- localhost:[4200-4202]
- localhost:[4200-4202]/index.html
- localhost:[4200-4202]/silent-refresh.html
Installing
1npm i angular-oauth2-oidc --save
Option 1: Standalone APIs
If you use Standalone Components introduced with Angular 14, you can use our standalone API (call to provideOAuthClient
) in your main.ts
to setup the OAuthClient
:
1// main.ts -- Angular 15+ version 2import { bootstrapApplication } from '@angular/platform-browser'; 3 4import { provideHttpClient } from '@angular/common/http'; 5 6import { AppComponent } from './app/app.component'; 7import { provideOAuthClient } from 'angular-oauth2-oidc'; 8 9bootstrapApplication(AppComponent, { 10 providers: [ 11 provideHttpClient(), 12 provideOAuthClient() 13 ] 14});
As Angular 14 does have Standalone Components but no Standalone API for its HttpClient
, you need to go with the traditional HttpClientModule
in this version:
1// main.ts -- Angular 14 version 2import { bootstrapApplication } from '@angular/platform-browser'; 3 4import { HttpClientModule } from '@angular/common/http'; 5 6import { AppComponent } from './app/app.component'; 7import { provideOAuthClient } from 'angular-oauth2-oidc'; 8import { importProvidersFrom } from '@angular/core'; 9 10bootstrapApplication(AppComponent, { 11 providers: [ 12 importProvidersFrom(HttpClientModule), 13 provideOAuthClient() 14 ] 15});
The provideOAuthClient
function takes the same parameters as the forRoot function of the OAuthModule that is still in place for the sake of compatibility with existing code bases.
Option 2: Using NgModules
1import { HttpClientModule } from '@angular/common/http'; 2import { OAuthModule } from 'angular-oauth2-oidc'; 3// etc. 4 5@NgModule({ 6 imports: [ 7 // etc. 8 HttpClientModule, 9 OAuthModule.forRoot() 10 ], 11 declarations: [ 12 AppComponent, 13 HomeComponent, 14 // etc. 15 ], 16 bootstrap: [ 17 AppComponent 18 ] 19}) 20export class AppModule { 21}
Logging in
Since Version 8, this library supports code flow and PKCE to align with the current draft of the OAuth 2.0 Security Best Current Practice document. This is also the foundation of the upcoming OAuth 2.1.
To configure your solution for code flow + PKCE you have to set the responseType
to code
:
1 import { AuthConfig } from 'angular-oauth2-oidc'; 2 3 export const authCodeFlowConfig: AuthConfig = { 4 // Url of the Identity Provider 5 issuer: 'https://idsvr4.azurewebsites.net', 6 7 // URL of the SPA to redirect the user to after login 8 redirectUri: window.location.origin + '/index.html', 9 10 // The SPA's id. The SPA is registerd with this id at the auth-server 11 // clientId: 'server.code', 12 clientId: 'spa', 13 14 // Just needed if your auth server demands a secret. In general, this 15 // is a sign that the auth server is not configured with SPAs in mind 16 // and it might not enforce further best practices vital for security 17 // such applications. 18 // dummyClientSecret: 'secret', 19 20 responseType: 'code', 21 22 // set the scope for the permissions the client should request 23 // The first four are defined by OIDC. 24 // Important: Request offline_access to get a refresh token 25 // The api scope is a usecase specific one 26 scope: 'openid profile email offline_access api', 27 28 showDebugInformation: true, 29 };
After this, you can initialize the code flow using:
1this.oauthService.initCodeFlow();
There is also a convenience method initLoginFlow
which initializes either the code flow or the implicit flow depending on your configuration.
1this.oauthService.initLoginFlow();
Also -- as shown in the readme -- you have to execute the following code when bootstrapping to make the library to fetch the token:
1this.oauthService.configure(authCodeFlowConfig); 2this.oauthService.loadDiscoveryDocumentAndTryLogin();
Logging out
The logOut method clears the used token store (by default sessionStorage
) and forwards the user to the auth servers logout endpoint if one was configured (manually or via the discovery document).
1this.oauthService.logOut();
If you want to revoke the existing access token and the existing refresh token before logging out, use the following method:
1this.oauthService.revokeTokenAndLogout();
Skipping the Login Form
If you don't want to display a login form that tells the user that they are redirected to the identity server, you can use the convenience function this.oauthService.loadDiscoveryDocumentAndLogin();
instead of this.oauthService.loadDiscoveryDocumentAndTryLogin();
when setting up the library.
This directly redirects the user to the identity server if there are no valid tokens. Ensure you have your issuer
set to your discovery document endpoint!
Calling a Web API with an Access Token
You can automate this task by switching sendAccessToken
on and by setting allowedUrls
to an array with prefixes for the respective URLs. Use lower case for the prefixes.
1OAuthModule.forRoot({
2 resourceServer: {
3 allowedUrls: ['http://www.angular.at/api'],
4 sendAccessToken: true
5 }
6})
If you need more versatility, you can look in the documentation how to setup a custom interceptor.
Token Refresh
Routing
If you use the PathLocationStrategy
(which is on by default) and have a general catch-all-route (path: '**'
) you should be fine. Otherwise look up the section Routing with the HashStrategy
in the documentation.
Implicit Flow
Nowadays, using code flow + PKCE -- as shown above -- is the recommended OAuth 2/OIDC flow for SPAs. To use the older implicit flow, lookup this docs: https://manfredsteyer.github.io/angular-oauth2-oidc/docs/additional-documentation/using-implicit-flow.html
More Documentation (!)
See the documentation for more information about this library.
Breaking Change in Version 9
With regards to tree shaking, beginning with version 9, the JwksValidationHandler
has been moved to a library of its own. If you need it for implementing implicit flow, please install it using npm:
npm i angular-oauth2-oidc-jwks --save
After that, you can import it into your application by using this:
1import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
instead of that:
1import { JwksValidationHandler } from 'angular-oauth2-oidc';
Please note, that this dependency is not needed for the code flow, which is nowadays the recommended flow for single page applications. This also results in smaller bundle sizes.
Breaking change in 9.1.0
The use of encodeURIComponent
on the argument passed to initImplicitFlow
and its Code Flow counterparts was mandatory before this version.
Since that was considered a bug, the need to do so was removed. Now the reverse is true if you're upgrading from before 9.0.0: you need to remove any call to encode URI components in your own application, as the library will now do it for you.
Tutorials
- Tutorial with Demo Servers available online
- Angular Authentication with OpenID Connect and Okta in 20 Minutes
- Add Authentication to Your Angular PWA
- Build an Ionic App with User Authentication
- On-Site Workshops
- Angular 6 with Auth0 using this library
Thanks to all Contributors
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 8/19 approved changesets -- score normalized to 4
Reason
0 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Reason
40 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-c75v-2vq8-878f
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-8hc4-vh64-cxmj
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-9mvj-f7w8-pvh2
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-xwcq-pm8m-c4vf
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-rh63-9qcf-83gf
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-wgrm-67xf-hhpq
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-3787-6prv-h9w3
- Warn: Project is vulnerable to: GHSA-9qxr-qj54-h672
- Warn: Project is vulnerable to: GHSA-m4v8-wqvr-p9f7
- Warn: Project is vulnerable to: GHSA-c24v-8rfc-w8vw
- Warn: Project is vulnerable to: GHSA-8jhw-289h-jh2g
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986 / GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to angular-oauth2-oidc
angular-oauth2-oidc-codeflow
[![Build Status](https://travis-ci.org/bechhansen/angular-oauth2-oidc.svg?branch=master)](https://travis-ci.org/bechhansen/angular-oauth2-oidc)
@sbb-esta/angular-keycloak
🛈 We recommend [angular-oauth2-oidc](https://www.npmjs.com/package/angular-oauth2-oidc) to be used for authentication. See the [documentation](https://manfredsteyer.github.io/angular-oauth2-oidc/docs/index.html) for details.
angular-oauth2-oidc-jwks
Support for OAuth 2 and OpenId Connect (OIDC) in Angular. Already prepared for the upcoming OAuth 2.1.
sofico-angular-oauth2-oidc
Forked repository of angular-oauth2-oidc