@ts-core/oauth 3.0.3 → 3.0.5

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.
Files changed (43) hide show
  1. package/cjs/OAuthBase.d.ts +7 -5
  2. package/cjs/OAuthBase.js +23 -6
  3. package/cjs/OAuthUser.d.ts +3 -3
  4. package/cjs/OAuthUser.js +1 -5
  5. package/cjs/go/{GoOAuth.d.ts → GoAuth.d.ts} +3 -3
  6. package/cjs/go/{GoOAuth.js → GoAuth.js} +20 -21
  7. package/cjs/go/GoUser.d.ts +1 -1
  8. package/cjs/ma/MaAuth.d.ts +10 -0
  9. package/cjs/ma/MaAuth.js +64 -0
  10. package/cjs/ma/MaUser.d.ts +8 -0
  11. package/cjs/ma/MaUser.js +28 -0
  12. package/cjs/public-api.d.ts +3 -3
  13. package/cjs/public-api.js +3 -3
  14. package/cjs/vk/VkAuth.d.ts +10 -0
  15. package/cjs/vk/{VkOAuth.js → VkAuth.js} +26 -16
  16. package/cjs/vk/VkUser.d.ts +1 -1
  17. package/cjs/ya/{YaOAuth.d.ts → YaAuth.d.ts} +3 -3
  18. package/cjs/ya/YaAuth.js +57 -0
  19. package/cjs/ya/YaUser.d.ts +1 -1
  20. package/esm/OAuthBase.d.ts +7 -5
  21. package/esm/OAuthBase.js +23 -6
  22. package/esm/OAuthUser.d.ts +3 -3
  23. package/esm/OAuthUser.js +1 -5
  24. package/esm/go/{GoOAuth.d.ts → GoAuth.d.ts} +3 -3
  25. package/esm/go/{GoOAuth.js → GoAuth.js} +18 -19
  26. package/esm/go/GoUser.d.ts +1 -1
  27. package/esm/ma/MaAuth.d.ts +10 -0
  28. package/esm/ma/MaAuth.js +60 -0
  29. package/esm/ma/MaUser.d.ts +8 -0
  30. package/esm/ma/MaUser.js +24 -0
  31. package/esm/public-api.d.ts +3 -3
  32. package/esm/public-api.js +3 -3
  33. package/esm/vk/VkAuth.d.ts +10 -0
  34. package/esm/vk/{VkOAuth.js → VkAuth.js} +24 -14
  35. package/esm/vk/VkUser.d.ts +1 -1
  36. package/esm/ya/{YaOAuth.d.ts → YaAuth.d.ts} +3 -3
  37. package/esm/ya/YaAuth.js +53 -0
  38. package/esm/ya/YaUser.d.ts +1 -1
  39. package/package.json +1 -1
  40. package/cjs/vk/VkOAuth.d.ts +0 -7
  41. package/cjs/ya/YaOAuth.js +0 -57
  42. package/esm/vk/VkOAuth.d.ts +0 -7
  43. package/esm/ya/YaOAuth.js +0 -53
@@ -1,8 +1,7 @@
1
1
  import { PromiseHandler, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
2
2
  export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
3
- protected window: Window;
4
3
  protected applicationId: string;
5
- protected scope?: string;
4
+ protected window?: Window;
6
5
  protected http: TransportHttp;
7
6
  protected timer: any;
8
7
  protected popUp: Window;
@@ -10,12 +9,14 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
10
9
  protected popUpWidth: number;
11
10
  protected popUpHeight: number;
12
11
  protected responseType: string;
13
- constructor(logger: ILogger, window: Window, applicationId: string, scope?: string);
12
+ protected _urlParams: Map<string, string>;
13
+ constructor(logger: ILogger, applicationId: string, window?: Window);
14
14
  protected open(): Promise<IOAuthDto>;
15
- protected openPopup(url: string, target: string): Window;
15
+ protected openPopup(): Window;
16
16
  protected checkPopUp: () => void;
17
+ protected getUrlParams(): URLSearchParams;
18
+ protected abstract getUrl(): string;
17
19
  protected messageHandler: (event: MessageEvent<IOAuthPopUpDto>) => void;
18
- protected abstract getAuthUrl(): string;
19
20
  protected get redirectUri(): string;
20
21
  protected get originUrl(): string;
21
22
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -24,6 +25,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
24
25
  getToken(): Promise<IOAuthDto>;
25
26
  close(): void;
26
27
  destroy(): void;
28
+ get urlParams(): Map<string, string>;
27
29
  }
