2019-03-13 02:28:12 -04:00
|
|
|
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from "@angular/core";
|
|
|
|
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api";
|
2019-03-24 15:13:30 -04:00
|
|
|
import { CapableWidget, WIDGET_API_VERSION_OPENID } from "../capable-widget";
|
2019-03-13 02:28:12 -04:00
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service";
|
|
|
|
import { SessionStorage } from "../../shared/SessionStorage";
|
|
|
|
import { FE_ScalarOpenIdRequestBody } from "../../shared/models/scalar-server-responses";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2019-03-13 02:28:12 -04:00
|
|
|
|
|
|
|
@Component({
|
2021-09-01 19:29:24 -04:00
|
|
|
selector: "my-reauth-example-widget-wrapper",
|
2019-03-13 02:28:12 -04:00
|
|
|
templateUrl: "reauth-example.component.html",
|
|
|
|
styleUrls: ["reauth-example.component.scss"],
|
|
|
|
})
|
2021-09-01 19:01:01 -04:00
|
|
|
export class ReauthExampleWidgetWrapperComponent
|
|
|
|
extends CapableWidget
|
|
|
|
implements OnInit, OnDestroy {
|
2019-03-15 22:46:04 -04:00
|
|
|
public busy = true; // busy until we load supported versions
|
2019-03-13 02:28:12 -04:00
|
|
|
public hasOpenId = false;
|
|
|
|
public userId: string;
|
|
|
|
public blocked = false;
|
|
|
|
public error = false;
|
2020-10-23 07:30:20 -04:00
|
|
|
public stateMessage: string;
|
2019-03-13 02:28:12 -04:00
|
|
|
|
2021-09-01 19:01:01 -04:00
|
|
|
constructor(
|
|
|
|
activatedRoute: ActivatedRoute,
|
|
|
|
private scalarApi: ScalarServerApiService,
|
|
|
|
private changeDetector: ChangeDetectorRef,
|
|
|
|
public translate: TranslateService
|
|
|
|
) {
|
2019-03-13 02:28:12 -04:00
|
|
|
super();
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate = translate;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate
|
|
|
|
.get("Checking client version...")
|
|
|
|
.subscribe((res: string) => {
|
|
|
|
this.stateMessage = res;
|
|
|
|
});
|
2019-03-13 02:28:12 -04:00
|
|
|
|
|
|
|
const params: any = activatedRoute.snapshot.queryParams;
|
|
|
|
ScalarWidgetApi.widgetId = params.widgetId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get widgetId(): string {
|
|
|
|
return ScalarWidgetApi.widgetId;
|
|
|
|
}
|
|
|
|
|
2019-03-24 15:13:30 -04:00
|
|
|
protected onSupportedVersionsFound(): void {
|
|
|
|
super.onSupportedVersionsFound();
|
|
|
|
if (!this.doesSupportAtLeastVersion(WIDGET_API_VERSION_OPENID)) {
|
|
|
|
this.busy = true;
|
|
|
|
this.error = true;
|
|
|
|
this.hasOpenId = false;
|
|
|
|
this.blocked = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate
|
|
|
|
.get("Your client is too old to use this widget, sorry")
|
|
|
|
.subscribe((res: string) => {
|
|
|
|
this.stateMessage = res;
|
|
|
|
});
|
2019-03-24 15:13:30 -04:00
|
|
|
} else {
|
|
|
|
this.busy = false;
|
|
|
|
this.error = false;
|
|
|
|
this.hasOpenId = false;
|
|
|
|
this.blocked = false;
|
|
|
|
this.stateMessage = null;
|
|
|
|
}
|
|
|
|
this.changeDetector.detectChanges();
|
2019-03-15 22:46:04 -04:00
|
|
|
}
|
|
|
|
|
2019-03-24 16:52:55 -04:00
|
|
|
public async onReauthStart(): Promise<any> {
|
2019-03-13 02:28:12 -04:00
|
|
|
this.busy = true;
|
|
|
|
this.error = false;
|
|
|
|
this.blocked = false;
|
|
|
|
this.hasOpenId = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate
|
|
|
|
.get("Please accept the prompt to verify your identity")
|
|
|
|
.subscribe((res: string) => {
|
|
|
|
this.stateMessage = res;
|
|
|
|
});
|
2019-03-24 16:52:55 -04:00
|
|
|
|
|
|
|
const response = await this.getOpenIdInfo();
|
|
|
|
if (response.blocked) {
|
|
|
|
this.busy = false;
|
|
|
|
this.blocked = true;
|
|
|
|
this.hasOpenId = false;
|
|
|
|
this.stateMessage = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.exchangeOpenIdInfo(response.openId);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.busy = false;
|
|
|
|
this.error = true;
|
|
|
|
this.hasOpenId = false;
|
|
|
|
this.stateMessage = "";
|
|
|
|
}
|
2019-03-13 02:28:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private async exchangeOpenIdInfo(openId: FE_ScalarOpenIdRequestBody) {
|
|
|
|
this.stateMessage = "Exchanging OpenID credentials for token...";
|
|
|
|
this.changeDetector.detectChanges();
|
|
|
|
const scalarTokenResp = await this.scalarApi.register(openId);
|
|
|
|
SessionStorage.scalarToken = scalarTokenResp.scalar_token;
|
|
|
|
const userInfo = await this.scalarApi.getAccount();
|
|
|
|
this.hasOpenId = true;
|
|
|
|
this.userId = userInfo.user_id;
|
|
|
|
this.blocked = false;
|
|
|
|
this.busy = false;
|
|
|
|
this.stateMessage = null;
|
|
|
|
}
|
|
|
|
}
|