component.ts 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16
import {HttpClient} from '@angular/common/http';
import {Component, OnInit} from '@angular/core';
17
import {AuthenticationMode, EnabledAuthenticationModes, LoginSkippableResponse, LoginSpec} from '@api/backendapi';
18
import {KdFile} from '@api/frontendapi';
19
import {StateService} from '@uirouter/core';
S
Sebastian Florek 已提交
20

21
import {K8SError} from '../common/errors/errors';
S
Sebastian Florek 已提交
22
import {NAMESPACE_STATE_PARAM} from '../common/params/params';
23
import {AuthService} from '../common/services/global/authentication';
S
Sebastian Florek 已提交
24
import {CONFIG} from '../index.config';
25 26 27
import {overviewState} from '../overview/state';

enum LoginModes {
28 29 30
  Kubeconfig = 'kubeconfig',
  Basic = 'basic',
  Token = 'token',
31 32 33
}

@Component({selector: 'kd-login', templateUrl: './template.html', styleUrls: ['./style.scss']})
34
export class LoginComponent implements OnInit {
35
  loginModes = LoginModes;
36 37 38
  selectedAuthenticationMode = LoginModes.Kubeconfig;
  // TODO handle errors
  errors: K8SError[];
39

40
  private enabledAuthenticationModes_: AuthenticationMode[] = [];
41
  private isLoginSkippable_ = false;
42 43 44 45
  private kubeconfig_: string;
  private token_: string;
  private username_: string;
  private password_: string;
46

47 48 49 50 51 52 53 54 55
  constructor(
      private readonly authService_: AuthService, private readonly state_: StateService,
      private readonly httpClient: HttpClient) {}

  ngOnInit(): void {
    this.httpClient.get<EnabledAuthenticationModes>('api/v1/login/modes')
        .subscribe((enabledModes: EnabledAuthenticationModes) => {
          this.enabledAuthenticationModes_ = enabledModes.modes;
        });
56 57 58 59 60

    this.httpClient.get<LoginSkippableResponse>('api/v1/login/skippable')
        .subscribe((loginSkippableResponse: LoginSkippableResponse) => {
          this.isLoginSkippable_ = loginSkippableResponse.skippable;
        });
61 62 63 64 65 66 67 68 69 70
  }

  getEnabledAuthenticationModes(): AuthenticationMode[] {
    if (this.enabledAuthenticationModes_.length > 0 &&
        this.enabledAuthenticationModes_.indexOf(LoginModes.Kubeconfig) < 0) {
      // Push this option to the beginning of the list
      this.enabledAuthenticationModes_.splice(0, 0, LoginModes.Kubeconfig);
    }

    return this.enabledAuthenticationModes_;
71 72
  }

73 74 75 76 77 78 79
  login(): void {
    this.authService_.login(this.getLoginSpec_(), (errors: K8SError[]) => {
      if (errors.length > 0) {
        this.errors = errors;
        return;
      }

S
Sebastian Florek 已提交
80
      this.state_.go(overviewState.name, {[NAMESPACE_STATE_PARAM]: CONFIG.defaultNamespace});
81 82
    });
  }
83 84 85

  skip(): void {
    this.authService_.skipLoginPage(true);
S
Sebastian Florek 已提交
86
    this.state_.go(overviewState.name, {[NAMESPACE_STATE_PARAM]: CONFIG.defaultNamespace});
87
  }
88

89 90 91 92
  isSkipButtonEnabled(): boolean {
    return this.isLoginSkippable_;
  }

93 94 95 96
  onChange(event: Event&KdFile): void {
    switch (this.selectedAuthenticationMode) {
      case (LoginModes.Kubeconfig):
        this.onFileLoad_(event as KdFile);
M
Marcin Maciaszczyk 已提交
97
        break;
98 99
      case (LoginModes.Token):
        this.token_ = (event.target as HTMLInputElement).value;
M
Marcin Maciaszczyk 已提交
100
        break;
101 102 103 104 105 106
      case (LoginModes.Basic):
        if ((event.target as HTMLInputElement).id === 'username') {
          this.username_ = (event.target as HTMLInputElement).value;
        } else {
          this.password_ = (event.target as HTMLInputElement).value;
        }
M
Marcin Maciaszczyk 已提交
107
        break;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      default:
    }
  }

  private onFileLoad_(file: KdFile): void {
    this.kubeconfig_ = file.content;
  }

  private getLoginSpec_(): LoginSpec {
    switch (this.selectedAuthenticationMode) {
      case (LoginModes.Kubeconfig):
        return {kubeConfig: this.kubeconfig_} as LoginSpec;
      case (LoginModes.Token):
        return {token: this.token_} as LoginSpec;
      case (LoginModes.Basic):
        return {username: this.username_, password: this.password_} as LoginSpec;
      default:
        return {} as LoginSpec;
    }
  }
128
}