@ts-core/oauth 3.0.15 → 3.0.16

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.
@@ -6,20 +6,24 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
6
6
  popUpWidth: number;
7
7
  popUpHeight: number;
8
8
  popUpTarget: string;
9
+ popUpIsCheckClose: boolean;
10
+ popUpMessageParser: IOAuthPopUpMessageParser;
9
11
  redirectUri: string;
10
12
  protected http: TransportHttp;
11
- protected timer: any;
12
13
  protected popUp: Window;
13
14
  protected promise: PromiseHandler<IOAuthDto>;
14
15
  protected responseType: string;
15
16
  protected _urlParams: Map<string, string>;
17
+ protected _popUpCheckCloseTimer: any;
16
18
  constructor(logger: ILogger, applicationId: string, window?: Window);
17
19
  protected open(): Promise<IOAuthDto>;
18
- protected openPopup(): Window;
19
- protected checkPopUp: () => void;
20
+ protected popupOpen(): Window;
21
+ protected popUpFocus(): void;
22
+ protected popUpCheckClose: () => void;
20
23
  protected getUrlParams(): URLSearchParams;
21
24
  protected abstract getUrl(): string;
22
- messageHandler: (event: MessageEvent<IOAuthPopUpDto>) => void;
25
+ protected messageHandler: (event: MessageEvent) => void;
26
+ protected browserMessageHandler(event: MessageEvent<IOAuthPopUpDto>): IOAuthPopUpDto;
23
27
  protected getOriginUrl(): string;
24
28
  protected getRedirectUri(): string;
25
29
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -28,6 +32,8 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
28
32
  getToken(): Promise<IOAuthDto>;
29
33
  close(): void;
30
34
  destroy(): void;
35
+ protected get popUpCheckCloseTimer(): any;
36
+ protected set popUpCheckCloseTimer(value: any);
31
37
  get urlParams(): Map<string, string>;
32
38
  }
33
39
  export interface IOAuthDto {
@@ -48,3 +54,4 @@ export interface IOAuthToken {
48
54
  expiresIn: number;
49
55
  accessToken: string;
50
56
  }
57
+ export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
package/cjs/OAuthBase.js CHANGED
@@ -17,17 +17,14 @@ class OAuthBase extends common_1.LoggerWrapper {
17
17
  super(logger);
18
18
  this.applicationId = applicationId;
19
19
  this.window = window;
20
- this.popUpWidth = 430;
21
- this.popUpHeight = 520;
22
- this.popUpTarget = '_blank';
23
- this.checkPopUp = () => {
20
+ this.popUpCheckClose = () => {
24
21
  if (_.isNil(this.popUp) || this.popUp.closed) {
25
22
  this.close();
26
23
  }
27
24
  };
28
25
  this.messageHandler = (event) => {
29
- let data = event.data;
30
- if (event.origin !== this.getOriginUrl() || !_.isObject(data)) {
26
+ let data = this.popUpMessageParser(event);
27
+ if (_.isNil(data)) {
31
28
  return;
32
29
  }
33
30
  if (!_.isEmpty(data.oAuthCodeOrToken)) {
@@ -41,6 +38,11 @@ class OAuthBase extends common_1.LoggerWrapper {
41
38
  }
42
39
  };
43
40
  this.http = new common_1.TransportHttp(logger, { method: 'get' });
41
+ this.popUpWidth = 430;
42
+ this.popUpHeight = 520;
43
+ this.popUpTarget = '_blank';
44
+ this.popUpIsCheckClose = true;
45
+ this.popUpMessageParser = this.browserMessageHandler;
44
46
  this._urlParams = new Map();
45
47
  this.urlParams.set('display', 'popup');
46
48
  }
@@ -53,20 +55,27 @@ class OAuthBase extends common_1.LoggerWrapper {
53
55
  return this.promise.promise;
54
56
  }
55
57
  this.promise = common_1.PromiseHandler.create();
56
- this.popUp = this.openPopup();
58
+ this.popUp = this.popupOpen();
59
+ this.popUpFocus();
60
+ if (this.popUpIsCheckClose) {
61
+ this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, common_1.DateUtil.MILLISECONDS_SECOND / 10);
62
+ }
57
63
  this.window.addEventListener('message', this.messageHandler, false);
58
- this.timer = setInterval(this.checkPopUp, common_1.DateUtil.MILLISECONDS_SECOND / 10);
59
64
  return this.promise.promise;
60
65
  });
61
66
  }
