From 7257209f63099cb0d8fc3743980069ff78e6457e Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 13:40:31 +0100 Subject: [PATCH 01/10] Update OidcSecurityService injection in app.component.ts --- .../src/app/app.component.ts | 8 +++++--- .../src/app/lazy/lazy.component.ts | 6 +++--- .../sample-code-flow-multi-AAD/src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- projects/sample-code-flow-par/src/app/app.component.ts | 4 ++-- .../sample-code-flow-par/src/app/home/home.component.ts | 6 +++--- projects/sample-code-flow-popup/src/app/app.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../sample-code-flow-standalone/src/app/app.component.ts | 4 ++-- .../src/app/forbidden/forbidden.component.ts | 6 +++--- .../src/app/home/home.component.ts | 6 +++--- .../src/app/navigation/navigation.component.ts | 6 +++--- .../sample-implicit-flow-google/src/app/app.component.ts | 9 ++++----- .../src/app/auto-login/auto-login.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/navigation/navigation.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/app.module.ts | 6 ++++-- .../src/app/home/home.component.ts | 6 +++--- 28 files changed, 78 insertions(+), 75 deletions(-) diff --git a/projects/sample-code-flow-lazy-loaded/src/app/app.component.ts b/projects/sample-code-flow-lazy-loaded/src/app/app.component.ts index 5a673e10..2849c69a 100644 --- a/projects/sample-code-flow-lazy-loaded/src/app/app.component.ts +++ b/projects/sample-code-flow-lazy-loaded/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,10 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) -export class AppComponent { +export class AppComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + title = 'sample-code-flow-lazy-loaded'; - constructor(public oidcSecurityService: OidcSecurityService) { + ngOnInit() { this.oidcSecurityService .checkAuth() .subscribe(({ isAuthenticated, accessToken }) => { diff --git a/projects/sample-code-flow-lazy-loaded/src/app/lazy/lazy.component.ts b/projects/sample-code-flow-lazy-loaded/src/app/lazy/lazy.component.ts index fa488f9c..5d72cb99 100644 --- a/projects/sample-code-flow-lazy-loaded/src/app/lazy/lazy.component.ts +++ b/projects/sample-code-flow-lazy-loaded/src/app/lazy/lazy.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,9 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./lazy.component.css'], }) export class LazyComponent implements OnInit { - isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-code-flow-multi-AAD/src/app/app.component.ts b/projects/sample-code-flow-multi-AAD/src/app/app.component.ts index 57612a8e..c8ed1afa 100644 --- a/projects/sample-code-flow-multi-AAD/src/app/app.component.ts +++ b/projects/sample-code-flow-multi-AAD/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-multi-AAD/src/app/home/home.component.ts b/projects/sample-code-flow-multi-AAD/src/app/home/home.component.ts index c979d65f..90f47acb 100644 --- a/projects/sample-code-flow-multi-AAD/src/app/home/home.component.ts +++ b/projects/sample-code-flow-multi-AAD/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + configurations = this.oidcSecurityService.getConfigurations(); userData$ = this.oidcSecurityService.userData$; isAuthenticated$ = this.oidcSecurityService.isAuthenticated$; - constructor(public oidcSecurityService: OidcSecurityService) {} - login(configId: string | undefined): void { this.oidcSecurityService.authorize(configId); } diff --git a/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/app.component.ts b/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/app.component.ts index 57612a8e..c8ed1afa 100644 --- a/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/app.component.ts +++ b/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/home/home.component.ts b/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/home/home.component.ts index 8a50ed33..74c2a5ee 100644 --- a/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/home/home.component.ts +++ b/projects/sample-code-flow-multi-Auth0-ID4-popup/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + configurations = this.oidcSecurityService.getConfigurations(); userData$ = this.oidcSecurityService.userData$; isAuthenticated$ = this.oidcSecurityService.isAuthenticated$; - constructor(public oidcSecurityService: OidcSecurityService) {} - login(configId: string): void { this.oidcSecurityService.authorize(configId); } diff --git a/projects/sample-code-flow-multi-Auth0-ID4/src/app/app.component.ts b/projects/sample-code-flow-multi-Auth0-ID4/src/app/app.component.ts index 57612a8e..c8ed1afa 100644 --- a/projects/sample-code-flow-multi-Auth0-ID4/src/app/app.component.ts +++ b/projects/sample-code-flow-multi-Auth0-ID4/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-multi-Auth0-ID4/src/app/home/home.component.ts b/projects/sample-code-flow-multi-Auth0-ID4/src/app/home/home.component.ts index c065b5d2..812451d8 100644 --- a/projects/sample-code-flow-multi-Auth0-ID4/src/app/home/home.component.ts +++ b/projects/sample-code-flow-multi-Auth0-ID4/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + configurations = this.oidcSecurityService.getConfigurations(); userData$ = this.oidcSecurityService.userData$; isAuthenticated$ = this.oidcSecurityService.isAuthenticated$; - constructor(public oidcSecurityService: OidcSecurityService) {} - login(configId: string | undefined): void { this.oidcSecurityService.authorize(configId); } diff --git a/projects/sample-code-flow-multi-Azure-B2C/src/app/app.component.ts b/projects/sample-code-flow-multi-Azure-B2C/src/app/app.component.ts index 57612a8e..c8ed1afa 100644 --- a/projects/sample-code-flow-multi-Azure-B2C/src/app/app.component.ts +++ b/projects/sample-code-flow-multi-Azure-B2C/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-multi-Azure-B2C/src/app/home/home.component.ts b/projects/sample-code-flow-multi-Azure-B2C/src/app/home/home.component.ts index 8c77cf29..89476dd3 100644 --- a/projects/sample-code-flow-multi-Azure-B2C/src/app/home/home.component.ts +++ b/projects/sample-code-flow-multi-Azure-B2C/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + configurations = this.oidcSecurityService.getConfigurations(); userData$ = this.oidcSecurityService.userData$; isAuthenticated$ = this.oidcSecurityService.isAuthenticated$; - constructor(public oidcSecurityService: OidcSecurityService) {} - login(configId: string | undefined): void { this.oidcSecurityService.authorize(configId); } diff --git a/projects/sample-code-flow-multi-iframe/src/app/app.component.ts b/projects/sample-code-flow-multi-iframe/src/app/app.component.ts index 57612a8e..c8ed1afa 100644 --- a/projects/sample-code-flow-multi-iframe/src/app/app.component.ts +++ b/projects/sample-code-flow-multi-iframe/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-multi-iframe/src/app/home/home.component.ts b/projects/sample-code-flow-multi-iframe/src/app/home/home.component.ts index 04b8e972..01faba24 100644 --- a/projects/sample-code-flow-multi-iframe/src/app/home/home.component.ts +++ b/projects/sample-code-flow-multi-iframe/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + configurations = this.oidcSecurityService.getConfigurations(); userData$ = this.oidcSecurityService.userData$; isAuthenticated$ = this.oidcSecurityService.isAuthenticated$; - constructor(public oidcSecurityService: OidcSecurityService) {} - login(configId: string | undefined): void { this.oidcSecurityService.authorize(configId); } diff --git a/projects/sample-code-flow-par/src/app/app.component.ts b/projects/sample-code-flow-par/src/app/app.component.ts index 842e79a7..687d6c3c 100644 --- a/projects/sample-code-flow-par/src/app/app.component.ts +++ b/projects/sample-code-flow-par/src/app/app.component.ts @@ -1,11 +1,11 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(private oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit() { this.oidcSecurityService diff --git a/projects/sample-code-flow-par/src/app/home/home.component.ts b/projects/sample-code-flow-par/src/app/home/home.component.ts index 3beced07..201a9ba7 100644 --- a/projects/sample-code-flow-par/src/app/home/home.component.ts +++ b/projects/sample-code-flow-par/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,12 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + configuration$ = this.oidcSecurityService.getConfiguration(); userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit() { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-popup/src/app/app.component.ts b/projects/sample-code-flow-popup/src/app/app.component.ts index e1f60be2..1aa41cb0 100644 --- a/projects/sample-code-flow-popup/src/app/app.component.ts +++ b/projects/sample-code-flow-popup/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,14 +7,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./app.component.css'], }) export class AppComponent { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; configuration$ = this.oidcSecurityService.getConfiguration(); isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit() { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-refresh-tokens/src/app/app.component.ts b/projects/sample-code-flow-refresh-tokens/src/app/app.component.ts index 6ce19352..7f0470df 100644 --- a/projects/sample-code-flow-refresh-tokens/src/app/app.component.ts +++ b/projects/sample-code-flow-refresh-tokens/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-refresh-tokens/src/app/home/home.component.ts b/projects/sample-code-flow-refresh-tokens/src/app/home/home.component.ts index 55499d3c..2b13f58d 100644 --- a/projects/sample-code-flow-refresh-tokens/src/app/home/home.component.ts +++ b/projects/sample-code-flow-refresh-tokens/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + configuration$ = this.oidcSecurityService.getConfiguration(); userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-standalone/src/app/app.component.ts b/projects/sample-code-flow-standalone/src/app/app.component.ts index b89f41da..0339e4fe 100644 --- a/projects/sample-code-flow-standalone/src/app/app.component.ts +++ b/projects/sample-code-flow-standalone/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { RouterOutlet } from '@angular/router'; import { OidcSecurityService } from 'angular-auth-oidc-client'; import { NavigationComponent } from './navigation/navigation.component'; @@ -11,7 +11,7 @@ import { NavigationComponent } from './navigation/navigation.component'; imports: [RouterOutlet, NavigationComponent], }) export class AppComponent implements OnInit { - constructor(private readonly oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-standalone/src/app/forbidden/forbidden.component.ts b/projects/sample-code-flow-standalone/src/app/forbidden/forbidden.component.ts index 6433011e..0e6d40df 100644 --- a/projects/sample-code-flow-standalone/src/app/forbidden/forbidden.component.ts +++ b/projects/sample-code-flow-standalone/src/app/forbidden/forbidden.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,9 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; standalone: true, }) export class ForbiddenComponent implements OnInit { - public isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(private readonly oidcSecurityService: OidcSecurityService) {} + public isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-code-flow-standalone/src/app/home/home.component.ts b/projects/sample-code-flow-standalone/src/app/home/home.component.ts index e5543f69..3a048192 100644 --- a/projects/sample-code-flow-standalone/src/app/home/home.component.ts +++ b/projects/sample-code-flow-standalone/src/app/home/home.component.ts @@ -1,5 +1,5 @@ import { AsyncPipe, JsonPipe } from '@angular/common'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -9,12 +9,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; imports: [AsyncPipe, JsonPipe], }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-standalone/src/app/navigation/navigation.component.ts b/projects/sample-code-flow-standalone/src/app/navigation/navigation.component.ts index ce8811b8..f03ee327 100644 --- a/projects/sample-code-flow-standalone/src/app/navigation/navigation.component.ts +++ b/projects/sample-code-flow-standalone/src/app/navigation/navigation.component.ts @@ -1,5 +1,5 @@ import { NgIf } from '@angular/common'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { RouterLink } from '@angular/router'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @@ -11,9 +11,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; imports: [RouterLink, NgIf], }) export class NavigationComponent implements OnInit { - isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-implicit-flow-google/src/app/app.component.ts b/projects/sample-implicit-flow-google/src/app/app.component.ts index f98a1bf6..eddc7c85 100644 --- a/projects/sample-implicit-flow-google/src/app/app.component.ts +++ b/projects/sample-implicit-flow-google/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { Router } from '@angular/router'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @@ -7,10 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor( - public oidcSecurityService: OidcSecurityService, - private readonly router: Router - ) {} + private readonly oidcSecurityService = inject(OidcSecurityService); + + private readonly router = inject(Router); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-implicit-flow-google/src/app/auto-login/auto-login.component.ts b/projects/sample-implicit-flow-google/src/app/auto-login/auto-login.component.ts index 397c4d63..9132f12b 100644 --- a/projects/sample-implicit-flow-google/src/app/auto-login/auto-login.component.ts +++ b/projects/sample-implicit-flow-google/src/app/auto-login/auto-login.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: './auto-login.component.html', }) export class AutoLoginComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-implicit-flow-google/src/app/home/home.component.ts b/projects/sample-implicit-flow-google/src/app/home/home.component.ts index 1dc68879..b8b84ffd 100644 --- a/projects/sample-implicit-flow-google/src/app/home/home.component.ts +++ b/projects/sample-implicit-flow-google/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,12 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-implicit-flow-google/src/app/navigation/navigation.component.ts b/projects/sample-implicit-flow-google/src/app/navigation/navigation.component.ts index 65ceb8f6..fe7f381d 100644 --- a/projects/sample-implicit-flow-google/src/app/navigation/navigation.component.ts +++ b/projects/sample-implicit-flow-google/src/app/navigation/navigation.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,9 +6,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'navigation.component.html', }) export class NavigationComponent implements OnInit { - isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-implicit-flow-silent-renew/src/app/app.component.ts b/projects/sample-implicit-flow-silent-renew/src/app/app.component.ts index 0dc5f04c..b257ab4c 100644 --- a/projects/sample-implicit-flow-silent-renew/src/app/app.component.ts +++ b/projects/sample-implicit-flow-silent-renew/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-implicit-flow-silent-renew/src/app/app.module.ts b/projects/sample-implicit-flow-silent-renew/src/app/app.module.ts index ba63a7ad..e2ecc147 100644 --- a/projects/sample-implicit-flow-silent-renew/src/app/app.module.ts +++ b/projects/sample-implicit-flow-silent-renew/src/app/app.module.ts @@ -1,4 +1,4 @@ -import { NgModule } from '@angular/core'; +import { NgModule, inject } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouterModule } from '@angular/router'; import { EventTypes, PublicEventsService } from 'angular-auth-oidc-client'; @@ -24,7 +24,9 @@ import { UnauthorizedComponent } from './unauthorized/unauthorized.component'; bootstrap: [AppComponent], }) export class AppModule { - constructor(private readonly eventService: PublicEventsService) { + private readonly eventService = inject(PublicEventsService); + + constructor() { this.eventService .registerForEvents() .pipe( diff --git a/projects/sample-implicit-flow-silent-renew/src/app/home/home.component.ts b/projects/sample-implicit-flow-silent-renew/src/app/home/home.component.ts index 915333fb..15179172 100644 --- a/projects/sample-implicit-flow-silent-renew/src/app/home/home.component.ts +++ b/projects/sample-implicit-flow-silent-renew/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,6 +6,8 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + configuration$ = this.oidcSecurityService.getConfiguration(); userData$ = this.oidcSecurityService.userData$; @@ -14,8 +16,6 @@ export class HomeComponent implements OnInit { checkSessionChanged$ = this.oidcSecurityService.checkSessionChanged$; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { From bfd32dde51a14b335b697b934ff7d7dce101d60d Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 13:46:43 +0100 Subject: [PATCH 02/10] Update authentication code --- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/app.component.ts | 6 +++--- .../src/app/forbidden/forbidden.component.html | 3 ++- .../src/app/forbidden/forbidden.component.ts | 18 +++--------------- .../src/app/home/home.component.ts | 6 +++--- .../src/app/navigation/navigation.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/forbidden/forbidden.component.ts | 6 +++--- .../src/app/home/home.component.ts | 6 +++--- .../src/app/navigation/navigation.component.ts | 6 +++--- .../src/app/app.component.ts | 5 +---- .../src/app/home/home.component.ts | 6 +++--- .../src/app/nav-menu/nav-menu.component.ts | 6 +++--- .../src/app/app.component.ts | 4 ++-- .../src/app/home/home.component.ts | 6 +++--- .../src/app/nav-menu/nav-menu.component.ts | 6 +++--- .../src/app/app.component.ts | 8 +++----- .../src/app/home/home.component.ts | 6 +++--- 19 files changed, 51 insertions(+), 67 deletions(-) diff --git a/projects/sample-code-flow-auth0/src/app/app.component.ts b/projects/sample-code-flow-auth0/src/app/app.component.ts index f24ea551..723ed6fc 100644 --- a/projects/sample-code-flow-auth0/src/app/app.component.ts +++ b/projects/sample-code-flow-auth0/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-auth0/src/app/home/home.component.ts b/projects/sample-code-flow-auth0/src/app/home/home.component.ts index 55499d3c..2b13f58d 100644 --- a/projects/sample-code-flow-auth0/src/app/home/home.component.ts +++ b/projects/sample-code-flow-auth0/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,14 +6,14 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + configuration$ = this.oidcSecurityService.getConfiguration(); userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-auto-login-all-routes/src/app/app.component.ts b/projects/sample-code-flow-auto-login-all-routes/src/app/app.component.ts index 78b7d901..1e98fa32 100644 --- a/projects/sample-code-flow-auto-login-all-routes/src/app/app.component.ts +++ b/projects/sample-code-flow-auto-login-all-routes/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,9 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./app.component.css'], }) export class AppComponent { - title = 'sample-code-flow-auto-login-all-routes'; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + title = 'sample-code-flow-auto-login-all-routes'; ngOnInit() { this.oidcSecurityService diff --git a/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.html b/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.html index 7f9829b8..fb754831 100644 --- a/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.html +++ b/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.html @@ -1,3 +1,4 @@
- Normally you should not see this, authenticated == {{ isAuthenticated }} + Normally you should not see this, authenticated == + {{ isAuthenticated$ | async }}
diff --git a/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.ts b/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.ts index f67a993a..268c7d2d 100644 --- a/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.ts +++ b/projects/sample-code-flow-auto-login-all-routes/src/app/forbidden/forbidden.component.ts @@ -1,22 +1,10 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ selector: 'app-forbidden', templateUrl: 'forbidden.component.html', }) -export class ForbiddenComponent implements OnInit { - public isAuthenticated = false; - - constructor(private oidcSecurityService: OidcSecurityService) {} - - ngOnInit() { - this.oidcSecurityService.isAuthenticated$.subscribe( - ({ isAuthenticated }) => { - this.isAuthenticated = isAuthenticated; - - console.warn('authenticated: ', isAuthenticated); - } - ); - } +export class ForbiddenComponent { + public isAuthenticated$ = inject(OidcSecurityService).isAuthenticated$; } diff --git a/projects/sample-code-flow-auto-login-all-routes/src/app/home/home.component.ts b/projects/sample-code-flow-auto-login-all-routes/src/app/home/home.component.ts index 44f8933b..5a720b9e 100644 --- a/projects/sample-code-flow-auto-login-all-routes/src/app/home/home.component.ts +++ b/projects/sample-code-flow-auto-login-all-routes/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,11 +6,11 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit() { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-auto-login-all-routes/src/app/navigation/navigation.component.ts b/projects/sample-code-flow-auto-login-all-routes/src/app/navigation/navigation.component.ts index cba9bcb8..4b8b7e33 100644 --- a/projects/sample-code-flow-auto-login-all-routes/src/app/navigation/navigation.component.ts +++ b/projects/sample-code-flow-auto-login-all-routes/src/app/navigation/navigation.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,9 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['navigation.component.css'], }) export class NavigationComponent implements OnInit { - isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + isAuthenticated = false; ngOnInit() { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-code-flow-auto-login/src/app/app.component.ts b/projects/sample-code-flow-auto-login/src/app/app.component.ts index c36e3f09..3a9bcede 100644 --- a/projects/sample-code-flow-auto-login/src/app/app.component.ts +++ b/projects/sample-code-flow-auto-login/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,7 +7,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./app.component.css'], }) export class AppComponent implements OnInit { - constructor(private readonly oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-auto-login/src/app/forbidden/forbidden.component.ts b/projects/sample-code-flow-auto-login/src/app/forbidden/forbidden.component.ts index 97de4338..9e546ca0 100644 --- a/projects/sample-code-flow-auto-login/src/app/forbidden/forbidden.component.ts +++ b/projects/sample-code-flow-auto-login/src/app/forbidden/forbidden.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,9 +6,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'forbidden.component.html', }) export class ForbiddenComponent implements OnInit { - public isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(private readonly oidcSecurityService: OidcSecurityService) {} + public isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-code-flow-auto-login/src/app/home/home.component.ts b/projects/sample-code-flow-auto-login/src/app/home/home.component.ts index 1dc68879..b8b84ffd 100644 --- a/projects/sample-code-flow-auto-login/src/app/home/home.component.ts +++ b/projects/sample-code-flow-auto-login/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,12 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-auto-login/src/app/navigation/navigation.component.ts b/projects/sample-code-flow-auto-login/src/app/navigation/navigation.component.ts index 09062d66..7dbb91fe 100644 --- a/projects/sample-code-flow-auto-login/src/app/navigation/navigation.component.ts +++ b/projects/sample-code-flow-auto-login/src/app/navigation/navigation.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,9 +7,9 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['navigation.component.css'], }) export class NavigationComponent implements OnInit { - isAuthenticated = false; + private readonly oidcSecurityService = inject(OidcSecurityService); - constructor(public oidcSecurityService: OidcSecurityService) {} + isAuthenticated = false; ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( diff --git a/projects/sample-code-flow-azure-b2c/src/app/app.component.ts b/projects/sample-code-flow-azure-b2c/src/app/app.component.ts index 69c9a5b3..3522d17c 100644 --- a/projects/sample-code-flow-azure-b2c/src/app/app.component.ts +++ b/projects/sample-code-flow-azure-b2c/src/app/app.component.ts @@ -1,10 +1,7 @@ import { Component } from '@angular/core'; -import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', }) -export class AppComponent { - constructor(public oidcSecurityService: OidcSecurityService) {} -} +export class AppComponent {} diff --git a/projects/sample-code-flow-azure-b2c/src/app/home/home.component.ts b/projects/sample-code-flow-azure-b2c/src/app/home/home.component.ts index 1dc68879..b8b84ffd 100644 --- a/projects/sample-code-flow-azure-b2c/src/app/home/home.component.ts +++ b/projects/sample-code-flow-azure-b2c/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,12 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-azure-b2c/src/app/nav-menu/nav-menu.component.ts b/projects/sample-code-flow-azure-b2c/src/app/nav-menu/nav-menu.component.ts index 8b660791..ee058efe 100644 --- a/projects/sample-code-flow-azure-b2c/src/app/nav-menu/nav-menu.component.ts +++ b/projects/sample-code-flow-azure-b2c/src/app/nav-menu/nav-menu.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,12 +7,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./nav-menu.component.css'], }) export class NavMenuComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + isExpanded = false; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-azuread/src/app/app.component.ts b/projects/sample-code-flow-azuread/src/app/app.component.ts index f145dffc..883b9ce6 100644 --- a/projects/sample-code-flow-azuread/src/app/app.component.ts +++ b/projects/sample-code-flow-azuread/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,7 +6,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'app.component.html', }) export class AppComponent { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); login(): void { console.log('start login'); diff --git a/projects/sample-code-flow-azuread/src/app/home/home.component.ts b/projects/sample-code-flow-azuread/src/app/home/home.component.ts index 1dc68879..b8b84ffd 100644 --- a/projects/sample-code-flow-azuread/src/app/home/home.component.ts +++ b/projects/sample-code-flow-azuread/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,12 +6,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + userData$ = this.oidcSecurityService.userData$; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-azuread/src/app/nav-menu/nav-menu.component.ts b/projects/sample-code-flow-azuread/src/app/nav-menu/nav-menu.component.ts index 8b660791..ee058efe 100644 --- a/projects/sample-code-flow-azuread/src/app/nav-menu/nav-menu.component.ts +++ b/projects/sample-code-flow-azuread/src/app/nav-menu/nav-menu.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -7,12 +7,12 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; styleUrls: ['./nav-menu.component.css'], }) export class NavMenuComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + isExpanded = false; isAuthenticated = false; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { diff --git a/projects/sample-code-flow-http-config/src/app/app.component.ts b/projects/sample-code-flow-http-config/src/app/app.component.ts index 37035e71..48b28f0d 100644 --- a/projects/sample-code-flow-http-config/src/app/app.component.ts +++ b/projects/sample-code-flow-http-config/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { EventTypes, OidcSecurityService, @@ -11,10 +11,8 @@ import { filter } from 'rxjs/operators'; templateUrl: 'app.component.html', }) export class AppComponent implements OnInit { - constructor( - public oidcSecurityService: OidcSecurityService, - private readonly eventService: PublicEventsService - ) {} + private readonly oidcSecurityService = inject(OidcSecurityService); + private readonly eventService = inject(PublicEventsService); ngOnInit(): void { this.oidcSecurityService diff --git a/projects/sample-code-flow-http-config/src/app/home/home.component.ts b/projects/sample-code-flow-http-config/src/app/home/home.component.ts index 68897361..5a4f8d0a 100644 --- a/projects/sample-code-flow-http-config/src/app/home/home.component.ts +++ b/projects/sample-code-flow-http-config/src/app/home/home.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ @@ -6,6 +6,8 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; templateUrl: 'home.component.html', }) export class HomeComponent implements OnInit { + private readonly oidcSecurityService = inject(OidcSecurityService); + configuration$ = this.oidcSecurityService.getConfiguration(); userData$ = this.oidcSecurityService.userData$; @@ -14,8 +16,6 @@ export class HomeComponent implements OnInit { checkSessionChanged$ = this.oidcSecurityService.checkSessionChanged$; - constructor(public oidcSecurityService: OidcSecurityService) {} - ngOnInit(): void { this.oidcSecurityService.isAuthenticated$.subscribe( ({ isAuthenticated }) => { From d5181f1203f20e1d876c28ddd31cd45f7967c8aa Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 13:59:58 +0100 Subject: [PATCH 03/10] Fix dependency injection in Angular services --- .../src/lib/interceptor/auth.interceptor.ts | 15 ++++++---- .../src/lib/login/login.service.ts | 20 ++++++++----- .../src/lib/login/par/par-login.service.ts | 29 ++++++++++++------- .../src/lib/login/par/par.service.ts | 17 ++++++----- .../lib/login/popup/popup-login.service.ts | 23 +++++++++------ .../src/lib/login/popup/popup.service.ts | 16 +++++----- .../response-type-validation.service.ts | 9 +++--- .../login/standard/standard-login.service.ts | 23 +++++++++------ .../logoff-revocation.service.ts | 26 ++++++++++------- .../lib/storage/browser-storage.service.ts | 9 +++--- .../src/lib/user-data/user.service.ts | 23 +++++++++------ .../src/lib/utils/crypto/crypto.service.ts | 7 +++-- .../platform-provider/platform.provider.ts | 6 ++-- .../lib/utils/redirect/redirect.service.ts | 4 +-- .../utils/tokenHelper/token-helper.service.ts | 9 +++--- .../validation/jwk-window-crypto.service.ts | 4 +-- .../validation/jwt-window-crypto.service.ts | 4 +-- .../validation/state-validation.service.ts | 23 +++++++++------ .../validation/token-validation.service.ts | 18 +++++++----- 19 files changed, 165 insertions(+), 120 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/interceptor/auth.interceptor.ts b/projects/angular-auth-oidc-client/src/lib/interceptor/auth.interceptor.ts index 42a63adf..d7d3b1f5 100644 --- a/projects/angular-auth-oidc-client/src/lib/interceptor/auth.interceptor.ts +++ b/projects/angular-auth-oidc-client/src/lib/interceptor/auth.interceptor.ts @@ -16,12 +16,15 @@ import { ClosestMatchingRouteService } from './closest-matching-route.service'; @Injectable() export class AuthInterceptor implements HttpInterceptor { - constructor( - private readonly authStateService: AuthStateService, - private readonly configurationService: ConfigurationService, - private readonly loggerService: LoggerService, - private readonly closestMatchingRouteService: ClosestMatchingRouteService - ) {} + private readonly authStateService = inject(AuthStateService); + + private readonly configurationService = inject(ConfigurationService); + + private readonly loggerService = inject(LoggerService); + + private readonly closestMatchingRouteService = inject( + ClosestMatchingRouteService + ); intercept( req: HttpRequest, diff --git a/projects/angular-auth-oidc-client/src/lib/login/login.service.ts b/projects/angular-auth-oidc-client/src/lib/login/login.service.ts index ca712a2d..caa7cdb7 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/login.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/login.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of } from 'rxjs'; import { AuthOptions } from '../auth-options'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -12,13 +12,17 @@ import { StandardLoginService } from './standard/standard-login.service'; @Injectable({ providedIn: 'root' }) export class LoginService { - constructor( - private readonly parLoginService: ParLoginService, - private readonly popUpLoginService: PopUpLoginService, - private readonly standardLoginService: StandardLoginService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly popupService: PopUpService - ) {} + private readonly parLoginService = inject(ParLoginService); + + private readonly popUpLoginService = inject(PopUpLoginService); + + private readonly standardLoginService = inject(StandardLoginService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly popupService = inject(PopUpService); login( configuration: OpenIdConfiguration | null, diff --git a/projects/angular-auth-oidc-client/src/lib/login/par/par-login.service.ts b/projects/angular-auth-oidc-client/src/lib/login/par/par-login.service.ts index ac708d23..b7d49421 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/par/par-login.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/par/par-login.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { switchMap, take } from 'rxjs/operators'; import { AuthOptions } from '../../auth-options'; @@ -18,16 +18,23 @@ import { ParService } from './par.service'; @Injectable({ providedIn: 'root' }) export class ParLoginService { - constructor( - private readonly loggerService: LoggerService, - private readonly responseTypeValidationService: ResponseTypeValidationService, - private readonly urlService: UrlService, - private readonly redirectService: RedirectService, - private readonly authWellKnownService: AuthWellKnownService, - private readonly popupService: PopUpService, - private readonly checkAuthService: CheckAuthService, - private readonly parService: ParService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly responseTypeValidationService = inject( + ResponseTypeValidationService + ); + + private readonly urlService = inject(UrlService); + + private readonly redirectService = inject(RedirectService); + + private readonly authWellKnownService = inject(AuthWellKnownService); + + private readonly popupService = inject(PopUpService); + + private readonly checkAuthService = inject(CheckAuthService); + + private readonly parService = inject(ParService); loginPar( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/login/par/par.service.ts b/projects/angular-auth-oidc-client/src/lib/login/par/par.service.ts index be3c4ccc..734b723b 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/par/par.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/par/par.service.ts @@ -1,5 +1,5 @@ import { HttpHeaders } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { catchError, map, retry, switchMap } from 'rxjs/operators'; import { DataService } from '../../api/data.service'; @@ -12,12 +12,15 @@ import { ParResponse } from './par-response'; @Injectable({ providedIn: 'root' }) export class ParService { - constructor( - private readonly loggerService: LoggerService, - private readonly urlService: UrlService, - private readonly dataService: DataService, - private readonly storagePersistenceService: StoragePersistenceService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly urlService = inject(UrlService); + + private readonly dataService = inject(DataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); postParRequest( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/login/popup/popup-login.service.ts b/projects/angular-auth-oidc-client/src/lib/login/popup/popup-login.service.ts index b61e0742..55cba8b7 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/popup/popup-login.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/popup/popup-login.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { switchMap, take, tap } from 'rxjs/operators'; import { AuthOptions } from '../../auth-options'; @@ -15,14 +15,19 @@ import { PopUpService } from './popup.service'; @Injectable({ providedIn: 'root' }) export class PopUpLoginService { - constructor( - private readonly loggerService: LoggerService, - private readonly responseTypeValidationService: ResponseTypeValidationService, - private readonly urlService: UrlService, - private readonly authWellKnownService: AuthWellKnownService, - private readonly popupService: PopUpService, - private readonly checkAuthService: CheckAuthService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly responseTypeValidationService = inject( + ResponseTypeValidationService + ); + + private readonly urlService = inject(UrlService); + + private readonly authWellKnownService = inject(AuthWellKnownService); + + private readonly popupService = inject(PopUpService); + + private readonly checkAuthService = inject(CheckAuthService); loginWithPopUpStandard( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/login/popup/popup.service.ts b/projects/angular-auth-oidc-client/src/lib/login/popup/popup.service.ts index cc05a049..cc047092 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/popup/popup.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/popup/popup.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { OpenIdConfiguration } from '../../config/openid-configuration'; import { LoggerService } from '../../logging/logger.service'; @@ -9,6 +9,14 @@ import { PopupResult } from './popup-result'; @Injectable({ providedIn: 'root' }) export class PopUpService { + private readonly loggerService = inject(LoggerService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly document = inject(DOCUMENT); + private readonly STORAGE_IDENTIFIER = 'popupauth'; private popUp: Window | null = null; @@ -25,12 +33,6 @@ export class PopUpService { return this.document.defaultView; } - constructor( - @Inject(DOCUMENT) private readonly document: Document, - private readonly loggerService: LoggerService, - private readonly storagePersistenceService: StoragePersistenceService - ) {} - isCurrentlyInPopup(config: OpenIdConfiguration): boolean { if (this.canAccessSessionStorage()) { const popup = this.storagePersistenceService.read( diff --git a/projects/angular-auth-oidc-client/src/lib/login/response-type-validation/response-type-validation.service.ts b/projects/angular-auth-oidc-client/src/lib/login/response-type-validation/response-type-validation.service.ts index 2d013314..8e96fa1d 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/response-type-validation/response-type-validation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/response-type-validation/response-type-validation.service.ts @@ -1,14 +1,13 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../../config/openid-configuration'; import { LoggerService } from '../../logging/logger.service'; import { FlowHelper } from '../../utils/flowHelper/flow-helper.service'; @Injectable({ providedIn: 'root' }) export class ResponseTypeValidationService { - constructor( - private readonly loggerService: LoggerService, - private readonly flowHelper: FlowHelper - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly flowHelper = inject(FlowHelper); hasConfigValidResponseType(configuration: OpenIdConfiguration): boolean { if (this.flowHelper.isCurrentFlowAnyImplicitFlow(configuration)) { diff --git a/projects/angular-auth-oidc-client/src/lib/login/standard/standard-login.service.ts b/projects/angular-auth-oidc-client/src/lib/login/standard/standard-login.service.ts index 290aa5d3..d9fe46d5 100644 --- a/projects/angular-auth-oidc-client/src/lib/login/standard/standard-login.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/login/standard/standard-login.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { AuthOptions } from '../../auth-options'; import { AuthWellKnownService } from '../../config/auth-well-known/auth-well-known.service'; import { OpenIdConfiguration } from '../../config/openid-configuration'; @@ -10,14 +10,19 @@ import { ResponseTypeValidationService } from '../response-type-validation/respo @Injectable({ providedIn: 'root' }) export class StandardLoginService { - constructor( - private readonly loggerService: LoggerService, - private readonly responseTypeValidationService: ResponseTypeValidationService, - private readonly urlService: UrlService, - private readonly redirectService: RedirectService, - private readonly authWellKnownService: AuthWellKnownService, - private readonly flowsDataService: FlowsDataService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly responseTypeValidationService = inject( + ResponseTypeValidationService + ); + + private readonly urlService = inject(UrlService); + + private readonly redirectService = inject(RedirectService); + + private readonly authWellKnownService = inject(AuthWellKnownService); + + private readonly flowsDataService = inject(FlowsDataService); loginStandard( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/logoff-revoke/logoff-revocation.service.ts b/projects/angular-auth-oidc-client/src/lib/logoff-revoke/logoff-revocation.service.ts index ecface23..b127b162 100644 --- a/projects/angular-auth-oidc-client/src/lib/logoff-revoke/logoff-revocation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/logoff-revoke/logoff-revocation.service.ts @@ -1,5 +1,5 @@ import { HttpHeaders } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { catchError, concatMap, retry, switchMap } from 'rxjs/operators'; import { DataService } from '../api/data.service'; @@ -15,15 +15,21 @@ import { UrlService } from '../utils/url/url.service'; @Injectable({ providedIn: 'root' }) export class LogoffRevocationService { - constructor( - private readonly dataService: DataService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly loggerService: LoggerService, - private readonly urlService: UrlService, - private readonly checkSessionService: CheckSessionService, - private readonly resetAuthDataService: ResetAuthDataService, - private readonly redirectService: RedirectService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly dataService = inject(DataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly urlService = inject(UrlService); + + private readonly checkSessionService = inject(CheckSessionService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly redirectService = inject(RedirectService); // Logs out on the server and the local client. // If the server state has changed, check session, then only a local logout. diff --git a/projects/angular-auth-oidc-client/src/lib/storage/browser-storage.service.ts b/projects/angular-auth-oidc-client/src/lib/storage/browser-storage.service.ts index 06a510a4..8ddcf0f6 100644 --- a/projects/angular-auth-oidc-client/src/lib/storage/browser-storage.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/storage/browser-storage.service.ts @@ -1,14 +1,13 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { LoggerService } from '../logging/logger.service'; import { AbstractSecurityStorage } from './abstract-security-storage'; @Injectable({ providedIn: 'root' }) export class BrowserStorageService { - constructor( - private readonly loggerService: LoggerService, - private readonly abstractSecurityStorage: AbstractSecurityStorage - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly abstractSecurityStorage = inject(AbstractSecurityStorage); read(key: string, configuration: OpenIdConfiguration): any { const { configId } = configuration; diff --git a/projects/angular-auth-oidc-client/src/lib/user-data/user.service.ts b/projects/angular-auth-oidc-client/src/lib/user-data/user.service.ts index e44feadd..42c8805f 100644 --- a/projects/angular-auth-oidc-client/src/lib/user-data/user.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/user-data/user.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { BehaviorSubject, Observable, of, throwError } from 'rxjs'; import { map, retry, switchMap } from 'rxjs/operators'; import { DataService } from '../api/data.service'; @@ -23,14 +23,19 @@ export class UserService { return this.userDataInternal$.asObservable(); } - constructor( - private readonly oidcDataService: DataService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly eventService: PublicEventsService, - private readonly loggerService: LoggerService, - private readonly tokenHelperService: TokenHelperService, - private readonly flowHelper: FlowHelper - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly tokenHelperService = inject(TokenHelperService); + + private readonly flowHelper = inject(FlowHelper); + + private readonly oidcDataService = inject(DataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly eventService = inject(PublicEventsService); getAndPersistUserDataInStore( currentConfiguration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/utils/crypto/crypto.service.ts b/projects/angular-auth-oidc-client/src/lib/utils/crypto/crypto.service.ts index 782834d5..aa955c45 100644 --- a/projects/angular-auth-oidc-client/src/lib/utils/crypto/crypto.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/utils/crypto/crypto.service.ts @@ -1,14 +1,15 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CryptoService { - constructor(@Inject(DOCUMENT) private readonly doc: Document) {} + private readonly document = inject(DOCUMENT); getCrypto(): any { // support for IE, (window.crypto || window.msCrypto) return ( - this.doc.defaultView?.crypto || (this.doc.defaultView as any)?.msCrypto + this.document.defaultView?.crypto || + (this.document.defaultView as any)?.msCrypto ); } } diff --git a/projects/angular-auth-oidc-client/src/lib/utils/platform-provider/platform.provider.ts b/projects/angular-auth-oidc-client/src/lib/utils/platform-provider/platform.provider.ts index 7a47788f..fa5465e8 100644 --- a/projects/angular-auth-oidc-client/src/lib/utils/platform-provider/platform.provider.ts +++ b/projects/angular-auth-oidc-client/src/lib/utils/platform-provider/platform.provider.ts @@ -1,11 +1,11 @@ import { isPlatformBrowser } from '@angular/common'; -import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; +import { Injectable, PLATFORM_ID, inject } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class PlatformProvider { + private readonly platformId = inject(PLATFORM_ID); + isBrowser(): boolean { return isPlatformBrowser(this.platformId); } - - constructor(@Inject(PLATFORM_ID) private readonly platformId: string) {} } diff --git a/projects/angular-auth-oidc-client/src/lib/utils/redirect/redirect.service.ts b/projects/angular-auth-oidc-client/src/lib/utils/redirect/redirect.service.ts index 4888f0c9..551bf4e3 100644 --- a/projects/angular-auth-oidc-client/src/lib/utils/redirect/redirect.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/utils/redirect/redirect.service.ts @@ -1,9 +1,9 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class RedirectService { - constructor(@Inject(DOCUMENT) private readonly document: Document) {} + private readonly document = inject(DOCUMENT); redirectTo(url: string): void { this.document.location.href = url; diff --git a/projects/angular-auth-oidc-client/src/lib/utils/tokenHelper/token-helper.service.ts b/projects/angular-auth-oidc-client/src/lib/utils/tokenHelper/token-helper.service.ts index acd67732..3e264478 100644 --- a/projects/angular-auth-oidc-client/src/lib/utils/tokenHelper/token-helper.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/utils/tokenHelper/token-helper.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../../config/openid-configuration'; import { LoggerService } from '../../logging/logger.service'; @@ -7,10 +7,9 @@ const PARTS_OF_TOKEN = 3; @Injectable({ providedIn: 'root' }) export class TokenHelperService { - constructor( - private readonly loggerService: LoggerService, - @Inject(DOCUMENT) private readonly document: Document - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly document = inject(DOCUMENT); getTokenExpirationDate(dataIdToken: any): Date { if (!Object.prototype.hasOwnProperty.call(dataIdToken, 'exp')) { diff --git a/projects/angular-auth-oidc-client/src/lib/validation/jwk-window-crypto.service.ts b/projects/angular-auth-oidc-client/src/lib/validation/jwk-window-crypto.service.ts index 8654adbb..d39ad45a 100644 --- a/projects/angular-auth-oidc-client/src/lib/validation/jwk-window-crypto.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/validation/jwk-window-crypto.service.ts @@ -1,9 +1,9 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { CryptoService } from '../utils/crypto/crypto.service'; @Injectable({ providedIn: 'root' }) export class JwkWindowCryptoService { - constructor(private readonly cryptoService: CryptoService) {} + private readonly cryptoService = inject(CryptoService); importVerificationKey( key: JsonWebKey, diff --git a/projects/angular-auth-oidc-client/src/lib/validation/jwt-window-crypto.service.ts b/projects/angular-auth-oidc-client/src/lib/validation/jwt-window-crypto.service.ts index 40312974..52c48071 100644 --- a/projects/angular-auth-oidc-client/src/lib/validation/jwt-window-crypto.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/validation/jwt-window-crypto.service.ts @@ -1,11 +1,11 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { from, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { CryptoService } from '../utils/crypto/crypto.service'; @Injectable({ providedIn: 'root' }) export class JwtWindowCryptoService { - constructor(private readonly cryptoService: CryptoService) {} + private readonly cryptoService = inject(CryptoService); generateCodeChallenge(codeVerifier: string): Observable { return this.calcHash(codeVerifier).pipe( diff --git a/projects/angular-auth-oidc-client/src/lib/validation/state-validation.service.ts b/projects/angular-auth-oidc-client/src/lib/validation/state-validation.service.ts index a975cdf5..ca657d0a 100644 --- a/projects/angular-auth-oidc-client/src/lib/validation/state-validation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/validation/state-validation.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of } from 'rxjs'; import { map, mergeMap } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -14,14 +14,19 @@ import { ValidationResult } from './validation-result'; @Injectable({ providedIn: 'root' }) export class StateValidationService { - constructor( - private readonly storagePersistenceService: StoragePersistenceService, - private readonly tokenValidationService: TokenValidationService, - private readonly tokenHelperService: TokenHelperService, - private readonly loggerService: LoggerService, - private readonly equalityService: EqualityService, - private readonly flowHelper: FlowHelper - ) {} + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly tokenValidationService = inject(TokenValidationService); + + private readonly tokenHelperService = inject(TokenHelperService); + + private readonly loggerService = inject(LoggerService); + + private readonly equalityService = inject(EqualityService); + + private readonly flowHelper = inject(FlowHelper); getValidatedStateResult( callbackContext: CallbackContext, diff --git a/projects/angular-auth-oidc-client/src/lib/validation/token-validation.service.ts b/projects/angular-auth-oidc-client/src/lib/validation/token-validation.service.ts index 4bea89d5..6335372f 100644 --- a/projects/angular-auth-oidc-client/src/lib/validation/token-validation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/validation/token-validation.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { base64url } from 'rfc4648'; import { from, Observable, of } from 'rxjs'; import { map, mergeMap, tap } from 'rxjs/operators'; @@ -72,13 +72,15 @@ export class TokenValidationService { 'PS512', ]; - constructor( - private readonly tokenHelperService: TokenHelperService, - private readonly loggerService: LoggerService, - private readonly jwkExtractor: JwkExtractor, - private readonly jwkWindowCryptoService: JwkWindowCryptoService, - private readonly jwtWindowCryptoService: JwtWindowCryptoService - ) {} + private readonly tokenHelperService = inject(TokenHelperService); + + private readonly loggerService = inject(LoggerService); + + private readonly jwkExtractor = inject(JwkExtractor); + + private readonly jwkWindowCryptoService = inject(JwkWindowCryptoService); + + private readonly jwtWindowCryptoService = inject(JwtWindowCryptoService); // id_token C7: The current time MUST be before the time represented by the exp Claim // (possibly allowing for some small leeway to account for clock skew). From 8622cc361a0724465cdbd13c7d060dc1135fdc66 Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:06:32 +0100 Subject: [PATCH 04/10] Fix dependency injection issues in logger and service classes --- .../src/lib/auth-state/auth-state.service.ts | 17 ++++--- .../src/lib/auth-state/check-auth.service.ts | 44 ++++++++++++------- .../lib/callback/refresh-session.service.ts | 39 ++++++++++------ .../src/lib/iframe/check-session.service.ts | 25 ++++++----- .../src/lib/iframe/existing-iframe.service.ts | 9 ++-- .../src/lib/iframe/silent-renew.service.ts | 32 +++++++++----- .../src/lib/logging/logger.service.ts | 4 +- .../src/lib/oidc.security.service.ts | 44 ++++++++++++------- 8 files changed, 133 insertions(+), 81 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts b/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts index 86c3f46e..cf183d80 100644 --- a/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts @@ -18,6 +18,16 @@ const DEFAULT_AUTHRESULT = { @Injectable({ providedIn: 'root' }) export class AuthStateService { + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly loggerService = inject(LoggerService); + + private readonly publicEventsService = inject(PublicEventsService); + + private readonly tokenValidationService = inject(TokenValidationService); + private readonly authenticatedInternal$ = new BehaviorSubject(DEFAULT_AUTHRESULT); @@ -27,13 +37,6 @@ export class AuthStateService { .pipe(distinctUntilChanged()); } - constructor( - private readonly storagePersistenceService: StoragePersistenceService, - private readonly loggerService: LoggerService, - private readonly publicEventsService: PublicEventsService, - private readonly tokenValidationService: TokenValidationService - ) {} - setAuthenticatedAndFireEvent(allConfigs: OpenIdConfiguration[]): void { const result = this.composeAuthenticatedResult(allConfigs); diff --git a/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts b/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts index 2a93cb56..24a40e10 100644 --- a/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts @@ -20,21 +20,35 @@ import { AuthStateService } from './auth-state.service'; @Injectable({ providedIn: 'root' }) export class CheckAuthService { - constructor( - private readonly checkSessionService: CheckSessionService, - private readonly currentUrlService: CurrentUrlService, - private readonly silentRenewService: SilentRenewService, - private readonly userService: UserService, - private readonly loggerService: LoggerService, - private readonly authStateService: AuthStateService, - private readonly callbackService: CallbackService, - private readonly refreshSessionService: RefreshSessionService, - private readonly periodicallyTokenCheckService: PeriodicallyTokenCheckService, - private readonly popupService: PopUpService, - private readonly autoLoginService: AutoLoginService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly publicEventsService: PublicEventsService - ) {} + private readonly checkSessionService = inject(CheckSessionService); + + private readonly currentUrlService = inject(CurrentUrlService); + + private readonly silentRenewService = inject(SilentRenewService); + + private readonly userService = inject(UserService); + + private readonly loggerService = inject(LoggerService); + + private readonly authStateService = inject(AuthStateService); + + private readonly callbackService = inject(CallbackService); + + private readonly refreshSessionService = inject(RefreshSessionService); + + private readonly periodicallyTokenCheckService = inject( + PeriodicallyTokenCheckService + ); + + private readonly popupService = inject(PopUpService); + + private readonly autoLoginService = inject(AutoLoginService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly publicEventsService = inject(PublicEventsService); private getConfig( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/callback/refresh-session.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/refresh-session.service.ts index b4e42f65..ce92d2c7 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/refresh-session.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/refresh-session.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { forkJoin, Observable, @@ -33,18 +33,31 @@ export const MAX_RETRY_ATTEMPTS = 3; @Injectable({ providedIn: 'root' }) export class RefreshSessionService { - constructor( - private readonly flowHelper: FlowHelper, - private readonly flowsDataService: FlowsDataService, - private readonly loggerService: LoggerService, - private readonly silentRenewService: SilentRenewService, - private readonly authStateService: AuthStateService, - private readonly authWellKnownService: AuthWellKnownService, - private readonly refreshSessionIframeService: RefreshSessionIframeService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly refreshSessionRefreshTokenService: RefreshSessionRefreshTokenService, - private readonly userService: UserService - ) {} + private readonly flowHelper = inject(FlowHelper); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly loggerService = inject(LoggerService); + + private readonly silentRenewService = inject(SilentRenewService); + + private readonly authStateService = inject(AuthStateService); + + private readonly authWellKnownService = inject(AuthWellKnownService); + + private readonly refreshSessionIframeService = inject( + RefreshSessionIframeService + ); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly refreshSessionRefreshTokenService = inject( + RefreshSessionRefreshTokenService + ); + + private readonly userService = inject(UserService); userForceRefreshSession( config: OpenIdConfiguration | null, diff --git a/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts b/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts index 791bbcf5..5ce101d2 100644 --- a/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/iframe/check-session.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable, NgZone, OnDestroy } from '@angular/core'; +import { Injectable, NgZone, OnDestroy, inject } from '@angular/core'; import { BehaviorSubject, Observable, of } from 'rxjs'; import { take } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -15,6 +15,20 @@ const IFRAME_FOR_CHECK_SESSION_IDENTIFIER = 'myiFrameForCheckSession'; @Injectable({ providedIn: 'root' }) export class CheckSessionService implements OnDestroy { + private readonly loggerService = inject(LoggerService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly iFrameService = inject(IFrameService); + + private readonly eventService = inject(PublicEventsService); + + private readonly zone = inject(NgZone); + + private readonly document = inject(DOCUMENT); + private checkSessionReceived = false; private scheduledHeartBeatRunning: any; @@ -37,15 +51,6 @@ export class CheckSessionService implements OnDestroy { return this.checkSessionChangedInternal$.asObservable(); } - constructor( - private readonly storagePersistenceService: StoragePersistenceService, - private readonly loggerService: LoggerService, - private readonly iFrameService: IFrameService, - private readonly eventService: PublicEventsService, - private readonly zone: NgZone, - @Inject(DOCUMENT) private readonly document: Document - ) {} - ngOnDestroy(): void { this.stop(); const windowAsDefaultView = this.document.defaultView; diff --git a/projects/angular-auth-oidc-client/src/lib/iframe/existing-iframe.service.ts b/projects/angular-auth-oidc-client/src/lib/iframe/existing-iframe.service.ts index bf6f74f4..9894976e 100644 --- a/projects/angular-auth-oidc-client/src/lib/iframe/existing-iframe.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/iframe/existing-iframe.service.ts @@ -1,14 +1,13 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { LoggerService } from '../logging/logger.service'; @Injectable({ providedIn: 'root' }) export class IFrameService { - constructor( - @Inject(DOCUMENT) private readonly document: Document, - private readonly loggerService: LoggerService - ) {} + private readonly document = inject(DOCUMENT); + + private readonly loggerService = inject(LoggerService); getExistingIFrame(identifier: string): HTMLIFrameElement | null { const iFrameOnParent = this.getIFrameFromParentWindow(identifier); diff --git a/projects/angular-auth-oidc-client/src/lib/iframe/silent-renew.service.ts b/projects/angular-auth-oidc-client/src/lib/iframe/silent-renew.service.ts index 1c0941c4..e1bf4abd 100644 --- a/projects/angular-auth-oidc-client/src/lib/iframe/silent-renew.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/iframe/silent-renew.service.ts @@ -1,5 +1,5 @@ import { HttpParams } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, Subject, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { AuthStateService } from '../auth-state/auth-state.service'; @@ -26,17 +26,25 @@ export class SilentRenewService { return this.refreshSessionWithIFrameCompletedInternal$.asObservable(); } - constructor( - private readonly iFrameService: IFrameService, - private readonly flowsService: FlowsService, - private readonly resetAuthDataService: ResetAuthDataService, - private readonly flowsDataService: FlowsDataService, - private readonly authStateService: AuthStateService, - private readonly loggerService: LoggerService, - private readonly flowHelper: FlowHelper, - private readonly implicitFlowCallbackService: ImplicitFlowCallbackService, - private readonly intervalService: IntervalService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly iFrameService = inject(IFrameService); + + private readonly flowsService = inject(FlowsService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly authStateService = inject(AuthStateService); + + private readonly flowHelper = inject(FlowHelper); + + private readonly implicitFlowCallbackService = inject( + ImplicitFlowCallbackService + ); + + private readonly intervalService = inject(IntervalService); getOrCreateIframe(config: OpenIdConfiguration): HTMLIFrameElement { const existingIframe = this.getExistingIframe(); diff --git a/projects/angular-auth-oidc-client/src/lib/logging/logger.service.ts b/projects/angular-auth-oidc-client/src/lib/logging/logger.service.ts index 0428b286..c76db263 100644 --- a/projects/angular-auth-oidc-client/src/lib/logging/logger.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/logging/logger.service.ts @@ -1,11 +1,11 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { AbstractLoggerService } from './abstract-logger.service'; import { LogLevel } from './log-level'; @Injectable({ providedIn: 'root' }) export class LoggerService { - constructor(private readonly abstractLoggerService: AbstractLoggerService) {} + private readonly abstractLoggerService = inject(AbstractLoggerService); logError( configuration: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/oidc.security.service.ts b/projects/angular-auth-oidc-client/src/lib/oidc.security.service.ts index 8760fc4c..7acaeee6 100644 --- a/projects/angular-auth-oidc-client/src/lib/oidc.security.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/oidc.security.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { concatMap, map } from 'rxjs/operators'; import { AuthOptions, LogoutAuthOptions } from './auth-options'; @@ -25,6 +25,32 @@ import { UrlService } from './utils/url/url.service'; @Injectable({ providedIn: 'root' }) export class OidcSecurityService { + private readonly checkSessionService = inject(CheckSessionService); + + private readonly checkAuthService = inject(CheckAuthService); + + private readonly userService = inject(UserService); + + private readonly tokenHelperService = inject(TokenHelperService); + + private readonly configurationService = inject(ConfigurationService); + + private readonly authStateService = inject(AuthStateService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly callbackService = inject(CallbackService); + + private readonly logoffRevocationService = inject(LogoffRevocationService); + + private readonly loginService = inject(LoginService); + + private readonly refreshSessionService = inject(RefreshSessionService); + + private readonly urlService = inject(UrlService); + + private readonly authWellKnownService = inject(AuthWellKnownService); + /** * Provides information about the user after they have logged in. * @@ -63,22 +89,6 @@ export class OidcSecurityService { return this.callbackService.stsCallback$; } - constructor( - private readonly checkSessionService: CheckSessionService, - private readonly checkAuthService: CheckAuthService, - private readonly userService: UserService, - private readonly tokenHelperService: TokenHelperService, - private readonly configurationService: ConfigurationService, - private readonly authStateService: AuthStateService, - private readonly flowsDataService: FlowsDataService, - private readonly callbackService: CallbackService, - private readonly logoffRevocationService: LogoffRevocationService, - private readonly loginService: LoginService, - private readonly refreshSessionService: RefreshSessionService, - private readonly urlService: UrlService, - private readonly authWellKnownService: AuthWellKnownService - ) {} - preloadAuthWellKnownDocument( configId?: string ): Observable { From 71a1dfa305a75e3d94e16abc7b24b1226aaa77a0 Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:09:48 +0100 Subject: [PATCH 05/10] Fix dependency injection in Angular Auth OIDC Client --- .../auth-well-known-data.service.ts | 9 ++++--- .../implicit-flow-callback-handler.service.ts | 15 ++++++------ .../iframe/refresh-session-iframe.service.ts | 24 +++++++++---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known-data.service.ts b/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known-data.service.ts index 48115bdd..d29549ec 100644 --- a/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known-data.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known-data.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { map, retry } from 'rxjs/operators'; import { DataService } from '../../api/data.service'; @@ -10,10 +10,9 @@ const WELL_KNOWN_SUFFIX = `/.well-known/openid-configuration`; @Injectable({ providedIn: 'root' }) export class AuthWellKnownDataService { - constructor( - private readonly http: DataService, - private readonly loggerService: LoggerService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly http = inject(DataService); getWellKnownEndPointsForConfig( config: OpenIdConfiguration diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/implicit-flow-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/implicit-flow-callback-handler.service.ts index e88694c6..475187e3 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/implicit-flow-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/implicit-flow-callback-handler.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of } from 'rxjs'; import { OpenIdConfiguration } from '../../config/openid-configuration'; import { LoggerService } from '../../logging/logger.service'; @@ -9,12 +9,13 @@ import { ResetAuthDataService } from '../reset-auth-data.service'; @Injectable({ providedIn: 'root' }) export class ImplicitFlowCallbackHandlerService { - constructor( - private readonly resetAuthDataService: ResetAuthDataService, - private readonly loggerService: LoggerService, - private readonly flowsDataService: FlowsDataService, - @Inject(DOCUMENT) private readonly document: Document - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly document = inject(DOCUMENT); // STEP 1 Code Flow // STEP 1 Implicit Flow diff --git a/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts b/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts index eb473a72..a084613c 100644 --- a/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts @@ -1,5 +1,4 @@ -import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core'; +import { Injectable, RendererFactory2, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { switchMap } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -9,17 +8,18 @@ import { SilentRenewService } from './silent-renew.service'; @Injectable({ providedIn: 'root' }) export class RefreshSessionIframeService { - private readonly renderer: Renderer2; + private readonly renderer = inject(RendererFactory2).createRenderer( + null, + null + ); - constructor( - @Inject(DOCUMENT) private readonly document: Document, - private readonly loggerService: LoggerService, - private readonly urlService: UrlService, - private readonly silentRenewService: SilentRenewService, - rendererFactory: RendererFactory2 - ) { - this.renderer = rendererFactory.createRenderer(null, null); - } + private readonly loggerService = inject(LoggerService); + + private readonly urlService = inject(UrlService); + + private readonly silentRenewService = inject(SilentRenewService); + + private readonly document = inject(Document); refreshSessionWithIframe( config: OpenIdConfiguration, From 5e7257d47d811b24728bc674e3dea4e4d4115d9d Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:14:00 +0100 Subject: [PATCH 06/10] Update dependency injection in services --- .../auto-login/auto-login-all-routes.guard.ts | 16 ++++--- .../src/lib/auto-login/auto-login.service.ts | 9 ++-- .../periodically-token-check.service.ts | 45 ++++++++++++------- .../refresh-session-refresh-token.service.ts | 15 ++++--- .../src/lib/config/config.service.ts | 28 +++++++----- .../src/lib/flows/flows-data.service.ts | 14 +++--- 6 files changed, 76 insertions(+), 51 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts index dc069578..79dd8001 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts @@ -17,13 +17,15 @@ import { AutoLoginService } from './auto-login.service'; */ @Injectable({ providedIn: 'root' }) export class AutoLoginAllRoutesGuard { - constructor( - private readonly autoLoginService: AutoLoginService, - private readonly checkAuthService: CheckAuthService, - private readonly loginService: LoginService, - private readonly configurationService: ConfigurationService, - private readonly router: Router - ) {} + private readonly autoLoginService = inject(AutoLoginService); + + private readonly checkAuthService = inject(CheckAuthService); + + private readonly loginService = inject(LoginService); + + private readonly configurationService = inject(ConfigurationService); + + private readonly router = inject(Router); canLoad(): Observable { const url = diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.service.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.service.ts index 0ed139f5..192cf4f5 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Router } from '@angular/router'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { StoragePersistenceService } from '../storage/storage-persistence.service'; @@ -7,10 +7,9 @@ const STORAGE_KEY = 'redirect'; @Injectable({ providedIn: 'root' }) export class AutoLoginService { - constructor( - private readonly storageService: StoragePersistenceService, - private readonly router: Router - ) {} + private readonly storageService = inject(StoragePersistenceService); + + private readonly router = inject(Router); checkSavedRedirectRouteAndNavigate(config: OpenIdConfiguration | null): void { if (!config) { diff --git a/projects/angular-auth-oidc-client/src/lib/callback/periodically-token-check.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/periodically-token-check.service.ts index 6ba61e91..907f14e5 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/periodically-token-check.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/periodically-token-check.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { forkJoin, Observable, of, throwError } from 'rxjs'; import { catchError, switchMap } from 'rxjs/operators'; import { AuthStateService } from '../auth-state/auth-state.service'; @@ -19,20 +19,35 @@ import { RefreshSessionRefreshTokenService } from './refresh-session-refresh-tok @Injectable({ providedIn: 'root' }) export class PeriodicallyTokenCheckService { - constructor( - private readonly resetAuthDataService: ResetAuthDataService, - private readonly flowHelper: FlowHelper, - private readonly flowsDataService: FlowsDataService, - private readonly loggerService: LoggerService, - private readonly userService: UserService, - private readonly authStateService: AuthStateService, - private readonly refreshSessionIframeService: RefreshSessionIframeService, - private readonly refreshSessionRefreshTokenService: RefreshSessionRefreshTokenService, - private readonly intervalService: IntervalService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly publicEventsService: PublicEventsService, - private readonly configurationService: ConfigurationService - ) {} + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly flowHelper = inject(FlowHelper); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly loggerService = inject(LoggerService); + + private readonly userService = inject(UserService); + + private readonly authStateService = inject(AuthStateService); + + private readonly refreshSessionIframeService = inject( + RefreshSessionIframeService + ); + + private readonly refreshSessionRefreshTokenService = inject( + RefreshSessionRefreshTokenService + ); + + private readonly intervalService = inject(IntervalService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly publicEventsService = inject(PublicEventsService); + + private readonly configurationService = inject(ConfigurationService); startTokenValidationPeriodically( allConfigs: OpenIdConfiguration[], diff --git a/projects/angular-auth-oidc-client/src/lib/callback/refresh-session-refresh-token.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/refresh-session-refresh-token.service.ts index 90949621..87867f16 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/refresh-session-refresh-token.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/refresh-session-refresh-token.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { catchError, finalize } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -10,12 +10,13 @@ import { IntervalService } from './interval.service'; @Injectable({ providedIn: 'root' }) export class RefreshSessionRefreshTokenService { - constructor( - private readonly loggerService: LoggerService, - private readonly resetAuthDataService: ResetAuthDataService, - private readonly flowsService: FlowsService, - private readonly intervalService: IntervalService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly flowsService = inject(FlowsService); + + private readonly intervalService = inject(IntervalService); refreshSessionWithRefreshTokens( config: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/config/config.service.ts b/projects/angular-auth-oidc-client/src/lib/config/config.service.ts index ba0934ac..cf1042a0 100644 --- a/projects/angular-auth-oidc-client/src/lib/config/config.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/config/config.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { forkJoin, Observable, of } from 'rxjs'; import { concatMap, map } from 'rxjs/operators'; import { LoggerService } from '../logging/logger.service'; @@ -14,17 +14,23 @@ import { ConfigValidationService } from './validation/config-validation.service' @Injectable({ providedIn: 'root' }) export class ConfigurationService { - private configsInternal: Record = {}; + private readonly loggerService = inject(LoggerService); + + private readonly publicEventsService = inject(PublicEventsService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly platformProvider = inject(PlatformProvider); - constructor( - private readonly loggerService: LoggerService, - private readonly publicEventsService: PublicEventsService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly configValidationService: ConfigValidationService, - private readonly platformProvider: PlatformProvider, - private readonly authWellKnownService: AuthWellKnownService, - private readonly loader: StsConfigLoader - ) {} + private readonly authWellKnownService = inject(AuthWellKnownService); + + private readonly loader = inject(StsConfigLoader); + + private readonly configValidationService = inject(ConfigValidationService); + + private configsInternal: Record = {}; hasManyConfigs(): boolean { return Object.keys(this.configsInternal).length > 1; diff --git a/projects/angular-auth-oidc-client/src/lib/flows/flows-data.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/flows-data.service.ts index 999c784b..b2a68666 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/flows-data.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/flows-data.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { LoggerService } from '../logging/logger.service'; import { StoragePersistenceService } from '../storage/storage-persistence.service'; @@ -7,11 +7,13 @@ import { RandomService } from './random/random.service'; @Injectable({ providedIn: 'root' }) export class FlowsDataService { - constructor( - private readonly storagePersistenceService: StoragePersistenceService, - private readonly randomService: RandomService, - private readonly loggerService: LoggerService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly randomService = inject(RandomService); createNonce(configuration: OpenIdConfiguration): string { const nonce = this.randomService.createRandom(40, configuration); From 2e422447ddc784c68fcb8b48d1d29f06d41189ab Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:18:59 +0100 Subject: [PATCH 07/10] Fix injection issue and update code formatting --- README.md | 4 +-- .../docs/documentation/auto-login.md | 36 ++++++++++++++----- .../docs/documentation/guards.md | 16 +++++++-- .../docs/documentation/login-logout.md | 5 +-- .../docs/documentation/public-events.md | 4 ++- .../angular-auth-oidc-client/docs/intro.md | 2 +- 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 62cc3c60..d3c911f5 100644 --- a/README.md +++ b/README.md @@ -112,14 +112,14 @@ export class AppModule {} And call the method `checkAuth()` from your `app.component.ts`. The method `checkAuth()` is needed to process the redirect from your Security Token Service and set the correct states. This method must be used to ensure the correct functioning of the library. ```ts -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, inject } from '@angular/core'; import { OidcSecurityService } from 'angular-auth-oidc-client'; @Component({ /*...*/ }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit() { this.oidcSecurityService diff --git a/docs/site/angular-auth-oidc-client/docs/documentation/auto-login.md b/docs/site/angular-auth-oidc-client/docs/documentation/auto-login.md index e641ce8b..464221b2 100644 --- a/docs/site/angular-auth-oidc-client/docs/documentation/auto-login.md +++ b/docs/site/angular-auth-oidc-client/docs/documentation/auto-login.md @@ -27,10 +27,15 @@ import { AutoLoginPartialRoutesGuard } from 'angular-auth-oidc-client'; const routes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'home' }, { path: 'home', component: HomeComponent }, - { path: 'protected', component: ProtectedComponent, canActivate: [AutoLoginPartialRoutesGuard] }, + { + path: 'protected', + component: ProtectedComponent, + canActivate: [AutoLoginPartialRoutesGuard], + }, { path: 'customers', - loadChildren: () => import('./customers/customers.module').then((m) => m.CustomersModule), + loadChildren: () => + import('./customers/customers.module').then((m) => m.CustomersModule), canLoad: [AutoLoginPartialRoutesGuard], }, { path: 'unauthorized', component: UnauthorizedComponent }, @@ -42,12 +47,14 @@ Please make sure to call `checkAuth()` like normal in your `app.component.ts`. ```ts export class AppComponent implements OnInit { - constructor(private oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit() { - this.oidcSecurityService.checkAuth().subscribe(({ isAuthenticated, userData, accessToken }) => { - // ... - }); + this.oidcSecurityService + .checkAuth() + .subscribe(({ isAuthenticated, userData, accessToken }) => { + // ... + }); } } ``` @@ -69,20 +76,31 @@ import { AutoLoginPartialRoutesGuard } from 'angular-auth-oidc-client'; const routes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'home' }, - { path: 'home', component: HomeComponent, canActivate: [AutoLoginPartialRoutesGuard] }, + { + path: 'home', + component: HomeComponent, + canActivate: [AutoLoginPartialRoutesGuard], + }, { path: 'callback', component: CallbackComponent }, // does nothing but setting up auth ]; ``` + ### Custom Params for the guard -If you need to pass custom params to the login request you can simply use the [data](https://angular.io/api/router/Route#data) attribute of the route. +If you need to pass custom params to the login request you can simply use the [data](https://angular.io/api/router/Route#data) attribute of the route. These parameters will then be appended to the login request + ```ts import { AutoLoginPartialRoutesGuard } from 'angular-auth-oidc-client'; const routes: Routes = [ { path: '', pathMatch: 'full', redirectTo: 'home' }, - { path: 'home', component: HomeComponent, canActivate: [AutoLoginPartialRoutesGuard], data:{custom:'param'} }, + { + path: 'home', + component: HomeComponent, + canActivate: [AutoLoginPartialRoutesGuard], + data: { custom: 'param' }, + }, { path: 'callback', component: CallbackComponent }, // does nothing but setting up auth ]; ``` diff --git a/docs/site/angular-auth-oidc-client/docs/documentation/guards.md b/docs/site/angular-auth-oidc-client/docs/documentation/guards.md index b708b17a..bfcdd9c3 100644 --- a/docs/site/angular-auth-oidc-client/docs/documentation/guards.md +++ b/docs/site/angular-auth-oidc-client/docs/documentation/guards.md @@ -13,16 +13,26 @@ Please refer to the auto-login guard in this repo as a reference. It is importan ```ts import { Injectable } from '@angular/core'; -import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; +import { + ActivatedRouteSnapshot, + CanActivate, + Router, + RouterStateSnapshot, + UrlTree, +} from '@angular/router'; import { OidcSecurityService } from 'angular-auth-oidc-client'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class AuthorizationGuard implements CanActivate { - constructor(private oidcSecurityService: OidcSecurityService, private router: Router) {} + private readonly oidcSecurityService = inject(OidcSecurityService); + private readonly router = inject(Router); - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { + canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): Observable { return this.oidcSecurityService.isAuthenticated$.pipe( take(1), map(({ isAuthenticated }) => { diff --git a/docs/site/angular-auth-oidc-client/docs/documentation/login-logout.md b/docs/site/angular-auth-oidc-client/docs/documentation/login-logout.md index 0e2fc77c..ad03f443 100644 --- a/docs/site/angular-auth-oidc-client/docs/documentation/login-logout.md +++ b/docs/site/angular-auth-oidc-client/docs/documentation/login-logout.md @@ -10,7 +10,7 @@ sidebar_position: 3 For logging in a user you can call the `authorize()` method: ```ts -constructor(private oidcSecurityService: OidcSecurityService) {} +private readonly oidcSecurityService = inject(OidcSecurityService); // ... this.oidcSecurityService.authorize(); @@ -126,7 +126,8 @@ A simplified page (instead of the application url) can be used. Here's an exampl - Transmitting authentication result ... (this popup will be closed automatically). + Transmitting authentication result ... (this popup will be closed + automatically). ``` diff --git a/docs/site/angular-auth-oidc-client/docs/documentation/public-events.md b/docs/site/angular-auth-oidc-client/docs/documentation/public-events.md index 268f773c..50a4a5b1 100644 --- a/docs/site/angular-auth-oidc-client/docs/documentation/public-events.md +++ b/docs/site/angular-auth-oidc-client/docs/documentation/public-events.md @@ -37,7 +37,9 @@ Using the `filter` operator from RxJS you can decide which events you are intere ```ts import { PublicEventsService } from 'angular-auth-oidc-client'; -constructor(private eventService: PublicEventsService) { +private readonly eventService = inject(PublicEventsService); + +ngOnInit() { this.eventService .registerForEvents() .pipe(filter((notification) => notification.type === EventTypes.CheckSessionReceived)) diff --git a/docs/site/angular-auth-oidc-client/docs/intro.md b/docs/site/angular-auth-oidc-client/docs/intro.md index 68a0a14d..60bf6f59 100644 --- a/docs/site/angular-auth-oidc-client/docs/intro.md +++ b/docs/site/angular-auth-oidc-client/docs/intro.md @@ -72,7 +72,7 @@ import { OidcSecurityService } from 'angular-auth-oidc-client'; /* ... */ }) export class AppComponent implements OnInit { - constructor(public oidcSecurityService: OidcSecurityService) {} + private readonly oidcSecurityService = inject(OidcSecurityService); ngOnInit() { this.oidcSecurityService.checkAuth().subscribe(({ isAuthenticated, userData}) => /* ... */); From 6dafbeb70816a9d641c6e58c6754dc28be504838 Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:25:14 +0100 Subject: [PATCH 08/10] Refactor dependency injection in several services --- .../src/lib/callback/callback.service.ts | 19 +++++----- .../callback/code-flow-callback.service.ts | 15 ++++---- .../implicit-flow-callback.service.ts | 13 +++---- .../auth-well-known.service.ts | 14 ++++---- .../validation/config-validation.service.ts | 2 +- .../code-flow-callback-handler.service.ts | 23 +++++++----- ...story-jwt-keys-callback-handler.service.ts | 26 ++++++++------ ...efresh-session-callback-handler.service.ts | 10 +++--- .../refresh-token-callback-handler.service.ts | 17 +++++---- ...ate-validation-callback-handler.service.ts | 18 +++++----- .../user-callback-handler.service.ts | 18 +++++----- .../src/lib/flows/flows.service.ts | 36 +++++++++++++------ .../src/lib/flows/random/random.service.ts | 7 ++-- .../src/lib/flows/reset-auth-data.service.ts | 15 ++++---- .../src/lib/flows/signin-key-data.service.ts | 14 ++++---- 15 files changed, 145 insertions(+), 102 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/callback/callback.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/callback.service.ts index 5f90d7b8..160a6f7a 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/callback.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/callback.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { tap } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -10,19 +10,22 @@ import { ImplicitFlowCallbackService } from './implicit-flow-callback.service'; @Injectable({ providedIn: 'root' }) export class CallbackService { + private readonly urlService = inject(UrlService); + + private readonly flowHelper = inject(FlowHelper); + + private readonly implicitFlowCallbackService = inject( + ImplicitFlowCallbackService + ); + + private readonly codeFlowCallbackService = inject(CodeFlowCallbackService); + private readonly stsCallbackInternal$ = new Subject(); get stsCallback$(): Observable { return this.stsCallbackInternal$.asObservable(); } - constructor( - private readonly urlService: UrlService, - private readonly flowHelper: FlowHelper, - private readonly implicitFlowCallbackService: ImplicitFlowCallbackService, - private readonly codeFlowCallbackService: CodeFlowCallbackService - ) {} - isCallback(currentUrl: string): boolean { if (!currentUrl) { return false; diff --git a/projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.ts index a1f2dca6..ecc9019e 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Router } from '@angular/router'; import { Observable, throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators'; @@ -10,12 +10,13 @@ import { IntervalService } from './interval.service'; @Injectable({ providedIn: 'root' }) export class CodeFlowCallbackService { - constructor( - private readonly flowsService: FlowsService, - private readonly flowsDataService: FlowsDataService, - private readonly intervalService: IntervalService, - private readonly router: Router - ) {} + private readonly flowsService = inject(FlowsService); + + private readonly router = inject(Router); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly intervalService = inject(IntervalService); authenticatedCallbackWithCode( urlToCheck: string, diff --git a/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts index d7a03828..c0e83fe1 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts @@ -10,12 +10,13 @@ import { IntervalService } from './interval.service'; @Injectable({ providedIn: 'root' }) export class ImplicitFlowCallbackService { - constructor( - private readonly flowsService: FlowsService, - private readonly router: Router, - private readonly flowsDataService: FlowsDataService, - private readonly intervalService: IntervalService - ) {} + private readonly flowsService = inject(FlowsService); + + private readonly router = inject(Router); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly intervalService = inject(IntervalService); authenticatedImplicitFlowCallback( config: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known.service.ts b/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known.service.ts index af861dad..575bf65a 100644 --- a/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/config/auth-well-known/auth-well-known.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators'; import { EventTypes } from '../../public-events/event-types'; @@ -10,11 +10,13 @@ import { AuthWellKnownEndpoints } from './auth-well-known-endpoints'; @Injectable({ providedIn: 'root' }) export class AuthWellKnownService { - constructor( - private readonly dataService: AuthWellKnownDataService, - private readonly publicEventsService: PublicEventsService, - private readonly storagePersistenceService: StoragePersistenceService - ) {} + private readonly dataService = inject(AuthWellKnownDataService); + + private readonly publicEventsService = inject(PublicEventsService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); storeWellKnownEndpoints( config: OpenIdConfiguration, diff --git a/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts b/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts index 0260c161..15196730 100644 --- a/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts @@ -6,7 +6,7 @@ import { allMultipleConfigRules, allRules } from './rules'; @Injectable({ providedIn: 'root' }) export class ConfigValidationService { - constructor(private readonly loggerService: LoggerService) {} + private readonly loggerService = inject(LoggerService); validateConfigs(passedConfigs: OpenIdConfiguration[]): boolean { return this.validateConfigsInternal( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/code-flow-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/code-flow-callback-handler.service.ts index 7664818e..3e29a7b4 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/code-flow-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/code-flow-callback-handler.service.ts @@ -1,5 +1,5 @@ import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError, timer } from 'rxjs'; import { catchError, mergeMap, retryWhen, switchMap } from 'rxjs/operators'; import { DataService } from '../../api/data.service'; @@ -13,14 +13,19 @@ import { FlowsDataService } from '../flows-data.service'; @Injectable({ providedIn: 'root' }) export class CodeFlowCallbackHandlerService { - constructor( - private readonly urlService: UrlService, - private readonly loggerService: LoggerService, - private readonly tokenValidationService: TokenValidationService, - private readonly flowsDataService: FlowsDataService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly dataService: DataService - ) {} + private readonly urlService = inject(UrlService); + + private readonly loggerService = inject(LoggerService); + + private readonly tokenValidationService = inject(TokenValidationService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly dataService = inject(DataService); // STEP 1 Code Flow codeFlowCallback( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/history-jwt-keys-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/history-jwt-keys-callback-handler.service.ts index a1a66b6e..ec15c158 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/history-jwt-keys-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/history-jwt-keys-callback-handler.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { catchError, switchMap, tap } from 'rxjs/operators'; import { AuthStateService } from '../../auth-state/auth-state.service'; @@ -17,15 +17,21 @@ const JWT_KEYS = 'jwtKeys'; @Injectable({ providedIn: 'root' }) export class HistoryJwtKeysCallbackHandlerService { - constructor( - private readonly loggerService: LoggerService, - private readonly authStateService: AuthStateService, - private readonly flowsDataService: FlowsDataService, - private readonly signInKeyDataService: SigninKeyDataService, - private readonly storagePersistenceService: StoragePersistenceService, - private readonly resetAuthDataService: ResetAuthDataService, - @Inject(DOCUMENT) private readonly document: Document - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly authStateService = inject(AuthStateService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly signInKeyDataService = inject(SigninKeyDataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly document = inject(DOCUMENT); // STEP 3 Code Flow, STEP 2 Implicit Flow, STEP 3 Refresh Token callbackHistoryAndResetJwtKeys( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts index dd667c31..dd095e8d 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts @@ -9,11 +9,11 @@ import { FlowsDataService } from '../flows-data.service'; @Injectable({ providedIn: 'root' }) export class RefreshSessionCallbackHandlerService { - constructor( - private readonly loggerService: LoggerService, - private readonly authStateService: AuthStateService, - private readonly flowsDataService: FlowsDataService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly authStateService = inject(AuthStateService); + + private readonly flowsDataService = inject(FlowsDataService); // STEP 1 Refresh session refreshSessionWithRefreshTokens( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-token-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-token-callback-handler.service.ts index 1c1772a6..a24311c2 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-token-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-token-callback-handler.service.ts @@ -1,5 +1,5 @@ import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError, timer } from 'rxjs'; import { catchError, mergeMap, retryWhen, switchMap } from 'rxjs/operators'; import { DataService } from '../../api/data.service'; @@ -11,12 +11,15 @@ import { AuthResult, CallbackContext } from '../callback-context'; @Injectable({ providedIn: 'root' }) export class RefreshTokenCallbackHandlerService { - constructor( - private readonly urlService: UrlService, - private readonly loggerService: LoggerService, - private readonly dataService: DataService, - private readonly storagePersistenceService: StoragePersistenceService - ) {} + private readonly urlService = inject(UrlService); + + private readonly loggerService = inject(LoggerService); + + private readonly dataService = inject(DataService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); // STEP 2 Refresh Token refreshTokensRequestTokens( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/state-validation-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/state-validation-callback-handler.service.ts index 8e2220d6..a9e3c14d 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/state-validation-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/state-validation-callback-handler.service.ts @@ -1,5 +1,5 @@ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { AuthStateService } from '../../auth-state/auth-state.service'; @@ -12,13 +12,15 @@ import { ResetAuthDataService } from '../reset-auth-data.service'; @Injectable({ providedIn: 'root' }) export class StateValidationCallbackHandlerService { - constructor( - private readonly loggerService: LoggerService, - private readonly stateValidationService: StateValidationService, - private readonly authStateService: AuthStateService, - private readonly resetAuthDataService: ResetAuthDataService, - @Inject(DOCUMENT) private readonly document: Document - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly stateValidationService = inject(StateValidationService); + + private readonly authStateService = inject(AuthStateService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); + + private readonly document = inject(DOCUMENT); // STEP 4 All flows callbackStateValidation( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/user-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/user-callback-handler.service.ts index 5ca190d4..d5014d7e 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/user-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/user-callback-handler.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { catchError, switchMap } from 'rxjs/operators'; import { AuthStateService } from '../../auth-state/auth-state.service'; @@ -12,13 +12,15 @@ import { ResetAuthDataService } from '../reset-auth-data.service'; @Injectable({ providedIn: 'root' }) export class UserCallbackHandlerService { - constructor( - private readonly loggerService: LoggerService, - private readonly authStateService: AuthStateService, - private readonly flowsDataService: FlowsDataService, - private readonly userService: UserService, - private readonly resetAuthDataService: ResetAuthDataService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly authStateService = inject(AuthStateService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly userService = inject(UserService); + + private readonly resetAuthDataService = inject(ResetAuthDataService); // STEP 5 userData callbackUser( diff --git a/projects/angular-auth-oidc-client/src/lib/flows/flows.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/flows.service.ts index 706b0ff6..9f097631 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/flows.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/flows.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { concatMap } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; @@ -13,15 +13,31 @@ import { UserCallbackHandlerService } from './callback-handling/user-callback-ha @Injectable({ providedIn: 'root' }) export class FlowsService { - constructor( - private readonly codeFlowCallbackHandlerService: CodeFlowCallbackHandlerService, - private readonly implicitFlowCallbackHandlerService: ImplicitFlowCallbackHandlerService, - private readonly historyJwtKeysCallbackHandlerService: HistoryJwtKeysCallbackHandlerService, - private readonly userHandlerService: UserCallbackHandlerService, - private readonly stateValidationCallbackHandlerService: StateValidationCallbackHandlerService, - private readonly refreshSessionCallbackHandlerService: RefreshSessionCallbackHandlerService, - private readonly refreshTokenCallbackHandlerService: RefreshTokenCallbackHandlerService - ) {} + private readonly codeFlowCallbackHandlerService = inject( + CodeFlowCallbackHandlerService + ); + + private readonly implicitFlowCallbackHandlerService = inject( + ImplicitFlowCallbackHandlerService + ); + + private readonly historyJwtKeysCallbackHandlerService = inject( + HistoryJwtKeysCallbackHandlerService + ); + + private readonly userHandlerService = inject(UserCallbackHandlerService); + + private readonly stateValidationCallbackHandlerService = inject( + StateValidationCallbackHandlerService + ); + + private readonly refreshSessionCallbackHandlerService = inject( + RefreshSessionCallbackHandlerService + ); + + private readonly refreshTokenCallbackHandlerService = inject( + RefreshTokenCallbackHandlerService + ); processCodeFlowCallback( urlToCheck: string, diff --git a/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts index 34a83fca..bc74a8fc 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts @@ -5,10 +5,9 @@ import { CryptoService } from '../../utils/crypto/crypto.service'; @Injectable({ providedIn: 'root' }) export class RandomService { - constructor( - private readonly cryptoService: CryptoService, - private readonly loggerService: LoggerService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly cryptoService = inject(CryptoService); createRandom( requiredLength: number, diff --git a/projects/angular-auth-oidc-client/src/lib/flows/reset-auth-data.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/reset-auth-data.service.ts index d43c9fdc..a081351e 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/reset-auth-data.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/reset-auth-data.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { AuthStateService } from '../auth-state/auth-state.service'; import { OpenIdConfiguration } from '../config/openid-configuration'; import { LoggerService } from '../logging/logger.service'; @@ -7,12 +7,13 @@ import { FlowsDataService } from './flows-data.service'; @Injectable({ providedIn: 'root' }) export class ResetAuthDataService { - constructor( - private readonly authStateService: AuthStateService, - private readonly flowsDataService: FlowsDataService, - private readonly userService: UserService, - private readonly loggerService: LoggerService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly userService = inject(UserService); + + private readonly flowsDataService = inject(FlowsDataService); + + private readonly authStateService = inject(AuthStateService); resetAuthorizationData( currentConfiguration: OpenIdConfiguration | null, diff --git a/projects/angular-auth-oidc-client/src/lib/flows/signin-key-data.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/signin-key-data.service.ts index df80eab2..0462af31 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/signin-key-data.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/signin-key-data.service.ts @@ -1,5 +1,5 @@ import { HttpResponse } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, throwError } from 'rxjs'; import { catchError, retry } from 'rxjs/operators'; import { DataService } from '../api/data.service'; @@ -10,11 +10,13 @@ import { JwtKeys } from '../validation/jwtkeys'; @Injectable({ providedIn: 'root' }) export class SigninKeyDataService { - constructor( - private readonly storagePersistenceService: StoragePersistenceService, - private readonly loggerService: LoggerService, - private readonly dataService: DataService - ) {} + private readonly loggerService = inject(LoggerService); + + private readonly storagePersistenceService = inject( + StoragePersistenceService + ); + + private readonly dataService = inject(DataService); getSigningKeys( currentConfiguration: OpenIdConfiguration From 29d3c7d43d6ac35984ac7160682344876a49623c Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:26:47 +0100 Subject: [PATCH 09/10] Update Angular dependencies --- .../src/lib/auth-state/auth-state.service.ts | 2 +- .../src/lib/auth-state/check-auth.service.ts | 2 +- .../src/lib/auto-login/auto-login-all-routes.guard.ts | 2 +- .../src/lib/callback/implicit-flow-callback.service.ts | 2 +- .../src/lib/config/validation/config-validation.service.ts | 2 +- .../refresh-session-callback-handler.service.ts | 2 +- .../src/lib/flows/random/random.service.ts | 2 +- .../src/lib/iframe/refresh-session-iframe.service.ts | 3 ++- 8 files changed, 9 insertions(+), 8 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts b/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts index cf183d80..29a1e56c 100644 --- a/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/auth-state/auth-state.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { BehaviorSubject, Observable, throwError } from 'rxjs'; import { distinctUntilChanged } from 'rxjs/operators'; import { OpenIdConfiguration } from '../config/openid-configuration'; diff --git a/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts b/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts index 24a40e10..7fee5de5 100644 --- a/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/auth-state/check-auth.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { forkJoin, Observable, of, throwError } from 'rxjs'; import { catchError, map, switchMap, tap } from 'rxjs/operators'; import { AutoLoginService } from '../auto-login/auto-login.service'; diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts index 79dd8001..7b4eae9b 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-all-routes.guard.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { ActivatedRouteSnapshot, Router, diff --git a/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts b/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts index c0e83fe1..6b6b1930 100644 --- a/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Router } from '@angular/router'; import { Observable, throwError } from 'rxjs'; import { catchError, tap } from 'rxjs/operators'; diff --git a/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts b/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts index 15196730..5abbd2cb 100644 --- a/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/config/validation/config-validation.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { LoggerService } from '../../logging/logger.service'; import { OpenIdConfiguration } from '../openid-configuration'; import { Level, RuleValidationResult } from './rule'; diff --git a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts index dd095e8d..e306f165 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/callback-handling/refresh-session-callback-handler.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { AuthStateService } from '../../auth-state/auth-state.service'; import { OpenIdConfiguration } from '../../config/openid-configuration'; diff --git a/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts b/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts index bc74a8fc..28c329bb 100644 --- a/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/flows/random/random.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { OpenIdConfiguration } from '../../config/openid-configuration'; import { LoggerService } from '../../logging/logger.service'; import { CryptoService } from '../../utils/crypto/crypto.service'; diff --git a/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts b/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts index a084613c..1b920172 100644 --- a/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts +++ b/projects/angular-auth-oidc-client/src/lib/iframe/refresh-session-iframe.service.ts @@ -1,3 +1,4 @@ +import { DOCUMENT } from '@angular/common'; import { Injectable, RendererFactory2, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { switchMap } from 'rxjs/operators'; @@ -19,7 +20,7 @@ export class RefreshSessionIframeService { private readonly silentRenewService = inject(SilentRenewService); - private readonly document = inject(Document); + private readonly document = inject(DOCUMENT); refreshSessionWithIframe( config: OpenIdConfiguration, From 04f002ad08c1308bbaab12bfb3d9f2dd82bd6c5b Mon Sep 17 00:00:00 2001 From: FabianGosebrink Date: Sat, 23 Mar 2024 14:28:25 +0100 Subject: [PATCH 10/10] Refactor AutoLoginPartialRoutesGuard to use dependency injection --- .../auto-login-partial-routes.guard.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts index e6a700b2..db84e9de 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts @@ -14,13 +14,15 @@ import { AutoLoginService } from './auto-login.service'; @Injectable({ providedIn: 'root' }) export class AutoLoginPartialRoutesGuard { - constructor( - private readonly autoLoginService: AutoLoginService, - private readonly authStateService: AuthStateService, - private readonly loginService: LoginService, - private readonly configurationService: ConfigurationService, - private readonly router: Router - ) {} + private readonly autoLoginService = inject(AutoLoginService); + + private readonly authStateService = inject(AuthStateService); + + private readonly loginService = inject(LoginService); + + private readonly configurationService = inject(ConfigurationService); + + private readonly router = inject(Router); canLoad(): Observable { const url =