@ts-core/oauth 3.0.18 → 3.0.20

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;
@@ -7,9 +8,10 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
7
8
  popUpHeight: number;
8
9
  popUpTarget: string;
9
10
  popUpIsCheckClose: boolean;
10
- popUpMessageParser: IOAuthPopUpMessageParser;
11
+ popUpMessageParser: IOAuthPopUpParser;
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;
@@ -23,7 +25,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
23
25
  protected getUrlParams(): URLSearchParams;
24
26
  protected abstract getUrl(): string;
25
27
  protected messageHandler: (event: MessageEvent) => void;
26
- protected browserMessageHandler(event: MessageEvent<IOAuthPopUpDto>): IOAuthPopUpDto;
28
+ protected browserInternalMessageHandler: IOAuthPopUpParser;
27
29
  protected getOriginUrl(): string;
28
30
  protected getRedirectUri(): string;
29
31
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -37,6 +39,9 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
37
39
  protected set popUpCheckCloseTimer(value: any);
38
40
  get urlParams(): Map<string, string>;
39
41
  get state(): string;
42
+ get events(): Observable<ObservableData<OAuthEvent, Window>>;
43
+ get popUpClosed(): Observable<Window>;
44
+ get popUpOpened(): Observable<Window>;
40
45
  }
41
46
  export interface IOAuthDto {
42
47
  codeOrToken: string;
@@ -56,4 +61,8 @@ export interface IOAuthToken {
56
61
  expiresIn: number;
57
62
  accessToken: string;
58
63
  }
59
- export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
64
+ export declare enum OAuthEvent {
65
+ POPUP_OPENED = "POPUP_OPENED",
66
+ POPUP_CLOSED = "POPUP_CLOSED"
67
+ }
68
+ export type IOAuthPopUpParser = (...params: any[]) => 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);
@@ -23,12 +24,16 @@ class OAuthBase extends common_1.LoggerWrapper {
23
24
  }
24
25
  };
25
26
  this.messageHandler = (event) => this.parsePopUpResult(this.popUpMessageParser(event));
27
+ this.browserInternalMessageHandler = (event) => {
28
+ return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
29
+ };
26
30
  this.http = new common_1.TransportHttp(logger, { method: 'get' });
31
+ this.subject = new rxjs_1.Subject();
27
32
  this.popUpWidth = 430;
28
33
  this.popUpHeight = 520;
29
34
  this.popUpTarget = '_blank';
30
35
  this.popUpIsCheckClose = true;
31
- this.popUpMessageParser = this.browserMessageHandler;
36
+ this.popUpMessageParser = this.browserInternalMessageHandler;
32
37
  this._urlParams = new Map();
33
38
  this.urlParams.set('state', common_1.RandomUtil.randomString());
34
39
  this.urlParams.set('display', 'popup');
@@ -43,6 +48,7 @@ class OAuthBase extends common_1.LoggerWrapper {
43
48
  }
44
49
  this.promise = common_1.PromiseHandler.create();
45
50
  this.popUp = this.popUpOpen();
51
+ this.subject.next(new common_1.ObservableData(OAuthEvent.POPUP_OPENED, this.popUp));
46
52
  this.popUpFocus();
47
53
  if (this.popUpIsCheckClose) {
48
54
  this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, common_1.DateUtil.MILLISECONDS_SECOND / 10);
@@ -71,9 +77,6 @@ class OAuthBase extends common_1.LoggerWrapper {
71
77
  this.urlParams.forEach((value, key) => item.append(key, value));
72
78
  return item;
73
79
  }
74
- browserMessageHandler(event) {
75
- return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
76
- }
77
80
  getOriginUrl() {
78
81
  return this.window.location.origin;
79
82
  }