28
30
  export interface IOAuthDto {
29
31
  codeOrToken: string;
package/cjs/OAuthBase.js CHANGED
@@ -13,11 +13,10 @@ exports.OAuthBase = void 0;
13
13
  const common_1 = require("@ts-core/common");
14
14
  const _ = require("lodash");
15
15
  class OAuthBase extends common_1.LoggerWrapper {
16
- constructor(logger, window, applicationId, scope) {
16
+ constructor(logger, applicationId, window) {
17
17
  super(logger);
18
- this.window = window;
19
18
  this.applicationId = applicationId;
20
- this.scope = scope;
19
+ this.window = window;
21
20
  this.popUpWidth = 640;
22
21
  this.popUpHeight = 480;
23
22
  this.checkPopUp = () => {
@@ -41,6 +40,8 @@ class OAuthBase extends common_1.LoggerWrapper {
41
40
  }
42
41
  };
43
42
  this.http = new common_1.TransportHttp(logger, { method: 'get' });
43
+ this._urlParams = new Map();
44
+ this.urlParams.set('display', 'popup');
44
45
  }
45
46
  open() {
46
47
  return __awaiter(this, void 0, void 0, function* () {
@@ -48,20 +49,29 @@ class OAuthBase extends common_1.LoggerWrapper {
48
49
  return this.promise.promise;
49
50
  }
50
51
  this.promise = common_1.PromiseHandler.create();
51
- this.popUp = this.openPopup(this.getAuthUrl(), '_blank');
52
+ this.popUp = this.openPopup();
52
53
  this.window.addEventListener('message', this.messageHandler, false);
53
54
  this.timer = setInterval(this.checkPopUp, common_1.DateUtil.MILLISECONDS_NANOSECOND / 2);
54
55
  return this.promise.promise;
55
56
  });
56
57
  }
57
- openPopup(url, target) {
58
+ openPopup() {
58
59
  let window = this.window;
59
60
  let top = (window.screen.height - this.popUpHeight) / 2;
60
61
  let left = (window.screen.width - this.popUpWidth) / 2;
61
- let item = window.open(url, target, `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
62
+ let item = window.open(this.getUrl(), '_blank', `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
62
63
  item.focus();
63
64
  return item;
64
65
  }
66
+ getUrlParams() {
67
+ let item = new URLSearchParams();
68
+ item.append('display', 'popup');
69
+ item.append('client_id', this.applicationId);
70
+ item.append('redirect_uri', this.redirectUri);
71
+ item.append('response_type', this.responseType);
72
+ this.urlParams.forEach((value, key) => item.append(key, value));
73
+ return item;
74
+ }
65
75
  get redirectUri() {
66
76
  return `${this.originUrl}/oauth`;
67
77
  }
@@ -99,10 +109,17 @@ class OAuthBase extends common_1.LoggerWrapper {
99
109
  }
100
110
  super.destroy();
101
111
  this.close();
112
+ if (!_.isNil(this.urlParams)) {
113
+ this.urlParams.clear();
114
+ this._urlParams = null;
115
+ }
102
116
  if (!_.isNil(this.http)) {
103
117
  this.http.destroy();
104
118
  this.http = null;
105
119
  }
106
120
  }
121
+ get urlParams() {
122
+ return this._urlParams;
123
+ }
107
124
  }
108
125
  exports.OAuthBase = OAuthBase;
@@ -1,5 +1,5 @@
1
1
  export declare abstract class OAuthUser {
2
- id: string | number;
2
+ id: string;
3
3
  name: string;
4
4
  city?: string;
5
5
  phone?: string;
@@ -17,7 +17,7 @@ export declare abstract class OAuthUser {
17
17
  telegram?: string;
18
18
  instagram?: string;
19
19
  birthday?: Date;
20
- constructor(item?: any);
21
- protected abstract parse(item: any): void;
20
+ constructor();
21
+ abstract parse(item: any): void;
22
22
  get location(): string;
23
23
  }
package/cjs/OAuthUser.js CHANGED
@@ -13,11 +13,7 @@ exports.OAuthUser = void 0;
13
13
  const _ = require("lodash");
14
14
  const class_transformer_1 = require("class-transformer");
15
15
  class OAuthUser {
16
- constructor(item) {
17
- if (!_.isNil(item)) {
18
- this.parse(item);
19
- }
20
- }
16
+ constructor() { }
21
17
  get location() {
22
18
  let items = new Array();
23
19
  if (!_.isEmpty(this.country)) {
@@ -1,9 +1,9 @@
1
1
  import { GoUser } from './GoUser';
2
2
  import { IOAuthDto, IOAuthToken, OAuthBase } from '../OAuthBase';
3
3
  import { ILogger } from '@ts-core/common';
4
- export declare class GoOAuth<T extends GoUser = GoUser> extends OAuthBase<T> {
5
- constructor(logger: ILogger, window: Window, applicationId: string, scope: string);
6
- protected getAuthUrl(): string;
4
+ export declare class GoAuth<T extends GoUser = GoUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrl(): string;
7
7
  getProfile(token: string): Promise<T>;
8
8
  getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
9
9
  }
@@ -9,45 +9,44 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.GoOAuth = void 0;
13
- const _ = require("lodash");
12
+ exports.GoAuth = void 0;
14
13
  const GoUser_1 = require("./GoUser");
15
14
  const OAuthBase_1 = require("../OAuthBase");
16
- class GoOAuth extends OAuthBase_1.OAuthBase {
17
- constructor(logger, window, applicationId, scope) {
18
- super(logger, window, applicationId, scope);
15
+ class GoAuth extends OAuthBase_1.OAuthBase {
16
+ constructor(logger, applicationId, window) {
17
+ super(logger, applicationId, window);
18
+ this.urlParams.set('scope', 'https://www.googleapis.com/auth/userinfo.profile');
19
19
  }
20
- getAuthUrl() {
21
- let params = new URLSearchParams();
22
- params.append('display', 'popup');
23
- params.append('client_id', this.applicationId);
24
- params.append('redirect_uri', this.redirectUri);
25
- params.append('response_type', this.responseType);
26
- if (!_.isEmpty(this.scope)) {
27
- params.append('scope', this.scope);
28
- }
29
- return `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
20
+ getUrl() {
21
+ return `https://accounts.google.com/o/oauth2/v2/auth?${this.getUrlParams().toString()}`;
30
22
  }
31
23
  getProfile(token) {
32
24
  return __awaiter(this, void 0, void 0, function* () {
33
- let item = yield this.http.call('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { 'Authorization': `Bearer ${token}` } });
34
- return new GoUser_1.GoUser(item);
25
+ let item = new GoUser_1.GoUser();
26
+ item.parse(yield this.http.call('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { 'Authorization': `Bearer ${token}` } }));
27
+ return item;
35
28
  });
36
29
  }
37
30
  getTokenByCode(dto, secret) {
38
31
  return __awaiter(this, void 0, void 0, function* () {
39
32
  let item = yield this.http.call('https://oauth2.googleapis.com/token', {
40
- method: 'post',
41
33
  data: {
42
34
  code: dto.codeOrToken,
43
35
  client_id: this.applicationId,
44
36
  client_secret: secret,
45
37
  redirect_uri: dto.redirectUri,
46
38
  grant_type: 'authorization_code'
47
- }
39
+ },
40
+ method: 'post'
48
41
  });
49
- return { idToken: item.id_token, tokenType: item.token_type, expiresIn: item.expires_in, accessToken: item.access_token, scope: item.scope };
42
+ return {
43
+ expiresIn: item.expires_in,
44
+ accessToken: item.access_token,
45
+ tokenType: item.token_type,
46
+ idToken: item.id_token,
47
+ scope: item.scope
48
+ };
50
49
  });
51
50
  }
52
51
  }
53
- exports.GoOAuth = GoOAuth;
52
+ exports.GoAuth = GoAuth;
@@ -2,5 +2,5 @@ import { OAuthUser } from "../OAuthUser";
2
2
  export declare class GoUser extends OAuthUser {
3
3
  givenName: string;
4
4
  familyName: string;
5
- protected parse(item: any): void;
5
+ parse(item: any): void;
6
6
  }
@@ -0,0 +1,10 @@
1
+ import { ILogger } from "@ts-core/common";
2
+ import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
+ import { MaUser } from "./MaUser";
4
+ export declare class MaAuth<T extends MaUser = MaUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrlParams(): URLSearchParams;
7
+ protected getUrl(): string;
8
+ getProfile(token: string): Promise<T>;
9
+ getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
10
+ }
@@ -0,0 +1,64 @@
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.MaAuth = void 0;
13
+ const common_1 = require("@ts-core/common");
14
+ const OAuthBase_1 = require("../OAuthBase");
15
+ const MaUser_1 = require("./MaUser");
16
+ const _ = require("lodash");
17
+ class MaAuth extends OAuthBase_1.OAuthBase {
18
+ constructor(logger, applicationId, window) {
19
+ super(logger, applicationId, window);
20
+ this.popUpWidth = 480;
21
+ this.popUpHeight = 520;
22
+ this.urlParams.set('scope', 'userinfo');
23
+ }
24
+ getUrlParams() {
25
+ let item = super.getUrlParams();
26
+ item.append('state', common_1.RandomUtil.randomString(43));
27
+ return item;
28
+ }
29
+ getUrl() {
30
+ return `https://oauth.mail.ru/login?${this.getUrlParams().toString()}`;
31
+ }
32
+ getProfile(token) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ let item = new MaUser_1.MaUser();
35
+ item.parse(yield this.http.call('https://oauth.mail.ru/userinfo', { data: { access_token: token } }));
36
+ return item;
37
+ });
38
+ }
39
+ getTokenByCode(dto, secret) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ let item = yield this.http.call('https://oauth.mail.ru/token', {
42
+ data: {
43
+ code: dto.codeOrToken,
44
+ client_id: this.applicationId,
45
+ client_secret: secret,
46
+ redirect_uri: dto.redirectUri,
47
+ grant_type: 'authorization_code'
48
+ },
49
+ method: 'post',
50
+ headers: { 'Content-Type': 'multipart/form-data' }
51
+ });
52
+ if (!_.isNil(item.error_description)) {
53
+ throw new common_1.ExtendedError(item.error_description);
54
+ }
55
+ return {
56
+ tokenType: item.token_type,
57
+ expiresIn: item.expires_in,
58
+ accessToken: item.access_token,
59
+ refreshToken: item.refresh_token,
60
+ };
61
+ });
62
+ }
63
+ }
64
+ exports.MaAuth = MaAuth;
@@ -0,0 +1,8 @@
1
+ import { OAuthUser } from "../OAuthUser";
2
+ export declare class MaUser extends OAuthUser {
3
+ clientId: string;
4
+ lastName: string;
5
+ nickname: string;
6
+ firstName: string;
7
+ parse(item: any): void;
8
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MaUser = void 0;
4
+ const OAuthUser_1 = require("../OAuthUser");
5
+ const _ = require("lodash");
6
+ class MaUser extends OAuthUser_1.OAuthUser {
7
+ parse(item) {
8
+ this.id = item.id;
9
+ this.name = item.name;
10
+ this.email = item.email;
11
+ this.locale = item.locale;
12
+ this.picture = item.image;
13
+ this.clientId = item.clientId;
14
+ this.nickname = item.nickname;
15
+ this.lastName = item.last_name;
16
+ this.firstName = item.first_name;
17
+ if (!_.isNil(item.gender)) {
18
+ this.isMale = item.gender === 'm';
19
+ }
20
+ if (!_.isNil(item.birthday)) {
21
+ let array = String(item.birthday).split('.');
22
+ if (array.length === 3) {
23
+ this.birthday = new Date(Number(array[2]), Number(array[1]) - 1, Number(array[0]));
24
+ }
25
+ }
26
+ }
27
+ }
28
+ exports.MaUser = MaUser;
@@ -1,8 +1,8 @@
1
1
  export * from './OAuthBase';
2
2
  export * from './OAuthUser';
3
3
  export * from './go/GoUser';
4
- export * from './go/GoOAuth';
4
+ export * from './go/GoAuth';
5
5
  export * from './vk/VkUser';
6
- export * from './vk/VkOAuth';
6
+ export * from './vk/VkAuth';
7
7
  export * from './ya/YaUser';
8
- export * from './ya/YaOAuth';
8
+ export * from './ya/YaAuth';
package/cjs/public-api.js CHANGED
@@ -17,8 +17,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./OAuthBase"), exports);
18
18
  __exportStar(require("./OAuthUser"), exports);
19
19
  __exportStar(require("./go/GoUser"), exports);
20
- __exportStar(require("./go/GoOAuth"), exports);
20
+ __exportStar(require("./go/GoAuth"), exports);
21
21
  __exportStar(require("./vk/VkUser"), exports);
22
- __exportStar(require("./vk/VkOAuth"), exports);
22
+ __exportStar(require("./vk/VkAuth"), exports);
23
23
  __exportStar(require("./ya/YaUser"), exports);
24
- __exportStar(require("./ya/YaOAuth"), exports);
24
+ __exportStar(require("./ya/YaAuth"), exports);
@@ -0,0 +1,10 @@
1
+ import { ILogger } from '@ts-core/common';
2
+ import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
+ import { VkUser } from "./VkUser";
4
+ export declare class VkAuth<T extends VkUser = VkUser> extends OAuthBase<T> {
5
+ v: string;
6
+ constructor(logger: ILogger, applicationId: string, window?: Window);
7
+ protected getUrl(): string;
8
+ getProfile(token: string, fields?: string): Promise<T>;
9
+ getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
10
+ }
@@ -9,26 +9,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.VkOAuth = void 0;
12
+ exports.VkAuth = void 0;
13
+ const common_1 = require("@ts-core/common");
13
14
  const _ = require("lodash");
14
15
  const OAuthBase_1 = require("../OAuthBase");
15
16
  const VkUser_1 = require("./VkUser");
16
- class VkOAuth extends OAuthBase_1.OAuthBase {
17
- getAuthUrl() {
18
- let params = new URLSearchParams();
19
- params.append('display', 'popup');
20
- params.append('client_id', this.applicationId);
21
- params.append('redirect_uri', this.redirectUri);
22
- params.append('response_type', this.responseType);
23
- if (!_.isEmpty(this.scope)) {
24
- params.append('scope', this.scope);
25
- }
26
- return `https://oauth.vk.com/authorize?${params.toString()}`;
17
+ class VkAuth extends OAuthBase_1.OAuthBase {
18
+ constructor(logger, applicationId, window) {
19
+ super(logger, applicationId, window);
20
+ this.v = '5.131';
21
+ this.urlParams.set('scope', 'status');
22
+ }
23
+ getUrl() {
24
+ return `https://oauth.vk.com/authorize?${this.getUrlParams().toString()}`;
27
25
  }
28
26
  getProfile(token, fields) {
29
27
  return __awaiter(this, void 0, void 0, function* () {
30
- let { response } = yield this.http.call('https://api.vk.com/method/users.get', { data: { access_token: token, v: '5.131', fields } });
31
- return new VkUser_1.VkUser(response[0]);
28
+ if (_.isNil(fields)) {
29
+ fields = 'photo_200,bdate,sex,city,country,status,about';
30
+ }
31
+ let { response } = yield this.http.call('https://api.vk.com/method/users.get', { data: { access_token: token, v: this.v, fields } });
32
+ let item = new VkUser_1.VkUser();
33
+ item.parse(response[0]);
34
+ return item;
32
35
  });
33
36
  }
34
37
  getTokenByCode(dto, secret) {
@@ -41,8 +44,15 @@ class VkOAuth extends OAuthBase_1.OAuthBase {
41
44
  client_secret: secret,
42
45
  }
43
46
  });
44
- return { userId: item.user_id, expiresIn: item.expires_in, accessToken: item.access_token };
47
+ if (!_.isNil(item.error_description)) {
48
+ throw new common_1.ExtendedError(item.error_description);
49
+ }
50
+ return {
51
+ expiresIn: item.expires_in,
52
+ accessToken: item.access_token,
53
+ userId: item.user_id,
54
+ };
45
55
  });
46
56
  }
47
57
  }
48
- exports.VkOAuth = VkOAuth;
58
+ exports.VkAuth = VkAuth;
@@ -1,5 +1,5 @@
1
1
  import { OAuthUser } from "../OAuthUser";
2
2
  export declare class VkUser extends OAuthUser {
3
3
  params: string;
4
- protected parse(item: any): void;
4
+ parse(item: any): void;
5
5
  }
@@ -1,9 +1,9 @@
1
1
  import { ILogger } from "@ts-core/common";
2
2
  import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
3
  import { YaUser } from "./YaUser";
4
- export declare class YaOAuth<T extends YaUser = YaUser> extends OAuthBase<T> {
5
- constructor(logger: ILogger, window: Window, applicationId: string, scope?: string);
6
- protected getAuthUrl(): string;
4
+ export declare class YaAuth<T extends YaUser = YaUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrl(): string;
7
7
  getProfile(token: string): Promise<T>;
8
8
  getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
9
9
  }
@@ -0,0 +1,57 @@
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.YaAuth = void 0;
13
+ const common_1 = require("@ts-core/common");
14
+ const OAuthBase_1 = require("../OAuthBase");
15
+ const YaUser_1 = require("./YaUser");
16
+ const _ = require("lodash");
17
+ class YaAuth extends OAuthBase_1.OAuthBase {
18
+ constructor(logger, applicationId, window) {
19
+ super(logger, applicationId, window);
20
+ this.popUpWidth = 480;
21
+ this.popUpHeight = 520;
22
+ }
23
+ getUrl() {
24
+ return `https://oauth.yandex.ru/authorize?${this.getUrlParams().toString()}`;
25
+ }
26
+ getProfile(token) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ let item = new YaUser_1.YaUser();
29
+ item.parse(yield this.http.call('https://login.yandex.ru/info', { data: { oauth_token: token } }));
30
+ return item;
31
+ });
32
+ }
33
+ getTokenByCode(dto, secret) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ let item = yield this.http.call('https://oauth.yandex.ru/token', {
36
+ data: {
37
+ code: dto.codeOrToken,
38
+ client_id: this.applicationId,
39
+ client_secret: secret,
40
+ grant_type: 'authorization_code'
41
+ },
42
+ method: 'post',
43
+ headers: { 'Content-Type': 'multipart/form-data' }
44
+ });
45
+ if (!_.isNil(item.error_description)) {
46
+ throw new common_1.ExtendedError(item.error_description);
47
+ }
48
+ return {
49
+ expiresIn: item.expires_in,
50
+ accessToken: item.access_token,
51
+ refreshToken: item.refresh_token,
52
+ tokenType: item.token_type,
53
+ };
54
+ });
55
+ }
56
+ }
57
+ exports.YaAuth = YaAuth;
@@ -6,5 +6,5 @@ export declare class YaUser extends OAuthUser {
6
6
  clientId: string;
7
7
  realName: string;
8
8
  displayName: string;
9
- protected parse(item: any): void;
9
+ parse(item: any): void;
10
10
  }
