From 74b600e467a8f33007d5a645665f460b66d50439 Mon Sep 17 00:00:00 2001 From: Janelle Law Date: Thu, 7 Oct 2021 13:44:35 -0400 Subject: [PATCH 1/4] Get username from v2 auth resp Update auth version to 2.1 --- src/app/Login/BasicAuthForm.tsx | 1 - src/app/Shared/Services/Login.service.tsx | 41 ++++++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/app/Login/BasicAuthForm.tsx b/src/app/Login/BasicAuthForm.tsx index e36814710..e84e87c46 100644 --- a/src/app/Login/BasicAuthForm.tsx +++ b/src/app/Login/BasicAuthForm.tsx @@ -75,7 +75,6 @@ export const BasicAuthForm: React.FunctionComponent = (props) => { const handleSubmit = React.useCallback((evt) => { props.onSubmit(evt, `${username}:${password}`, 'Basic', rememberMe); - context.login.setUsername(username); }, [props, props.onSubmit, username, password, context.login, rememberMe]); // FIXME Patternfly Form component onSubmit is not triggered by Enter keydown when the Form contains diff --git a/src/app/Shared/Services/Login.service.tsx b/src/app/Shared/Services/Login.service.tsx index 9b285f1a5..7145ec297 100644 --- a/src/app/Shared/Services/Login.service.tsx +++ b/src/app/Shared/Services/Login.service.tsx @@ -37,7 +37,7 @@ */ import { Observable, ObservableInput, of, ReplaySubject } from 'rxjs'; import { fromFetch } from 'rxjs/fetch'; -import { catchError, first, map, tap } from 'rxjs/operators'; +import { catchError, concatMap, first, tap } from 'rxjs/operators'; export class LoginService { @@ -67,7 +67,7 @@ export class LoginService { token = this.useCacheItemIfAvailable(this.TOKEN_KEY, token); - return fromFetch(`${this.authority}/api/v1/auth`, { + return fromFetch(`${this.authority}/api/v2.1/auth`, { credentials: 'include', mode: 'cors', method: 'POST', @@ -75,27 +75,24 @@ export class LoginService { headers: this.getAuthHeaders(token, method), }) .pipe( - map(response => { + concatMap(response => { if (!this.authMethod.isStopped) { this.authMethod.next(response.ok ? method : (response.headers.get('X-WWW-Authenticate') || '')); } - return response.ok; + return response.json(); + }), + first(), + tap((response: any) => { + this.completeAuthMethod(method); + this.setUsername(response.data.result.username); + this.token.next(token); + this.authenticated.next(true); }), catchError((e: Error): ObservableInput => { window.console.error(JSON.stringify(e)); this.authMethod.complete(); return of(false); }), - first(), - tap(v => { - if (v) { - this.authMethod.next(method); - this.authMethod.complete(); - this.setCacheItem(this.METHOD_KEY, method); - this.token.next(token); - this.authenticated.next(true); - } - }) ); } @@ -137,11 +134,6 @@ export class LoginService { this.authenticated.next(false); } - setUsername(username: string): void { - this.setCacheItem(this.USER_KEY, username); - this.username.next(username); - } - rememberToken(token: string): void { this.setCacheItem(this.TOKEN_KEY, token); } @@ -150,6 +142,17 @@ export class LoginService { this.removeCacheItem(this.TOKEN_KEY); } + private setUsername(username: string): void { + this.setCacheItem(this.USER_KEY, username); + this.username.next(username); + } + + private completeAuthMethod(method: string): void { + this.authMethod.next(method); + this.setCacheItem(this.METHOD_KEY, method); + this.authMethod.complete(); + } + private getCacheItem(key: string): string { const item = sessionStorage.getItem(key); return (!!item) ? item : ''; From b0675f4a7daabf8616ad8cd00a83c601b78ed8a0 Mon Sep 17 00:00:00 2001 From: Janelle Law Date: Tue, 12 Oct 2021 14:33:36 -0400 Subject: [PATCH 2/4] fixup! Get username from v2 auth resp --- src/app/Shared/Services/Login.service.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/Shared/Services/Login.service.tsx b/src/app/Shared/Services/Login.service.tsx index 7145ec297..8fc25623c 100644 --- a/src/app/Shared/Services/Login.service.tsx +++ b/src/app/Shared/Services/Login.service.tsx @@ -83,10 +83,12 @@ export class LoginService { }), first(), tap((response: any) => { + if(response.ok) { this.completeAuthMethod(method); this.setUsername(response.data.result.username); this.token.next(token); this.authenticated.next(true); + } }), catchError((e: Error): ObservableInput => { window.console.error(JSON.stringify(e)); From e69b2f81d5d1523961243957ea703f0d92e43b3e Mon Sep 17 00:00:00 2001 From: Janelle Law Date: Wed, 13 Oct 2021 12:02:38 -0400 Subject: [PATCH 3/4] Add api response interface --- src/app/Shared/Services/Login.service.tsx | 32 ++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/app/Shared/Services/Login.service.tsx b/src/app/Shared/Services/Login.service.tsx index 8fc25623c..afc58849b 100644 --- a/src/app/Shared/Services/Login.service.tsx +++ b/src/app/Shared/Services/Login.service.tsx @@ -37,7 +37,7 @@ */ import { Observable, ObservableInput, of, ReplaySubject } from 'rxjs'; import { fromFetch } from 'rxjs/fetch'; -import { catchError, concatMap, first, tap } from 'rxjs/operators'; +import { catchError, concatMap, first, map, tap } from 'rxjs/operators'; export class LoginService { @@ -82,14 +82,17 @@ export class LoginService { return response.json(); }), first(), - tap((response: any) => { - if(response.ok) { + tap((jsonResp: AuthV2Response) => { + if(jsonResp.meta.status === 'OK') { this.completeAuthMethod(method); - this.setUsername(response.data.result.username); + this.setUsername(jsonResp.data.result.username); this.token.next(token); this.authenticated.next(true); } }), + map((jsonResp: AuthV2Response) => { + return jsonResp.meta.status === 'OK'; + }), catchError((e: Error): ObservableInput => { window.console.error(JSON.stringify(e)); this.authMethod.complete(); @@ -179,3 +182,24 @@ export class LoginService { } } + +interface ApiResponse { + meta: Meta; + data: Object; +} + +interface Meta { + status: string; + type: string; +} + +interface AuthV2Response extends ApiResponse { + data: AuthResult; +} +interface AuthResult { + result: Username; +} + +interface Username { + username: string; +} From 28398db037687ac880add9cb5f16006355e05902 Mon Sep 17 00:00:00 2001 From: Janelle Law Date: Wed, 13 Oct 2021 15:10:05 -0400 Subject: [PATCH 4/4] fixup! Add api response interface --- src/app/Shared/Services/Login.service.tsx | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/app/Shared/Services/Login.service.tsx b/src/app/Shared/Services/Login.service.tsx index afc58849b..a0cbda70f 100644 --- a/src/app/Shared/Services/Login.service.tsx +++ b/src/app/Shared/Services/Login.service.tsx @@ -194,12 +194,9 @@ interface Meta { } interface AuthV2Response extends ApiResponse { - data: AuthResult; -} -interface AuthResult { - result: Username; -} - -interface Username { - username: string; + data: { + result: { + username: string; + } + }; }