@ts-core/oauth 3.0.27 → 3.0.29
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 +4 -3
- package/cjs/keycloak/KeyCloakAuth.d.ts +18 -0
- package/cjs/keycloak/KeyCloakAuth.js +77 -0
- package/cjs/keycloak/KeyCloakUser.d.ts +8 -0
- package/cjs/keycloak/KeyCloakUser.js +16 -0
- package/cjs/public-api.d.ts +2 -0
- package/cjs/public-api.js +2 -0
- package/esm/OAuthBase.d.ts +4 -3
- package/esm/keycloak/KeyCloakAuth.d.ts +18 -0
- package/esm/keycloak/KeyCloakAuth.js +73 -0
- package/esm/keycloak/KeyCloakUser.d.ts +8 -0
- package/esm/keycloak/KeyCloakUser.js +12 -0
- package/esm/public-api.d.ts +2 -0
- package/esm/public-api.js +2 -0
- package/package.json +1 -1
package/cjs/OAuthBase.d.ts
CHANGED
|
@@ -53,14 +53,15 @@ export interface IOAuthPopUpDto {
|
|
|
53
53
|
oAuthCodeOrToken: string;
|
|
54
54
|
}
|
|
55
55
|
export interface IOAuthToken {
|
|
56
|
+
expiresIn: number;
|
|
57
|
+
accessToken: string;
|
|
56
58
|
state?: string;
|
|
57
59
|
scope?: string;
|
|
58
60
|
userId?: number;
|
|
59
|
-
tokenType?: string;
|
|
60
61
|
idToken?: string;
|
|
62
|
+
tokenType?: string;
|
|
61
63
|
refreshToken?: string;
|
|
62
|
-
|
|
63
|
-
accessToken: string;
|
|
64
|
+
refreshExpiresIn?: string;
|
|
64
65
|
}
|
|
65
66
|
export declare enum OAuthEvent {
|
|
66
67
|
POPUP_OPENED = "POPUP_OPENED",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ILogger } from "@ts-core/common";
|
|
2
|
+
import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
|
|
3
|
+
import { KeyCloakUser } from "./KeyCloakUser";
|
|
4
|
+
export declare class KeyCloakAuth<T extends KeyCloakUser = KeyCloakUser> extends OAuthBase<T> {
|
|
5
|
+
protected _settings: IKeyCloakAuthSettings;
|
|
6
|
+
constructor(logger: ILogger, settings: IKeyCloakAuthSettings, window?: Window);
|
|
7
|
+
protected getBaseUrl(): string;
|
|
8
|
+
getPopUpUrl(): string;
|
|
9
|
+
getProfile(token: string): Promise<T>;
|
|
10
|
+
getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
get settings(): IKeyCloakAuthSettings;
|
|
13
|
+
}
|
|
14
|
+
export interface IKeyCloakAuthSettings {
|
|
15
|
+
url: string;
|
|
16
|
+
realm: string;
|
|
17
|
+
applicationId: string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
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.KeyCloakAuth = void 0;
|
|
13
|
+
const common_1 = require("@ts-core/common");
|
|
14
|
+
const _ = require("lodash");
|
|
15
|
+
const OAuthBase_1 = require("../OAuthBase");
|
|
16
|
+
const KeyCloakUser_1 = require("./KeyCloakUser");
|
|
17
|
+
const url_1 = require("url");
|
|
18
|
+
class KeyCloakAuth extends OAuthBase_1.OAuthBase {
|
|
19
|
+
constructor(logger, settings, window) {
|
|
20
|
+
super(logger, settings.applicationId, window);
|
|
21
|
+
this._settings = settings;
|
|
22
|
+
this.urlParams.set('scope', 'openid');
|
|
23
|
+
this.urlParams.set('state', common_1.RandomUtil.randomString(10));
|
|
24
|
+
this.urlParams.set('response_mode', 'query');
|
|
25
|
+
}
|
|
26
|
+
getBaseUrl() {
|
|
27
|
+
return `${this.settings.url}/realms/${this.settings.realm}/protocol/openid-connect`;
|
|
28
|
+
}
|
|
29
|
+
getPopUpUrl() {
|
|
30
|
+
return `${this.getBaseUrl()}/auth?${this.getUrlParams().toString()}`;
|
|
31
|
+
}
|
|
32
|
+
getProfile(token) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
let item = new KeyCloakUser_1.KeyCloakUser();
|
|
35
|
+
item.parse(yield this.http.call(`${this.getBaseUrl()}/userinfo`, { headers: { Authorization: `Bearer ${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(`${this.getBaseUrl()}/token`, {
|
|
42
|
+
data: new url_1.URLSearchParams({
|
|
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': 'application/x-www-form-urlencoded' }
|
|
51
|
+
});
|
|
52
|
+
if (!_.isNil(item.error_description)) {
|
|
53
|
+
throw new common_1.ExtendedError(item.error_description);
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
scope: item.scope,
|
|
57
|
+
state: item.session_state,
|
|
58
|
+
tokenType: item.token_type,
|
|
59
|
+
expiresIn: item.expires_in,
|
|
60
|
+
accessToken: item.access_token,
|
|
61
|
+
refreshToken: item.refresh_token,
|
|
62
|
+
refreshExpiresIn: item.refresh_expires_in,
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
destroy() {
|
|
67
|
+
if (this.isDestroyed) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
super.destroy();
|
|
71
|
+
this._settings = null;
|
|
72
|
+
}
|
|
73
|
+
get settings() {
|
|
74
|
+
return this._settings;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.KeyCloakAuth = KeyCloakAuth;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KeyCloakUser = void 0;
|
|
4
|
+
const OAuthUser_1 = require("../OAuthUser");
|
|
5
|
+
class KeyCloakUser extends OAuthUser_1.OAuthUser {
|
|
6
|
+
parse(item) {
|
|
7
|
+
this.id = item.sub;
|
|
8
|
+
this.name = item.name;
|
|
9
|
+
this.email = item.email;
|
|
10
|
+
this.isEmailVerified = item.email_verified;
|
|
11
|
+
this.preferredUsername = item.preferred_username;
|
|
12
|
+
this.givenName = item.given_name;
|
|
13
|
+
this.familyName = item.family_name;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.KeyCloakUser = KeyCloakUser;
|
package/cjs/public-api.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ 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 './keycloak/KeyCloakAuth';
|
|
13
|
+
export * from './keycloak/KeyCloakUser';
|
|
12
14
|
export * from './external/browser';
|
|
13
15
|
export * from './external/cordovaOAuthPlugin';
|
|
14
16
|
export * from './external/cordovaInAppBrowserPlugin';
|
package/cjs/public-api.js
CHANGED
|
@@ -25,6 +25,8 @@ __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("./keycloak/KeyCloakAuth"), exports);
|
|
29
|
+
__exportStar(require("./keycloak/KeyCloakUser"), exports);
|
|
28
30
|
__exportStar(require("./external/browser"), exports);
|
|
29
31
|
__exportStar(require("./external/cordovaOAuthPlugin"), exports);
|
|
30
32
|
__exportStar(require("./external/cordovaInAppBrowserPlugin"), exports);
|
package/esm/OAuthBase.d.ts
CHANGED
|
@@ -53,14 +53,15 @@ export interface IOAuthPopUpDto {
|
|
|
53
53
|
oAuthCodeOrToken: string;
|
|
54
54
|
}
|
|
55
55
|
export interface IOAuthToken {
|
|
56
|
+
expiresIn: number;
|
|
57
|
+
accessToken: string;
|
|
56
58
|
state?: string;
|
|
57
59
|
scope?: string;
|
|
58
60
|
userId?: number;
|
|
59
|
-
tokenType?: string;
|
|
60
61
|
idToken?: string;
|
|
62
|
+
tokenType?: string;
|
|
61
63
|
refreshToken?: string;
|
|
62
|
-
|
|
63
|
-
accessToken: string;
|
|
64
|
+
refreshExpiresIn?: string;
|
|
64
65
|
}
|
|
65
66
|
export declare enum OAuthEvent {
|
|
66
67
|
POPUP_OPENED = "POPUP_OPENED",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ILogger } from "@ts-core/common";
|
|
2
|
+
import { IOAuthDto, IOAuthToken, OAuthBase } from "../OAuthBase";
|
|
3
|
+
import { KeyCloakUser } from "./KeyCloakUser";
|
|
4
|
+
export declare class KeyCloakAuth<T extends KeyCloakUser = KeyCloakUser> extends OAuthBase<T> {
|
|
5
|
+
protected _settings: IKeyCloakAuthSettings;
|
|
6
|
+
constructor(logger: ILogger, settings: IKeyCloakAuthSettings, window?: Window);
|
|
7
|
+
protected getBaseUrl(): string;
|
|
8
|
+
getPopUpUrl(): string;
|
|
9
|
+
getProfile(token: string): Promise<T>;
|
|
10
|
+
getTokenByCode(dto: IOAuthDto, secret: string): Promise<IOAuthToken>;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
get settings(): IKeyCloakAuthSettings;
|
|
13
|
+
}
|
|
14
|
+
export interface IKeyCloakAuthSettings {
|
|
15
|
+
url: string;
|
|
16
|
+
realm: string;
|
|
17
|
+
applicationId: string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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 * as _ from 'lodash';
|
|
12
|
+
import { OAuthBase } from "../OAuthBase";
|
|
13
|
+
import { KeyCloakUser } from "./KeyCloakUser";
|
|
14
|
+
import { URLSearchParams } from "url";
|
|
15
|
+
export class KeyCloakAuth extends OAuthBase {
|
|
16
|
+
constructor(logger, settings, window) {
|
|
17
|
+
super(logger, settings.applicationId, window);
|
|
18
|
+
this._settings = settings;
|
|
19
|
+
this.urlParams.set('scope', 'openid');
|
|
20
|
+
this.urlParams.set('state', RandomUtil.randomString(10));
|
|
21
|
+
this.urlParams.set('response_mode', 'query');
|
|
22
|
+
}
|
|
23
|
+
getBaseUrl() {
|
|
24
|
+
return `${this.settings.url}/realms/${this.settings.realm}/protocol/openid-connect`;
|
|
25
|
+
}
|
|
26
|
+
getPopUpUrl() {
|
|
27
|
+
return `${this.getBaseUrl()}/auth?${this.getUrlParams().toString()}`;
|
|
28
|
+
}
|
|
29
|
+
getProfile(token) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
let item = new KeyCloakUser();
|
|
32
|
+
item.parse(yield this.http.call(`${this.getBaseUrl()}/userinfo`, { headers: { Authorization: `Bearer ${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(`${this.getBaseUrl()}/token`, {
|
|
39
|
+
data: new URLSearchParams({
|
|
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': 'application/x-www-form-urlencoded' }
|
|
48
|
+
});
|
|
49
|
+
if (!_.isNil(item.error_description)) {
|
|
50
|
+
throw new ExtendedError(item.error_description);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
scope: item.scope,
|
|
54
|
+
state: item.session_state,
|
|
55
|
+
tokenType: item.token_type,
|
|
56
|
+
expiresIn: item.expires_in,
|
|
57
|
+
accessToken: item.access_token,
|
|
58
|
+
refreshToken: item.refresh_token,
|
|
59
|
+
refreshExpiresIn: item.refresh_expires_in,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
destroy() {
|
|
64
|
+
if (this.isDestroyed) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
super.destroy();
|
|
68
|
+
this._settings = null;
|
|
69
|
+
}
|
|
70
|
+
get settings() {
|
|
71
|
+
return this._settings;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OAuthUser } from "../OAuthUser";
|
|
2
|
+
export class KeyCloakUser extends OAuthUser {
|
|
3
|
+
parse(item) {
|
|
4
|
+
this.id = item.sub;
|
|
5
|
+
this.name = item.name;
|
|
6
|
+
this.email = item.email;
|
|
7
|
+
this.isEmailVerified = item.email_verified;
|
|
8
|
+
this.preferredUsername = item.preferred_username;
|
|
9
|
+
this.givenName = item.given_name;
|
|
10
|
+
this.familyName = item.family_name;
|
|
11
|
+
}
|
|
12
|
+
}
|
package/esm/public-api.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ 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 './keycloak/KeyCloakAuth';
|
|
13
|
+
export * from './keycloak/KeyCloakUser';
|
|
12
14
|
export * from './external/browser';
|
|
13
15
|
export * from './external/cordovaOAuthPlugin';
|
|
14
16
|
export * from './external/cordovaInAppBrowserPlugin';
|
package/esm/public-api.js
CHANGED
|
@@ -9,6 +9,8 @@ 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 './keycloak/KeyCloakAuth';
|
|
13
|
+
export * from './keycloak/KeyCloakUser';
|
|
12
14
|
export * from './external/browser';
|
|
13
15
|
export * from './external/cordovaOAuthPlugin';
|
|
14
16
|
export * from './external/cordovaInAppBrowserPlugin';
|