zavadil-ts-common 1.2.35 → 1.2.37

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.
@@ -23,7 +23,7 @@ export class RestClientWithOAuth extends RestClient {
23
23
 
24
24
  private defaultPrivilege: string;
25
25
 
26
- private redirecting: boolean = false;
26
+ private redirecting?: string;
27
27
 
28
28
  constructor(url: string, defaultPrivilege: string = '*') {
29
29
  super(url);
@@ -36,15 +36,28 @@ export class RestClientWithOAuth extends RestClient {
36
36
  this.tokenManager = new LazyAsync<OAuthTokenManager>(() => this.getTokenManagerInternal());
37
37
  }
38
38
 
39
+ isRedirecting(): boolean {
40
+ return StringUtil.isEmpty(this.redirecting);
41
+ }
42
+
43
+ redirectingTo(): string {
44
+ return StringUtil.getNonEmpty(this.redirecting);
45
+ }
46
+
47
+ redirectTo(url: string) {
48
+ console.log(`Redirecting to ${url}`);
49
+ this.redirecting = url;
50
+ document.location.href = url;
51
+ }
52
+
39
53
  initializeIdToken(): Promise<any> {
40
54
  const urlToken = this.getIdTokenFromUrl();
41
55
  if (urlToken !== null) {
42
- return this.setIdTokenRaw(urlToken)
43
- .then(() => {
44
- console.log("Redirecting to url without id token");
45
- this.redirecting = true;
46
- document.location.href = this.deleteIdTokenFromUrl();
47
- });
56
+ return this
57
+ .setIdTokenRaw(urlToken)
58
+ .then(
59
+ () => this.redirectTo(this.deleteIdTokenFromUrl())
60
+ );
48
61
  } else {
49
62
  const storageToken = this.getIdTokenFromLocalStorage();
50
63
  if (storageToken) return this.setIdToken(storageToken);
@@ -52,6 +65,22 @@ export class RestClientWithOAuth extends RestClient {
52
65
  return Promise.reject("No valid ID token!");
53
66
  }
54
67
 
68
+ /**
69
+ * Attempt to get ID token from URL or storage, redirect to login page when not successful
70
+ */
71
+ redirectToLogin(): Promise<any> {
72
+ if (this.redirecting) return Promise.reject("Already redirecting!");
73
+ return this.getServerInfo().then(
74
+ (si) => {
75
+ const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${this.deleteIdTokenFromUrl()}`;
76
+ this.redirectTo(location);
77
+ }
78
+ ).catch((err) => {
79
+ console.error('Redirection failed: OAuth info not fetched:', err);
80
+ return Promise.reject(err);
81
+ });
82
+ }
83
+
55
84
  /**
56
85
  * Attempt to get ID token from URL or storage, redirect to login page when not successful
57
86
  */
@@ -62,19 +91,8 @@ export class RestClientWithOAuth extends RestClient {
62
91
  })
63
92
  .catch(
64
93
  (reason) => {
65
- if (this.redirecting) return Promise.reject("Already redirecting!");
66
94
  console.log('OAuth initialization failed:', reason);
67
- return this.getServerInfo().then(
68
- (si) => {
69
- const location = `${si.oauthServerUrl}/login?app_name=${si.targetAudience}&redirect_url=${this.deleteIdTokenFromUrl()}`;
70
- console.log('Redirecting to login page:', location);
71
- this.redirecting = true;
72
- document.location.href = location;
73
- }
74
- ).catch((err) => {
75
- console.log('Cannot redirect: OAuth info not fetched:', reason);
76
- return Promise.reject(err);
77
- });
95
+ return this.redirectToLogin();
78
96
  }
79
97
  );
80
98
  }
@@ -96,7 +114,7 @@ export class RestClientWithOAuth extends RestClient {
96
114
  deleteIdTokenFromUrl(): string {
97
115
  const url = new URL(document.location.toString());
98
116
  url.searchParams.delete(this.tokenName);
99
- return url.toString();
117
+ return StringUtil.trimTrailingSlashes(url.toString());
100
118
  }
101
119
 
102
120
  getIdTokenFromUrl(): string | null {
@@ -3,11 +3,19 @@ import {JsonUtil} from "../src/util/JsonUtil";
3
3
  import {RestClient} from "../src";
4
4
 
5
5
  describe('testing StringUtil', () => {
6
- test('getNonEmpty', () => {
7
- expect(StringUtil.getNonEmpty('test')).toBe('test');
8
- expect(StringUtil.getNonEmpty('', 'test')).toBe('test');
9
- expect(StringUtil.getNonEmpty('', null, undefined, 'test')).toBe('test');
10
- expect(StringUtil.getNonEmpty('', 'test', undefined, 'test2')).toBe('test');
6
+ test('isEmpty', () => {
7
+ expect(StringUtil.isEmpty('test')).toBe(false);
8
+ expect(StringUtil.isEmpty(' ')).toBe(false);
9
+ expect(StringUtil.isEmpty('')).toBe(true);
10
+ expect(StringUtil.isEmpty(null)).toBe(true);
11
+ expect(StringUtil.isEmpty(undefined)).toBe(true);
12
+ });
13
+ test('isBlank', () => {
14
+ expect(StringUtil.isBlank('test')).toBe(false);
15
+ expect(StringUtil.isBlank(' ')).toBe(true);
16
+ expect(StringUtil.isBlank('')).toBe(true);
17
+ expect(StringUtil.isBlank(null)).toBe(true);
18
+ expect(StringUtil.isBlank(undefined)).toBe(true);
11
19
  });
12
20
  test('getNonEmpty', () => {
13
21
  expect(StringUtil.getNonEmpty('test')).toBe('test');