webin-authentication.service.ts 5.37 KB
Newer Older
Rasko Leinonen's avatar
Rasko Leinonen committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright 2018 EMBL - European Bioinformatics Institute
 * 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.
 */

12 13 14
// https://medium.com/@ryanchenkie_40935/angular-webin-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8

import { Injectable } from '@angular/core';
15
import { HttpClient, HttpHeaders } from '@angular/common/http';
Rasko Leinonen's avatar
Rasko Leinonen committed
16
import { Observable } from 'rxjs';
17
import { environment } from '../environments/environment';
18
import { WebinAuthenticationServiceInterface } from './webin-authentication.service.interface';
19
import { WebinAuthenticationResultInterface } from './webin-authentication-result.interface';
rajkumar's avatar
rajkumar committed
20
import { UtilService } from './util/Util-services';
21 22

@Injectable()
23
export class WebinAuthenticationService implements WebinAuthenticationServiceInterface {
24

25 26
  redirectUrl: string;

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  get username(): string {
    return sessionStorage.getItem('username');
  }
  set username(username: string) {
    sessionStorage.setItem('username', username);
  }

  get token(): string {
    return sessionStorage.getItem('token');
  }
  set token(token: string) {
    sessionStorage.setItem('token', token);
  }

  get authenticated(): boolean {
    if (sessionStorage['authenticated']) {
      return JSON.parse(sessionStorage.getItem('authenticated'));
    }
    return false;
  }
  set authenticated(authenticated: boolean) {
    sessionStorage.setItem('authenticated', JSON.stringify(authenticated));
  }

  get account(): string {
    return sessionStorage.getItem('account');
  }
  set account(account: string) {
    sessionStorage.setItem('account', account);
  }

  get ega(): boolean {
    return JSON.parse(sessionStorage.getItem('ega'));
  }
  set ega(ega: boolean) {
    sessionStorage.setItem('ega', JSON.stringify(ega));
  }

65
  get loginDate(): Date {
66 67
    return JSON.parse(sessionStorage.getItem('loginDate'));
  }
68
  set loginDate(loginDate: Date) {
69 70 71
    sessionStorage.setItem('loginDate', JSON.stringify(loginDate));
  }

72
  get logoutDate(): Date {
73 74
    return JSON.parse(sessionStorage.getItem('logoutDate'));
  }
75
  set logoutDate(logoutDate: Date) {
76 77
    sessionStorage.setItem('logoutDate', JSON.stringify(logoutDate));
  }
78

rajkumar's avatar
rajkumar committed
79

rajkumar's avatar
rajkumar committed
80 81

  constructor(private _http: HttpClient,
rajkumar's avatar
rajkumar committed
82
    private util: UtilService) { }
83

84
  /*
85
  getAuthorizationHeader() {
86 87
      console.log('** Webin authorization header **');
      return 'Basic ' + btoa(this.username + ':' + this.password);
88
  }
89
  */
90

91
  getAuthorizationTokenHeader() {
rajkumar's avatar
rajkumar committed
92 93
    //console.log('** Webin authorization token header **');
    return 'Bearer ' + this.token;
94 95
  }

96
  logout() {
97
    // console.log('logout');
98 99 100 101 102 103 104 105

    sessionStorage.removeItem('username');
    sessionStorage.removeItem('token');
    sessionStorage.removeItem('authenticated');
    sessionStorage.removeItem('account');
    sessionStorage.removeItem('ega');
    sessionStorage.removeItem('loginDate');
    sessionStorage.removeItem('logoutDate');
rajkumar's avatar
rajkumar committed
106
    sessionStorage.removeItem('submissionAccount');
rajkumar's avatar
rajkumar committed
107

108 109
  }

110
  login(username: string, password: string): Observable<WebinAuthenticationResultInterface> {
Rajkumar-D's avatar
Rajkumar-D committed
111
    const baseUrl: string = environment.webinAuthUrl + "/login";
112
    // console.log('Webin authentication login', baseUrl);
113

114
    this.username = username;
115
    const today = new Date();
116
    this.loginDate = today;
117
    this.logoutDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7);
118

rajkumar's avatar
rajkumar committed
119
    const body = { authRealms: ['ENA', 'EGA'], password: password, username: this.username };
120
    const headers: HttpHeaders = new HttpHeaders()
121 122 123
      .append('Content-Type', 'application/json')
      .append('Accept', '*/*');

124
    return this._http.post<WebinAuthenticationResultInterface>(baseUrl, body, { headers, withCredentials: false });
125
  }
126

127
  loginToken(username: string, password: string): Observable<string> {
Rajkumar-D's avatar
Rajkumar-D committed
128
    const baseUrl: string = environment.webinAuthUrl + "/token";
129
    // console.log('Webin authentication token', baseUrl);
130 131

    this.username = username;
132
    const today = new Date();
133
    this.loginDate = today;
134
    this.logoutDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7);
135

rajkumar's avatar
rajkumar committed
136
    const body = { authRealms: ['ENA', 'EGA'], password: password, username: this.username };
137 138 139
    const headers: HttpHeaders = new HttpHeaders()
      .append('Content-Type', 'application/json')
      .append('Accept', '*/*');
rajkumar's avatar
rajkumar committed
140 141 142 143
    /*var txt=this._http.post(baseUrl, body, { headers, withCredentials: false, responseType: 'text' });
    console.log(txt)
    return txt;*/
    return this._http.post(baseUrl, body, { headers, withCredentials: false, responseType: 'text' });
144
  }
rajkumar's avatar
rajkumar committed
145

rajkumar's avatar
rajkumar committed
146
  getSubmissionAccount() {
rajkumar's avatar
rajkumar committed
147 148
    return sessionStorage.getItem('submissionAccount');
  }
rajkumar's avatar
rajkumar committed
149 150 151 152 153 154

  async setSubmissionAccount() {
    (await this.util.getAccountDetails()).
      subscribe((data: any) => {
        sessionStorage.setItem('submissionAccount', JSON.stringify(data));
      });
rajkumar's avatar
rajkumar committed
155
  }
Rajkumar-D's avatar
Rajkumar-D committed
156 157 158 159 160 161 162

  isBroker(): boolean {
    var submissionAccount = JSON.parse(
      sessionStorage.getItem('submissionAccount')
    );
    return submissionAccount["brokerName"] != "";
  }
163
}