@@ -1,8 +1,7 @@
1
1
  import { PromiseHandler, LoggerWrapper, ILogger, TransportHttp } from "@ts-core/common";
2
2
  export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
3
- protected window: Window;
4
3
  protected applicationId: string;
5
- protected scope?: string;
4
+ protected window?: Window;
6
5
  protected http: TransportHttp;
7
6
  protected timer: any;
8
7
  protected popUp: Window;
@@ -10,12 +9,14 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
10
9
  protected popUpWidth: number;
11
10
  protected popUpHeight: number;
12
11
  protected responseType: string;
13
- constructor(logger: ILogger, window: Window, applicationId: string, scope?: string);
12
+ protected _urlParams: Map<string, string>;
13
+ constructor(logger: ILogger, applicationId: string, window?: Window);
14
14
  protected open(): Promise<IOAuthDto>;
15
- protected openPopup(url: string, target: string): Window;
15
+ protected openPopup(): Window;
16
16
  protected checkPopUp: () => void;
17
+ protected getUrlParams(): URLSearchParams;
18
+ protected abstract getUrl(): string;
17
19
  protected messageHandler: (event: MessageEvent<IOAuthPopUpDto>) => void;
18
- protected abstract getAuthUrl(): string;
19
20
  protected get redirectUri(): string;
