Use new utility functions in reauth demo

This commit is contained in:
Travis Ralston 2019-03-24 14:52:55 -06:00
parent bb874b1fa0
commit 5e50e9641c

View File

@ -1,6 +1,5 @@
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from "@angular/core"; import { ChangeDetectorRef, Component, OnDestroy, OnInit } from "@angular/core";
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api"; import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api";
import { Subscription } from "rxjs";
import { CapableWidget, WIDGET_API_VERSION_OPENID } from "../capable-widget"; import { CapableWidget, WIDGET_API_VERSION_OPENID } from "../capable-widget";
import { ActivatedRoute } from "@angular/router"; import { ActivatedRoute } from "@angular/router";
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service"; import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service";
@ -21,9 +20,6 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
public error = false; public error = false;
public stateMessage = "Checking client version..."; public stateMessage = "Checking client version...";
private widgetReplySubscription: Subscription;
private widgetRequestSubscription: Subscription;
constructor(activatedRoute: ActivatedRoute, constructor(activatedRoute: ActivatedRoute,
private scalarApi: ScalarServerApiService, private scalarApi: ScalarServerApiService,
private changeDetector: ChangeDetectorRef) { private changeDetector: ChangeDetectorRef) {
@ -37,62 +33,6 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
return ScalarWidgetApi.widgetId; return ScalarWidgetApi.widgetId;
} }
public ngOnInit(): void {
super.ngOnInit();
this.widgetReplySubscription = ScalarWidgetApi.replyReceived.subscribe(async response => {
const data = response.response;
if (response.action !== "get_openid") return;
try {
if (data.state === "request") {
this.stateMessage = "Waiting for you to accept the prompt...";
} else if (data.state === "allowed") {
await this.exchangeOpenIdInfo(data);
} else {
this.blocked = true;
this.busy = false;
this.hasOpenId = false;
this.stateMessage = null;
}
} catch (e) {
console.error(e);
this.error = true;
this.busy = false;
this.stateMessage = null;
}
this.changeDetector.detectChanges();
});
this.widgetRequestSubscription = ScalarWidgetApi.requestReceived.subscribe(async request => {
if (request.action !== "openid_credentials") return;
ScalarWidgetApi.replyAcknowledge(request);
try {
if (request.data.success) {
await this.exchangeOpenIdInfo(request.data);
} else {
this.blocked = true;
this.busy = false;
this.stateMessage = null;
}
} catch (e) {
console.error(e);
this.error = true;
this.busy = false;
this.stateMessage = null;
}
this.changeDetector.detectChanges();
});
}
public ngOnDestroy() {
super.ngOnDestroy();
if (this.widgetReplySubscription) this.widgetReplySubscription.unsubscribe();
if (this.widgetRequestSubscription) this.widgetRequestSubscription.unsubscribe();
}
protected onSupportedVersionsFound(): void { protected onSupportedVersionsFound(): void {
super.onSupportedVersionsFound(); super.onSupportedVersionsFound();
if (!this.doesSupportAtLeastVersion(WIDGET_API_VERSION_OPENID)) { if (!this.doesSupportAtLeastVersion(WIDGET_API_VERSION_OPENID)) {
@ -111,12 +51,31 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
this.changeDetector.detectChanges(); this.changeDetector.detectChanges();
} }
public onReauthStart(): void { public async onReauthStart(): Promise<any> {
this.busy = true; this.busy = true;
this.error = false; this.error = false;
this.blocked = false; this.blocked = false;
this.hasOpenId = false; this.hasOpenId = false;
ScalarWidgetApi.requestOpenID(); this.stateMessage = "Please accept the prompt to verify your identity";
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 = "";
}
} }
private async exchangeOpenIdInfo(openId: FE_ScalarOpenIdRequestBody) { private async exchangeOpenIdInfo(openId: FE_ScalarOpenIdRequestBody) {