zavadil-ts-common 1.2.21 → 1.2.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +9 -7
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/oauth/OAuthTokenManager.d.ts +2 -2
- package/dist/oauth/RestClientWithOAuth.d.ts +7 -5
- package/package.json +1 -1
- package/src/oauth/OAuthTokenManager.ts +3 -5
- package/src/oauth/RestClientWithOAuth.ts +25 -23
@@ -90,19 +90,17 @@ export class OAuthTokenManager {
|
|
90
90
|
this.eventManager.triggerEvent('id-token-changed', token);
|
91
91
|
}
|
92
92
|
|
93
|
-
verifyIdToken(token: string): Promise<
|
93
|
+
verifyIdToken(token: string): Promise<any> {
|
94
94
|
return this.oAuthServer.verifyIdToken(token)
|
95
|
-
.then((t) => this.setIdToken(t))
|
96
|
-
.then(() => true)
|
95
|
+
.then((t) => this.setIdToken(t));
|
97
96
|
}
|
98
97
|
|
99
|
-
login(login: string, password: string): Promise<
|
98
|
+
login(login: string, password: string): Promise<any> {
|
100
99
|
this.reset();
|
101
100
|
return this.oAuthServer.requestIdTokenFromLogin({login: login, password: password, targetAudience: this.audience})
|
102
101
|
.then(
|
103
102
|
(t) => {
|
104
103
|
this.setIdToken(t);
|
105
|
-
return true;
|
106
104
|
})
|
107
105
|
}
|
108
106
|
|
@@ -13,6 +13,8 @@ export type ServerOAuthInfoPayload = {
|
|
13
13
|
|
14
14
|
export class RestClientWithOAuth extends RestClient {
|
15
15
|
|
16
|
+
private tokenName = 'token';
|
17
|
+
|
16
18
|
private insecureClient: RestClient;
|
17
19
|
|
18
20
|
private tokenManager: LazyAsync<OAuthTokenManager>;
|
@@ -32,41 +34,36 @@ export class RestClientWithOAuth extends RestClient {
|
|
32
34
|
this.tokenManager = new LazyAsync<OAuthTokenManager>(() => this.getTokenManagerInternal());
|
33
35
|
}
|
34
36
|
|
35
|
-
initializeIdToken(): Promise<
|
37
|
+
initializeIdToken(): Promise<any> {
|
36
38
|
const urlToken = this.getIdTokenFromUrl();
|
37
39
|
if (urlToken !== null) {
|
38
|
-
return this.setIdTokenRaw(urlToken)
|
40
|
+
return this.setIdTokenRaw(urlToken)
|
41
|
+
.then(() => {
|
42
|
+
console.log("Redirecting to url without id token");
|
43
|
+
this.deleteIdTokenFromUrl();
|
44
|
+
//document.location =;
|
45
|
+
});
|
39
46
|
} else {
|
40
47
|
const storageToken = this.getIdTokenFromLocalStorage();
|
41
48
|
if (storageToken) return this.setIdToken(storageToken);
|
42
49
|
}
|
43
|
-
return Promise.
|
50
|
+
return Promise.reject("No valid ID token!");
|
44
51
|
}
|
45
52
|
|
46
53
|
/**
|
47
54
|
* Attempt to get ID token from URL or storage, redirect to login page when not successful
|
48
55
|
*/
|
49
|
-
initialize(): Promise<
|
56
|
+
initialize(): Promise<any> {
|
50
57
|
return this.initializeIdToken()
|
51
|
-
.then(
|
52
|
-
|
53
|
-
if (success) return this.checkAccessToken();
|
54
|
-
return Promise.reject("ID token initialization failed!");
|
55
|
-
}
|
56
|
-
).then(
|
57
|
-
(success: boolean) => {
|
58
|
-
if (!success) return Promise.reject("Access token initialization failed!");
|
59
|
-
return true;
|
60
|
-
}
|
61
|
-
).catch(
|
58
|
+
.then(() => this.checkAccessToken())
|
59
|
+
.catch(
|
62
60
|
(reason) => {
|
63
61
|
console.log('OAuth initialization failed:', reason);
|
64
62
|
return this.getServerInfo().then(
|
65
63
|
(si) => {
|
66
64
|
const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${document.location}`;
|
67
|
-
console.log('Redirecting:', location);
|
65
|
+
console.log('Redirecting to login page:', location);
|
68
66
|
document.location = location;
|
69
|
-
return Promise.resolve(false);
|
70
67
|
}
|
71
68
|
).catch((err) => {
|
72
69
|
console.log('Cannot redirect: OAuth info not fetched:', reason);
|
@@ -90,9 +87,15 @@ export class RestClientWithOAuth extends RestClient {
|
|
90
87
|
return this.defaultPrivilege;
|
91
88
|
}
|
92
89
|
|
90
|
+
deleteIdTokenFromUrl() {
|
91
|
+
const up = new URLSearchParams(document.location.search);
|
92
|
+
up.delete(this.tokenName);
|
93
|
+
document.location.search = up.toString();
|
94
|
+
}
|
95
|
+
|
93
96
|
getIdTokenFromUrl(): string | null {
|
94
97
|
const up = new URLSearchParams(document.location.search);
|
95
|
-
return up.get(
|
98
|
+
return up.get(this.tokenName);
|
96
99
|
}
|
97
100
|
|
98
101
|
getIdTokenFromLocalStorage(): IdTokenPayload | null | undefined {
|
@@ -139,17 +142,16 @@ export class RestClientWithOAuth extends RestClient {
|
|
139
142
|
return this.tokenManager.get();
|
140
143
|
}
|
141
144
|
|
142
|
-
login(login: string, password: string): Promise<
|
145
|
+
login(login: string, password: string): Promise<any> {
|
143
146
|
return this.getTokenManager().then((m) => m.login(login, password));
|
144
147
|
}
|
145
148
|
|
146
|
-
setIdToken(token: IdTokenPayload): Promise<
|
149
|
+
setIdToken(token: IdTokenPayload): Promise<any> {
|
147
150
|
return this.getTokenManager()
|
148
|
-
.then((m) => m.setIdToken(token))
|
149
|
-
.then(() => true)
|
151
|
+
.then((m) => m.setIdToken(token));
|
150
152
|
}
|
151
153
|
|
152
|
-
setIdTokenRaw(token: string): Promise<
|
154
|
+
setIdTokenRaw(token: string): Promise<any> {
|
153
155
|
return this.getTokenManager().then(m => m.verifyIdToken(token))
|
154
156
|
}
|
155
157
|
|