20
21
  protected get originUrl(): string;
21
22
  abstract getProfile(token: string, ...params: any[]): Promise<T>;
@@ -24,6 +25,7 @@ export declare abstract class OAuthBase<T = any> extends LoggerWrapper {
24
25
  getToken(): Promise<IOAuthDto>;
25
26
  close(): void;
26
27
  destroy(): void;
28
+ get urlParams(): Map<string, string>;
27
29
  }
28
30
  export interface IOAuthDto {
29
31
  codeOrToken: string;
package/esm/OAuthBase.js CHANGED
@@ -10,11 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { PromiseHandler, DateUtil, LoggerWrapper, TransportHttp } from "@ts-core/common";
11
11
  import * as _ from 'lodash';
12
12
  export class OAuthBase extends LoggerWrapper {
13
- constructor(logger, window, applicationId, scope) {
13
+ constructor(logger, applicationId, window) {
14
14
  super(logger);
15
- this.window = window;
16
15
  this.applicationId = applicationId;
17
- this.scope = scope;
16
+ this.window = window;
18
17
  this.popUpWidth = 640;
19
18
  this.popUpHeight = 480;
20
19
  this.checkPopUp = () => {
@@ -38,6 +37,8 @@ export class OAuthBase extends LoggerWrapper {
38
37
  }
39
38
  };
40
39
  this.http = new TransportHttp(logger, { method: 'get' });
40
+ this._urlParams = new Map();
41
+ this.urlParams.set('display', 'popup');
41
42
  }
42
43
  open() {
43
44
  return __awaiter(this, void 0, void 0, function* () {
@@ -45,20 +46,29 @@ export class OAuthBase extends LoggerWrapper {
45
46
  return this.promise.promise;
46
47
  }
47
48
  this.promise = PromiseHandler.create();
48
- this.popUp = this.openPopup(this.getAuthUrl(), '_blank');
49
+ this.popUp = this.openPopup();
49
50
  this.window.addEventListener('message', this.messageHandler, false);
50
51
  this.timer = setInterval(this.checkPopUp, DateUtil.MILLISECONDS_NANOSECOND / 2);
51
52
  return this.promise.promise;
52
53
  });
53
54
  }
54
- openPopup(url, target) {
55
+ openPopup() {
55
56
  let window = this.window;
56
57
  let top = (window.screen.height - this.popUpHeight) / 2;
57
58
  let left = (window.screen.width - this.popUpWidth) / 2;
58
- let item = window.open(url, target, `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
59
+ let item = window.open(this.getUrl(), '_blank', `scrollbars=yes,width=${this.popUpWidth},height=${this.popUpHeight},top=${top},left=${left}`);
59
60
  item.focus();
60
61
  return item;
61
62
  }
63
+ getUrlParams() {
64
+ let item = new URLSearchParams();
65
+ item.append('display', 'popup');
66
+ item.append('client_id', this.applicationId);
67
+ item.append('redirect_uri', this.redirectUri);
68
+ item.append('response_type', this.responseType);
69
+ this.urlParams.forEach((value, key) => item.append(key, value));
70
+ return item;
71
+ }
62
72
  get redirectUri() {
63
73
  return `${this.originUrl}/oauth`;
64
74
  }
@@ -96,9 +106,16 @@ export class OAuthBase extends LoggerWrapper {
96
106
  }
97
107
  super.destroy();
98
108
  this.close();
109
+ if (!_.isNil(this.urlParams)) {
110
+ this.urlParams.clear();
111
+ this._urlParams = null;
112
+ }
99
113
  if (!_.isNil(this.http)) {
100
114
  this.http.destroy();
101
115
  this.http = null;
102
116
  }
103
117
  }
118
+ get urlParams() {
119
+ return this._urlParams;
120
+ }
104
121
  }
@@ -1,5 +1,5 @@
1
1
  export declare abstract class OAuthUser {
2
- id: string | number;
2
+ id: string;
3
3
  name: string;
4
4
  city?: string;
5
5
  phone?: string;
@@ -17,7 +17,7 @@ export declare abstract class OAuthUser {
17
17
  telegram?: string;
18
18
  instagram?: string;
19
19
  birthday?: Date;
20
- constructor(item?: any);
21
- protected abstract parse(item: any): void;
20
+ constructor();
21
+ abstract parse(item: any): void;
22
22
  get location(): string;
23
23
  }
package/esm/OAuthUser.js CHANGED
@@ -10,11 +10,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  import * as _ from 'lodash';
11
11
  import { Type } from 'class-transformer';
12
12
  export class OAuthUser {
13
- constructor(item) {
14
- if (!_.isNil(item)) {
15
- this.parse(item);
16
- }
17
- }
13
+ constructor() { }
18
14
  get location() {
19
15
  let items = new Array();
20
16
  if (!_.isEmpty(this.country)) {
@@ -1,9 +1,9 @@
1
1
  import { GoUser } from './GoUser';
2
2
  import { IOAuthDto, IOAuthToken, OAuthBase } from '../OAuthBase';
3
3
  import { ILogger } from '@ts-core/common';
4
- export declare class GoOAuth<T extends GoUser = GoUser> extends OAuthBase<T> {
5
- constructor(logger: ILogger, window: Window, applicationId: string, scope: string);
6
- protected getAuthUrl(): string;
4
+ export declare class GoAuth<T extends GoUser = GoUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrl(): string;
7
7
  getProfile(token: string): Promise<T>;
8
8
  getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
9
9
  }
@@ -7,43 +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 * as _ from 'lodash';
11
10
  import { GoUser } from './GoUser';
12
11
  import { OAuthBase } from '../OAuthBase';
13
- export class GoOAuth extends OAuthBase {
14
- constructor(logger, window, applicationId, scope) {
15
- super(logger, window, applicationId, scope);
12
+ export class GoAuth extends OAuthBase {
13
+ constructor(logger, applicationId, window) {
14
+ super(logger, applicationId, window);
15
+ this.urlParams.set('scope', 'https://www.googleapis.com/auth/userinfo.profile');
16
16
  }
17
- getAuthUrl() {
18
- let params = new URLSearchParams();
19
- params.append('display', 'popup');
20
- params.append('client_id', this.applicationId);
21
- params.append('redirect_uri', this.redirectUri);
22
- params.append('response_type', this.responseType);
23
- if (!_.isEmpty(this.scope)) {
24
- params.append('scope', this.scope);
25
- }
26
- return `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
17
+ getUrl() {
18
+ return `https://accounts.google.com/o/oauth2/v2/auth?${this.getUrlParams().toString()}`;
27
19
  }
28
20
  getProfile(token) {
29
21
  return __awaiter(this, void 0, void 0, function* () {
30
- let item = yield this.http.call('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { 'Authorization': `Bearer ${token}` } });
31
- return new GoUser(item);
22
+ let item = new GoUser();
23
+ item.parse(yield this.http.call('https://www.googleapis.com/oauth2/v3/userinfo', { headers: { 'Authorization': `Bearer ${token}` } }));
24
+ return item;
32
25
  });
33
26
  }
34
27
  getTokenByCode(dto, secret) {
35
28
  return __awaiter(this, void 0, void 0, function* () {
36
29
  let item = yield this.http.call('https://oauth2.googleapis.com/token', {
37
- method: 'post',
38
30
  data: {
39
31
  code: dto.codeOrToken,
40
32
  client_id: this.applicationId,
41
33
  client_secret: secret,
42
34
  redirect_uri: dto.redirectUri,
43
35
  grant_type: 'authorization_code'
44
- }
36
+ },
37
+ method: 'post'
45
38
  });
46
- return { idToken: item.id_token, tokenType: item.token_type, expiresIn: item.expires_in, accessToken: item.access_token, scope: item.scope };
39
+ return {
40
+ expiresIn: item.expires_in,
41
+ accessToken: item.access_token,
42
+ tokenType: item.token_type,
43
+ idToken: item.id_token,
44
+ scope: item.scope
45
+ };
47
46
  });
48
47
  }
49
48
  }
@@ -2,5 +2,5 @@ import { OAuthUser } from "../OAuthUser";
2
2
  export declare class GoUser extends OAuthUser {
3
3
  givenName: string;
4
4
  familyName: string;
5
- protected parse(item: any): void;
5
+ parse(item: any): void;
6
6
  }
@@ -0,0 +1,10 @@
1
+ import { ILogger } from "@ts-core/common";
2
+ import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
+ import { MaUser } from "./MaUser";
4
+ export declare class MaAuth<T extends MaUser = MaUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrlParams(): URLSearchParams;
7
+ protected getUrl(): string;
8
+ getProfile(token: string): Promise<T>;
9
+ getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
10
+ }
@@ -0,0 +1,60 @@
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 { ExtendedError, RandomUtil } from "@ts-core/common";
11
+ import { OAuthBase } from "../OAuthBase";
12
+ import { MaUser } from "./MaUser";
13
+ import * as _ from 'lodash';
14
+ export class MaAuth extends OAuthBase {
15
+ constructor(logger, applicationId, window) {
16
+ super(logger, applicationId, window);
17
+ this.popUpWidth = 480;
18
+ this.popUpHeight = 520;
19
+ this.urlParams.set('scope', 'userinfo');
20
+ }
21
+ getUrlParams() {
22
+ let item = super.getUrlParams();
23
+ item.append('state', RandomUtil.randomString(43));
24
+ return item;
25
+ }
26
+ getUrl() {
27
+ return `https://oauth.mail.ru/login?${this.getUrlParams().toString()}`;
28
+ }
29
+ getProfile(token) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ let item = new MaUser();
32
+ item.parse(yield this.http.call('https://oauth.mail.ru/userinfo', { data: { access_token: token } }));
33
+ return item;
34
+ });
35
+ }
36
+ getTokenByCode(dto, secret) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ let item = yield this.http.call('https://oauth.mail.ru/token', {
39
+ data: {
40
+ code: dto.codeOrToken,
41
+ client_id: this.applicationId,
42
+ client_secret: secret,
43
+ redirect_uri: dto.redirectUri,
44
+ grant_type: 'authorization_code'
45
+ },
46
+ method: 'post',
47
+ headers: { 'Content-Type': 'multipart/form-data' }
48
+ });
49
+ if (!_.isNil(item.error_description)) {
50
+ throw new ExtendedError(item.error_description);
51
+ }
52
+ return {
53
+ tokenType: item.token_type,
54
+ expiresIn: item.expires_in,
55
+ accessToken: item.access_token,
56
+ refreshToken: item.refresh_token,
57
+ };
58
+ });
59
+ }
60
+ }
@@ -0,0 +1,8 @@
1
+ import { OAuthUser } from "../OAuthUser";
2
+ export declare class MaUser extends OAuthUser {
3
+ clientId: string;
4
+ lastName: string;
5
+ nickname: string;
6
+ firstName: string;
7
+ parse(item: any): void;
8
+ }
@@ -0,0 +1,24 @@
1
+ import { OAuthUser } from "../OAuthUser";
2
+ import * as _ from 'lodash';
3
+ export class MaUser extends OAuthUser {
4
+ parse(item) {
5
+ this.id = item.id;
6
+ this.name = item.name;
7
+ this.email = item.email;
8
+ this.locale = item.locale;
9
+ this.picture = item.image;
10
+ this.clientId = item.clientId;
11
+ this.nickname = item.nickname;
12
+ this.lastName = item.last_name;
13
+ this.firstName = item.first_name;
14
+ if (!_.isNil(item.gender)) {
15
+ this.isMale = item.gender === 'm';
16
+ }
17
+ if (!_.isNil(item.birthday)) {
18
+ let array = String(item.birthday).split('.');
19
+ if (array.length === 3) {
20
+ this.birthday = new Date(Number(array[2]), Number(array[1]) - 1, Number(array[0]));
21
+ }
22
+ }
23
+ }
24
+ }
@@ -1,8 +1,8 @@
1
1
  export * from './OAuthBase';
