未验证 提交 13aafe3f 编写于 作者: S Shu Muto 提交者: GitHub

Save login mode into cookie (#5246)

And use it next time if it exists.
上级 fb340bd5
...@@ -23,6 +23,7 @@ export interface Config { ...@@ -23,6 +23,7 @@ export interface Config {
csrfHeaderName: string; csrfHeaderName: string;
authTokenHeaderName: string; authTokenHeaderName: string;
defaultNamespace: string; defaultNamespace: string;
authModeCookieName: string;
} }
export const CONFIG: Config = { export const CONFIG: Config = {
...@@ -31,6 +32,7 @@ export const CONFIG: Config = { ...@@ -31,6 +32,7 @@ export const CONFIG: Config = {
csrfHeaderName: 'X-CSRF-TOKEN', csrfHeaderName: 'X-CSRF-TOKEN',
skipLoginPageCookieName: 'skipLoginPage', skipLoginPageCookieName: 'skipLoginPage',
defaultNamespace: 'default', defaultNamespace: 'default',
authModeCookieName: 'authMode',
}; };
// Override default material tooltip values. // Override default material tooltip values.
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; 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 {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button'; import {MatButtonModule} from '@angular/material/button';
...@@ -23,6 +23,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations'; ...@@ -23,6 +23,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing'; import {RouterTestingModule} from '@angular/router/testing';
import {EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi'; import {EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi';
import {Config, CONFIG_DI_TOKEN} from '../index.config';
import {K8SError, KdError} from 'common/errors/errors'; import {K8SError, KdError} from 'common/errors/errors';
import {AuthService} from 'common/services/global/authentication'; import {AuthService} from 'common/services/global/authentication';
import {from, Observable, of} from 'rxjs'; import {from, Observable, of} from 'rxjs';
...@@ -39,6 +40,7 @@ const queries = { ...@@ -39,6 +40,7 @@ const queries = {
}; };
const username = 'kubedude'; const username = 'kubedude';
const password = 'supersecret'; const password = 'supersecret';
const MOCK_CONFIG_DI_TOKEN = new InjectionToken<Config>('kd.config');
class MockAuthService { class MockAuthService {
login(loginSpec: LoginSpec): Observable<K8SError[]> { login(loginSpec: LoginSpec): Observable<K8SError[]> {
...@@ -124,6 +126,10 @@ describe('LoginComponent', () => { ...@@ -124,6 +126,10 @@ describe('LoginComponent', () => {
provide: PluginsConfigService, provide: PluginsConfigService,
useClass: MockPluginsConfigService, useClass: MockPluginsConfigService,
}, },
{
provide: CONFIG_DI_TOKEN,
useValue: MOCK_CONFIG_DI_TOKEN,
},
], ],
}).compileComponents(); }).compileComponents();
})); }));
......
...@@ -13,12 +13,14 @@ ...@@ -13,12 +13,14 @@
// limitations under the License. // limitations under the License.
import {HttpClient, HttpErrorResponse} from '@angular/common/http'; 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 {ActivatedRoute, Router} from '@angular/router';
import {AuthenticationMode, EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi'; import {AuthenticationMode, EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi';
import {KdError, KdFile, StateError} from '@api/frontendapi'; import {KdError, KdFile, StateError} from '@api/frontendapi';
import {CookieService} from 'ngx-cookie-service';
import {map} from 'rxjs/operators'; import {map} from 'rxjs/operators';
import {Config, CONFIG_DI_TOKEN} from '../index.config';
import {AsKdError, K8SError} from '../common/errors/errors'; import {AsKdError, K8SError} from '../common/errors/errors';
import {AuthService} from '../common/services/global/authentication'; import {AuthService} from '../common/services/global/authentication';
import {PluginsConfigService} from '../common/services/global/plugin'; import {PluginsConfigService} from '../common/services/global/plugin';
...@@ -36,7 +38,7 @@ enum LoginModes { ...@@ -36,7 +38,7 @@ enum LoginModes {
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
loginModes = LoginModes; loginModes = LoginModes;
selectedAuthenticationMode = LoginModes.Kubeconfig; selectedAuthenticationMode = '';
errors: KdError[] = []; errors: KdError[] = [];
private enabledAuthenticationModes_: AuthenticationMode[] = []; private enabledAuthenticationModes_: AuthenticationMode[] = [];
...@@ -48,20 +50,27 @@ export class LoginComponent implements OnInit { ...@@ -48,20 +50,27 @@ export class LoginComponent implements OnInit {
constructor( constructor(
private readonly authService_: AuthService, private readonly authService_: AuthService,
private readonly cookies_: CookieService,
private readonly state_: Router, private readonly state_: Router,
private readonly http_: HttpClient, private readonly http_: HttpClient,
private readonly ngZone_: NgZone, private readonly ngZone_: NgZone,
private readonly route_: ActivatedRoute, private readonly route_: ActivatedRoute,
private readonly pluginConfigService_: PluginsConfigService, private readonly pluginConfigService_: PluginsConfigService,
@Inject(CONFIG_DI_TOKEN) private readonly CONFIG: Config,
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.selectedAuthenticationMode =
this.selectedAuthenticationMode || this.cookies_.get(this.CONFIG.authModeCookieName) || '';
this.http_ this.http_
.get<EnabledAuthenticationModes>('api/v1/login/modes') .get<EnabledAuthenticationModes>('api/v1/login/modes')
.subscribe((enabledModes: EnabledAuthenticationModes) => { .subscribe((enabledModes: EnabledAuthenticationModes) => {
this.enabledAuthenticationModes_ = enabledModes.modes; this.enabledAuthenticationModes_ = enabledModes.modes;
this.enabledAuthenticationModes_.push(LoginModes.Kubeconfig); 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_ this.http_
...@@ -82,6 +91,16 @@ export class LoginComponent implements OnInit { ...@@ -82,6 +91,16 @@ export class LoginComponent implements OnInit {
} }
login(): void { login(): void {
this.cookies_.set(
this.CONFIG.authModeCookieName,
this.selectedAuthenticationMode,
null,
null,
null,
false,
'Strict',
);
this.authService_.login(this.getLoginSpec_()).subscribe( this.authService_.login(this.getLoginSpec_()).subscribe(
(errors: K8SError[]) => { (errors: K8SError[]) => {
if (errors.length > 0) { if (errors.length > 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册