@edirect/oidc-client 1.1.0 → 2.0.0
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/.eslintrc.js +46 -0
- package/.husky/pre-commit +4 -0
- package/.husky/pre-push +4 -0
- package/.prettierrc.js +6 -0
- package/dist/connect.d.ts +11 -0
- package/dist/connect.js +42 -0
- package/dist/connect.js.map +1 -0
- package/dist/grant-type/client_credentials.d.ts +16 -0
- package/dist/grant-type/client_credentials.js +57 -0
- package/dist/grant-type/client_credentials.js.map +1 -0
- package/dist/grant-type/password.d.ts +14 -0
- package/dist/grant-type/password.js +57 -0
- package/dist/grant-type/password.js.map +1 -0
- package/dist/grant-type/sso.d.ts +14 -0
- package/dist/grant-type/sso.js +64 -0
- package/dist/grant-type/sso.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/token.d.ts +6 -0
- package/dist/token.js +23 -0
- package/dist/token.js.map +1 -0
- package/dist/types/connect.type.d.ts +12 -0
- package/dist/types/connect.type.js +3 -0
- package/dist/types/connect.type.js.map +1 -0
- package/dist/types/error-handler.type.d.ts +5 -0
- package/dist/types/error-handler.type.js +3 -0
- package/dist/types/error-handler.type.js.map +1 -0
- package/dist/types/token-set.type.d.ts +21 -0
- package/dist/types/token-set.type.js +3 -0
- package/dist/types/token-set.type.js.map +1 -0
- package/dist/utils/error-handler.d.ts +5 -0
- package/dist/utils/error-handler.js +13 -0
- package/dist/utils/error-handler.js.map +1 -0
- package/dist/utils/parse-token.d.ts +3 -0
- package/dist/utils/parse-token.js +26 -0
- package/dist/utils/parse-token.js.map +1 -0
- package/package.json +21 -14
- package/.eslintrc.json +0 -165
- package/client_credentials.js +0 -56
- package/connect.js +0 -59
- package/index.js +0 -12
- package/password.js +0 -64
- package/sso.js +0 -64
- package/test.js +0 -26
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
root: true,
|
|
3
|
+
env: {
|
|
4
|
+
node: true,
|
|
5
|
+
es6: true,
|
|
6
|
+
},
|
|
7
|
+
parserOptions: {
|
|
8
|
+
ecmaFeatures: {
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
ecmaVersion: 2018,
|
|
12
|
+
sourceType: 'module',
|
|
13
|
+
project: './tsconfig.json',
|
|
14
|
+
tsconfigRootDir: __dirname,
|
|
15
|
+
},
|
|
16
|
+
ignorePatterns: ['node_modules/*', 'dist/*', '!.prettierrc.js'],
|
|
17
|
+
extends: ['eslint:recommended'],
|
|
18
|
+
overrides: [
|
|
19
|
+
{
|
|
20
|
+
files: ['./src/**/*.ts'],
|
|
21
|
+
parser: '@typescript-eslint/parser',
|
|
22
|
+
settings: {
|
|
23
|
+
react: {
|
|
24
|
+
version: 'detect',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
env: {
|
|
28
|
+
browser: true,
|
|
29
|
+
node: true,
|
|
30
|
+
es6: true,
|
|
31
|
+
},
|
|
32
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
|
|
33
|
+
rules: {
|
|
34
|
+
'@typescript-eslint/no-unused-vars': ['error'],
|
|
35
|
+
|
|
36
|
+
'@typescript-eslint/explicit-function-return-type': [
|
|
37
|
+
'warn',
|
|
38
|
+
{
|
|
39
|
+
allowExpressions: true,
|
|
40
|
+
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
};
|
package/.husky/pre-push
ADDED
package/.prettierrc.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Client } from 'openid-client';
|
|
2
|
+
import { ErrorHandlerType } from './types/error-handler.type';
|
|
3
|
+
import { Connect } from './types/connect.type';
|
|
4
|
+
export default class OidcConnect {
|
|
5
|
+
client: Client;
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
redirectUri?: string;
|
|
8
|
+
ErrorHandler: ErrorHandlerType;
|
|
9
|
+
constructor();
|
|
10
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, responseTypes, grantTypes, timeout, }: Connect): Promise<Client>;
|
|
11
|
+
}
|
package/dist/connect.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const openid_client_1 = require("openid-client");
|
|
7
|
+
const http_1 = __importDefault(require("http"));
|
|
8
|
+
const https_1 = __importDefault(require("https"));
|
|
9
|
+
const error_handler_1 = __importDefault(require("./utils/error-handler"));
|
|
10
|
+
const httpAgent = new http_1.default.Agent({ keepAlive: true });
|
|
11
|
+
const httpsAgent = new https_1.default.Agent({ keepAlive: true });
|
|
12
|
+
class OidcConnect {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.ErrorHandler = error_handler_1.default;
|
|
15
|
+
}
|
|
16
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, responseTypes, grantTypes, timeout, }) {
|
|
17
|
+
this.baseUrl = baseUrl;
|
|
18
|
+
this.redirectUri = redirectUri;
|
|
19
|
+
const oidcConfiguration = {
|
|
20
|
+
client_id: clientId,
|
|
21
|
+
client_secret: clientSecret,
|
|
22
|
+
redirect_uris: [redirectUri],
|
|
23
|
+
response_types: responseTypes,
|
|
24
|
+
grant_types: grantTypes,
|
|
25
|
+
};
|
|
26
|
+
const protocol = (baseUrl.includes('https') && 'https:') || 'http:';
|
|
27
|
+
const agent = (baseUrl.includes('https') && httpsAgent) || httpAgent;
|
|
28
|
+
if (openid_client_1.custom && openid_client_1.custom.setHttpOptionsDefaults) {
|
|
29
|
+
openid_client_1.custom.setHttpOptionsDefaults({
|
|
30
|
+
timeout: timeout || 60000,
|
|
31
|
+
protocol,
|
|
32
|
+
agent,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return openid_client_1.Issuer.discover(`${baseUrl}/${oidcPath}`).then((oidcIssuer) => {
|
|
36
|
+
this.client = new oidcIssuer.Client(oidcConfiguration);
|
|
37
|
+
return this.client;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.default = OidcConnect;
|
|
42
|
+
//# sourceMappingURL=connect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":";;;;;AAAA,iDAAuE;AACvE,gDAAwB;AACxB,kDAA0B;AAE1B,0EAAiD;AAIjD,MAAM,SAAS,GAAG,IAAI,cAAI,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AACtD,MAAM,UAAU,GAAG,IAAI,eAAK,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AAExD,MAAqB,WAAW;IAS9B;QACE,IAAI,CAAC,YAAY,GAAG,uBAAY,CAAC;IACnC,CAAC;IAED,OAAO,CAAC,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,WAAW,EACX,aAAa,EACb,UAAU,EACV,OAAO,GACC;QACR,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,MAAM,iBAAiB,GAAmB;YACxC,SAAS,EAAE,QAAQ;YACnB,aAAa,EAAE,YAAY;YAC3B,aAAa,EAAE,CAAC,WAAqB,CAAC;YACtC,cAAc,EAAE,aAAa;YAC7B,WAAW,EAAE,UAAU;SACxB,CAAC;QAEF,MAAM,QAAQ,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,IAAI,OAAO,CAAC;QACpE,MAAM,KAAK,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,IAAI,SAAS,CAAC;QAErE,IAAI,sBAAM,IAAI,sBAAM,CAAC,sBAAsB,EAAE;YAC3C,sBAAM,CAAC,sBAAsB,CAAC;gBAC5B,OAAO,EAAE,OAAO,IAAI,KAAK;gBACzB,QAAQ;gBACR,KAAK;aACN,CAAC,CAAC;SACJ;QAED,OAAO,sBAAM,CAAC,QAAQ,CAAC,GAAG,OAAO,IAAI,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;YACnE,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;YACvD,OAAO,IAAI,CAAC,MAAM,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlDD,8BAkDC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Client } from 'openid-client';
|
|
2
|
+
import OidcConnect from '../connect';
|
|
3
|
+
import { Connect } from '../types/connect.type';
|
|
4
|
+
import { GrantTypeErrorHandlerType } from '../types/error-handler.type';
|
|
5
|
+
import { TokenSet } from '../types/token-set.type';
|
|
6
|
+
declare class OidcClient extends OidcConnect {
|
|
7
|
+
errorHandler: GrantTypeErrorHandlerType;
|
|
8
|
+
setToken: (token: TokenSet, revert?: boolean) => TokenSet;
|
|
9
|
+
getToken: (revert?: boolean) => TokenSet | null;
|
|
10
|
+
constructor();
|
|
11
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }: Connect): Promise<Client>;
|
|
12
|
+
getAccessToken(data: Record<string, unknown>): Promise<TokenSet | null | void>;
|
|
13
|
+
getRefreshToken(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
declare const _default: OidcClient;
|
|
16
|
+
export default _default;
|
|
@@ -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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const connect_1 = __importDefault(require("../connect"));
|
|
16
|
+
const token_1 = __importDefault(require("../token"));
|
|
17
|
+
const grantType = 'client_credentials';
|
|
18
|
+
class OidcClient extends connect_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.errorHandler = this.ErrorHandler(grantType);
|
|
22
|
+
const { getToken, setToken } = new token_1.default();
|
|
23
|
+
this.getToken = getToken;
|
|
24
|
+
this.setToken = setToken;
|
|
25
|
+
}
|
|
26
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }) {
|
|
27
|
+
return super.connect({
|
|
28
|
+
baseUrl,
|
|
29
|
+
oidcPath,
|
|
30
|
+
clientId,
|
|
31
|
+
clientSecret,
|
|
32
|
+
redirectUri,
|
|
33
|
+
responseTypes: ['none'],
|
|
34
|
+
grantTypes: [grantType],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
getAccessToken(data) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
if (!this.client)
|
|
40
|
+
throw new Error(`[${grantType}] Client not initialized.`);
|
|
41
|
+
if (this.getToken())
|
|
42
|
+
return this.getToken();
|
|
43
|
+
const params = Object.assign({ grant_type: grantType }, data);
|
|
44
|
+
return this.client
|
|
45
|
+
.grant(params)
|
|
46
|
+
.then((tokenSet) => this.setToken(tokenSet))
|
|
47
|
+
.catch(this.errorHandler);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
getRefreshToken() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
throw new Error(`[${grantType}] This method is not allowed to this grant type.`);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.default = new OidcClient();
|
|
57
|
+
//# sourceMappingURL=client_credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client_credentials.js","sourceRoot":"","sources":["../../src/grant-type/client_credentials.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,yDAAqC;AACrC,qDAA6B;AAK7B,MAAM,SAAS,GAAG,oBAAoB,CAAC;AAEvC,MAAM,UAAW,SAAQ,iBAAW;IAKlC;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;QAEjD,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,IAAI,eAAK,EAAE,CAAC;QAC3C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,OAAO,CAAC,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,WAAW,GACH;QACR,OAAO,KAAK,CAAC,OAAO,CAAC;YACnB,OAAO;YACP,QAAQ;YACR,QAAQ;YACR,YAAY;YACZ,WAAW;YACX,aAAa,EAAE,CAAC,MAAM,CAAC;YACvB,UAAU,EAAE,CAAC,SAAS,CAAC;SACxB,CAAC,CAAC;IACL,CAAC;IAEK,cAAc,CAClB,IAA6B;;YAE7B,IAAI,CAAC,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,IAAI,SAAS,2BAA2B,CAAC,CAAC;YAC5E,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;YAE5C,MAAM,MAAM,mBACV,UAAU,EAAE,SAAS,IAClB,IAAI,CACR,CAAC;YAEF,OAAO,IAAI,CAAC,MAAM;iBACf,KAAK,CAAC,MAAM,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,QAA+B,CAAC,CAAC;iBAClE,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC9B,CAAC;KAAA;IAEK,eAAe;;YACnB,MAAM,IAAI,KAAK,CACb,IAAI,SAAS,kDAAkD,CAChE,CAAC;QACJ,CAAC;KAAA;CACF;AAED,kBAAe,IAAI,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Client } from 'openid-client';
|
|
2
|
+
import { Connect } from '../types/connect.type';
|
|
3
|
+
import { GrantTypeErrorHandlerType } from '../types/error-handler.type';
|
|
4
|
+
import { TokenSet } from '../types/token-set.type';
|
|
5
|
+
import OidcConnect from '../connect';
|
|
6
|
+
declare class OidcClient extends OidcConnect {
|
|
7
|
+
errorHandler: GrantTypeErrorHandlerType;
|
|
8
|
+
constructor();
|
|
9
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }: Connect): Promise<Client>;
|
|
10
|
+
getAccessToken(data: Record<string, unknown>): Promise<TokenSet | null | void>;
|
|
11
|
+
getRefreshToken(tokenSet: TokenSet): Promise<TokenSet | null | void>;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: OidcClient;
|
|
14
|
+
export default _default;
|
|
@@ -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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const connect_1 = __importDefault(require("../connect"));
|
|
16
|
+
const parse_token_1 = __importDefault(require("../utils/parse-token"));
|
|
17
|
+
const grantType = 'password';
|
|
18
|
+
class OidcClient extends connect_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.errorHandler = this.ErrorHandler(grantType);
|
|
22
|
+
}
|
|
23
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }) {
|
|
24
|
+
return super.connect({
|
|
25
|
+
baseUrl,
|
|
26
|
+
oidcPath,
|
|
27
|
+
clientId,
|
|
28
|
+
clientSecret,
|
|
29
|
+
redirectUri,
|
|
30
|
+
responseTypes: ['none'],
|
|
31
|
+
grantTypes: [grantType, 'refresh_token'],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
getAccessToken(data) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (!this.client)
|
|
37
|
+
throw new Error(`[${grantType}] Client not initialized.`);
|
|
38
|
+
const params = Object.assign({ grant_type: grantType }, data);
|
|
39
|
+
return this.client
|
|
40
|
+
.grant(params)
|
|
41
|
+
.then((tokenSet) => (0, parse_token_1.default)(tokenSet))
|
|
42
|
+
.catch(this.errorHandler);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getRefreshToken(tokenSet) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
if (!this.client)
|
|
48
|
+
throw new Error(`[${grantType}] Client not initialized.`);
|
|
49
|
+
return this.client
|
|
50
|
+
.refresh(tokenSet.refreshToken)
|
|
51
|
+
.then((tokenSet) => (0, parse_token_1.default)(tokenSet))
|
|
52
|
+
.catch(this.errorHandler);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.default = new OidcClient();
|
|
57
|
+
//# sourceMappingURL=password.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"password.js","sourceRoot":"","sources":["../../src/grant-type/password.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAKA,yDAAqC;AACrC,uEAA8C;AAE9C,MAAM,SAAS,GAAG,UAAU,CAAC;AAE7B,MAAM,UAAW,SAAQ,iBAAW;IAGlC;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAED,OAAO,CAAC,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,WAAW,GACH;QACR,OAAO,KAAK,CAAC,OAAO,CAAC;YACnB,OAAO;YACP,QAAQ;YACR,QAAQ;YACR,YAAY;YACZ,WAAW;YACX,aAAa,EAAE,CAAC,MAAM,CAAC;YACvB,UAAU,EAAE,CAAC,SAAS,EAAE,eAAe,CAAC;SACzC,CAAC,CAAC;IACL,CAAC;IAEK,cAAc,CAClB,IAA6B;;YAE7B,IAAI,CAAC,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,IAAI,SAAS,2BAA2B,CAAC,CAAC;YAE5E,MAAM,MAAM,mBACV,UAAU,EAAE,SAAS,IAClB,IAAI,CACR,CAAC;YAEF,OAAO,IAAI,CAAC,MAAM;iBACf,KAAK,CAAC,MAAM,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,qBAAU,EAAC,QAA+B,CAAC,CAAC;iBAC/D,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC9B,CAAC;KAAA;IAEK,eAAe,CAAC,QAAkB;;YACtC,IAAI,CAAC,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,IAAI,SAAS,2BAA2B,CAAC,CAAC;YAE5E,OAAO,IAAI,CAAC,MAAM;iBACf,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC;iBAC9B,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,qBAAU,EAAC,QAA+B,CAAC,CAAC;iBAC/D,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC9B,CAAC;KAAA;CACF;AAED,kBAAe,IAAI,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Client } from 'openid-client';
|
|
2
|
+
import OidcConnect from '../connect';
|
|
3
|
+
import { Connect } from '../types/connect.type';
|
|
4
|
+
import { TokenSet } from '../types/token-set.type';
|
|
5
|
+
import { GrantTypeErrorHandlerType } from '../types/error-handler.type';
|
|
6
|
+
declare class OidcClient extends OidcConnect {
|
|
7
|
+
errorHandler: GrantTypeErrorHandlerType;
|
|
8
|
+
constructor();
|
|
9
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }: Connect): Promise<Client>;
|
|
10
|
+
getAccessToken(data: Record<string, unknown>): Promise<TokenSet | null | void>;
|
|
11
|
+
getRefreshToken(tokenSet: TokenSet): Promise<TokenSet | null | void>;
|
|
12
|
+
}
|
|
13
|
+
declare const _default: OidcClient;
|
|
14
|
+
export default _default;
|
|
@@ -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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const connect_1 = __importDefault(require("../connect"));
|
|
16
|
+
const parse_token_1 = __importDefault(require("../utils/parse-token"));
|
|
17
|
+
const grantType = 'sso';
|
|
18
|
+
class OidcClient extends connect_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.errorHandler = this.ErrorHandler(grantType);
|
|
22
|
+
}
|
|
23
|
+
connect({ baseUrl, oidcPath, clientId, clientSecret, redirectUri, }) {
|
|
24
|
+
return super.connect({
|
|
25
|
+
baseUrl,
|
|
26
|
+
oidcPath,
|
|
27
|
+
clientId,
|
|
28
|
+
clientSecret,
|
|
29
|
+
redirectUri,
|
|
30
|
+
responseTypes: ['none'],
|
|
31
|
+
grantTypes: ['sso', 'refresh_token'],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
getAccessToken(data) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
try {
|
|
37
|
+
const params = Object.assign({ grant_type: 'sso' }, data);
|
|
38
|
+
return this.client
|
|
39
|
+
.grant(params)
|
|
40
|
+
.then((tokenSet) => (0, parse_token_1.default)(tokenSet))
|
|
41
|
+
.catch(this.errorHandler);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
getRefreshToken(tokenSet) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
try {
|
|
51
|
+
const refreshTokenSet = yield this.client
|
|
52
|
+
.refresh(tokenSet.refreshToken)
|
|
53
|
+
.then((tokenSet) => (0, parse_token_1.default)(tokenSet))
|
|
54
|
+
.catch(this.errorHandler);
|
|
55
|
+
return refreshTokenSet;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.default = new OidcClient();
|
|
64
|
+
//# sourceMappingURL=sso.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sso.js","sourceRoot":"","sources":["../../src/grant-type/sso.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,yDAAqC;AAIrC,uEAA8C;AAE9C,MAAM,SAAS,GAAG,KAAK,CAAC;AAExB,MAAM,UAAW,SAAQ,iBAAW;IAGlC;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAED,OAAO,CAAC,EACN,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,WAAW,GACH;QACR,OAAO,KAAK,CAAC,OAAO,CAAC;YACnB,OAAO;YACP,QAAQ;YACR,QAAQ;YACR,YAAY;YACZ,WAAW;YACX,aAAa,EAAE,CAAC,MAAM,CAAC;YACvB,UAAU,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC;SACrC,CAAC,CAAC;IACL,CAAC;IAEK,cAAc,CAClB,IAA6B;;YAE7B,IAAI;gBACF,MAAM,MAAM,mBACV,UAAU,EAAE,KAAK,IACd,IAAI,CACR,CAAC;gBAEF,OAAO,IAAI,CAAC,MAAM;qBACf,KAAK,CAAC,MAAM,CAAC;qBACb,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,qBAAU,EAAC,QAA+B,CAAC,CAAC;qBAC/D,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;aAC7B;YAAC,OAAO,KAAK,EAAE;gBACd,OAAO,IAAI,CAAC;aACb;QACH,CAAC;KAAA;IAEK,eAAe,CAAC,QAAkB;;YACtC,IAAI;gBACF,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,MAAM;qBACtC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC;qBAC9B,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAA,qBAAU,EAAC,QAA+B,CAAC,CAAC;qBAC/D,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBAE5B,OAAO,eAAe,CAAC;aACxB;YAAC,OAAO,KAAK,EAAE;gBACd,OAAO,IAAI,CAAC;aACb;QACH,CAAC;KAAA;CACF;AAED,kBAAe,IAAI,UAAU,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.default = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
+
const oidc = yield Promise.resolve().then(() => __importStar(require(`./grant-type/${params.grantType}`)));
|
|
33
|
+
yield oidc.connect({
|
|
34
|
+
baseUrl: params.baseUrl,
|
|
35
|
+
oidcPath: params.oidcPath,
|
|
36
|
+
clientId: params.clientId,
|
|
37
|
+
clientSecret: params.clientSecret,
|
|
38
|
+
});
|
|
39
|
+
return oidc;
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,kBAAe,CAAO,MAAe,EAAoB,EAAE;IACzD,MAAM,IAAI,GAAG,wDAAa,gBAAgB,MAAM,CAAC,SAAS,EAAE,GAAC,CAAC;IAE9D,MAAM,IAAI,CAAC,OAAO,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,YAAY,EAAE,MAAM,CAAC,YAAY;KAClC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC,CAAA,CAAC"}
|
package/dist/token.d.ts
ADDED
package/dist/token.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const parse_token_1 = __importDefault(require("./utils/parse-token"));
|
|
7
|
+
class Token {
|
|
8
|
+
setToken(token, revert = false) {
|
|
9
|
+
this.token = (0, parse_token_1.default)(token, revert);
|
|
10
|
+
return this.token;
|
|
11
|
+
}
|
|
12
|
+
getToken(revert = false) {
|
|
13
|
+
if (!this.token)
|
|
14
|
+
return null;
|
|
15
|
+
const expiresAt = this.token.expiresAt || this.token.expires_at;
|
|
16
|
+
const now = Math.floor(new Date().getTime() / 1000);
|
|
17
|
+
if (expiresAt - 60 <= now)
|
|
18
|
+
return null;
|
|
19
|
+
return (revert && (0, parse_token_1.default)(this.token, revert)) || this.token;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = Token;
|
|
23
|
+
//# sourceMappingURL=token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":";;;;;AACA,sEAA6C;AAE7C,MAAqB,KAAK;IAGxB,QAAQ,CAAC,KAAe,EAAE,MAAM,GAAG,KAAK;QACtC,IAAI,CAAC,KAAK,GAAG,IAAA,qBAAU,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAEvC,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,QAAQ,CAAC,MAAM,GAAG,KAAK;QACrB,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAO,IAAI,CAAC;QAE7B,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC;QAChE,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;QAEpD,IAAI,SAAS,GAAG,EAAE,IAAI,GAAG;YAAE,OAAO,IAAI,CAAC;QAEvC,OAAO,CAAC,MAAM,IAAI,IAAA,qBAAU,EAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC;IAClE,CAAC;CACF;AAnBD,wBAmBC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ResponseType } from 'openid-client';
|
|
2
|
+
export declare type Connect = {
|
|
3
|
+
grantType?: string;
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
oidcPath: string;
|
|
6
|
+
clientId: string;
|
|
7
|
+
clientSecret: string;
|
|
8
|
+
responseTypes: ResponseType[];
|
|
9
|
+
grantTypes: string[];
|
|
10
|
+
timeout?: number;
|
|
11
|
+
redirectUri?: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.type.js","sourceRoot":"","sources":["../../src/types/connect.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.type.js","sourceRoot":"","sources":["../../src/types/error-handler.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare type TokenSetParsed = {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
expiresAt: number;
|
|
4
|
+
expiresIn: number;
|
|
5
|
+
idToken: string;
|
|
6
|
+
refreshToken: string;
|
|
7
|
+
scope: string;
|
|
8
|
+
sessionState: string;
|
|
9
|
+
tokenType: string;
|
|
10
|
+
};
|
|
11
|
+
export declare type TokenSetSource = {
|
|
12
|
+
access_token: string;
|
|
13
|
+
expires_at: number;
|
|
14
|
+
expires_in: number;
|
|
15
|
+
id_token: string;
|
|
16
|
+
refresh_token: string;
|
|
17
|
+
scope: string;
|
|
18
|
+
session_state: string;
|
|
19
|
+
token_type: string;
|
|
20
|
+
};
|
|
21
|
+
export declare type TokenSet = TokenSetSource & TokenSetParsed;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-set.type.js","sourceRoot":"","sources":["../../src/types/token-set.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (grantType) => (error) => {
|
|
4
|
+
const { name, code, url, message } = error;
|
|
5
|
+
if (name && code && url) {
|
|
6
|
+
throw new Error(`[${grantType}] Error: ${JSON.stringify({ name, code, url })}`);
|
|
7
|
+
}
|
|
8
|
+
if (message) {
|
|
9
|
+
throw new Error(`[${grantType}] Error: ${message}`);
|
|
10
|
+
}
|
|
11
|
+
throw error;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=error-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/utils/error-handler.ts"],"names":[],"mappings":";;AAAA,kBAAe,CAAC,SAAiB,EAAE,EAAE,CACnC,CAAC,KAA8C,EAAE,EAAE;IACjD,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;IAE3C,IAAI,IAAI,IAAI,IAAI,IAAI,GAAG,EAAE;QACvB,MAAM,IAAI,KAAK,CACb,IAAI,SAAS,YAAY,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,CAC/D,CAAC;KACH;IAED,IAAI,OAAO,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,IAAI,SAAS,YAAY,OAAO,EAAE,CAAC,CAAC;KACrD;IAED,MAAM,KAAK,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tokenMapper = {
|
|
4
|
+
accessToken: 'access_token',
|
|
5
|
+
expiresAt: 'expires_at',
|
|
6
|
+
expiresIn: 'expires_in',
|
|
7
|
+
idToken: 'id_token',
|
|
8
|
+
refreshToken: 'refresh_token',
|
|
9
|
+
scope: 'scope',
|
|
10
|
+
sessionState: 'session_state',
|
|
11
|
+
tokenType: 'token_type',
|
|
12
|
+
};
|
|
13
|
+
exports.default = (tokenSet, revert = false) => {
|
|
14
|
+
const tokenSetParsed = {};
|
|
15
|
+
Object.keys(tokenMapper).forEach((mapperKey) => {
|
|
16
|
+
const key = ((revert && tokenMapper[mapperKey]) ||
|
|
17
|
+
mapperKey);
|
|
18
|
+
const value = (revert && tokenSet[mapperKey]) ||
|
|
19
|
+
tokenSet[tokenMapper[mapperKey]];
|
|
20
|
+
if (typeof value !== 'undefined') {
|
|
21
|
+
tokenSetParsed[key] = value;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return tokenSetParsed;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=parse-token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-token.js","sourceRoot":"","sources":["../../src/utils/parse-token.ts"],"names":[],"mappings":";;AAMA,MAAM,WAAW,GAAuD;IACtE,WAAW,EAAE,cAAc;IAC3B,SAAS,EAAE,YAAY;IACvB,SAAS,EAAE,YAAY;IACvB,OAAO,EAAE,UAAU;IACnB,YAAY,EAAE,eAAe;IAC7B,KAAK,EAAE,OAAO;IACd,YAAY,EAAE,eAAe;IAC7B,SAAS,EAAE,YAAY;CACxB,CAAC;AAEF,kBAAe,CAAC,QAAkB,EAAE,MAAM,GAAG,KAAK,EAAY,EAAE;IAC9D,MAAM,cAAc,GAAG,EAAqC,CAAC;IAE7D,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;QAC7C,MAAM,GAAG,GAAG,CAAC,CAAC,MAAM,IAAI,WAAW,CAAC,SAAiC,CAAC,CAAC;YACrE,SAAS,CAAyB,CAAC;QACrC,MAAM,KAAK,GACT,CAAC,MAAM,IAAI,QAAQ,CAAC,SAAiC,CAAC,CAAC;YACvD,QAAQ,CAAC,WAAW,CAAC,SAAiC,CAAC,CAAC,CAAC;QAE3D,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;YAChC,cAAc,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SAC7B;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,cAA0B,CAAC;AACpC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edirect/oidc-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "OpenId Connect Client",
|
|
5
|
-
"
|
|
5
|
+
"author": "Bolttech <devs@bolttech.io>",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
6
9
|
"packageScope": "@edirect",
|
|
7
|
-
"author": "EDirectInsure",
|
|
8
|
-
"license": "ISC",
|
|
9
10
|
"engines": {
|
|
10
|
-
"node": "^
|
|
11
|
+
"node": "^14.x.x"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
+
"build": "npm run clean && npx tsc --project tsconfig.json",
|
|
15
|
+
"clean": "rimraf dist",
|
|
16
|
+
"lint": "npm run eslint && npm run prettier",
|
|
17
|
+
"prettier": "prettier --parser typescript --single-quote --write \"./src/**/*.ts\"",
|
|
18
|
+
"eslint": "eslint --fix \"./src/**/*.ts\""
|
|
14
19
|
},
|
|
15
20
|
"dependencies": {
|
|
16
21
|
"openid-client": "^3.8.3"
|
|
17
22
|
},
|
|
18
23
|
"devDependencies": {
|
|
19
|
-
"
|
|
20
|
-
"eslint-
|
|
21
|
-
"eslint
|
|
22
|
-
"eslint
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
24
|
+
"@types/node": "^17.0.12",
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^5.10.1",
|
|
26
|
+
"@typescript-eslint/parser": "^5.10.1",
|
|
27
|
+
"eslint": "^8.7.0",
|
|
28
|
+
"husky": "^7.0.4",
|
|
29
|
+
"prettier": "^2.5.1",
|
|
30
|
+
"rimraf": "^3.0.2",
|
|
31
|
+
"typescript": "^4.5.5"
|
|
26
32
|
},
|
|
27
33
|
"husky": {
|
|
28
34
|
"hooks": {
|
|
29
|
-
"pre-commit": "npm run
|
|
35
|
+
"pre-commit": "npm run lint",
|
|
36
|
+
"pre-push": "npm run build"
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
39
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": [
|
|
3
|
-
"standard",
|
|
4
|
-
"plugin:node/recommended",
|
|
5
|
-
"plugin:promise/recommended",
|
|
6
|
-
"plugin:import/errors",
|
|
7
|
-
"plugin:import/warnings"
|
|
8
|
-
],
|
|
9
|
-
"plugins": [
|
|
10
|
-
"standard"
|
|
11
|
-
],
|
|
12
|
-
"rules": {
|
|
13
|
-
"camelcase": [
|
|
14
|
-
0
|
|
15
|
-
],
|
|
16
|
-
"indent": [
|
|
17
|
-
2,
|
|
18
|
-
2,
|
|
19
|
-
{
|
|
20
|
-
"SwitchCase": 2
|
|
21
|
-
}
|
|
22
|
-
],
|
|
23
|
-
"object-curly-spacing": [
|
|
24
|
-
"error",
|
|
25
|
-
"always"
|
|
26
|
-
],
|
|
27
|
-
"array-bracket-spacing": [
|
|
28
|
-
"error",
|
|
29
|
-
"always"
|
|
30
|
-
],
|
|
31
|
-
"eqeqeq": [
|
|
32
|
-
"error",
|
|
33
|
-
"always"
|
|
34
|
-
],
|
|
35
|
-
"no-else-return": [
|
|
36
|
-
"error",
|
|
37
|
-
{
|
|
38
|
-
"allowElseIf": false
|
|
39
|
-
}
|
|
40
|
-
],
|
|
41
|
-
"no-multi-spaces": "error",
|
|
42
|
-
"no-new-wrappers": "error",
|
|
43
|
-
"no-redeclare": "error",
|
|
44
|
-
"no-undef-init": "error",
|
|
45
|
-
"no-unused-vars": "error",
|
|
46
|
-
"no-use-before-define": [
|
|
47
|
-
"error",
|
|
48
|
-
{
|
|
49
|
-
"functions": false
|
|
50
|
-
}
|
|
51
|
-
],
|
|
52
|
-
"comma-dangle": [
|
|
53
|
-
"error",
|
|
54
|
-
"never"
|
|
55
|
-
],
|
|
56
|
-
"comma-spacing": [
|
|
57
|
-
"error",
|
|
58
|
-
{
|
|
59
|
-
"before": false,
|
|
60
|
-
"after": true
|
|
61
|
-
}
|
|
62
|
-
],
|
|
63
|
-
"comma-style": [
|
|
64
|
-
"error",
|
|
65
|
-
"last"
|
|
66
|
-
],
|
|
67
|
-
"no-implied-eval": "error",
|
|
68
|
-
"no-mixed-operators": "off",
|
|
69
|
-
"node/exports-style": [
|
|
70
|
-
"error",
|
|
71
|
-
"module.exports"
|
|
72
|
-
],
|
|
73
|
-
"node/no-unpublished-require": "off",
|
|
74
|
-
"promise/no-callback-in-promise": "off",
|
|
75
|
-
"promise/no-nesting": "off",
|
|
76
|
-
"max-len": [
|
|
77
|
-
"error",
|
|
78
|
-
{
|
|
79
|
-
"code": 120,
|
|
80
|
-
"tabWidth": 2,
|
|
81
|
-
"comments": 100,
|
|
82
|
-
"ignoreTrailingComments": true,
|
|
83
|
-
"ignoreUrls": true,
|
|
84
|
-
"ignoreStrings": true,
|
|
85
|
-
"ignoreTemplateLiterals": true,
|
|
86
|
-
"ignoreRegExpLiterals": true
|
|
87
|
-
}
|
|
88
|
-
],
|
|
89
|
-
"max-lines": [
|
|
90
|
-
"error",
|
|
91
|
-
{
|
|
92
|
-
"max": 350,
|
|
93
|
-
"skipBlankLines": true,
|
|
94
|
-
"skipComments": true
|
|
95
|
-
}
|
|
96
|
-
],
|
|
97
|
-
"complexity": [
|
|
98
|
-
"error",
|
|
99
|
-
{
|
|
100
|
-
"max": 20
|
|
101
|
-
}
|
|
102
|
-
],
|
|
103
|
-
"prefer-arrow-callback": [
|
|
104
|
-
"error",
|
|
105
|
-
{
|
|
106
|
-
"allowNamedFunctions": false,
|
|
107
|
-
"allowUnboundThis": true
|
|
108
|
-
}
|
|
109
|
-
],
|
|
110
|
-
"arrow-parens": [
|
|
111
|
-
2,
|
|
112
|
-
"as-needed"
|
|
113
|
-
],
|
|
114
|
-
"dot-notation": [
|
|
115
|
-
"error",
|
|
116
|
-
{
|
|
117
|
-
"allowKeywords": true
|
|
118
|
-
}
|
|
119
|
-
],
|
|
120
|
-
"require-atomic-updates": "error",
|
|
121
|
-
"no-regex-spaces": "error",
|
|
122
|
-
"no-template-curly-in-string": "error",
|
|
123
|
-
"no-unreachable": "error",
|
|
124
|
-
"no-return-await": "error",
|
|
125
|
-
"no-return-assign": "error",
|
|
126
|
-
"no-unused-expressions": "error",
|
|
127
|
-
"no-useless-return": "error",
|
|
128
|
-
"no-throw-literal": "error",
|
|
129
|
-
"handle-callback-err": "error",
|
|
130
|
-
"key-spacing": [
|
|
131
|
-
"error",
|
|
132
|
-
{
|
|
133
|
-
"beforeColon": false,
|
|
134
|
-
"afterColon": true,
|
|
135
|
-
"mode": "strict"
|
|
136
|
-
}
|
|
137
|
-
],
|
|
138
|
-
"linebreak-style": [
|
|
139
|
-
"error",
|
|
140
|
-
"unix"
|
|
141
|
-
],
|
|
142
|
-
"no-trailing-spaces": "error",
|
|
143
|
-
"no-whitespace-before-property": "error",
|
|
144
|
-
"no-unneeded-ternary": "error",
|
|
145
|
-
"prefer-object-spread": "error",
|
|
146
|
-
"array-callback-return": "error",
|
|
147
|
-
"arrow-body-style": [
|
|
148
|
-
"error",
|
|
149
|
-
"as-needed"
|
|
150
|
-
],
|
|
151
|
-
"semi": [
|
|
152
|
-
"error",
|
|
153
|
-
"always"
|
|
154
|
-
],
|
|
155
|
-
"node/no-extraneous-require": [
|
|
156
|
-
"error",
|
|
157
|
-
{
|
|
158
|
-
"allowModules": ["qs"]
|
|
159
|
-
}
|
|
160
|
-
]
|
|
161
|
-
},
|
|
162
|
-
"globals": {
|
|
163
|
-
"logs": true
|
|
164
|
-
}
|
|
165
|
-
}
|
package/client_credentials.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
const OidcConnect = require('./connect');
|
|
2
|
-
|
|
3
|
-
class OidcAuthorizationCode extends OidcConnect {
|
|
4
|
-
connect ({
|
|
5
|
-
baseUrl,
|
|
6
|
-
oidcPath,
|
|
7
|
-
clientId,
|
|
8
|
-
clientSecret,
|
|
9
|
-
redirectUri
|
|
10
|
-
}) {
|
|
11
|
-
return super.connect({
|
|
12
|
-
baseUrl,
|
|
13
|
-
oidcPath,
|
|
14
|
-
clientId,
|
|
15
|
-
clientSecret,
|
|
16
|
-
redirectUri,
|
|
17
|
-
responseTypes: [ 'none' ],
|
|
18
|
-
grantTypes: [ 'client_credentials' ]
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
tokenParse (tokenSet, revert = false) {
|
|
23
|
-
const tokenSetParsed = {};
|
|
24
|
-
|
|
25
|
-
Object.keys(this.tokenMapper).forEach(mapperKey => {
|
|
26
|
-
const key = revert && this.tokenMapper[mapperKey] || mapperKey;
|
|
27
|
-
const value = revert && tokenSet[mapperKey] ||
|
|
28
|
-
tokenSet[this.tokenMapper[mapperKey]];
|
|
29
|
-
|
|
30
|
-
if (typeof value !== 'undefined') {
|
|
31
|
-
tokenSetParsed[key] = value;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
return tokenSetParsed;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async getAccessToken (data) {
|
|
39
|
-
try {
|
|
40
|
-
const params = {
|
|
41
|
-
grant_type: 'client_credentials',
|
|
42
|
-
...data
|
|
43
|
-
};
|
|
44
|
-
return this.client.grant(params)
|
|
45
|
-
.then(tokenSet => this.tokenParse(tokenSet));
|
|
46
|
-
} catch (error) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async getRefreshToken () {
|
|
52
|
-
throw new Error('This method is not allowed to client_credentials grant type.');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
module.exports = new OidcAuthorizationCode();
|
package/connect.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
const { custom, Issuer } = require('openid-client');
|
|
2
|
-
const http = require('http');
|
|
3
|
-
const https = require('https');
|
|
4
|
-
const httpAgent = new http.Agent({ keepAlive: true });
|
|
5
|
-
const httpsAgent = new https.Agent({ keepAlive: true });
|
|
6
|
-
|
|
7
|
-
class OidcConnect {
|
|
8
|
-
constructor () {
|
|
9
|
-
this.tokenMapper = {
|
|
10
|
-
accessToken: 'access_token',
|
|
11
|
-
expiresAt: 'expires_at',
|
|
12
|
-
expiresIn: 'expires_in',
|
|
13
|
-
idToken: 'id_token',
|
|
14
|
-
refreshToken: 'refresh_token',
|
|
15
|
-
scope: 'scope',
|
|
16
|
-
sessionState: 'session_state',
|
|
17
|
-
tokenType: 'token_type'
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
connect ({
|
|
22
|
-
baseUrl,
|
|
23
|
-
oidcPath,
|
|
24
|
-
clientId,
|
|
25
|
-
clientSecret,
|
|
26
|
-
redirectUri,
|
|
27
|
-
responseTypes,
|
|
28
|
-
grantTypes,
|
|
29
|
-
timeout
|
|
30
|
-
}) {
|
|
31
|
-
this.baseUrl = baseUrl;
|
|
32
|
-
this.redirectUri = redirectUri;
|
|
33
|
-
|
|
34
|
-
const oidcConfiguration = {
|
|
35
|
-
client_id: clientId,
|
|
36
|
-
client_secret: clientSecret,
|
|
37
|
-
redirect_uris: [ redirectUri ],
|
|
38
|
-
response_types: responseTypes,
|
|
39
|
-
grant_types: grantTypes
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const protocol = (baseUrl.includes('https') && 'https:') || 'http:';
|
|
43
|
-
const agent = (baseUrl.includes('https') && httpsAgent) || httpAgent;
|
|
44
|
-
|
|
45
|
-
if (custom && custom.setHttpOptionsDefaults) {
|
|
46
|
-
custom.setHttpOptionsDefaults({
|
|
47
|
-
timeout: timeout || 60000, protocol, agent
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return Issuer.discover(`${baseUrl}/${oidcPath}`)
|
|
52
|
-
.then(oidcIssuer => {
|
|
53
|
-
this.client = new oidcIssuer.Client(oidcConfiguration);
|
|
54
|
-
return this.client;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
module.exports = OidcConnect;
|
package/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module.exports = async params => {
|
|
2
|
-
const oidc = require(`./${params.grantType}`);
|
|
3
|
-
|
|
4
|
-
await oidc.connect({
|
|
5
|
-
baseUrl: params.baseUrl,
|
|
6
|
-
oidcPath: params.oidcPath,
|
|
7
|
-
clientId: params.clientId,
|
|
8
|
-
clientSecret: params.clientSecret
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
return oidc;
|
|
12
|
-
};
|
package/password.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const OidcConnect = require('./connect');
|
|
2
|
-
|
|
3
|
-
class OidcAuthorizationCode extends OidcConnect {
|
|
4
|
-
connect ({
|
|
5
|
-
baseUrl,
|
|
6
|
-
oidcPath,
|
|
7
|
-
clientId,
|
|
8
|
-
clientSecret,
|
|
9
|
-
redirectUri
|
|
10
|
-
}) {
|
|
11
|
-
return super.connect({
|
|
12
|
-
baseUrl,
|
|
13
|
-
oidcPath,
|
|
14
|
-
clientId,
|
|
15
|
-
clientSecret,
|
|
16
|
-
redirectUri,
|
|
17
|
-
responseTypes: [ 'none' ],
|
|
18
|
-
grantTypes: [ 'password', 'refresh_token' ]
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
tokenParse (tokenSet, revert = false) {
|
|
23
|
-
const tokenSetParsed = {};
|
|
24
|
-
|
|
25
|
-
Object.keys(this.tokenMapper).forEach(mapperKey => {
|
|
26
|
-
const key = revert && this.tokenMapper[mapperKey] || mapperKey;
|
|
27
|
-
const value = revert && tokenSet[mapperKey] ||
|
|
28
|
-
tokenSet[this.tokenMapper[mapperKey]];
|
|
29
|
-
|
|
30
|
-
if (typeof value !== 'undefined') {
|
|
31
|
-
tokenSetParsed[key] = value;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
return tokenSetParsed;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async getAccessToken (data) {
|
|
39
|
-
try {
|
|
40
|
-
const params = {
|
|
41
|
-
grant_type: 'password',
|
|
42
|
-
...data
|
|
43
|
-
};
|
|
44
|
-
return this.client.grant(params)
|
|
45
|
-
.then(tokenSet => this.tokenParse(tokenSet));
|
|
46
|
-
} catch (error) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async getRefreshToken (tokenSet) {
|
|
52
|
-
try {
|
|
53
|
-
const refreshTokenSet = await this.client
|
|
54
|
-
.refresh(tokenSet.refreshToken)
|
|
55
|
-
.then(tokenSet => this.tokenParse(tokenSet));
|
|
56
|
-
|
|
57
|
-
return refreshTokenSet;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = new OidcAuthorizationCode();
|
package/sso.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const OidcConnect = require('./connect');
|
|
2
|
-
|
|
3
|
-
class OidcAuthorizationCode extends OidcConnect {
|
|
4
|
-
connect ({
|
|
5
|
-
baseUrl,
|
|
6
|
-
oidcPath,
|
|
7
|
-
clientId,
|
|
8
|
-
clientSecret,
|
|
9
|
-
redirectUri
|
|
10
|
-
}) {
|
|
11
|
-
return super.connect({
|
|
12
|
-
baseUrl,
|
|
13
|
-
oidcPath,
|
|
14
|
-
clientId,
|
|
15
|
-
clientSecret,
|
|
16
|
-
redirectUri,
|
|
17
|
-
responseTypes: [ 'none' ],
|
|
18
|
-
grantTypes: [ 'sso', 'refresh_token' ]
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
tokenParse (tokenSet, revert = false) {
|
|
23
|
-
const tokenSetParsed = {};
|
|
24
|
-
|
|
25
|
-
Object.keys(this.tokenMapper).forEach(mapperKey => {
|
|
26
|
-
const key = revert && this.tokenMapper[mapperKey] || mapperKey;
|
|
27
|
-
const value = revert && tokenSet[mapperKey] ||
|
|
28
|
-
tokenSet[this.tokenMapper[mapperKey]];
|
|
29
|
-
|
|
30
|
-
if (typeof value !== 'undefined') {
|
|
31
|
-
tokenSetParsed[key] = value;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
return tokenSetParsed;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async getAccessToken (data) {
|
|
39
|
-
try {
|
|
40
|
-
const params = {
|
|
41
|
-
grant_type: 'sso',
|
|
42
|
-
...data
|
|
43
|
-
};
|
|
44
|
-
return this.client.grant(params)
|
|
45
|
-
.then(tokenSet => this.tokenParse(tokenSet));
|
|
46
|
-
} catch (error) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async getRefreshToken (tokenSet) {
|
|
52
|
-
try {
|
|
53
|
-
const refreshTokenSet = await this.client
|
|
54
|
-
.refresh(tokenSet.refreshToken)
|
|
55
|
-
.then(tokenSet => this.tokenParse(tokenSet));
|
|
56
|
-
|
|
57
|
-
return refreshTokenSet;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
module.exports = new OidcAuthorizationCode();
|
package/test.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
(async () => {
|
|
2
|
-
const OidcClient = require('./index');
|
|
3
|
-
|
|
4
|
-
const oidcClient = await OidcClient({
|
|
5
|
-
grantType: 'sso',
|
|
6
|
-
baseUrl: 'http://localhost:9090',
|
|
7
|
-
oidcPath: 'oidc',
|
|
8
|
-
clientId: '5c7fbd62f8905a001897998d',
|
|
9
|
-
clientSecret: '78MLLA1230pL8q76WLK6h7B64PFvt76z',
|
|
10
|
-
redirectUri: 'localhost:3000/en'
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const loginData = {
|
|
14
|
-
username: 'fwd#danilo.medeiros@edirectinsure.com',
|
|
15
|
-
ssoKey: '123123123'
|
|
16
|
-
}; // only necessary to password grant type
|
|
17
|
-
|
|
18
|
-
const accessTokenSet = await oidcClient
|
|
19
|
-
.getAccessToken(loginData || undefined);
|
|
20
|
-
console.log('\n', { accessTokenSet }, '\n');
|
|
21
|
-
|
|
22
|
-
// not applicable to client_credentials grant type
|
|
23
|
-
const refreshTokenSet = await oidcClient
|
|
24
|
-
.getRefreshToken(accessTokenSet);
|
|
25
|
-
console.log('\n', { refreshTokenSet }, '\n');
|
|
26
|
-
})();
|