2
2
  export * from './OAuthUser';
3
3
  export * from './go/GoUser';
4
- export * from './go/GoOAuth';
4
+ export * from './go/GoAuth';
5
5
  export * from './vk/VkUser';
6
- export * from './vk/VkOAuth';
6
+ export * from './vk/VkAuth';
7
7
  export * from './ya/YaUser';
8
- export * from './ya/YaOAuth';
8
+ export * from './ya/YaAuth';
package/esm/public-api.js CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './OAuthBase';
2
2
  export * from './OAuthUser';
3
3
  export * from './go/GoUser';
4
- export * from './go/GoOAuth';
4
+ export * from './go/GoAuth';
5
5
  export * from './vk/VkUser';
6
- export * from './vk/VkOAuth';
6
+ export * from './vk/VkAuth';
7
7
  export * from './ya/YaUser';
8
- export * from './ya/YaOAuth';
8
+ export * from './ya/YaAuth';
@@ -0,0 +1,10 @@
1
+ import { ILogger } from '@ts-core/common';
2
+ import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
+ import { VkUser } from "./VkUser";
4
+ export declare class VkAuth<T extends VkUser = VkUser> extends OAuthBase<T> {
5
+ v: string;
6
+ constructor(logger: ILogger, applicationId: string, window?: Window);
7
+ protected getUrl(): string;
8
+ getProfile(token: string, fields?: string): Promise<T>;
9
+ getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
10
+ }
@@ -7,25 +7,28 @@ 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 { ExtendedError } from '@ts-core/common';
10
11
  import * as _ from 'lodash';