@@ -109,6 +112,9 @@ class OAuthBase extends common_1.LoggerWrapper {
109
112
  close() {
110
113
  this.window.removeEventListener('message', this.messageHandler, false);
111
114
  this.popUpCheckCloseTimer = null;
115
+ if (!_.isNil(this.subject)) {
116
+ this.subject.next(new common_1.ObservableData(OAuthEvent.POPUP_CLOSED, this.popUp));
117
+ }
112
118
  if (!_.isNil(this.popUp)) {
113
119
  this.popUp.close();
114
120
  this.popUp = null;
@@ -132,6 +138,10 @@ class OAuthBase extends common_1.LoggerWrapper {
132
138
  this.http.destroy();
133
139
  this.http = null;
134
140
  }
141
+ if (!_.isNil(this.subject)) {
142
+ this.subject.complete();
143
+ this.subject = null;
144
+ }
135
145
  }
136
146
  get popUpCheckCloseTimer() {
137
147
  return this._popUpCheckCloseTimer;
@@ -149,6 +159,20 @@ class OAuthBase extends common_1.LoggerWrapper {
149
159
  get state() {
150
160
  return !_.isNil(this.urlParams) ? this.urlParams.get('state') : null;
151
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
+ }
152
171
  }
153
172
  exports.OAuthBase = OAuthBase;
154
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 = {}));
@@ -0,0 +1,3 @@
1
+ import { IOAuthPopUpDto, OAuthBase } from '../OAuthBase';
2
+ export declare const backendPropertiesSet: (item: OAuthBase, redirectUri: string, method: IOAuthBackendMethod, timeout?: number) => void;
3
+ export type IOAuthBackendMethod = (state: string) => Promise<IOAuthPopUpDto>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.backendPropertiesSet = void 0;
13
+ const common_1 = require("@ts-core/common");
14
+ const rxjs_1 = require("rxjs");
15
+ const backendPropertiesSet = (item, redirectUri, method, timeout = common_1.DateUtil.MILLISECONDS_SECOND) => {
16
+ item.redirectUri = redirectUri;
17
+ item.popUpIsCheckClose = false;
18
+ item.popUpOpened.pipe((0, rxjs_1.delay)(timeout), (0, rxjs_1.takeUntil)(item.destroyed)).subscribe(() => __awaiter(void 0, void 0, void 0, function* () { return item.parsePopUpResult(yield method(item.state)); }));
19
+ };
20
+ exports.backendPropertiesSet = backendPropertiesSet;
@@ -0,0 +1,2 @@
1
+ import { OAuthBase } from '../OAuthBase';
2
+ export declare const cordovaPropertiesSet: (item: OAuthBase, packageName: string) => void;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cordovaPropertiesSet = void 0;
4
+ const OAuthParser_1 = require("../OAuthParser");
5
+ const _ = require("lodash");
6
+ const cordovaPropertiesSet = (item, packageName) => {
7
+ item.popUpTarget = 'oauth:cordova';
8
+ item.redirectUri = `${packageName}://oauth_callback`;
9
+ item.popUpIsCheckClose = false;
10
+ item.popUpMessageParser = cordovaPopUpMessageParser;
11
+ };
12
+ exports.cordovaPropertiesSet = cordovaPropertiesSet;
13
+ function cordovaPopUpMessageParser(event) {
14
+ let data = event.data;
15
+ let prefix = 'oauth::';
16
+ return _.isString(data) && data.includes(prefix) ? OAuthParser_1.OAuthParser.parse(JSON.parse(data.substring(prefix.length)), '') : null;
17
+ }
@@ -9,3 +9,5 @@ export * from './ya/YaUser';
9
9
  export * from './ya/YaAuth';
10
10
  export * from './ma/MaAuth';
11
11
  export * from './ma/MaUser';
12
+ export * from './external/backend';
13
+ export * from './external/cordova';
package/cjs/public-api.js CHANGED
@@ -25,3 +25,5 @@ __exportStar(require("./ya/YaUser"), exports);
25
25
  __exportStar(require("./ya/YaAuth"), exports);
26
26
  __exportStar(require("./ma/MaAuth"), exports);
27
27
  __exportStar(require("./ma/MaUser"), exports);
28
+ __exportStar(require("./external/backend"), exports);
29
+ __exportStar(require("./external/cordova"), exports);
@@ -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;
@@ -7,9 +8,10 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
7
8
  popUpHeight: number;
8
9
  popUpTarget: string;
9
10
  popUpIsCheckClose: boolean;
10
- popUpMessageParser: IOAuthPopUpMessageParser;
11
+ popUpMessageParser: IOAuthPopUpParser;
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;
@@ -23,7 +25,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
23
25
  protected getUrlParams(): URLSearchParams;
24
26
  protected abstract getUrl(): string;
25
27
  protected messageHandler: (event: MessageEvent) => void;
26
- protected browserMessageHandler(event: MessageEvent<IOAuthPopUpDto>): IOAuthPopUpDto;
28
+ protected browserInternalMessageHandler: IOAuthPopUpParser;
27
29
  protected getOriginUrl(): string;
28
30
  protected getRedirectUri(): string;
29
31
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -37,6 +39,9 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
37
39
  protected set popUpCheckCloseTimer(value: any);
38
40
  get urlParams(): Map<string, string>;
39
41
  get state(): string;
42
+ get events(): Observable<ObservableData<OAuthEvent, Window>>;
43
+ get popUpClosed(): Observable<Window>;
44
+ get popUpOpened(): Observable<Window>;
40
45
  }
