This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
reauth-example.component.ts
112 lines (103 loc) · 3.84 KB
/
reauth-example.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from "@angular/core";
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api";
import { CapableWidget, WIDGET_API_VERSION_OPENID } from "../capable-widget";
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";
import { TranslateService } from "@ngx-translate/core";
@Component({
selector: "my-reauth-example-widget-wrapper",
templateUrl: "reauth-example.component.html",
styleUrls: ["reauth-example.component.scss"],
})
export class ReauthExampleWidgetWrapperComponent
extends CapableWidget
implements OnInit, OnDestroy {
public busy = true; // busy until we load supported versions
public hasOpenId = false;
public userId: string;
public blocked = false;
public error = false;
public stateMessage: string;
constructor(
activatedRoute: ActivatedRoute,
private scalarApi: ScalarServerApiService,
private changeDetector: ChangeDetectorRef,
public translate: TranslateService
) {
super();
this.translate = translate;
this.translate
.get("Checking client version...")
.subscribe((res: string) => {
this.stateMessage = res;
});
const params: any = activatedRoute.snapshot.queryParams;
ScalarWidgetApi.widgetId = params.widgetId;
}
public get widgetId(): string {
return ScalarWidgetApi.widgetId;
}
protected onSupportedVersionsFound(): void {
super.onSupportedVersionsFound();
if (!this.doesSupportAtLeastVersion(WIDGET_API_VERSION_OPENID)) {
this.busy = true;
this.error = true;
this.hasOpenId = false;
this.blocked = false;
this.translate
.get("Your client is too old to use this widget, sorry")
.subscribe((res: string) => {
this.stateMessage = res;
});
} else {
this.busy = false;
this.error = false;
this.hasOpenId = false;
this.blocked = false;
this.stateMessage = null;
}
this.changeDetector.detectChanges();
}
public async onReauthStart(): Promise<any> {
this.busy = true;
this.error = false;
this.blocked = false;
this.hasOpenId = false;
this.translate
.get("Please accept the prompt to verify your identity")
.subscribe((res: string) => {
this.stateMessage = res;
});
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) {
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;
}
}