11
12
  import { OAuthBase } from "../OAuthBase";
12
13
  import { VkUser } from "./VkUser";
13
- export class VkOAuth extends OAuthBase {
14
- getAuthUrl() {
15
- let params = new URLSearchParams();
16
- params.append('display', 'popup');
17
- params.append('client_id', this.applicationId);
18
- params.append('redirect_uri', this.redirectUri);
19
- params.append('response_type', this.responseType);
20
- if (!_.isEmpty(this.scope)) {
21
- params.append('scope', this.scope);
22
- }
23
- return `https://oauth.vk.com/authorize?${params.toString()}`;
14
+ export class VkAuth extends OAuthBase {
15
+ constructor(logger, applicationId, window) {
16
+ super(logger, applicationId, window);
17
+ this.v = '5.131';
18
+ this.urlParams.set('scope', 'status');
19
+ }
20
+ getUrl() {
21
+ return `https://oauth.vk.com/authorize?${this.getUrlParams().toString()}`;
24
22
  }
25
23
  getProfile(token, fields) {
26
24
  return __awaiter(this, void 0, void 0, function* () {
27
- let { response } = yield this.http.call('https://api.vk.com/method/users.get', { data: { access_token: token, v: '5.131', fields } });
28
- return new VkUser(response[0]);
25
+ if (_.isNil(fields)) {
26
+ fields = 'photo_200,bdate,sex,city,country,status,about';
27
+ }
28
+ let { response } = yield this.http.call('https://api.vk.com/method/users.get', { data: { access_token: token, v: this.v, fields } });
29
+ let item = new VkUser();
30
+ item.parse(response[0]);
31
+ return item;
29
32
  });
30
33
  }
31
34
  getTokenByCode(dto, secret) {
@@ -38,7 +41,14 @@ export class VkOAuth extends OAuthBase {
38
41
  client_secret: secret,
39
42
  }
40
43
  });
41
- return { userId: item.user_id, expiresIn: item.expires_in, accessToken: item.access_token };
44
+ if (!_.isNil(item.error_description)) {
45
+ throw new ExtendedError(item.error_description);
46
+ }
47
+ return {
48
+ expiresIn: item.expires_in,
49
+ accessToken: item.access_token,
50
+ userId: item.user_id,
51
+ };
42
52
  });
