zavadil-ts-common 1.1.38 → 1.1.40

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.
@@ -2,7 +2,7 @@ export * from './EventManager';
2
2
  export * from './UserAlerts';
3
3
  export * from './RestClient';
4
4
  export * from './OAuthRestClient';
5
- export * from './OAuthSessionManager';
5
+ export * from './OAuthTokenManager';
6
6
  export * from './OAuthSubject';
7
7
  export * from './CancellablePromise';
8
8
  export * from './Lazy';
@@ -0,0 +1,11 @@
1
+ export type EntityBase = {
2
+ id?: number | null;
3
+ created_on?: Date;
4
+ last_update_on?: Date;
5
+ }
6
+
7
+ export type EntityWithName = EntityBase & {
8
+ name: string;
9
+ }
10
+
11
+ export type LookupTableEntity = EntityWithName & {}
@@ -1,143 +0,0 @@
1
- import {AccessTokenPayload, IdTokenPayload, OAuthRestClient, SessionPayload} from "./OAuthRestClient";
2
-
3
- export class OAuthSessionManager {
4
-
5
- oAuthServer: OAuthRestClient;
6
-
7
- audience: string;
8
-
9
- idToken?: IdTokenPayload;
10
-
11
- accessToken?: AccessTokenPayload;
12
-
13
- sessionId?: string;
14
-
15
- session?: SessionPayload;
16
-
17
- constructor(oAuthServerBaseUrl: string, targetAudience: string) {
18
- this.audience = targetAudience;
19
- this.oAuthServer = new OAuthRestClient(oAuthServerBaseUrl);
20
- setInterval(() => this.checkAccessTokenRefresh(), 5000);
21
- }
22
-
23
- isTokenExpired(expires?: Date | null): boolean {
24
- if (expires === undefined || expires === null) return false;
25
- const now = Date.now();
26
- const exp = new Date(expires).getTime();
27
- return (exp < now);
28
- }
29
-
30
- isTokenReadyForRefresh(expires?: Date | null): boolean {
31
- if (expires === undefined || expires === null) return false;
32
- const now = Date.now();
33
- const exp = new Date(expires).getTime() - (1000 * 10);
34
- return (exp < now);
35
- }
36
-
37
- isValidIdToken(idToken?: IdTokenPayload): boolean {
38
- return idToken !== undefined && !this.isTokenExpired(idToken.expires);
39
- }
40
-
41
- hasValidIdToken(): boolean {
42
- return this.isValidIdToken(this.idToken);
43
- }
44
-
45
- isValidAccessToken(accessToken?: AccessTokenPayload): boolean {
46
- return accessToken !== undefined && !this.isTokenExpired(accessToken.expires);
47
- }
48
-
49
- hasValidAccessToken(): boolean {
50
- return this.isValidAccessToken(this.accessToken);
51
- }
52
-
53
- reset() {
54
- this.sessionId = undefined;
55
- this.session = undefined;
56
- this.idToken = undefined;
57
- this.accessToken = undefined;
58
- }
59
-
60
- initializeSessionId(sessionId: string) {
61
- this.reset();
62
- this.sessionId = sessionId;
63
- }
64
-
65
- getSession(): Promise<SessionPayload> {
66
- if (this.session !== undefined) {
67
- return Promise.resolve(this.session);
68
- } else {
69
- if (this.sessionId === undefined) {
70
- return Promise.reject("No session ID!");
71
- }
72
- return this.oAuthServer
73
- .loadSessionById(this.sessionId)
74
- .then((session: SessionPayload) => {
75
- this.session = session;
76
- return this.session;
77
- });
78
- }
79
- }
80
-
81
- getIdToken(): Promise<string> {
82
- if (this.hasValidIdToken()) {
83
- return Promise.resolve(String(this.idToken?.idToken));
84
- } else {
85
- if (this.sessionId === undefined) {
86
- return Promise.reject("No session ID!");
87
- }
88
- return this.oAuthServer
89
- .requestIdTokenFromSession({sessionId: this.sessionId, targetAudience: this.audience})
90
- .then((idToken: IdTokenPayload) => {
91
- if (!this.isValidIdToken(idToken)) {
92
- return Promise.reject("Received ID token is not valid!");
93
- }
94
- this.idToken = idToken;
95
- })
96
- .then(() => this.getIdToken());
97
- }
98
- }
99
-
100
- getAccessToken(): Promise<string> {
101
- if (this.hasValidAccessToken()) {
102
- return Promise.resolve(String(this.accessToken?.accessToken));
103
- } else {
104
- return this.getIdToken()
105
- .then(
106
- (idToken: string) => this.oAuthServer
107
- .requestAccessToken({idToken: idToken, targetAudience: this.audience})
108
- .then((act: AccessTokenPayload) => {
109
- if (!this.isValidAccessToken(act)) {
110
- return Promise.reject("Received access token is not valid!");
111
- }
112
- this.accessToken = act;
113
- })
114
- )
115
- .then(() => this.getAccessToken());
116
- }
117
- }
118
-
119
- refreshAccessToken() {
120
- if (this.accessToken === undefined) throw new Error("No access token to be refreshed!");
121
- this.oAuthServer.refreshAccessToken(
122
- {
123
- targetAudience: this.audience,
124
- accessToken: this.accessToken?.accessToken,
125
- refreshToken: this.accessToken?.refreshToken
126
- }
127
- ).then(
128
- (act: AccessTokenPayload) => {
129
- if (!this.isValidAccessToken(act)) {
130
- throw new Error("Refreshed access token is not valid!");
131
- }
132
- this.accessToken = act;
133
- }
134
- )
135
- }
136
-
137
- checkAccessTokenRefresh() {
138
- if (this.hasValidAccessToken() && this.isTokenReadyForRefresh(this.accessToken?.expires)) {
139
- this.refreshAccessToken();
140
- }
141
- }
142
- }
143
-