zavadil-ts-common 1.2.21 → 1.2.22

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.
@@ -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<boolean> {
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<boolean> {
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,35 @@ export class RestClientWithOAuth extends RestClient {
32
34
  this.tokenManager = new LazyAsync<OAuthTokenManager>(() => this.getTokenManagerInternal());
33
35
  }
34
36
 
35
- initializeIdToken(): Promise<boolean> {
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
+ document.location = this.deleteIdTokenFromUrl();
44
+ });
39
45
  } else {
40
46
  const storageToken = this.getIdTokenFromLocalStorage();
41
47
  if (storageToken) return this.setIdToken(storageToken);
42
48
  }
43
- return Promise.resolve(false);
49
+ return Promise.reject("No valid ID token!");
44
50
  }
45
51
 
46
52
  /**
47
53
  * Attempt to get ID token from URL or storage, redirect to login page when not successful
48
54
  */
49
- initialize(): Promise<boolean> {
55
+ initialize(): Promise<any> {
50
56
  return this.initializeIdToken()
51
- .then(
52
- (success: boolean) => {
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(
57
+ .then(() => this.checkAccessToken())
58
+ .catch(
62
59
  (reason) => {
63
60
  console.log('OAuth initialization failed:', reason);
64
61
  return this.getServerInfo().then(
65
62
  (si) => {
66
63
  const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${document.location}`;
67
- console.log('Redirecting:', location);
64
+ console.log('Redirecting to login page:', location);
68
65
  document.location = location;
69
- return Promise.resolve(false);
70
66
  }
71
67
  ).catch((err) => {
72
68
  console.log('Cannot redirect: OAuth info not fetched:', reason);
@@ -90,9 +86,15 @@ export class RestClientWithOAuth extends RestClient {
90
86
  return this.defaultPrivilege;
91
87
  }
92
88
 
89
+ deleteIdTokenFromUrl(): string {
90
+ const up = new URLSearchParams(document.location.search);
91
+ up.delete(this.tokenName);
92
+ return up.toString();
93
+ }
94
+
93
95
  getIdTokenFromUrl(): string | null {
94
96
  const up = new URLSearchParams(document.location.search);
95
- return up.get('token');
97
+ return up.get(this.tokenName);
96
98
  }
97
99
 
98
100
  getIdTokenFromLocalStorage(): IdTokenPayload | null | undefined {
@@ -139,17 +141,16 @@ export class RestClientWithOAuth extends RestClient {
139
141
  return this.tokenManager.get();
140
142
  }
141
143
 
142
- login(login: string, password: string): Promise<boolean> {
144
+ login(login: string, password: string): Promise<any> {
143
145
  return this.getTokenManager().then((m) => m.login(login, password));
144
146
  }
145
147
 
146
- setIdToken(token: IdTokenPayload): Promise<boolean> {
148
+ setIdToken(token: IdTokenPayload): Promise<any> {
147
149
  return this.getTokenManager()
148
- .then((m) => m.setIdToken(token))
149
- .then(() => true)
150
+ .then((m) => m.setIdToken(token));
150
151
  }
151
152
 
152
- setIdTokenRaw(token: string): Promise<boolean> {
153
+ setIdTokenRaw(token: string): Promise<any> {
153
154
  return this.getTokenManager().then(m => m.verifyIdToken(token))
154
155
  }
155
156