@ts-core/oauth 3.0.17 → 3.0.19

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.
@@ -1,4 +1,5 @@
1
- import { PromiseHandler, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
1
+ import { PromiseHandler, ObservableData, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
2
+ import { Observable, Subject } from "rxjs";
2
3
  export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
3
4
  protected applicationId: string;
4
5
  protected window?: Window;
@@ -10,6 +11,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
10
11
  popUpMessageParser: IOAuthPopUpMessageParser;
11
12
  redirectUri: string;
12
13
  protected http: TransportHttp;
14
+ protected subject: Subject<ObservableData<OAuthEvent, Window>>;
13
15
  protected popUp: Window;
14
16
  protected promise: PromiseHandler<IOAuthDto>;
15
17
  protected responseType: string;
@@ -28,6 +30,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
28
30
  protected getRedirectUri(): string;
29
31
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
30
32
  abstract getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
33
+ parsePopUpResult(data: IOAuthPopUpDto): void;
31
34
  getCode(): Promise<IOAuthDto>;
32
35
  getToken(): Promise<IOAuthDto>;
33
36
  close(): void;
@@ -35,6 +38,10 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
35
38
  protected get popUpCheckCloseTimer(): any;
36
39
  protected set popUpCheckCloseTimer(value: any);
37
40
  get urlParams(): Map<string, string>;
41
+ get state(): string;
42
+ get events(): Observable<ObservableData<OAuthEvent, Window>>;
43
+ get popUpClosed(): Observable<Window>;
44
+ get popUpOpened(): Observable<Window>;
38
45
  }
39
46
  export interface IOAuthDto {
40
47
  codeOrToken: string;
@@ -54,4 +61,8 @@ export interface IOAuthToken {
54
61
  expiresIn: number;
55
62
  accessToken: string;
56
63
  }
64
+ export declare enum OAuthEvent {
65
+ POPUP_OPENED = "POPUP_OPENED",
66
+ POPUP_CLOSED = "POPUP_CLOSED"
67
+ }
57
68
  export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
package/cjs/OAuthBase.js CHANGED
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.OAuthBase = void 0;
12
+ exports.OAuthEvent = exports.OAuthBase = void 0;
13
13
  const common_1 = require("@ts-core/common");
14
14
  const _ = require("lodash");
15
+ const rxjs_1 = require("rxjs");
15
16
  class OAuthBase extends common_1.LoggerWrapper {
16
17
  constructor(logger, applicationId, window) {
17
18
  super(logger);
@@ -22,28 +23,16 @@ class OAuthBase extends common_1.LoggerWrapper {
22
23
  this.close();
23
24
  }
24
25
  };
25
- this.messageHandler = (event) => {
26
- let data = this.popUpMessageParser(event);
27
- if (_.isNil(data)) {
28
- return;
29
- }
30
- if (!_.isEmpty(data.oAuthCodeOrToken)) {
31
- this.promise.resolve({ redirectUri: this.getRedirectUri(), codeOrToken: data.oAuthCodeOrToken });
32
- }
33
- if (!_.isEmpty(data.oAuthError)) {
34
- this.promise.reject(data.oAuthError);
35
- }
36
- if (!this.promise.isPending) {
37
- this.close();
38
- }
39
- };
26
+ this.messageHandler = (event) => this.parsePopUpResult(this.popUpMessageParser(event));
40
27
  this.http = new common_1.TransportHttp(logger, { method: 'get' });
28
+ this.subject = new rxjs_1.Subject();
41
29
  this.popUpWidth = 430;
42
30
  this.popUpHeight = 520;
43
31
  this.popUpTarget = '_blank';
44
32
  this.popUpIsCheckClose = true;
45
33
  this.popUpMessageParser = this.browserMessageHandler;
46
34
  this._urlParams = new Map();
35
+ this.urlParams.set('state', common_1.RandomUtil.randomString());
47
36
  this.urlParams.set('display', 'popup');
48
37
  }
49
38
  open() {
@@ -56,6 +45,7 @@ class OAuthBase extends common_1.LoggerWrapper {
56
45
  }
57
46
  this.promise = common_1.PromiseHandler.create();
58
47
  this.popUp = this.popUpOpen();
48
+ this.subject.next(new common_1.ObservableData(OAuthEvent.POPUP_OPENED, this.popUp));
59
49
  this.popUpFocus();
60
50
  if (this.popUpIsCheckClose) {
61
51
  this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, common_1.DateUtil.MILLISECONDS_SECOND / 10);
@@ -93,6 +83,20 @@ class OAuthBase extends common_1.LoggerWrapper {
93
83
  getRedirectUri() {
94
84
  return !_.isNil(this.redirectUri) ? this.redirectUri : `${this.getOriginUrl()}/oauth`;
95
85
  }
86
+ parsePopUpResult(data) {
87
+ if (_.isNil(data)) {
88
+ return;
89
+ }
90
+ if (!_.isEmpty(data.oAuthCodeOrToken)) {
91
+ this.promise.resolve({ redirectUri: this.getRedirectUri(), codeOrToken: data.oAuthCodeOrToken });
92
+ }
93
+ if (!_.isEmpty(data.oAuthError)) {
94
+ this.promise.reject(data.oAuthError);
95
+ }
96
+ if (!this.promise.isPending) {
97
+ this.close();
98
+ }
99
+ }
96
100
  getCode() {
97
101
  return __awaiter(this, void 0, void 0, function* () {
98
102
  this.responseType = 'code';
@@ -108,6 +112,9 @@ class OAuthBase extends common_1.LoggerWrapper {
108
112
  close() {
109
113
  this.window.removeEventListener('message', this.messageHandler, false);
110
114
  this.popUpCheckCloseTimer = null;
115
+ if (!_.isNil(this.subject)) {
116
+ this.subject.next(new common_1.ObservableData(OAuthEvent.POPUP_CLOSED, this.popUp));
117
+ }
111
118
  if (!_.isNil(this.popUp)) {
112
119
  this.popUp.close();
113
120
  this.popUp = null;
@@ -131,6 +138,10 @@ class OAuthBase extends common_1.LoggerWrapper {
131
138
  this.http.destroy();
132
139
  this.http = null;
133
140
  }
141
+ if (!_.isNil(this.subject)) {
142
+ this.subject.complete();
143
+ this.subject = null;
144
+ }
134
145
  }
135
146
  get popUpCheckCloseTimer() {
136
147
  return this._popUpCheckCloseTimer;
@@ -145,6 +156,23 @@ class OAuthBase extends common_1.LoggerWrapper {
145
156
  get urlParams() {
146
157
  return this._urlParams;
147
158
  }
159
+ get state() {
160
+ return !_.isNil(this.urlParams) ? this.urlParams.get('state') : null;
161
+ }
162
+ get events() {
163
+ return !_.isNil(this.subject) ? this.subject.asObservable() : null;
164
+ }
165
+ get popUpClosed() {
166
+ return this.events.pipe((0, rxjs_1.filter)(item => item.type === OAuthEvent.POPUP_CLOSED), (0, rxjs_1.map)(item => item.data));
167
+ }
168
+ get popUpOpened() {
169
+ return this.events.pipe((0, rxjs_1.filter)(item => item.type === OAuthEvent.POPUP_OPENED), (0, rxjs_1.map)(item => item.data));
170
+ }
148
171
  }
149
172
  exports.OAuthBase = OAuthBase;
150
173
  OAuthBase.ERROR_WINDOW_CLOSED = 'WINDOW_CLOSED';
174
+ var OAuthEvent;
175
+ (function (OAuthEvent) {
176
+ OAuthEvent["POPUP_OPENED"] = "POPUP_OPENED";
177
+ OAuthEvent["POPUP_CLOSED"] = "POPUP_CLOSED";
178
+ })(OAuthEvent = exports.OAuthEvent || (exports.OAuthEvent = {}));
@@ -2,5 +2,5 @@ import { IOAuthPopUpDto } from './OAuthBase';
2
2
  export declare class OAuthParser {
3
3
  static NAMES: Array<string>;
4
4
  static ERRORS: Array<string>;
5
- static parse(params: any, fragment: string): IOAuthPopUpDto;
5
+ static parse(params: any, fragment?: string): IOAuthPopUpDto;
6
6
  }
@@ -6,7 +6,9 @@ class OAuthParser {
6
6
  static parse(params, fragment) {
7
7
  let item = new Object();
8
8
  _.forIn(params, (value, key) => item[key] = value);
9
- new URLSearchParams(fragment).forEach((value, key) => item[key] = value);
9
+ if (!_.isEmpty(fragment)) {
10
+ new URLSearchParams(fragment).forEach((value, key) => item[key] = value);
11
+ }
10
12
  let oAuthError = null;
11
13
  let oAuthCodeOrToken = null;
12
14
  for (let key in item) {
@@ -1,4 +1,5 @@
1
- import { PromiseHandler, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
1
+ import { PromiseHandler, ObservableData, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
2
+ import { Observable, Subject } from "rxjs";
2
3
  export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
3
4
  protected applicationId: string;
4
5
  protected window?: Window;
@@ -10,6 +11,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
10
11
  popUpMessageParser: IOAuthPopUpMessageParser;
11
12
  redirectUri: string;
12
13
  protected http: TransportHttp;
14
+ protected subject: Subject<ObservableData<OAuthEvent, Window>>;
13
15
  protected popUp: Window;
14
16
  protected promise: PromiseHandler<IOAuthDto>;
15
17
  protected responseType: string;
@@ -28,6 +30,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
28
30
  protected getRedirectUri(): string;
29
31
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
30
32
  abstract getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
33
+ parsePopUpResult(data: IOAuthPopUpDto): void;
31
34
  getCode(): Promise<IOAuthDto>;
32
35
  getToken(): Promise<IOAuthDto>;
33
36
  close(): void;
@@ -35,6 +38,10 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
35
38
  protected get popUpCheckCloseTimer(): any;
36
39
  protected set popUpCheckCloseTimer(value: any);
37
40
  get urlParams(): Map<string, string>;
41
+ get state(): string;
42
+ get events(): Observable<ObservableData<OAuthEvent, Window>>;
43
+ get popUpClosed(): Observable<Window>;
44
+ get popUpOpened(): Observable<Window>;
38
45
  }
39
46
  export interface IOAuthDto {
40
47
  codeOrToken: string;
@@ -54,4 +61,8 @@ export interface IOAuthToken {
54
61
  expiresIn: number;
55
62
  accessToken: string;
56
63
  }
64
+ export declare enum OAuthEvent {
65
+ POPUP_OPENED = "POPUP_OPENED",
66
+ POPUP_CLOSED = "POPUP_CLOSED"
67
+ }
57
68
  export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
package/esm/OAuthBase.js CHANGED
@@ -7,8 +7,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { PromiseHandler, DateUtil, LoggerWrapper, TransportHttp } from "@ts-core/common";
10
+ import { PromiseHandler, DateUtil, ObservableData, LoggerWrapper, TransportHttp, RandomUtil } from "@ts-core/common";
11
11
  import * as _ from 'lodash';
12
+ import { filter, map, Subject } from "rxjs";
12
13
  export class OAuthBase extends LoggerWrapper {
13
14
  constructor(logger, applicationId, window) {
14
15
  super(logger);
@@ -19,28 +20,16 @@ export class OAuthBase extends LoggerWrapper {
19
20
  this.close();
20
21
  }
21
22
  };
22
- this.messageHandler = (event) => {
23
- let data = this.popUpMessageParser(event);
24
- if (_.isNil(data)) {
25
- return;
26
- }
27
- if (!_.isEmpty(data.oAuthCodeOrToken)) {
28
- this.promise.resolve({ redirectUri: this.getRedirectUri(), codeOrToken: data.oAuthCodeOrToken });
29
- }
30
- if (!_.isEmpty(data.oAuthError)) {
31
- this.promise.reject(data.oAuthError);
32
- }
33
- if (!this.promise.isPending) {
34
- this.close();
35
- }
36
- };
23
+ this.messageHandler = (event) => this.parsePopUpResult(this.popUpMessageParser(event));
37
24
  this.http = new TransportHttp(logger, { method: 'get' });
25
+ this.subject = new Subject();
38
26
  this.popUpWidth = 430;
39
27
  this.popUpHeight = 520;
40
28
  this.popUpTarget = '_blank';
41
29
  this.popUpIsCheckClose = true;
42
30
  this.popUpMessageParser = this.browserMessageHandler;
43
31
  this._urlParams = new Map();
32
+ this.urlParams.set('state', RandomUtil.randomString());
44
33
  this.urlParams.set('display', 'popup');
45
34
  }
46
35
  open() {
@@ -53,6 +42,7 @@ export class OAuthBase extends LoggerWrapper {
53
42
  }
54
43
  this.promise = PromiseHandler.create();
55
44
  this.popUp = this.popUpOpen();
45
+ this.subject.next(new ObservableData(OAuthEvent.POPUP_OPENED, this.popUp));
56
46
  this.popUpFocus();
57
47
  if (this.popUpIsCheckClose) {
58
48
  this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, DateUtil.MILLISECONDS_SECOND / 10);
@@ -90,6 +80,20 @@ export class OAuthBase extends LoggerWrapper {
90
80
  getRedirectUri() {
91
81
  return !_.isNil(this.redirectUri) ? this.redirectUri : `${this.getOriginUrl()}/oauth`;
92
82
  }
83
+ parsePopUpResult(data) {
84
+ if (_.isNil(data)) {
85
+ return;
86
+ }
87
+ if (!_.isEmpty(data.oAuthCodeOrToken)) {
88
+ this.promise.resolve({ redirectUri: this.getRedirectUri(), codeOrToken: data.oAuthCodeOrToken });
89
+ }
90
+ if (!_.isEmpty(data.oAuthError)) {
91
+ this.promise.reject(data.oAuthError);
92
+ }
93
+ if (!this.promise.isPending) {
94
+ this.close();
95
+ }
96
+ }
93
97
  getCode() {
94
98
  return __awaiter(this, void 0, void 0, function* () {
95
99
  this.responseType = 'code';
@@ -105,6 +109,9 @@ export class OAuthBase extends LoggerWrapper {
105
109
  close() {
106
110
  this.window.removeEventListener('message', this.messageHandler, false);
107
111
  this.popUpCheckCloseTimer = null;
112
+ if (!_.isNil(this.subject)) {
113
+ this.subject.next(new ObservableData(OAuthEvent.POPUP_CLOSED, this.popUp));
114
+ }
108
115
  if (!_.isNil(this.popUp)) {
109
116
  this.popUp.close();
110
117
  this.popUp = null;
@@ -128,6 +135,10 @@ export class OAuthBase extends LoggerWrapper {
128
135
  this.http.destroy();
129
136
  this.http = null;
130
137
  }
138
+ if (!_.isNil(this.subject)) {
139
+ this.subject.complete();
140
+ this.subject = null;
141
+ }
131
142
  }
132
143
  get popUpCheckCloseTimer() {
133
144
  return this._popUpCheckCloseTimer;
@@ -142,5 +153,22 @@ export class OAuthBase extends LoggerWrapper {
142
153
  get urlParams() {
143
154
  return this._urlParams;
144
155
  }
156
+ get state() {
157
+ return !_.isNil(this.urlParams) ? this.urlParams.get('state') : null;
158
+ }
159
+ get events() {
160
+ return !_.isNil(this.subject) ? this.subject.asObservable() : null;
161
+ }
162
+ get popUpClosed() {
163
+ return this.events.pipe(filter(item => item.type === OAuthEvent.POPUP_CLOSED), map(item => item.data));
164
+ }
165
+ get popUpOpened() {
166
+ return this.events.pipe(filter(item => item.type === OAuthEvent.POPUP_OPENED), map(item => item.data));
167
+ }
145
168
  }
146
169
  OAuthBase.ERROR_WINDOW_CLOSED = 'WINDOW_CLOSED';
170
+ export var OAuthEvent;
171
+ (function (OAuthEvent) {
172
+ OAuthEvent["POPUP_OPENED"] = "POPUP_OPENED";
173
+ OAuthEvent["POPUP_CLOSED"] = "POPUP_CLOSED";
174
+ })(OAuthEvent || (OAuthEvent = {}));
@@ -2,5 +2,5 @@ import { IOAuthPopUpDto } from './OAuthBase';
2
2
  export declare class OAuthParser {
3
3
  static NAMES: Array<string>;
4
4
  static ERRORS: Array<string>;
5
- static parse(params: any, fragment: string): IOAuthPopUpDto;
5
+ static parse(params: any, fragment?: string): IOAuthPopUpDto;
6
6
  }
@@ -3,7 +3,9 @@ export class OAuthParser {
3
3
  static parse(params, fragment) {
4
4
  let item = new Object();
5
5
  _.forIn(params, (value, key) => item[key] = value);
6
- new URLSearchParams(fragment).forEach((value, key) => item[key] = value);
6
+ if (!_.isEmpty(fragment)) {
7
+ new URLSearchParams(fragment).forEach((value, key) => item[key] = value);
8
+ }
7
9
  let oAuthError = null;
8
10
  let oAuthCodeOrToken = null;
9
11
  for (let key in item) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-core/oauth",
3
- "version": "3.0.17",
3
+ "version": "3.0.19",
4
4
  "description": "Classes and utils for oauth",
5
5
  "main": "./cjs/public-api.js",
6
6
  "module": "./esm/public-api.js",