-
Notifications
You must be signed in to change notification settings - Fork 47
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
Showing
54 changed files
with
661 additions
and
276 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
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,14 +1,17 @@ | ||
import { HttpClient } from "@angular/common/http"; | ||
import { HttpClient, HttpContext } from "@angular/common/http"; | ||
import { Injectable } from "@angular/core"; | ||
import { Observable } from "rxjs"; | ||
import { BYPASS_TOKEN } from "../http/token.interceptor"; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
providedIn: 'root' | ||
}) | ||
export class ApiService { | ||
constructor(private client: HttpClient) { } | ||
constructor(private httpClient: HttpClient) { } | ||
|
||
getApi(): Observable<any> { | ||
return this.client.get<any>('/api'); | ||
} | ||
getApi(): Observable<any> { | ||
return this.httpClient.get<any>('/api', { | ||
context: new HttpContext().set(BYPASS_TOKEN, true) | ||
}) | ||
} | ||
} |
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
68 changes: 0 additions & 68 deletions
68
web-app/src/app/authentication/authentication.component.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 interface User { | ||
username: string | ||
displayName: 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 |
---|---|---|
@@ -1,29 +1,65 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpContextToken } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpContextToken, HttpStatusCode, HttpErrorResponse, HttpClient } from '@angular/common/http'; | ||
import { catchError, Observable, Subject, switchMap, throwError } from 'rxjs'; | ||
import { LocalStorageService } from './local-storage.service'; | ||
import { AuthenticationDialogComponent } from '../ingress/authentication/authentication-dialog.component'; | ||
import { MatDialog } from '@angular/material/dialog'; | ||
|
||
export const BYPASS_TOKEN = new HttpContextToken(() => false); | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TokenInterceptorService implements HttpInterceptor { | ||
isRefreshingToken: boolean = false | ||
tokenSubject: Subject<void> = new Subject<void>() | ||
|
||
constructor(private localStorageService: LocalStorageService) { } | ||
constructor( | ||
public dialog: MatDialog, | ||
private localStorageService: LocalStorageService | ||
) { } | ||
|
||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
if (req.context.get(BYPASS_TOKEN) === true) { | ||
const bypassToken = req.context.get(BYPASS_TOKEN) === true | ||
if (bypassToken) { | ||
return next.handle(req); | ||
} | ||
|
||
const token = this.localStorageService.getToken(); | ||
if (req.url.startsWith('/api/')) { | ||
return next.handle(this.tokenRequest(req)).pipe( | ||
catchError((error) => { | ||
if (error instanceof HttpErrorResponse) { | ||
if (error.status === HttpStatusCode.Unauthorized) { | ||
if (!this.isRefreshingToken) { | ||
this.isRefreshingToken = true | ||
this.dialog.open(AuthenticationDialogComponent, { | ||
width: '600px', | ||
disableClose: true, | ||
autoFocus: false | ||
}).afterClosed().subscribe(() => { | ||
this.isRefreshingToken = false | ||
this.tokenSubject.next() | ||
}) | ||
} | ||
|
||
return this.tokenSubject.pipe( | ||
switchMap(() => { | ||
return next.handle(this.tokenRequest(req)) | ||
}) | ||
) | ||
} | ||
} | ||
|
||
if (token && req.url.startsWith('/api/')) { | ||
const newReq = req.clone({ headers: req.headers.set('Authorization', `Bearer ${token}`) }); | ||
return next.handle(newReq) | ||
return throwError(() => error) | ||
}) | ||
) | ||
} else { | ||
return next.handle(req) | ||
} | ||
} | ||
|
||
tokenRequest(req: HttpRequest<any>): HttpRequest<any> { | ||
const token = this.localStorageService.getToken(); | ||
return req.clone({ headers: req.headers.set('Authorization', `Bearer ${token}`) }); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
web-app/src/app/ingress/authentication/authentication-dialog.component.html
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,8 @@ | ||
<div> | ||
<h3 class="mat-dialog-title">Please signin to continue to Mage</h3> | ||
<div class="mat-dialog-content"> | ||
<div class="ingress" > | ||
<ingress [landing]="false" [api]="api" (complete)="onIngress()"></ingress> | ||
</div> | ||
</div> | ||
</div> |
4 changes: 4 additions & 0 deletions
4
web-app/src/app/ingress/authentication/authentication-dialog.component.scss
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 @@ | ||
.ingress { | ||
width: 100%; | ||
height: 100%; | ||
} |
19 changes: 19 additions & 0 deletions
19
web-app/src/app/ingress/authentication/authentication-dialog.component.spec.ts
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,19 @@ | ||
import { TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { AuthorizeComponent } from '../../authentication/authorize.component'; | ||
import { AuthenticationDialogComponent } from './authentication-dialog.component'; | ||
|
||
describe('Authentication Dialog', () => { | ||
let component: AuthenticationDialogComponent; | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [AuthorizeComponent] | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
web-app/src/app/ingress/authentication/authentication-dialog.component.ts
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,28 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { ApiService } from '../../api/api.service'; | ||
import { Api } from '../../api/api.entity'; | ||
import { MatDialogRef } from '@angular/material/dialog'; | ||
|
||
@Component({ | ||
selector: 'authentication-dialog', | ||
templateUrl: 'authentication-dialog.component.html', | ||
styleUrls: ['./authentication-dialog.component.scss'] | ||
}) | ||
export class AuthenticationDialogComponent implements OnInit { | ||
api: Api | ||
|
||
constructor( | ||
private apiService: ApiService, | ||
public dialogRef: MatDialogRef<AuthenticationDialogComponent> | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.apiService.getApi().subscribe((api: Api) => { | ||
this.api = api | ||
}) | ||
} | ||
|
||
onIngress(): void { | ||
this.dialogRef.close() | ||
} | ||
} |
21 changes: 10 additions & 11 deletions
21
...hentication/authentication.component.html → ...hentication/authentication.component.html
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
Oops, something went wrong.