]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts
import ceph pacific 16.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / auth.service.ts
1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3 import { ActivatedRoute, Router } from '@angular/router';
4
5 import * as _ from 'lodash';
6 import { Observable } from 'rxjs';
7 import { tap } from 'rxjs/operators';
8
9 import { Credentials } from '../models/credentials';
10 import { LoginResponse } from '../models/login-response';
11 import { AuthStorageService } from '../services/auth-storage.service';
12
13 @Injectable({
14 providedIn: 'root'
15 })
16 export class AuthService {
17 constructor(
18 private authStorageService: AuthStorageService,
19 private http: HttpClient,
20 private router: Router,
21 private route: ActivatedRoute
22 ) {}
23
24 check(token: string) {
25 return this.http.post('api/auth/check', { token: token });
26 }
27
28 login(credentials: Credentials): Observable<LoginResponse> {
29 return this.http.post('api/auth', credentials).pipe(
30 tap((resp: LoginResponse) => {
31 this.authStorageService.set(
32 resp.username,
33 resp.permissions,
34 resp.sso,
35 resp.pwdExpirationDate,
36 resp.pwdUpdateRequired
37 );
38 })
39 );
40 }
41
42 logout(callback: Function = null) {
43 return this.http.post('api/auth/logout', null).subscribe((resp: any) => {
44 this.authStorageService.remove();
45 const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/login');
46 this.router.navigate([url], { skipLocationChange: true });
47 if (callback) {
48 callback();
49 }
50 window.location.replace(resp.redirect_url);
51 });
52 }
53 }