Skip to content

Commit

Permalink
adding support for popup-auth (disabled by default in all envs).
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Sep 5, 2023
1 parent ad83fa7 commit 9be431c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
52 changes: 48 additions & 4 deletions frontend/src/app/services/lighthouse.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Observable, of, throwError, fromEvent } from 'rxjs';
import {environment} from '../../environments/environment';
import {map, tap} from 'rxjs/operators';
import {concatMap, delay, retryWhen, timeout, first, map, filter, catchError, tap} from 'rxjs/operators';
import {ResponseWrapper} from '../models/response-wrapper';
import {LighthouseSourceMetadata} from '../models/lighthouse/lighthouse-source-metadata';
import * as Oauth from '@panva/oauth4webapi';
Expand All @@ -13,6 +13,8 @@ import {LighthouseSourceSearch} from '../models/lighthouse/lighthouse-source-sea
import {HTTP_CLIENT_TOKEN} from "../dependency-injection";
import {MedicalSourcesFilter} from './medical-sources-filter.service';

export const sourceConnectDesktopTimeout = 24*5000 //wait 2 minutes (5 * 24 = 120)

@Injectable({
providedIn: 'root'
})
Expand Down Expand Up @@ -163,8 +165,23 @@ export class LighthouseService {
redirectUrlParts.search = redirectParams.toString()
console.log(redirectUrlParts.toString());

// Simulate a mouse click:
window.location.href = redirectUrlParts.toString();

//if we're in desktop mode, we can open a new window, rather than redirecting the current window (which is an app frame)
if(environment.environment_desktop && environment.popup_source_auth){
//@ts-ignore
let openedWindow = window.runtime.BrowserOpenURL(redirectUrlParts.toString());

this.waitForDesktopCodeOrTimeout(openedWindow, sourceType).subscribe(async (codeData) => {
//TODO: redirect to the callback url with the code.
console.log("DONE WAITING FOR CODE")
})

//now wait for response from the opened window
} else {
//redirect to the url in the same window
window.location.href = redirectUrlParts.toString();
}

}

async swapOauthToken(sourceType: string, sourceMetadata: LighthouseSourceMetadata, expectedSourceStateInfo: SourceState, code: string): Promise<any>{
Expand Down Expand Up @@ -234,5 +251,32 @@ export class LighthouseService {
})
return parts.join(separator);
}

private waitForDesktopCodeOrTimeout(openedWindow: Window, sourceType: string): Observable<any> {
console.log(`waiting for postMessage notification from ${sourceType} window`)

//new code to listen to post message
return fromEvent(window, 'message')
.pipe(
//throw an error if we wait more than 2 minutes (this will close the window)
timeout(sourceConnectDesktopTimeout),
//make sure we're only listening to events from the "opened" window.
filter((event: MessageEvent) => event.source == openedWindow),
//after filtering, we should only have one event to handle.
first(),
map((event) => {
console.log(`received postMessage notification from ${sourceType} window & sending acknowledgment`, event)
// @ts-ignore
event.source.postMessage(JSON.stringify({close:true}), event.origin);
}),
catchError((err) => {
console.warn(`timed out waiting for notification from ${sourceType} (${sourceConnectDesktopTimeout/1000}s), closing window`)
openedWindow.self.close()
return throwError(err)
})
)
}


}

2 changes: 2 additions & 0 deletions frontend/src/environments/environment.cloud_sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export const environment = {
environment_cloud: true,
environment_desktop: false,
environment_name: "sandbox",
popup_source_auth: false,

lighthouse_api_endpoint_base: 'https://lighthouse.fastenhealth.com/sandbox',

//used to specify the couchdb server that we're going to use (can be relative or absolute). Must not have trailing slash
Expand Down
1 change: 1 addition & 0 deletions frontend/src/environments/environment.desktop_prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const environment = {
environment_cloud: false,
environment_desktop: true,
environment_name: "desktop_prod",
popup_source_auth: false,

lighthouse_api_endpoint_base: 'https://lighthouse.fastenhealth.com/v1',

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/environments/environment.desktop_sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export const environment = {
environment_cloud: false,
environment_desktop: true,
environment_name: "desktop_sandbox",
popup_source_auth: false,

lighthouse_api_endpoint_base: 'https://lighthouse.fastenhealth.com/sandbox',


//used to specify the api server that we're going to use (can be relative or absolute). Must not have trailing slash
fasten_api_endpoint_base: '/api',
};
1 change: 1 addition & 0 deletions frontend/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const environment = {
environment_cloud: false,
environment_desktop: false,
environment_name: "prod",
popup_source_auth: false,

lighthouse_api_endpoint_base: 'https://lighthouse.fastenhealth.com/v1',

Expand Down
1 change: 1 addition & 0 deletions frontend/src/environments/environment.sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const environment = {
environment_cloud: false,
environment_desktop: false,
environment_name: "sandbox",
popup_source_auth: false,

lighthouse_api_endpoint_base: 'https://lighthouse.fastenhealth.com/sandbox',

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const environment = {

// is the application running in a desktop environment (Wails). If so we will use hash based routing
environment_desktop: false,
//when environment_desktop=true, we can use the postMessage api to communicate with the desktop app (otherwise use redirects)
popup_source_auth: false,

// the environment name, `sandbox`, `prod`, `beta`
environment_name: "sandbox",
Expand Down

0 comments on commit 9be431c

Please sign in to comment.