未验证 提交 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 {
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.
......
......@@ -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<Config>('kd.config');
class MockAuthService {
login(loginSpec: LoginSpec): Observable<K8SError[]> {
......@@ -124,6 +126,10 @@ describe('LoginComponent', () => {
provide: PluginsConfigService,
useClass: MockPluginsConfigService,
},
{
provide: CONFIG_DI_TOKEN,
useValue: MOCK_CONFIG_DI_TOKEN,
},
],
}).compileComponents();
}));
......
......@@ -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<EnabledAuthenticationModes>('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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册