adding support for popup-auth (disabled by default in all envs).

This commit is contained in:
Jason Kulatunga 2023-09-05 07:30:10 -07:00
parent ad83fa7ae1
commit 9be431c2a6
No known key found for this signature in database
7 changed files with 56 additions and 5 deletions

View File

@ -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';
@ -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'
})
@ -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>{
@ -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)
})
)
}
}

View File

@ -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

View File

@ -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',

View File

@ -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',
};

View File

@ -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',

View File

@ -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',

View File

@ -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",