43
53
  }
44
54
  }
@@ -1,5 +1,5 @@
1
1
  import { OAuthUser } from "../OAuthUser";
2
2
  export declare class VkUser extends OAuthUser {
3
3
  params: string;
4
- protected parse(item: any): void;
4
+ parse(item: any): void;
5
5
  }
@@ -1,9 +1,9 @@
1
1
  import { ILogger } from "@ts-core/common";
2
2
  import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
3
3
  import { YaUser } from "./YaUser";
4
- export declare class YaOAuth<T extends YaUser = YaUser> extends OAuthBase<T> {
5
- constructor(logger: ILogger, window: Window, applicationId: string, scope?: string);
6
- protected getAuthUrl(): string;
4
+ export declare class YaAuth<T extends YaUser = YaUser> extends OAuthBase<T> {
5
+ constructor(logger: ILogger, applicationId: string, window?: Window);
6
+ protected getUrl(): string;
7
7
  getProfile(token: string): Promise<T>;
8
8
  getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
9
9
  }
@@ -0,0 +1,53 @@
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 { ExtendedError } from "@ts-core/common";
11
+ import { OAuthBase } from "../OAuthBase";
12
+ import { YaUser } from "./YaUser";
13
+ import * as _ from 'lodash';
14
+ export class YaAuth extends OAuthBase {
15
+ constructor(logger, applicationId, window) {
16
+ super(logger, applicationId, window);
17
+ this.popUpWidth = 480;
18
+ this.popUpHeight = 520;
19
+ }
20
+ getUrl() {
21
+ return `https://oauth.yandex.ru/authorize?${this.getUrlParams().toString()}`;
22
+ }
23
+ getProfile(token) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ let item = new YaUser();
26
+ item.parse(yield this.http.call('https://login.yandex.ru/info', { data: { oauth_token: token } }));
27
+ return item;
28
+ });
29
+ }
30
+ getTokenByCode(dto, secret) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ let item = yield this.http.call('https://oauth.yandex.ru/token', {
33
+ data: {
34
+ code: dto.codeOrToken,
35
+ client_id: this.applicationId,
36
+ client_secret: secret,
37
+ grant_type: 'authorization_code'
38
+ },
39
+ method: 'post',
40
+ headers: { 'Content-Type': 'multipart/form-data' }
41
+ });
42
+ if (!_.isNil(item.error_description)) {
43
+ throw new ExtendedError(item.error_description);
44
+ }
45
+ return {
46
+ expiresIn: item.expires_in,
47
+ accessToken: item.access_token,
48
+ refreshToken: item.refresh_token,
49
+ tokenType: item.token_type,
50
+ };
51
+ });
52
+ }
53
+ }
@@ -6,5 +6,5 @@ export declare class YaUser extends OAuthUser {
6
6
  clientId: string;
7
7
  realName: string;
8
8
  displayName: string;
9
- protected parse(item: any): void;
9
+ parse(item: any): void;
10
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-core/oauth",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
4
4
  "description": "Classes and utils for oauth",
5
5
  "main": "./cjs/public-api.js",
6
6
  "module": "./esm/public-api.js",
@@ -1,7 +0,0 @@
1
- import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
2
- import { VkUser } from "./VkUser";
3
- export declare class VkOAuth<T extends VkUser = VkUser> extends OAuthBase<T> {
4
- protected getAuthUrl(): string;
5
- getProfile(token: string, fields: string): Promise<T>;
6
- getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
7
- }
package/cjs/ya/YaOAuth.js DELETED
@@ -1,57 +0,0 @@
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.YaOAuth = void 0;
13
- const OAuthBase_1 = require("../OAuthBase");
14
- const YaUser_1 = require("./YaUser");
15
- const _ = require("lodash");
16
- const axios_1 = require("axios");
17
- class YaOAuth extends OAuthBase_1.OAuthBase {
18
- constructor(logger, window, applicationId, scope) {
19
- super(logger, window, applicationId, scope);
20
- this.popUpWidth = 480;
21
- this.popUpHeight = 520;
22
- }
23
- getAuthUrl() {
24
- let params = new URLSearchParams();
25
- params.append('display', 'popup');
26
- params.append('client_id', this.applicationId);
27
- params.append('redirect_uri', this.redirectUri);
28
- params.append('response_type', this.responseType);
29
- if (!_.isEmpty(this.scope)) {
30
- params.append('scope', this.scope);
31
- }
32
- return `https://oauth.yandex.ru/authorize?${params.toString()}`;
33
- }
34
- getProfile(token) {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- let item = yield this.http.call('https://login.yandex.ru/info', { data: { oauth_token: token } });
37
- return new YaUser_1.YaUser(item);
38
- });
39
- }
40
- getTokenByCode(dto, secret) {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- let { data } = yield axios_1.default.postForm('https://oauth.yandex.ru/token', {
43
- code: dto.codeOrToken,
44
- client_id: this.applicationId,
45
- client_secret: secret,
46
- grant_type: 'authorization_code'
47
- });
48
- return {
49
- tokenType: data.token_type,
50
- expiresIn: data.expires_in,
51
- accessToken: data.access_token,
52
- refreshToken: data.refresh_token,
53
- };
54
- });
55
- }
56
- }
57
- exports.YaOAuth = YaOAuth;
@@ -1,7 +0,0 @@
1
- import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
2
- import { VkUser } from "./VkUser";
3
- export declare class VkOAuth<T extends VkUser = VkUser> extends OAuthBase<T> {
4
- protected getAuthUrl(): string;
5
- getProfile(token: string, fields: string): Promise<T>;
6
- getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
7
- }
package/esm/ya/YaOAuth.js DELETED
@@ -1,53 +0,0 @@
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 { OAuthBase } from "../OAuthBase";
11
- import { YaUser } from "./YaUser";
12
- import * as _ from 'lodash';
13
- import axios from 'axios';
14
- export class YaOAuth extends OAuthBase {
15
- constructor(logger, window, applicationId, scope) {
16
- super(logger, window, applicationId, scope);
17
- this.popUpWidth = 480;
18
- this.popUpHeight = 520;
19
- }
20
- getAuthUrl() {
21
- let params = new URLSearchParams();
22
- params.append('display', 'popup');
23
- params.append('client_id', this.applicationId);
24
- params.append('redirect_uri', this.redirectUri);
25
- params.append('response_type', this.responseType);
26
- if (!_.isEmpty(this.scope)) {
27
- params.append('scope', this.scope);
28
- }
29
- return `https://oauth.yandex.ru/authorize?${params.toString()}`;
30
- }
31
- getProfile(token) {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- let item = yield this.http.call('https://login.yandex.ru/info', { data: { oauth_token: token } });
34
- return new YaUser(item);
35
- });
36
- }
37
- getTokenByCode(dto, secret) {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- let { data } = yield axios.postForm('https://oauth.yandex.ru/token', {
40
- code: dto.codeOrToken,
41
- client_id: this.applicationId,
42
- client_secret: secret,
43
- grant_type: 'authorization_code'
44
- });
45
- return {
46
- tokenType: data.token_type,
47
- expiresIn: data.expires_in,
48
- accessToken: data.access_token,
49
- refreshToken: data.refresh_token,
50
- };
51
- });
52
- }
53
- }