62
- openPopup() {
67
+ popupOpen() {
63
68
  let window = this.window;
64
69
  let top = (window.screen.height - this.popUpHeight) / 2;
65
70
  let left = (window.screen.width - this.popUpWidth) / 2;
66
71
  let item = window.open(this.getUrl(), this.popUpTarget, `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
67
- item.focus();
68
72
  return item;
69
73
  }
74
+ popUpFocus() {
75
+ if (!_.isNil(this.popUp) && !this.popUp.closed) {
76
+ this.popUp.focus();
77
+ }
78
+ }
70
79
  getUrlParams() {
71
80
  let item = new URLSearchParams();
72
81
  item.append('client_id', this.applicationId);
@@ -75,6 +84,9 @@ class OAuthBase extends common_1.LoggerWrapper {
75
84
  this.urlParams.forEach((value, key) => item.append(key, value));
76
85
  return item;
77
86
  }
87
+ browserMessageHandler(event) {
88
+ return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
89
+ }
78
90
  getOriginUrl() {
79
91
  return this.window.location.origin;
80
92
  }
@@ -95,8 +107,7 @@ class OAuthBase extends common_1.LoggerWrapper {
95
107
  }
96
108
  close() {
97
109
  this.window.removeEventListener('message', this.messageHandler, false);
98
- clearInterval(this.timer);
99
- this.timer = null;
110
+ this.popUpCheckCloseTimer = null;
100
111
  if (!_.isNil(this.popUp)) {
101
112
  this.popUp.close();
102
113
  this.popUp = null;
@@ -121,6 +132,16 @@ class OAuthBase extends common_1.LoggerWrapper {
121
132
  this.http = null;
122
133
  }
123
134
  }
135
+ get popUpCheckCloseTimer() {
136
+ return this._popUpCheckCloseTimer;
137
+ }
138
+ set popUpCheckCloseTimer(value) {
139
+ if (value === this._popUpCheckCloseTimer) {
140
+ return;
141
+ }
142
+ clearInterval(this._popUpCheckCloseTimer);
143
+ this._popUpCheckCloseTimer = value;
144
+ }
124
145
  get urlParams() {
125
146
  return this._urlParams;
126
147
  }
@@ -6,20 +6,24 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
6
6
  popUpWidth: number;
7
7
  popUpHeight: number;
8
8
  popUpTarget: string;
9
+ popUpIsCheckClose: boolean;
10
+ popUpMessageParser: IOAuthPopUpMessageParser;
9
11
  redirectUri: string;
10
12
  protected http: TransportHttp;
11
- protected timer: any;
12
13
  protected popUp: Window;
13
14
  protected promise: PromiseHandler<IOAuthDto>;
14
15
  protected responseType: string;
15
16
  protected _urlParams: Map<string, string>;
17
+ protected _popUpCheckCloseTimer: any;
16
18
  constructor(logger: ILogger, applicationId: string, window?: Window);
17
19
  protected open(): Promise<IOAuthDto>;
18
- protected openPopup(): Window;
19
- protected checkPopUp: () => void;
20
+ protected popupOpen(): Window;
21
+ protected popUpFocus(): void;
22
+ protected popUpCheckClose: () => void;
20
23
  protected getUrlParams(): URLSearchParams;
21
24
  protected abstract getUrl(): string;
22
- messageHandler: (event: MessageEvent<IOAuthPopUpDto>) => void;
25
+ protected messageHandler: (event: MessageEvent) => void;
26
+ protected browserMessageHandler(event: MessageEvent<IOAuthPopUpDto>): IOAuthPopUpDto;
23
27
  protected getOriginUrl(): string;
24
28
  protected getRedirectUri(): string;
25
29
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -28,6 +32,8 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
28
32
  getToken(): Promise<IOAuthDto>;
29
33
  close(): void;
30
34
  destroy(): void;
35
+ protected get popUpCheckCloseTimer(): any;
36
+ protected set popUpCheckCloseTimer(value: any);
31
37
  get urlParams(): Map<string, string>;
32
38
  }
33
39
  export interface IOAuthDto {
@@ -48,3 +54,4 @@ export interface IOAuthToken {
48
54
  expiresIn: number;
49
55
  accessToken: string;
50
56
  }
57
+ export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
package/esm/OAuthBase.js CHANGED
@@ -14,17 +14,14 @@ export class OAuthBase extends LoggerWrapper {
14
14
  super(logger);
15
15
  this.applicationId = applicationId;
16
16
  this.window = window;
17
- this.popUpWidth = 430;
18
- this.popUpHeight = 520;
19
- this.popUpTarget = '_blank';
20
- this.checkPopUp = () => {
17
+ this.popUpCheckClose = () => {
21
18
  if (_.isNil(this.popUp) || this.popUp.closed) {
22
19
  this.close();
23
20
  }
24
21
  };
25
22
  this.messageHandler = (event) => {
26
- let data = event.data;
27
- if (event.origin !== this.getOriginUrl() || !_.isObject(data)) {
23
+ let data = this.popUpMessageParser(event);
24
+ if (_.isNil(data)) {
28
25
  return;
29
26
  }
30
27
  if (!_.isEmpty(data.oAuthCodeOrToken)) {
@@ -38,6 +35,11 @@ export class OAuthBase extends LoggerWrapper {
38
35
  }
39
36
  };
40
37
  this.http = new TransportHttp(logger, { method: 'get' });
38
+ this.popUpWidth = 430;
39
+ this.popUpHeight = 520;
40
+ this.popUpTarget = '_blank';
41
+ this.popUpIsCheckClose = true;
42
+ this.popUpMessageParser = this.browserMessageHandler;
41
43
  this._urlParams = new Map();
42
44
  this.urlParams.set('display', 'popup');
43
45
  }
@@ -50,20 +52,27 @@ export class OAuthBase extends LoggerWrapper {
50
52
  return this.promise.promise;
51
53
  }
52
54
  this.promise = PromiseHandler.create();
53
- this.popUp = this.openPopup();
55
+ this.popUp = this.popupOpen();
56
+ this.popUpFocus();
57
+ if (this.popUpIsCheckClose) {
58
+ this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, DateUtil.MILLISECONDS_SECOND / 10);
59
+ }
54
60
  this.window.addEventListener('message', this.messageHandler, false);
55
- this.timer = setInterval(this.checkPopUp, DateUtil.MILLISECONDS_SECOND / 10);
56
61
  return this.promise.promise;
57
62
  });
58
63
  }
59
- openPopup() {
64
+ popupOpen() {
60
65
  let window = this.window;
61
66
  let top = (window.screen.height - this.popUpHeight) / 2;
62
67
  let left = (window.screen.width - this.popUpWidth) / 2;
63
68
  let item = window.open(this.getUrl(), this.popUpTarget, `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
64
- item.focus();
65
69
  return item;
66
70
  }
71
+ popUpFocus() {
72
+ if (!_.isNil(this.popUp) && !this.popUp.closed) {
73
+ this.popUp.focus();
74
+ }
75
+ }
67
76
  getUrlParams() {
68
77
  let item = new URLSearchParams();
69
78
  item.append('client_id', this.applicationId);
@@ -72,6 +81,9 @@ export class OAuthBase extends LoggerWrapper {
72
81
  this.urlParams.forEach((value, key) => item.append(key, value));
73
82
  return item;
74
83
  }
84
+ browserMessageHandler(event) {
85
+ return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
86
+ }
75
87
  getOriginUrl() {
76
88
  return this.window.location.origin;
77
89
  }
@@ -92,8 +104,7 @@ export class OAuthBase extends LoggerWrapper {
92
104
  }
93
105
  close() {
94
106
  this.window.removeEventListener('message', this.messageHandler, false);
95
- clearInterval(this.timer);
96
- this.timer = null;
107
+ this.popUpCheckCloseTimer = null;
97
108
  if (!_.isNil(this.popUp)) {
98
109
  this.popUp.close();
99
110
  this.popUp = null;
@@ -118,6 +129,16 @@ export class OAuthBase extends LoggerWrapper {
118
129
  this.http = null;
119
130
  }
120
131
  }
132
+ get popUpCheckCloseTimer() {
133
+ return this._popUpCheckCloseTimer;
134
+ }
135
+ set popUpCheckCloseTimer(value) {
136
+ if (value === this._popUpCheckCloseTimer) {
137
+ return;
138
+ }
139
+ clearInterval(this._popUpCheckCloseTimer);
140
+ this._popUpCheckCloseTimer = value;
141
+ }
121
142
  get urlParams() {
122
143
  return this._urlParams;
123
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-core/oauth",
3
- "version": "3.0.15",
3
+ "version": "3.0.16",
4
4
  "description": "Classes and utils for oauth",
5
5
  "main": "./cjs/public-api.js",
6
6
  "module": "./esm/public-api.js",