41
46
  export interface IOAuthDto {
42
47
  codeOrToken: string;
@@ -56,4 +61,8 @@ export interface IOAuthToken {
56
61
  expiresIn: number;
57
62
  accessToken: string;
58
63
  }
59
- export type IOAuthPopUpMessageParser = (event: MessageEvent) => IOAuthPopUpDto;
64
+ export declare enum OAuthEvent {
65
+ POPUP_OPENED = "POPUP_OPENED",
66
+ POPUP_CLOSED = "POPUP_CLOSED"
67
+ }
68
+ export type IOAuthPopUpParser = (...params: any[]) => 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, RandomUtil } 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);
@@ -20,12 +21,16 @@ export class OAuthBase extends LoggerWrapper {
20
21
  }
21
22
  };
22
23
  this.messageHandler = (event) => this.parsePopUpResult(this.popUpMessageParser(event));
24
+ this.browserInternalMessageHandler = (event) => {
25
+ return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
26
+ };
23
27
  this.http = new TransportHttp(logger, { method: 'get' });
28
+ this.subject = new Subject();
24
29
  this.popUpWidth = 430;
25
30
  this.popUpHeight = 520;
26
31
  this.popUpTarget = '_blank';
27
32
  this.popUpIsCheckClose = true;
28
- this.popUpMessageParser = this.browserMessageHandler;
33
+ this.popUpMessageParser = this.browserInternalMessageHandler;
29
34
  this._urlParams = new Map();
30
35
  this.urlParams.set('state', RandomUtil.randomString());
31
36
  this.urlParams.set('display', 'popup');
@@ -40,6 +45,7 @@ export class OAuthBase extends LoggerWrapper {
40
45
  }
41
46
  this.promise = PromiseHandler.create();
42
47
  this.popUp = this.popUpOpen();
48
+ this.subject.next(new ObservableData(OAuthEvent.POPUP_OPENED, this.popUp));
43
49
  this.popUpFocus();
44
50
  if (this.popUpIsCheckClose) {
45
51
  this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, DateUtil.MILLISECONDS_SECOND / 10);
@@ -68,9 +74,6 @@ export class OAuthBase extends LoggerWrapper {
68
74
  this.urlParams.forEach((value, key) => item.append(key, value));
69
75
  return item;
70
76
  }
71
- browserMessageHandler(event) {
72
- return event.origin === this.getOriginUrl() && _.isObject(event.data) ? event.data : null;
73
- }
74
77
  getOriginUrl() {
75
78
  return this.window.location.origin;
76
79
  }
