-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1545743
commit a36a5bc
Showing
28 changed files
with
448 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"name": "FinServe", | ||
"env": "Mocked Backend", | ||
"api": "http://localhost:3000/api" | ||
"api": "http://localhost:3000/api", | ||
"auth": { | ||
"api": "http://localhost:3000/api/auth/token" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"name": "FinServe", | ||
"env": "$env$", | ||
"api": "$api$" | ||
"api": "$api$", | ||
"auth": { | ||
"api": "$auth-api$" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AuthGuard } from './auth.guard'; | ||
|
||
describe('AuthGuard', () => { | ||
let guard: AuthGuard; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
guard = TestBed.inject(AuthGuard); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable, inject } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router'; | ||
import { Observable, tap } from 'rxjs'; | ||
import { AuthService } from '../services/auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AuthGuard implements CanActivate { | ||
|
||
private readonly router = inject(Router); | ||
|
||
private readonly authService = inject(AuthService); | ||
|
||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> | boolean { | ||
if (route.data?.['roles']) { | ||
return this.authService.hasRoles(route.data['roles']) | ||
.pipe( | ||
tap(value => { | ||
if (!value) { | ||
this.error('Access denied'); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private error(message: string): void { | ||
this.router.navigate(['/error'], { | ||
skipLocationChange: true, | ||
state: { | ||
message | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AuthInterceptor } from './auth.interceptor'; | ||
|
||
describe('AuthInterceptor', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
providers: [ | ||
AuthInterceptor | ||
] | ||
})); | ||
|
||
it('should be created', () => { | ||
const interceptor: AuthInterceptor = TestBed.inject(AuthInterceptor); | ||
expect(interceptor).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Injectable, inject } from '@angular/core'; | ||
import { | ||
HttpRequest, | ||
HttpHandler, | ||
HttpEvent, | ||
HttpInterceptor | ||
} from '@angular/common/http'; | ||
import { Observable, switchMap, take, tap } from 'rxjs'; | ||
import { AuthService } from '../services/auth.service'; | ||
import { APP_CONFIG } from '../model/app-config'; | ||
|
||
@Injectable() | ||
export class AuthInterceptor implements HttpInterceptor { | ||
|
||
private readonly appConfig = inject(APP_CONFIG); | ||
|
||
private readonly authService = inject(AuthService); | ||
|
||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
if (request.url === this.appConfig.auth.api) { | ||
|
||
return next.handle(request); | ||
} else { | ||
|
||
if (!this.authService.isTokenExpired()) { | ||
return next.handle(this.addAuthHeader(request)); | ||
} | ||
|
||
if (this.authService.isTokenRefreshing) { | ||
return this.authService.tokenRefreshed$ | ||
.pipe( | ||
take(1), | ||
switchMap(() => next.handle(this.addAuthHeader(request))) | ||
); | ||
} else { | ||
return this.authService.exchangeToken() | ||
.pipe( | ||
switchMap(() => next.handle(this.addAuthHeader(request))) | ||
); | ||
} | ||
} | ||
|
||
} | ||
|
||
private addAuthHeader(request: HttpRequest<unknown>): HttpRequest<unknown> { | ||
return request.clone({ | ||
setHeaders: { | ||
Authorization: 'Bearer ' + JSON.stringify(this.authService.token) | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,7 @@ export interface AppConfig { | |
name: string; | ||
env: string; | ||
api: string; | ||
auth: { | ||
api: string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { UserRole } from "./user-role.enum"; | ||
|
||
export interface AuthToken { | ||
name?: string; | ||
roles?: UserRole[]; | ||
expiresAt?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum UserRole { | ||
user = 'User', | ||
administrator = 'Administrator' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AppService } from './app.service'; | ||
|
||
describe('AppService', () => { | ||
let service: AppService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(AppService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.