zavadil-ts-common 1.1.70 → 1.1.72

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.
@@ -40,7 +40,7 @@ export class OAuthTokenManager {
40
40
  }
41
41
 
42
42
  isValidIdToken(idToken?: IdTokenPayload): boolean {
43
- return idToken !== undefined && StringUtil.notEmpty(idToken.idToken) && !this.isTokenExpired(idToken.expires);
43
+ return idToken !== undefined && StringUtil.notEmpty(idToken.token) && !this.isTokenExpired(idToken.expires);
44
44
  }
45
45
 
46
46
  hasValidIdToken(): boolean {
@@ -48,7 +48,7 @@ export class OAuthTokenManager {
48
48
  }
49
49
 
50
50
  isValidAccessToken(accessToken?: AccessTokenPayload): boolean {
51
- return accessToken !== undefined && StringUtil.notEmpty(accessToken.accessToken) && !this.isTokenExpired(accessToken.expires);
51
+ return accessToken !== undefined && StringUtil.notEmpty(accessToken.token) && !this.isTokenExpired(accessToken.expires);
52
52
  }
53
53
 
54
54
  hasValidAccessToken(privilege: string): boolean {
@@ -56,7 +56,8 @@ export class OAuthTokenManager {
56
56
  }
57
57
 
58
58
  reset() {
59
- this.setIdToken(undefined);
59
+ this.idToken = undefined;
60
+ this.eventManager.triggerEvent('id-token-changed', undefined);
60
61
  this.accessTokens.clear();
61
62
  }
62
63
 
@@ -66,15 +67,15 @@ export class OAuthTokenManager {
66
67
  }
67
68
  if (this.isTokenReadyForRefresh(this.idToken.issuedAt, this.idToken.expires)) {
68
69
  return this.oAuthServer
69
- .refreshIdToken({idToken: this.idToken.idToken})
70
+ .refreshIdToken({idToken: this.idToken.token})
70
71
  .then(
71
72
  (t) => {
72
73
  this.setIdToken(t);
73
- return t.idToken;
74
+ return t.token;
74
75
  }
75
76
  );
76
77
  }
77
- return Promise.resolve(this.idToken.idToken);
78
+ return Promise.resolve(this.idToken.token);
78
79
  }
79
80
 
80
81
  setIdToken(token?: IdTokenPayload) {
@@ -86,7 +87,6 @@ export class OAuthTokenManager {
86
87
  }
87
88
 
88
89
  verifyIdToken(token: string): Promise<boolean> {
89
- console.log('verifying id token', token);
90
90
  return this.oAuthServer.verifyIdToken(token)
91
91
  .then((t) => this.setIdToken(t))
92
92
  .then(() => true)
@@ -119,10 +119,10 @@ export class OAuthTokenManager {
119
119
 
120
120
  getAccessToken(privilege: string): Promise<string> {
121
121
  const existing = this.accessTokens.get(privilege);
122
- if (existing === undefined || !this.isValidAccessToken(existing)) return this.getAccessTokenInternal(privilege).then((t) => t.accessToken);
122
+ if (existing === undefined || !this.isValidAccessToken(existing)) return this.getAccessTokenInternal(privilege).then((t) => t.token);
123
123
  // preload access token if it is going to expire soon
124
124
  if (this.isTokenReadyForRefresh(existing.issuedAt, existing.expires)) this.getAccessTokenInternal(privilege);
125
- return Promise.resolve(existing.accessToken);
125
+ return Promise.resolve(existing.token);
126
126
  }
127
127
 
128
128
  }
@@ -3,6 +3,7 @@ import {RestClient, RestClientHeaders} from "../client";
3
3
  import {IdTokenPayload} from "./OAuthRestClient";
4
4
  import {LazyAsync} from "../cache";
5
5
  import {DateUtil, StringUtil} from "../util";
6
+ import {JsonUtil} from "../util/JsonUtil";
6
7
 
7
8
  export type ServerOAuthInfoPayload = {
8
9
  debugMode?: boolean;
@@ -72,6 +73,10 @@ export class RestClientWithOAuth extends RestClient {
72
73
  );
73
74
  }
74
75
 
76
+ logout(): Promise<any> {
77
+ return this.getTokenManager().then((m) => m.reset());
78
+ }
79
+
75
80
  /**
76
81
  * Override this if a different privilege is needed for different endpoints
77
82
  * @param url
@@ -85,15 +90,8 @@ export class RestClientWithOAuth extends RestClient {
85
90
  return up.get('token');
86
91
  }
87
92
 
88
- getIdTokenFromLocalStorage(): IdTokenPayload | null {
89
- const raw = localStorage.getItem('id-token');
90
- if (!raw) return null;
91
- const obj = JSON.parse(raw);
92
- return {
93
- idToken: obj.idToken,
94
- issuedAt: DateUtil.parseDate(obj.issuedAt) || new Date(),
95
- expires: DateUtil.parseDate(obj.expires)
96
- }
93
+ getIdTokenFromLocalStorage(): IdTokenPayload | null | undefined {
94
+ return JsonUtil.parse(localStorage.getItem('id-token'));
97
95
  }
98
96
 
99
97
  saveIdTokenToLocalStorage(token: IdTokenPayload | null) {
@@ -13,4 +13,8 @@ export class JsonUtil {
13
13
  if (StringUtil.isBlank(json)) return undefined;
14
14
  return JSON.parse(StringUtil.getNonEmpty(json), JsonUtil.dateParser);
15
15
  }
16
+
17
+ static parse(json?: string | null): any {
18
+ return JsonUtil.parseWithDates(json);
19
+ }
16
20
  }