@@ -106,6 +109,9 @@ export class OAuthBase extends LoggerWrapper {
106
109
  close() {
107
110
  this.window.removeEventListener('message', this.messageHandler, false);
108
111
  this.popUpCheckCloseTimer = null;
112
+ if (!_.isNil(this.subject)) {
113
+ this.subject.next(new ObservableData(OAuthEvent.POPUP_CLOSED, this.popUp));
114
+ }
109
115
  if (!_.isNil(this.popUp)) {
110
116
  this.popUp.close();
111
117
  this.popUp = null;
@@ -129,6 +135,10 @@ export class OAuthBase extends LoggerWrapper {
129
135
  this.http.destroy();
130
136
  this.http = null;
131
137
  }
138
+ if (!_.isNil(this.subject)) {
139
+ this.subject.complete();
140
+ this.subject = null;
141
+ }
132
142
  }
133
143
  get popUpCheckCloseTimer() {
134
144
  return this._popUpCheckCloseTimer;
@@ -146,5 +156,19 @@ export class OAuthBase extends LoggerWrapper {
146
156
  get state() {
147
157
  return !_.isNil(this.urlParams) ? this.urlParams.get('state') : null;
148
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
+ }
149
168
  }
150
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 = {}));
@@ -0,0 +1,3 @@
1
+ import { IOAuthPopUpDto, OAuthBase } from '../OAuthBase';
2
+ export declare const backendPropertiesSet: (item: OAuthBase, redirectUri: string, method: IOAuthBackendMethod, timeout?: number) => void;
3
+ export type IOAuthBackendMethod = (state: string) => Promise<IOAuthPopUpDto>;
@@ -0,0 +1,16 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { DateUtil } from '@ts-core/common';
11
+ import { delay, takeUntil } from 'rxjs';
12
+ export const backendPropertiesSet = (item, redirectUri, method, timeout = DateUtil.MILLISECONDS_SECOND) => {
13
+ item.redirectUri = redirectUri;
14
+ item.popUpIsCheckClose = false;
15
+ item.popUpOpened.pipe(delay(timeout), takeUntil(item.destroyed)).subscribe(() => __awaiter(void 0, void 0, void 0, function* () { return item.parsePopUpResult(yield method(item.state)); }));
16
+ };
@@ -0,0 +1,2 @@
1
+ import { OAuthBase } from '../OAuthBase';
2
+ export declare const cordovaPropertiesSet: (item: OAuthBase, packageName: string) => void;
@@ -0,0 +1,13 @@
1
+ import { OAuthParser } from '../OAuthParser';
2
+ import * as _ from 'lodash';
3
+ export const cordovaPropertiesSet = (item, packageName) => {
4
+ item.popUpTarget = 'oauth:cordova';
5
+ item.redirectUri = `${packageName}://oauth_callback`;
6
+ item.popUpIsCheckClose = false;
7
+ item.popUpMessageParser = cordovaPopUpMessageParser;
8
+ };
9
+ function cordovaPopUpMessageParser(event) {
10
+ let data = event.data;
11
+ let prefix = 'oauth::';
12
+ return _.isString(data) && data.includes(prefix) ? OAuthParser.parse(JSON.parse(data.substring(prefix.length)), '') : null;
13
+ }
@@ -9,3 +9,5 @@ export * from './ya/YaUser';
9
9
  export * from './ya/YaAuth';
10
10
  export * from './ma/MaAuth';
11
11
  export * from './ma/MaUser';
12
+ export * from './external/backend';
13
+ export * from './external/cordova';
package/esm/public-api.js CHANGED
@@ -9,3 +9,5 @@ export * from './ya/YaUser';
9
9
  export * from './ya/YaAuth';
10
10
  export * from './ma/MaAuth';
11
11
  export * from './ma/MaUser';
12
+ export * from './external/backend';
13
+ export * from './external/cordova';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-core/oauth",
3
- "version": "3.0.18",
3
+ "version": "3.0.20",
4
4
  "description": "Classes and utils for oauth",
5
5
  "main": "./cjs/public-api.js",
6
6
  "module": "./esm/public-api.js",