From 4595a001713216dc68bceaa9b679cf2828dec71c Mon Sep 17 00:00:00 2001
From: Chris Hines <chris.hines@monash.edu>
Date: Wed, 25 Sep 2019 11:18:01 +0800
Subject: [PATCH] make cloudcmd authentication almost work

---
 src/app/tes.service.ts | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/app/tes.service.ts b/src/app/tes.service.ts
index 7175200..cd6b991 100644
--- a/src/app/tes.service.ts
+++ b/src/app/tes.service.ts
@@ -1,6 +1,6 @@
 import { Injectable, EventEmitter } from '@angular/core';
 import { HttpClientModule, HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
-import { Observable, Subject, BehaviorSubject, of, from } from 'rxjs';
+import { Observable, Subject, BehaviorSubject, of, from, interval } from 'rxjs';
 import { fromEvent, throwError, Subscription, merge } from 'rxjs';
 import { catchError, map, tap, filter } from 'rxjs/operators';
 import { Job } from './job';
@@ -12,7 +12,7 @@ import { BatchInterface } from './batchinterface';
 import { ComputesitesService } from './computesites.service';
 import { StrudelappsService } from './strudelapps.service';
 import { timer } from 'rxjs/observable/timer';
-import { repeat, takeUntil } from 'rxjs/operators';
+import { repeat, takeUntil, take } from 'rxjs/operators';
 import { LocationStrategy, Location } from '@angular/common';
 import { ActivatedRoute, Router } from '@angular/router';
 import { ModaldialogComponent } from './modaldialog/modaldialog.component';
@@ -53,6 +53,7 @@ private cancelRequests$: Subject<boolean>;
 public identitySubject: BehaviorSubject<Identity>;
 public appSubject: BehaviorSubject<Strudelapp>;
 private openapps: any[];
+private authdone$: Subject<boolean>;
 
 // public batchinterface: {[id: string] : BatchInterface};
 
@@ -71,6 +72,7 @@ private openapps: any[];
     this.apiserver = new BehaviorSubject<APIServer>(null);
     this.apiservers = new BehaviorSubject<APIServer[]>([]);
     this.cancelRequests$ = new Subject<boolean>();
+    this.authdone$ = new Subject<boolean>();
     this.openapps = [];
     //this.identitySubject = new BehaviorSubject<Identity>(null);
     //this.appSubject = new BehaviorSubject<Strudelapp>(null);
@@ -463,24 +465,40 @@ addUserHealth(identity,resp) {
    this.getAppInstance(job, action);
  }
 
+ private windowLoaded(window: any, location: string): boolean {
+   console.log('in windowloaded testing');
+   console.log(window.location);
+   try {
+     if (window.location.toString() == location) {
+       console.log('location match');
+       return true;
+     }
+     console.log('location not match');
+     return false;
+   } catch {
+     return true;
+   }
+ }
+
  public openAppWindow(url: any, job: Job, action: AppAction) {
      var re = /^https:\/\/([a-z0-9\.-]+)\/?/;
      let twshost = this.twsproxy.replace(re,"$1");
      console.log('in openappwindow url is ',url);
      let windowloc = url.replace(/\{twsproxy\}/g,this.twsproxy).replace(/twshost/g,twshost);
      console.log('window loc is',windowloc);
-     let user='chines';
-     let pass='pass';
      var authwindow = null;
    
      if (action.client.usebasicauth) {
        let authwindowloc = windowloc.replace(/^https:\/\//,'https://'+job.appinst.username+':'+job.appinst.password+'@');
-       console.log('authwindowloc is',authwindowloc);
-       console.log(job.appinst);
        authwindow = window.open(authwindowloc);
-       //windowloc = authwindowloc;
+       this.authdone$.pipe(take(1)).subscribe( () => { authwindow.close() ; this.finishAppWindow(windowloc,job) });
+       interval(500).pipe(takeUntil(this.authdone$),filter((v) => this.windowLoaded(authwindow, authwindowloc))).subscribe( () => this.authdone$.next(true));
+     } else {
+       this.finishAppWindow(windowloc,job);
      }
+ }
 
+   public finishAppWindow(windowloc: any, job:Job) {
      let appwindow = window.open(windowloc);
      if (appwindow == null) {
          this.statusMsg.next('It looks like a window failed to open. Please check your popup blocker settings (Strudel 2 needs to be able to open a window to your application');
@@ -490,10 +508,6 @@ addUserHealth(identity,resp) {
        return
      }
      this.openapps.push({'window':appwindow,'job':job})
-     if (authwindow !== null) {
-      authwindow.close();
-     }
-     
  }
 
  public connect(job: Job, action: AppAction, appinst?: any) {
-- 
GitLab