@ts-core/oauth 3.0.30 → 3.1.1
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.
- package/cjs/OAuthBase.d.ts +12 -42
- package/cjs/OAuthBase.js +24 -116
- package/cjs/OAuthParser.js +10 -7
- package/cjs/PopUpBase.d.ts +45 -0
- package/cjs/PopUpBase.js +145 -0
- package/cjs/external/browser.d.ts +3 -0
- package/cjs/external/browser.js +10 -19
- package/cjs/external/cordovaInAppBrowserPlugin.js +8 -8
- package/cjs/external/cordovaOAuthPlugin.js +8 -8
- package/cjs/go/GoAuth.d.ts +1 -1
- package/cjs/go/GoAuth.js +3 -3
- package/cjs/keycloak/KeycloakAuth.d.ts +1 -1
- package/cjs/keycloak/KeycloakAuth.js +6 -7
- package/cjs/ma/MaAuth.d.ts +1 -1
- package/cjs/ma/MaAuth.js +4 -4
- package/cjs/public-api.d.ts +2 -1
- package/cjs/public-api.js +2 -1
- package/cjs/vk/VkAuth.d.ts +1 -1
- package/cjs/vk/VkAuth.js +3 -3
- package/cjs/ya/YaAuth.d.ts +1 -1
- package/cjs/ya/YaAuth.js +2 -2
- package/esm/OAuthBase.d.ts +12 -42
- package/esm/OAuthBase.js +24 -116
- package/esm/OAuthParser.js +10 -7
- package/esm/PopUpBase.d.ts +45 -0
- package/esm/PopUpBase.js +141 -0
- package/esm/external/browser.d.ts +3 -0
- package/esm/external/browser.js +7 -18
- package/esm/external/cordovaInAppBrowserPlugin.js +8 -8
- package/esm/external/cordovaOAuthPlugin.js +8 -8
- package/esm/go/GoAuth.d.ts +1 -1
- package/esm/go/GoAuth.js +3 -3
- package/esm/keycloak/KeycloakAuth.d.ts +1 -1
- package/esm/keycloak/KeycloakAuth.js +5 -6
- package/esm/ma/MaAuth.d.ts +1 -1
- package/esm/ma/MaAuth.js +4 -4
- package/esm/public-api.d.ts +2 -1
- package/esm/public-api.js +2 -1
- package/esm/vk/VkAuth.d.ts +1 -1
- package/esm/vk/VkAuth.js +3 -3
- package/esm/ya/YaAuth.d.ts +1 -1
- package/esm/ya/YaAuth.js +2 -2
- package/package.json +2 -5
|
@@ -13,21 +13,20 @@ exports.KeycloakAuth = void 0;
|
|
|
13
13
|
const common_1 = require("@ts-core/common");
|
|
14
14
|
const OAuthBase_1 = require("../OAuthBase");
|
|
15
15
|
const KeycloakUser_1 = require("./KeycloakUser");
|
|
16
|
-
const url_1 = require("url");
|
|
17
16
|
const _ = require("lodash");
|
|
18
17
|
class KeycloakAuth extends OAuthBase_1.OAuthBase {
|
|
19
18
|
constructor(logger, settings, window) {
|
|
20
19
|
super(logger, settings.applicationId, window);
|
|
21
20
|
this._settings = settings;
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
21
|
+
this.params.set('scope', 'openid');
|
|
22
|
+
this.params.set('state', common_1.RandomUtil.randomString(10));
|
|
23
|
+
this.params.set('response_mode', 'query');
|
|
25
24
|
}
|
|
26
25
|
getBaseUrl() {
|
|
27
26
|
return `${this.settings.url}/realms/${this.settings.realm}/protocol/openid-connect`;
|
|
28
27
|
}
|
|
29
|
-
|
|
30
|
-
return `${this.getBaseUrl()}/auth?${this.
|
|
28
|
+
popUpUrl() {
|
|
29
|
+
return `${this.getBaseUrl()}/auth?${this.getParams().toString()}`;
|
|
31
30
|
}
|
|
32
31
|
getProfile(token) {
|
|
33
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -39,7 +38,7 @@ class KeycloakAuth extends OAuthBase_1.OAuthBase {
|
|
|
39
38
|
getTokenByCode(dto, secret) {
|
|
40
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
40
|
let item = yield this.http.call(`${this.getBaseUrl()}/token`, {
|
|
42
|
-
data: new
|
|
41
|
+
data: new URLSearchParams({
|
|
43
42
|
code: dto.codeOrToken,
|
|
44
43
|
client_id: this.applicationId,
|
|
45
44
|
client_secret: secret,
|
package/cjs/ma/MaAuth.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
|
|
|
3
3
|
import { MaUser } from "./MaUser";
|
|
4
4
|
export declare class MaAuth<T extends MaUser = MaUser> extends OAuthBase<T> {
|
|
5
5
|
constructor(logger: ILogger, applicationId: string, window?: Window);
|
|
6
|
-
|
|
6
|
+
popUpUrl(): string;
|
|
7
7
|
getProfile(token: string): Promise<T>;
|
|
8
8
|
getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
9
9
|
}
|
package/cjs/ma/MaAuth.js
CHANGED
|
@@ -17,11 +17,11 @@ const _ = require("lodash");
|
|
|
17
17
|
class MaAuth extends OAuthBase_1.OAuthBase {
|
|
18
18
|
constructor(logger, applicationId, window) {
|
|
19
19
|
super(logger, applicationId, window);
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
20
|
+
this.params.set('scope', 'userinfo');
|
|
21
|
+
this.params.set('state', common_1.RandomUtil.randomString(43));
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return `https://oauth.mail.ru/login?${this.
|
|
23
|
+
popUpUrl() {
|
|
24
|
+
return `https://oauth.mail.ru/login?${this.getParams().toString()}`;
|
|
25
25
|
}
|
|
26
26
|
getProfile(token) {
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
package/cjs/public-api.d.ts
CHANGED
package/cjs/public-api.js
CHANGED
|
@@ -14,8 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./PopUpBase"), exports);
|
|
18
18
|
__exportStar(require("./OAuthUser"), exports);
|
|
19
|
+
__exportStar(require("./OAuthBase"), exports);
|
|
19
20
|
__exportStar(require("./OAuthParser"), exports);
|
|
20
21
|
__exportStar(require("./go/GoUser"), exports);
|
|
21
22
|
__exportStar(require("./go/GoAuth"), exports);
|
package/cjs/vk/VkAuth.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare class VkAuth<T extends VkUser = VkUser> extends OAuthBase<T> {
|
|
|
5
5
|
v: string;
|
|
6
6
|
private email;
|
|
7
7
|
constructor(logger: ILogger, applicationId: string, window?: Window);
|
|
8
|
-
|
|
8
|
+
popUpUrl(): string;
|
|
9
9
|
getProfile(token: string, fields?: string): Promise<T>;
|
|
10
10
|
getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
11
11
|
}
|
package/cjs/vk/VkAuth.js
CHANGED
|
@@ -18,10 +18,10 @@ class VkAuth extends OAuthBase_1.OAuthBase {
|
|
|
18
18
|
constructor(logger, applicationId, window) {
|
|
19
19
|
super(logger, applicationId, window);
|
|
20
20
|
this.v = '5.131';
|
|
21
|
-
this.
|
|
21
|
+
this.params.set('scope', 'status,email');
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return `https://oauth.vk.com/authorize?${this.
|
|
23
|
+
popUpUrl() {
|
|
24
|
+
return `https://oauth.vk.com/authorize?${this.getParams().toString()}`;
|
|
25
25
|
}
|
|
26
26
|
getProfile(token, fields) {
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
package/cjs/ya/YaAuth.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
|
|
2
2
|
import { YaUser } from "./YaUser";
|
|
3
3
|
export declare class YaAuth<T extends YaUser = YaUser> extends OAuthBase<T> {
|
|
4
|
-
|
|
4
|
+
popUpUrl(): string;
|
|
5
5
|
getProfile(token: string): Promise<T>;
|
|
6
6
|
getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
7
7
|
}
|
package/cjs/ya/YaAuth.js
CHANGED
|
@@ -15,8 +15,8 @@ const OAuthBase_1 = require("../OAuthBase");
|
|
|
15
15
|
const YaUser_1 = require("./YaUser");
|
|
16
16
|
const _ = require("lodash");
|
|
17
17
|
class YaAuth extends OAuthBase_1.OAuthBase {
|
|
18
|
-
|
|
19
|
-
return `https://oauth.yandex.ru/authorize?${this.
|
|
18
|
+
popUpUrl() {
|
|
19
|
+
return `https://oauth.yandex.ru/authorize?${this.getParams().toString()}`;
|
|
20
20
|
}
|
|
21
21
|
getProfile(token) {
|
|
22
22
|
return __awaiter(this, void 0, void 0, function* () {
|
package/esm/OAuthBase.d.ts
CHANGED
|
@@ -1,56 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare abstract class OAuthBase<T = any> extends
|
|
1
|
+
import { ILogger, TransportHttp, ExtendedError } from "@ts-core/common";
|
|
2
|
+
import { PopUpBase } from "./PopUpBase";
|
|
3
|
+
export declare abstract class OAuthBase<T = any> extends PopUpBase<IOAuthDto> {
|
|
4
4
|
protected applicationId: string;
|
|
5
|
-
protected window?: Window;
|
|
6
|
-
static ERROR_WINDOW_CLOSED: string;
|
|
7
|
-
popUpWidth: number;
|
|
8
|
-
popUpHeight: number;
|
|
9
|
-
popUpTarget: string;
|
|
10
|
-
popUpOpener: IOAuthPopUpOpener;
|
|
11
|
-
popUpMessageParser: IOAuthPopUpMessageParser;
|
|
12
5
|
redirectUri: string;
|
|
13
|
-
isRejectWhenPopUpClosed: boolean;
|
|
14
6
|
protected http: TransportHttp;
|
|
15
|
-
protected subject: Subject<ObservableData<OAuthEvent, Window>>;
|
|
16
|
-
protected popUp: Window;
|
|
17
|
-
protected promise: PromiseHandler<IOAuthDto>;
|
|
18
7
|
protected responseType: string;
|
|
19
|
-
protected _urlParams: Map<string, string>;
|
|
20
|
-
protected _popUpCheckCloseTimer: any;
|
|
21
8
|
constructor(logger: ILogger, applicationId: string, window?: Window);
|
|
22
|
-
protected
|
|
23
|
-
protected
|
|
24
|
-
protected
|
|
25
|
-
protected
|
|
26
|
-
protected
|
|
27
|
-
protected popUpMessageHandler: (event: MessageEvent) => void;
|
|
28
|
-
abstract getPopUpUrl(): string;
|
|
29
|
-
getOriginUrl(): string;
|
|
30
|
-
getRedirectUri(): string;
|
|
9
|
+
protected getRedirectUri(): string;
|
|
10
|
+
protected getParams(): URLSearchParams;
|
|
11
|
+
protected parseMessageData(item: any): IOAuthDto;
|
|
12
|
+
protected isMessageError(item: any): boolean;
|
|
13
|
+
protected parseMessageError(item: any): ExtendedError;
|
|
31
14
|
abstract getProfile(token: string, ...params: any[]): Promise<T>;
|
|
32
15
|
abstract getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
33
|
-
parseResponse(data: IOAuthPopUpDto): void;
|
|
34
16
|
getCode(): Promise<IOAuthDto>;
|
|
35
17
|
getToken(): Promise<IOAuthDto>;
|
|
36
|
-
|
|
18
|
+
parse(item: any): void;
|
|
37
19
|
destroy(): void;
|
|
38
|
-
protected get isPopUpOpened(): boolean;
|
|
39
|
-
protected get popUpCheckCloseTimer(): any;
|
|
40
|
-
protected set popUpCheckCloseTimer(value: any);
|
|
41
|
-
get urlParams(): Map<string, string>;
|
|
42
20
|
get state(): string;
|
|
43
|
-
get events(): Observable<ObservableData<OAuthEvent, Window>>;
|
|
44
|
-
get popUpClosed(): Observable<Window>;
|
|
45
|
-
get popUpOpened(): Observable<Window>;
|
|
46
21
|
}
|
|
47
22
|
export interface IOAuthDto {
|
|
48
23
|
codeOrToken: string;
|
|
49
24
|
redirectUri: string;
|
|
50
25
|
}
|
|
51
26
|
export interface IOAuthPopUpDto {
|
|
52
|
-
|
|
53
|
-
|
|
27
|
+
oauthError?: string;
|
|
28
|
+
oauthCodeOrToken?: string;
|
|
29
|
+
oauthErrorDescription?: string;
|
|
54
30
|
}
|
|
55
31
|
export interface IOAuthToken {
|
|
56
32
|
expiresIn: number;
|
|
@@ -63,9 +39,3 @@ export interface IOAuthToken {
|
|
|
63
39
|
refreshToken?: string;
|
|
64
40
|
refreshExpiresIn?: string;
|
|
65
41
|
}
|
|
66
|
-
export declare enum OAuthEvent {
|
|
67
|
-
POPUP_OPENED = "POPUP_OPENED",
|
|
68
|
-
POPUP_CLOSED = "POPUP_CLOSED"
|
|
69
|
-
}
|
|
70
|
-
export type IOAuthPopUpOpener = <T extends OAuthBase>(item: T, window: Window) => Window;
|
|
71
|
-
export type IOAuthPopUpMessageParser = <T extends OAuthBase>(item: T, event: MessageEvent) => IOAuthPopUpDto;
|
package/esm/OAuthBase.js
CHANGED
|
@@ -7,79 +7,42 @@ 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 {
|
|
10
|
+
import { TransportHttp, RandomUtil, ExtendedError } from "@ts-core/common";
|
|
11
11
|
import * as _ from 'lodash';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
12
|
+
import { Subject } from "rxjs";
|
|
13
|
+
import { PopUpBase } from "./PopUpBase";
|
|
14
|
+
import { OAuthBrowserPropertiesSet } from "./public-api";
|
|
15
|
+
export class OAuthBase extends PopUpBase {
|
|
15
16
|
constructor(logger, applicationId, window) {
|
|
16
|
-
super(logger);
|
|
17
|
+
super(logger, window);
|
|
17
18
|
this.applicationId = applicationId;
|
|
18
|
-
this.window = window;
|
|
19
|
-
this.popUpCheckClose = () => {
|
|
20
|
-
if (!this.isPopUpOpened) {
|
|
21
|
-
this.close();
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
this.popUpOpenHandler = () => this.popUpOpener(this, this.window);
|
|
25
|
-
this.popUpMessageHandler = (event) => this.parseResponse(this.popUpMessageParser(this, event));
|
|
26
19
|
this.http = new TransportHttp(logger, { method: 'get' });
|
|
27
20
|
this.subject = new Subject();
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.popUpTarget = '_blank';
|
|
31
|
-
this._urlParams = new Map();
|
|
32
|
-
this.urlParams.set('state', RandomUtil.randomString());
|
|
33
|
-
this.urlParams.set('display', 'popup');
|
|
21
|
+
this.params.set('state', RandomUtil.randomString());
|
|
22
|
+
this.params.set('display', 'popup');
|
|
34
23
|
OAuthBrowserPropertiesSet(this);
|
|
35
24
|
}
|
|
36
|
-
|
|
37
|
-
return
|
|
38
|
-
if (!_.isNil(this.promise)) {
|
|
39
|
-
this.popUpFocus();
|
|
40
|
-
return this.promise.promise;
|
|
41
|
-
}
|
|
42
|
-
this.promise = PromiseHandler.create();
|
|
43
|
-
this.popUp = this.popUpOpenHandler();
|
|
44
|
-
this.popUpFocus();
|
|
45
|
-
this.window.addEventListener('message', this.popUpMessageHandler, false);
|
|
46
|
-
this.subject.next(new ObservableData(OAuthEvent.POPUP_OPENED, this.popUp));
|
|
47
|
-
this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, DateUtil.MILLISECONDS_SECOND / 5);
|
|
48
|
-
return this.promise.promise;
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
popUpFocus() {
|
|
52
|
-
if (this.isPopUpOpened && _.isFunction(this.popUp.focus)) {
|
|
53
|
-
this.popUp.focus();
|
|
54
|
-
}
|
|
25
|
+
getRedirectUri() {
|
|
26
|
+
return !_.isNil(this.redirectUri) ? this.redirectUri : `${this.originUrl}/oauth`;
|
|
55
27
|
}
|
|
56
|
-
|
|
57
|
-
let item =
|
|
28
|
+
getParams() {
|
|
29
|
+
let item = super.getParams();
|
|
58
30
|
item.append('client_id', this.applicationId);
|
|
59
31
|
item.append('redirect_uri', this.getRedirectUri());
|
|
60
32
|
item.append('response_type', this.responseType);
|
|
61
|
-
this.urlParams.forEach((value, key) => item.append(key, value));
|
|
62
33
|
return item;
|
|
63
34
|
}
|
|
64
|
-
|
|
65
|
-
return
|
|
35
|
+
parseMessageData(item) {
|
|
36
|
+
return {
|
|
37
|
+
codeOrToken: item.oauthCodeOrToken,
|
|
38
|
+
redirectUri: this.getRedirectUri()
|
|
39
|
+
};
|
|
66
40
|
}
|
|
67
|
-
|
|
68
|
-
return !_.isNil(
|
|
41
|
+
isMessageError(item) {
|
|
42
|
+
return !_.isNil(item.oauthError);
|
|
69
43
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (!_.isEmpty(data.oAuthCodeOrToken)) {
|
|
75
|
-
this.promise.resolve({ redirectUri: this.getRedirectUri(), codeOrToken: data.oAuthCodeOrToken });
|
|
76
|
-
this.promise = null;
|
|
77
|
-
}
|
|
78
|
-
if (!_.isEmpty(data.oAuthError)) {
|
|
79
|
-
this.promise.reject(data.oAuthError);
|
|
80
|
-
this.promise = null;
|
|
81
|
-
}
|
|
82
|
-
this.close();
|
|
44
|
+
parseMessageError(item) {
|
|
45
|
+
return new ExtendedError(item.oauthError, item.oauthErrorDescription);
|
|
83
46
|
}
|
|
84
47
|
getCode() {
|
|
85
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -93,75 +56,20 @@ export class OAuthBase extends LoggerWrapper {
|
|
|
93
56
|
return this.open();
|
|
94
57
|
});
|
|
95
58
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.popUpCheckCloseTimer = null;
|
|
99
|
-
if (_.isNil(this.popUp)) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
this.subject.next(new ObservableData(OAuthEvent.POPUP_CLOSED, this.popUp));
|
|
103
|
-
this.popUp.close();
|
|
104
|
-
this.popUp = null;
|
|
105
|
-
if (this.isRejectWhenPopUpClosed && !_.isNil(this.promise)) {
|
|
106
|
-
this.promise.reject(OAuthBase.ERROR_WINDOW_CLOSED);
|
|
107
|
-
this.promise = null;
|
|
108
|
-
}
|
|
59
|
+
parse(item) {
|
|
60
|
+
super.popUpMessageParse(item);
|
|
109
61
|
}
|
|
110
62
|
destroy() {
|
|
111
63
|
if (this.isDestroyed) {
|
|
112
64
|
return;
|
|
113
65
|
}
|
|
114
66
|
super.destroy();
|
|
115
|
-
this.close();
|
|
116
|
-
if (!_.isNil(this.promise)) {
|
|
117
|
-
this.promise.reject(OAuthBase.ERROR_WINDOW_CLOSED);
|
|
118
|
-
this.promise = null;
|
|
119
|
-
}
|
|
120
|
-
if (!_.isNil(this.urlParams)) {
|
|
121
|
-
this.urlParams.clear();
|
|
122
|
-
this._urlParams = null;
|
|
123
|
-
}
|
|
124
67
|
if (!_.isNil(this.http)) {
|
|
125
68
|
this.http.destroy();
|
|
126
69
|
this.http = null;
|
|
127
70
|
}
|
|
128
|
-
if (!_.isNil(this.subject)) {
|
|
129
|
-
this.subject.complete();
|
|
130
|
-
this.subject = null;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
get isPopUpOpened() {
|
|
134
|
-
return !_.isNil(this.popUp) && !this.popUp.closed;
|
|
135
|
-
}
|
|
136
|
-
get popUpCheckCloseTimer() {
|
|
137
|
-
return this._popUpCheckCloseTimer;
|
|
138
|
-
}
|
|
139
|
-
set popUpCheckCloseTimer(value) {
|
|
140
|
-
if (value === this._popUpCheckCloseTimer) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
clearInterval(this._popUpCheckCloseTimer);
|
|
144
|
-
this._popUpCheckCloseTimer = value;
|
|
145
|
-
}
|
|
146
|
-
get urlParams() {
|
|
147
|
-
return this._urlParams;
|
|
148
71
|
}
|
|
149
72
|
get state() {
|
|
150
|
-
return !_.isNil(this.
|
|
151
|
-
}
|
|
152
|
-
get events() {
|
|
153
|
-
return !_.isNil(this.subject) ? this.subject.asObservable() : null;
|
|
154
|
-
}
|
|
155
|
-
get popUpClosed() {
|
|
156
|
-
return this.events.pipe(filter(item => item.type === OAuthEvent.POPUP_CLOSED), map(item => item.data));
|
|
157
|
-
}
|
|
158
|
-
get popUpOpened() {
|
|
159
|
-
return this.events.pipe(filter(item => item.type === OAuthEvent.POPUP_OPENED), map(item => item.data));
|
|
73
|
+
return !_.isNil(this.params) ? this.params.get('state') : null;
|
|
160
74
|
}
|
|
161
75
|
}
|
|
162
|
-
OAuthBase.ERROR_WINDOW_CLOSED = 'WINDOW_CLOSED';
|
|
163
|
-
export var OAuthEvent;
|
|
164
|
-
(function (OAuthEvent) {
|
|
165
|
-
OAuthEvent["POPUP_OPENED"] = "POPUP_OPENED";
|
|
166
|
-
OAuthEvent["POPUP_CLOSED"] = "POPUP_CLOSED";
|
|
167
|
-
})(OAuthEvent || (OAuthEvent = {}));
|
package/esm/OAuthParser.js
CHANGED
|
@@ -6,18 +6,21 @@ export class OAuthParser {
|
|
|
6
6
|
if (!_.isEmpty(fragment)) {
|
|
7
7
|
new URLSearchParams(fragment).forEach((value, key) => item[key] = value);
|
|
8
8
|
}
|
|
9
|
-
let
|
|
10
|
-
let
|
|
9
|
+
let oauthError = null;
|
|
10
|
+
let oauthCodeOrToken = null;
|
|
11
11
|
for (let key in item) {
|
|
12
12
|
let value = item[key];
|
|
13
13
|
if (OAuthParser.NAMES.includes(key)) {
|
|
14
|
-
|
|
14
|
+
oauthCodeOrToken = value;
|
|
15
15
|
}
|
|
16
16
|
else if (OAuthParser.ERRORS.includes(key)) {
|
|
17
|
-
|
|
17
|
+
oauthError = value;
|
|
18
18
|
}
|
|
19
|
-
if (!_.isEmpty(
|
|
20
|
-
return {
|
|
19
|
+
if (!_.isEmpty(oauthCodeOrToken)) {
|
|
20
|
+
return { oauthCodeOrToken };
|
|
21
|
+
}
|
|
22
|
+
else if (!_.isEmpty(oauthError)) {
|
|
23
|
+
return { oauthErrorDescription: item.error_description, oauthError };
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
26
|
;
|
|
@@ -25,4 +28,4 @@ export class OAuthParser {
|
|
|
25
28
|
}
|
|
26
29
|
}
|
|
27
30
|
OAuthParser.NAMES = ['code', 'access_token'];
|
|
28
|
-
OAuthParser.ERRORS = ['
|
|
31
|
+
OAuthParser.ERRORS = ['error', 'error_description'];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { PromiseHandler, ObservableData, LoggerWrapper, ILogger, ExtendedError } from "@ts-core/common";
|
|
2
|
+
import { Observable, Subject } from "rxjs";
|
|
3
|
+
export declare abstract class PopUpBase<U> extends LoggerWrapper {
|
|
4
|
+
static ERROR_WINDOW_CLOSED: string;
|
|
5
|
+
popUpWidth: number;
|
|
6
|
+
popUpHeight: number;
|
|
7
|
+
popUpTarget: string;
|
|
8
|
+
popUpOpener: IPopUpOpener;
|
|
9
|
+
popUpMessageEventParser: IPopUpMessageEventParser;
|
|
10
|
+
protected popUp: Window;
|
|
11
|
+
protected window: Window;
|
|
12
|
+
protected subject: Subject<ObservableData<PopUpEvent, Window>>;
|
|
13
|
+
protected promise: PromiseHandler<U, ExtendedError>;
|
|
14
|
+
protected _params: Map<string, string>;
|
|
15
|
+
protected _popUpCheckCloseTimer: any;
|
|
16
|
+
isRejectWhenPopUpClosed: boolean;
|
|
17
|
+
constructor(logger: ILogger, window?: Window);
|
|
18
|
+
protected popUpFocus(): void;
|
|
19
|
+
protected popUpCheckClose: () => void;
|
|
20
|
+
protected getParams(): URLSearchParams;
|
|
21
|
+
protected popUpMessageHandler(event: MessageEvent): void;
|
|
22
|
+
protected popUpMessageParse(item: any): void;
|
|
23
|
+
protected popUpMessageHandlerProxy: (event: MessageEvent) => void;
|
|
24
|
+
protected abstract isMessageError(item: any): boolean;
|
|
25
|
+
protected abstract parseMessageData(item: any): U;
|
|
26
|
+
protected abstract parseMessageError(item: any): ExtendedError;
|
|
27
|
+
open(...params: any[]): Promise<U>;
|
|
28
|
+
close(): void;
|
|
29
|
+
destroy(): void;
|
|
30
|
+
abstract popUpUrl(): string;
|
|
31
|
+
protected get isPopUpOpened(): boolean;
|
|
32
|
+
protected get popUpCheckCloseTimer(): any;
|
|
33
|
+
protected set popUpCheckCloseTimer(value: any);
|
|
34
|
+
get originUrl(): string;
|
|
35
|
+
get params(): Map<string, string>;
|
|
36
|
+
get events(): Observable<ObservableData<PopUpEvent, Window>>;
|
|
37
|
+
get closed(): Observable<Window>;
|
|
38
|
+
get opened(): Observable<Window>;
|
|
39
|
+
}
|
|
40
|
+
export type IPopUpOpener = <T extends PopUpBase<U>, U>(popUp: T, window: Window) => Window;
|
|
41
|
+
export type IPopUpMessageEventParser = (event: MessageEvent) => any;
|
|
42
|
+
export declare enum PopUpEvent {
|
|
43
|
+
OPENED = "OPENED",
|
|
44
|
+
CLOSED = "CLOSED"
|
|
45
|
+
}
|
package/esm/PopUpBase.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
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 { PromiseHandler, DateUtil, ObservableData, LoggerWrapper, ExtendedError } from "@ts-core/common";
|
|
11
|
+
import { filter, map, Subject } from "rxjs";
|
|
12
|
+
import { popUpOpener } from "./external";
|
|
13
|
+
import * as _ from 'lodash';
|
|
14
|
+
export class PopUpBase extends LoggerWrapper {
|
|
15
|
+
constructor(logger, window) {
|
|
16
|
+
super(logger);
|
|
17
|
+
this.popUpCheckClose = () => {
|
|
18
|
+
if (!this.isPopUpOpened) {
|
|
19
|
+
this.close();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
this.popUpMessageHandlerProxy = (event) => this.popUpMessageHandler(event);
|
|
23
|
+
this.window = window;
|
|
24
|
+
this.subject = new Subject();
|
|
25
|
+
this._params = new Map();
|
|
26
|
+
this.popUpWidth = 430;
|
|
27
|
+
this.popUpHeight = 520;
|
|
28
|
+
this.popUpTarget = '_blank';
|
|
29
|
+
this.popUpOpener = popUpOpener;
|
|
30
|
+
this.isRejectWhenPopUpClosed = true;
|
|
31
|
+
}
|
|
32
|
+
popUpFocus() {
|
|
33
|
+
if (this.isPopUpOpened && _.isFunction(this.popUp.focus)) {
|
|
34
|
+
this.popUp.focus();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
getParams() {
|
|
38
|
+
let item = new URLSearchParams();
|
|
39
|
+
this.params.forEach((value, key) => item.append(key, value));
|
|
40
|
+
return item;
|
|
41
|
+
}
|
|
42
|
+
popUpMessageHandler(event) {
|
|
43
|
+
if (event.origin === this.originUrl && !_.isNil(event.data)) {
|
|
44
|
+
this.popUpMessageParse(this.popUpMessageEventParser(event));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
popUpMessageParse(item) {
|
|
48
|
+
if (_.isNil(item)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (this.isMessageError(item)) {
|
|
52
|
+
this.promise.reject(this.parseMessageError(item));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.promise.resolve(this.parseMessageData(item));
|
|
56
|
+
}
|
|
57
|
+
this.close();
|
|
58
|
+
}
|
|
59
|
+
open(...params) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
if (!_.isNil(this.promise)) {
|
|
62
|
+
this.popUpFocus();
|
|
63
|
+
return this.promise.promise;
|
|
64
|
+
}
|
|
65
|
+
this.promise = PromiseHandler.create();
|
|
66
|
+
this.popUp = this.popUpOpener(this, this.window);
|
|
67
|
+
this.popUpFocus();
|
|
68
|
+
this.window.addEventListener('message', this.popUpMessageHandlerProxy, false);
|
|
69
|
+
this.subject.next(new ObservableData(PopUpEvent.OPENED, this.popUp));
|
|
70
|
+
this.popUpCheckCloseTimer = setInterval(this.popUpCheckClose, DateUtil.MILLISECONDS_SECOND / 5);
|
|
71
|
+
return this.promise.promise;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
close() {
|
|
75
|
+
this.window.removeEventListener('message', this.popUpMessageHandlerProxy, false);
|
|
76
|
+
this.popUpCheckCloseTimer = null;
|
|
77
|
+
if (_.isNil(this.popUp)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.subject.next(new ObservableData(PopUpEvent.CLOSED, this.popUp));
|
|
81
|
+
this.popUp.close();
|
|
82
|
+
this.popUp = null;
|
|
83
|
+
if (this.isRejectWhenPopUpClosed && !_.isNil(this.promise)) {
|
|
84
|
+
this.promise.reject(new ExtendedError(PopUpBase.ERROR_WINDOW_CLOSED, PopUpBase.ERROR_WINDOW_CLOSED));
|
|
85
|
+
this.promise = null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
destroy() {
|
|
89
|
+
if (this.isDestroyed) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
super.destroy();
|
|
93
|
+
this.close();
|
|
94
|
+
if (!_.isNil(this.promise)) {
|
|
95
|
+
this.promise.reject(new ExtendedError(PopUpBase.ERROR_WINDOW_CLOSED, PopUpBase.ERROR_WINDOW_CLOSED));
|
|
96
|
+
this.promise = null;
|
|
97
|
+
}
|
|
98
|
+
if (!_.isNil(this.params)) {
|
|
99
|
+
this.params.clear();
|
|
100
|
+
this._params = null;
|
|
101
|
+
}
|
|
102
|
+
if (!_.isNil(this.subject)) {
|
|
103
|
+
this.subject.complete();
|
|
104
|
+
this.subject = null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
get isPopUpOpened() {
|
|
108
|
+
return !_.isNil(this.popUp) && !this.popUp.closed;
|
|
109
|
+
}
|
|
110
|
+
get popUpCheckCloseTimer() {
|
|
111
|
+
return this._popUpCheckCloseTimer;
|
|
112
|
+
}
|
|
113
|
+
set popUpCheckCloseTimer(value) {
|
|
114
|
+
if (value === this._popUpCheckCloseTimer) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
clearInterval(this._popUpCheckCloseTimer);
|
|
118
|
+
this._popUpCheckCloseTimer = value;
|
|
119
|
+
}
|
|
120
|
+
get originUrl() {
|
|
121
|
+
return this.window.location.origin;
|
|
122
|
+
}
|
|
123
|
+
get params() {
|
|
124
|
+
return this._params;
|
|
125
|
+
}
|
|
126
|
+
get events() {
|
|
127
|
+
return !_.isNil(this.subject) ? this.subject.asObservable() : null;
|
|
128
|
+
}
|
|
129
|
+
get closed() {
|
|
130
|
+
return this.events.pipe(filter(item => item.type === PopUpEvent.CLOSED), map(item => item.data));
|
|
131
|
+
}
|
|
132
|
+
get opened() {
|
|
133
|
+
return this.events.pipe(filter(item => item.type === PopUpEvent.OPENED), map(item => item.data));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
PopUpBase.ERROR_WINDOW_CLOSED = 'WINDOW_CLOSED';
|
|
137
|
+
export var PopUpEvent;
|
|
138
|
+
(function (PopUpEvent) {
|
|
139
|
+
PopUpEvent["OPENED"] = "OPENED";
|
|
140
|
+
PopUpEvent["CLOSED"] = "CLOSED";
|
|
141
|
+
})(PopUpEvent || (PopUpEvent = {}));
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { OAuthBase } from '../OAuthBase';
|
|
2
|
+
import { PopUpBase } from '../PopUpBase';
|
|
2
3
|
export declare const OAuthBrowserPropertiesSet: (item: OAuthBase) => void;
|
|
4
|
+
export declare function popUpOpener<T extends PopUpBase<U>, U>(popUp: T, window: Window): Window;
|
|
5
|
+
export declare function popUpMessageEventParser(event: MessageEvent): any;
|
package/esm/external/browser.js
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
1
|
import * as _ from 'lodash';
|
|
2
|
-
import { ObjectUtil } from '@ts-core/common';
|
|
3
2
|
export const OAuthBrowserPropertiesSet = (item) => {
|
|
4
3
|
item.popUpOpener = popUpOpener;
|
|
5
|
-
item.
|
|
4
|
+
item.popUpMessageEventParser = popUpMessageEventParser;
|
|
6
5
|
item.isRejectWhenPopUpClosed = true;
|
|
7
6
|
};
|
|
8
|
-
function popUpOpener(
|
|
9
|
-
let top = (window.screen.height -
|
|
10
|
-
let left = (window.screen.width -
|
|
11
|
-
return window.open(
|
|
7
|
+
export function popUpOpener(popUp, window) {
|
|
8
|
+
let top = (window.screen.height - popUp.popUpHeight) / 2;
|
|
9
|
+
let left = (window.screen.width - popUp.popUpWidth) / 2;
|
|
10
|
+
return window.open(popUp.popUpUrl(), popUp.popUpTarget, `scrollbars=yes,width=${popUp.popUpWidth},height=${popUp.popUpHeight},top=${top},left=${left}`);
|
|
12
11
|
}
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
let data = event.data;
|
|
18
|
-
if (!_.isObject(data)) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
if (!ObjectUtil.hasOwnProperty(data, 'oAuthCodeOrToken') && !ObjectUtil.hasOwnProperty(data, 'oAuthError')) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
return data;
|
|
12
|
+
export function popUpMessageEventParser(event) {
|
|
13
|
+
return !_.isNil(event.data.oauthError) || !_.isNil(event.data.oauthCodeOrToken) ? event.data : null;
|
|
25
14
|
}
|