component.ts 4.7 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 17
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Component, NgZone, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
S
Shu Muto 已提交
18 19 20 21 22 23
import {
  AuthenticationMode,
  EnabledAuthenticationModes,
  LoginSkippableResponse,
  LoginSpec,
} from '@api/backendapi';
24 25
import { KdError, KdFile, StateError } from '@api/frontendapi';
import { map } from 'rxjs/operators';
S
Shu Muto 已提交
26

27
import { AsKdError, K8SError } from '../common/errors/errors';
S
Shu Muto 已提交
28
import { AuthService } from '../common/services/global/authentication';
29 30

enum LoginModes {
31 32 33
  Kubeconfig = 'kubeconfig',
  Basic = 'basic',
  Token = 'token',
34 35
}

S
Shu Muto 已提交
36 37 38 39 40
@Component({
  selector: 'kd-login',
  templateUrl: './template.html',
  styleUrls: ['./style.scss'],
})
41
export class LoginComponent implements OnInit {
42
  loginModes = LoginModes;
43
  selectedAuthenticationMode = LoginModes.Kubeconfig;
44
  errors: KdError[] = [];
45

46
  private enabledAuthenticationModes_: AuthenticationMode[] = [];
47
  private isLoginSkippable_ = false;
48 49 50 51
  private kubeconfig_: string;
  private token_: string;
  private username_: string;
  private password_: string;
52

53
  constructor(
S
Shu Muto 已提交
54
    private readonly authService_: AuthService,
55 56 57 58
    private readonly state_: Router,
    private readonly httpClient: HttpClient,
    private readonly ngZone_: NgZone,
    private readonly route_: ActivatedRoute
S
Shu Muto 已提交
59
  ) {}
60 61

  ngOnInit(): void {
S
Shu Muto 已提交
62 63 64 65 66 67 68 69 70 71 72
    this.httpClient
      .get<EnabledAuthenticationModes>('api/v1/login/modes')
      .subscribe((enabledModes: EnabledAuthenticationModes) => {
        this.enabledAuthenticationModes_ = enabledModes.modes;
      });

    this.httpClient
      .get<LoginSkippableResponse>('api/v1/login/skippable')
      .subscribe((loginSkippableResponse: LoginSkippableResponse) => {
        this.isLoginSkippable_ = loginSkippableResponse.skippable;
      });
73 74 75 76 77 78 79 80

    this.route_.paramMap
      .pipe(map(() => window.history.state))
      .subscribe((state: StateError) => {
        if (state.error) {
          this.errors = [state.error];
        }
      });
81 82 83
  }

  getEnabledAuthenticationModes(): AuthenticationMode[] {
S
Shu Muto 已提交
84 85 86 87
    if (
      this.enabledAuthenticationModes_.length > 0 &&
      this.enabledAuthenticationModes_.indexOf(LoginModes.Kubeconfig) < 0
    ) {
88 89 90 91 92
      // Push this option to the beginning of the list
      this.enabledAuthenticationModes_.splice(0, 0, LoginModes.Kubeconfig);
    }

    return this.enabledAuthenticationModes_;
93 94
  }

95
  login(): void {
96 97 98 99 100 101
    this.authService_.login(this.getLoginSpec_()).subscribe(
      (errors: K8SError[]) => {
        if (errors.length > 0) {
          this.errors = errors.map(error => error.toKdError());
          return;
        }
102

103 104 105 106 107 108 109 110
        this.ngZone_.run(() => {
          this.state_.navigate(['overview']);
        });
      },
      (err: HttpErrorResponse) => {
        this.errors = [AsKdError(err)];
      }
    );
111
  }
112 113 114

  skip(): void {
    this.authService_.skipLoginPage(true);
115
    this.state_.navigate(['overview']);
116
  }
117

118 119 120 121
  isSkipButtonEnabled(): boolean {
    return this.isLoginSkippable_;
  }

S
Shu Muto 已提交
122
  onChange(event: Event & KdFile): void {
123
    switch (this.selectedAuthenticationMode) {
S
Shu Muto 已提交
124
      case LoginModes.Kubeconfig:
125
        this.onFileLoad_(event as KdFile);
M
Marcin Maciaszczyk 已提交
126
        break;
S
Shu Muto 已提交
127
      case LoginModes.Token:
128
        this.token_ = (event.target as HTMLInputElement).value;
M
Marcin Maciaszczyk 已提交
129
        break;
S
Shu Muto 已提交
130
      case LoginModes.Basic:
131 132 133 134 135
        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 已提交
136
        break;
137 138 139 140 141 142 143 144 145 146
      default:
    }
  }

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

  private getLoginSpec_(): LoginSpec {
    switch (this.selectedAuthenticationMode) {
S
Shu Muto 已提交
147 148 149 150 151 152 153 154 155
      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;
156 157 158 159
      default:
        return {} as LoginSpec;
    }
  }
160
}