webin-authentication.interceptor.ts 2.55 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.
 */

rajkumar's avatar
rajkumar committed
12 13
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
Rasko Leinonen's avatar
Rasko Leinonen committed
14
import { Observable } from 'rxjs';
15 16

import { WebinAuthenticationService } from './webin-authentication.service';
17
import { environment } from '../environments/environment';
18 19 20 21

@Injectable()
export class WebinAuthenticationInterceptor implements HttpInterceptor {

rajkumar's avatar
rajkumar committed
22
  constructor(private injector: Injector) { }
23

24
  //twitterToken="AAAAAAAAAAAAAAAAAAAAACHPOQAAAAAAK%2FQU%2BR0MB%2BcMXOiWrljWlX3%2BZ%2BU%3DFV9fOpABfT8s69ntGFAUNPwEf6fgcvNk3svOBfYXlKRsuEvUWd"
rajkumar's avatar
rajkumar committed
25

26 27
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

rajkumar's avatar
rajkumar committed
28 29
    console.log(req.url);

Rajkumar-D's avatar
Rajkumar-D committed
30
    if (!req.url.startsWith(environment.webinAuthUrl + "/login") &&
rajkumar's avatar
rajkumar committed
31 32
      !req.url.startsWith(environment.webinReportServiceUrl + "/checklist-groups") &&
      !req.url.startsWith(environment.webinReportServiceUrl + "/checklists") &&
Rajkumar-D's avatar
Rajkumar-D committed
33
      !req.url.startsWith(environment.webinAuthUrl + "/token") &&
rajkumar's avatar
rajkumar committed
34 35 36 37 38
      !(req.url.startsWith(environment.webinAdminServiceUrl + "/submission-account") && req.method === "POST") &&
      !req.url.startsWith(environment.webinAdminServiceUrl + "/country") &&
      !req.url.startsWith(environment.pupMedUrl) &&
      !req.url.startsWith(environment.webinAdminServiceUrl + "/request-password-change")) {
      //console.log('Webin authentication interceptor');
39
      const webinAuthenticationService = this.injector.get(WebinAuthenticationService);
rajkumar's avatar
rajkumar committed
40
      //console.log(webinAuthenticationService.getAuthorizationTokenHeader())
rajkumar's avatar
rajkumar committed
41
      const authReq = req.clone({ headers: req.headers.set('Authorization', webinAuthenticationService.getAuthorizationTokenHeader()) });
42
      return next.handle(authReq);
43

rajkumar's avatar
rajkumar committed
44
    } else if (req.url.startsWith("https://api.twitter.com/1.1")) {
45 46
      //const tweetReq = req.clone({headers: req.headers.set('Authorization', this.twitterToken)});
      //return next.handle(tweetReq);
rajkumar's avatar
rajkumar committed
47 48
    } else {
      return next.handle(req);
49 50
    }
  }
51
}