From 13aafe3fae5bee57083e567048f96d127b6c5e4a Mon Sep 17 00:00:00 2001 From: Shu Muto Date: Fri, 12 Jun 2020 16:59:55 +0900 Subject: [PATCH] Save login mode into cookie (#5246) And use it next time if it exists. --- src/app/frontend/index.config.ts | 2 ++ src/app/frontend/login/component.spec.ts | 8 +++++++- src/app/frontend/login/component.ts | 25 +++++++++++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/app/frontend/index.config.ts b/src/app/frontend/index.config.ts index e2135cccc..0e2650bbb 100644 --- a/src/app/frontend/index.config.ts +++ b/src/app/frontend/index.config.ts @@ -23,6 +23,7 @@ export interface Config { csrfHeaderName: string; authTokenHeaderName: string; defaultNamespace: string; + authModeCookieName: string; } export const CONFIG: Config = { @@ -31,6 +32,7 @@ export const CONFIG: Config = { csrfHeaderName: 'X-CSRF-TOKEN', skipLoginPageCookieName: 'skipLoginPage', defaultNamespace: 'default', + authModeCookieName: 'authMode', }; // Override default material tooltip values. diff --git a/src/app/frontend/login/component.spec.ts b/src/app/frontend/login/component.spec.ts index 8dd96296d..a45c6d63d 100644 --- a/src/app/frontend/login/component.spec.ts +++ b/src/app/frontend/login/component.spec.ts @@ -13,7 +13,7 @@ // limitations under the License. import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; -import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; +import {CUSTOM_ELEMENTS_SCHEMA, InjectionToken} from '@angular/core'; import {async, ComponentFixture, TestBed} from '@angular/core/testing'; import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {MatButtonModule} from '@angular/material/button'; @@ -23,6 +23,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations'; import {ActivatedRoute, Router} from '@angular/router'; import {RouterTestingModule} from '@angular/router/testing'; import {EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi'; +import {Config, CONFIG_DI_TOKEN} from '../index.config'; import {K8SError, KdError} from 'common/errors/errors'; import {AuthService} from 'common/services/global/authentication'; import {from, Observable, of} from 'rxjs'; @@ -39,6 +40,7 @@ const queries = { }; const username = 'kubedude'; const password = 'supersecret'; +const MOCK_CONFIG_DI_TOKEN = new InjectionToken('kd.config'); class MockAuthService { login(loginSpec: LoginSpec): Observable { @@ -124,6 +126,10 @@ describe('LoginComponent', () => { provide: PluginsConfigService, useClass: MockPluginsConfigService, }, + { + provide: CONFIG_DI_TOKEN, + useValue: MOCK_CONFIG_DI_TOKEN, + }, ], }).compileComponents(); })); diff --git a/src/app/frontend/login/component.ts b/src/app/frontend/login/component.ts index f5539203a..bb25600e3 100644 --- a/src/app/frontend/login/component.ts +++ b/src/app/frontend/login/component.ts @@ -13,12 +13,14 @@ // limitations under the License. import {HttpClient, HttpErrorResponse} from '@angular/common/http'; -import {Component, NgZone, OnInit} from '@angular/core'; +import {Component, Inject, NgZone, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {AuthenticationMode, EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi'; import {KdError, KdFile, StateError} from '@api/frontendapi'; +import {CookieService} from 'ngx-cookie-service'; import {map} from 'rxjs/operators'; +import {Config, CONFIG_DI_TOKEN} from '../index.config'; import {AsKdError, K8SError} from '../common/errors/errors'; import {AuthService} from '../common/services/global/authentication'; import {PluginsConfigService} from '../common/services/global/plugin'; @@ -36,7 +38,7 @@ enum LoginModes { }) export class LoginComponent implements OnInit { loginModes = LoginModes; - selectedAuthenticationMode = LoginModes.Kubeconfig; + selectedAuthenticationMode = ''; errors: KdError[] = []; private enabledAuthenticationModes_: AuthenticationMode[] = []; @@ -48,20 +50,27 @@ export class LoginComponent implements OnInit { constructor( private readonly authService_: AuthService, + private readonly cookies_: CookieService, private readonly state_: Router, private readonly http_: HttpClient, private readonly ngZone_: NgZone, private readonly route_: ActivatedRoute, private readonly pluginConfigService_: PluginsConfigService, + @Inject(CONFIG_DI_TOKEN) private readonly CONFIG: Config, ) {} ngOnInit(): void { + this.selectedAuthenticationMode = + this.selectedAuthenticationMode || this.cookies_.get(this.CONFIG.authModeCookieName) || ''; + this.http_ .get('api/v1/login/modes') .subscribe((enabledModes: EnabledAuthenticationModes) => { this.enabledAuthenticationModes_ = enabledModes.modes; this.enabledAuthenticationModes_.push(LoginModes.Kubeconfig); - this.selectedAuthenticationMode = this.enabledAuthenticationModes_[0] as LoginModes; + this.selectedAuthenticationMode = this.selectedAuthenticationMode + ? (this.selectedAuthenticationMode as LoginModes) + : (this.enabledAuthenticationModes_[0] as LoginModes); }); this.http_ @@ -82,6 +91,16 @@ export class LoginComponent implements OnInit { } login(): void { + this.cookies_.set( + this.CONFIG.authModeCookieName, + this.selectedAuthenticationMode, + null, + null, + null, + false, + 'Strict', + ); + this.authService_.login(this.getLoginSpec_()).subscribe( (errors: K8SError[]) => { if (errors.length > 0) { -- GitLab