chyz 2.0.1-rc.3 → 2.0.1-rc.36
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/BaseChyz.ts +623 -0
- package/README.md +518 -518
- package/base/ActionFilter.ts +88 -0
- package/base/BadRequestHttpException.ts +14 -0
- package/base/{BaseError.js → BaseError.ts} +26 -18
- package/base/Behavior.ts +6 -0
- package/base/CBaseObject.ts +17 -0
- package/base/CEvents.ts +24 -0
- package/base/{CRequest.js → CRequest.ts} +6 -2
- package/base/CWebController.ts +90 -0
- package/base/Component.ts +66 -0
- package/{model/RouteDefinition.js → base/Configurable.ts} +4 -2
- package/base/DataErrorDbException.ts +16 -0
- package/base/DbConnection.ts +55 -0
- package/base/ForbiddenHttpException.ts +15 -0
- package/base/InvalidArgumentException.ts +16 -0
- package/base/InvalidConfigException.ts +16 -0
- package/base/{Logs.js → Logs.ts} +41 -24
- package/base/{Model.js → Model.ts} +225 -158
- package/base/{ModelManager.js → ModelManager.ts} +7 -7
- package/base/NotFoundHttpException.ts +14 -0
- package/base/RestClient.ts +26 -0
- package/base/UnauthorizedHttpException.ts +17 -0
- package/base/ValidationHttpException.ts +14 -0
- package/base/db/Exception.ts +14 -0
- package/base/index.ts +19 -0
- package/decorator/Middleware.ts +9 -0
- package/decorator/{controller.js → controller.ts} +7 -9
- package/decorator/{delete.js → delete.ts} +9 -9
- package/decorator/enums/ControllerDecoratorParams.ts +5 -0
- package/decorator/{get.js → get.ts} +11 -10
- package/decorator/index.ts +5 -0
- package/decorator/{post.js → post.ts} +10 -10
- package/decorator/{put.js → put.ts} +9 -9
- package/filters/AccessControl.ts +74 -0
- package/filters/AccessRule.ts +182 -0
- package/filters/auth/{AuthInterface.d.ts → AuthInterface.ts} +15 -7
- package/filters/auth/AuthMethod.ts +100 -0
- package/filters/auth/HttpBasicAuth.ts +79 -0
- package/filters/auth/HttpBearerAuth.ts +34 -0
- package/filters/auth/HttpHeaderAuth.ts +61 -0
- package/filters/auth/JwtHttpBearerAuth.ts +83 -0
- package/filters/auth/KeyCloakHttpBearerAuth.ts +114 -0
- package/filters/auth/index.ts +4 -0
- package/filters/index.ts +2 -0
- package/index.ts +80 -0
- package/log/config/log4js.json +55 -55
- package/model/RouteDefinition.ts +18 -0
- package/package.json +63 -61
- package/rbac/AuthAssignment.ts +50 -0
- package/rbac/{AuthItem.js → AuthItem.ts} +22 -16
- package/rbac/{AuthItemChild.js → AuthItemChild.ts} +21 -14
- package/rbac/AuthManager.ts +399 -0
- package/rbac/index.ts +12 -0
- package/{web/IdentityInterface.js → requiments/Glob.ts} +3 -2
- package/requiments/{ReflectUtil.js → ReflectUtil.ts} +6 -4
- package/requiments/{Utils.js → Utils.ts} +40 -34
- package/validators/BooleanValidator.ts +0 -0
- package/validators/CompareValidator.ts +0 -0
- package/validators/DateValidator.ts +0 -0
- package/validators/EmailValidator.ts +0 -0
- package/validators/Validator.ts +27 -0
- package/web/{IdentityInterface.d.ts → IdentityInterface.ts} +15 -2
- package/web/WebUser.ts +187 -0
- package/BaseChyz.d.ts +0 -74
- package/BaseChyz.d.ts.map +0 -1
- package/BaseChyz.js +0 -569
- package/base/ActionFilter.d.ts +0 -33
- package/base/ActionFilter.d.ts.map +0 -1
- package/base/ActionFilter.js +0 -90
- package/base/BadRequestHttpException.d.ts +0 -6
- package/base/BadRequestHttpException.d.ts.map +0 -1
- package/base/BadRequestHttpException.js +0 -17
- package/base/BaseError.d.ts +0 -44
- package/base/BaseError.d.ts.map +0 -1
- package/base/Behavior.d.ts +0 -4
- package/base/Behavior.d.ts.map +0 -1
- package/base/Behavior.js +0 -7
- package/base/CBaseObject.d.ts +0 -5
- package/base/CBaseObject.d.ts.map +0 -1
- package/base/CBaseObject.js +0 -16
- package/base/CEvents.d.ts +0 -13
- package/base/CEvents.d.ts.map +0 -1
- package/base/CEvents.js +0 -24
- package/base/CRequest.d.ts +0 -2
- package/base/CRequest.d.ts.map +0 -1
- package/base/CWebController.d.ts +0 -44
- package/base/CWebController.d.ts.map +0 -1
- package/base/CWebController.js +0 -81
- package/base/Component.d.ts +0 -21
- package/base/Component.d.ts.map +0 -1
- package/base/Component.js +0 -57
- package/base/Configurable.d.ts +0 -3
- package/base/Configurable.d.ts.map +0 -1
- package/base/Configurable.js +0 -8
- package/base/DataErrorDbException.d.ts +0 -6
- package/base/DataErrorDbException.d.ts.map +0 -1
- package/base/DataErrorDbException.js +0 -17
- package/base/DbConnection.d.ts +0 -13
- package/base/DbConnection.d.ts.map +0 -1
- package/base/DbConnection.js +0 -53
- package/base/ForbiddenHttpException.d.ts +0 -5
- package/base/ForbiddenHttpException.d.ts.map +0 -1
- package/base/ForbiddenHttpException.js +0 -16
- package/base/InvalidArgumentException.d.ts +0 -6
- package/base/InvalidArgumentException.d.ts.map +0 -1
- package/base/InvalidArgumentException.js +0 -17
- package/base/InvalidConfigException.d.ts +0 -6
- package/base/InvalidConfigException.d.ts.map +0 -1
- package/base/InvalidConfigException.js +0 -17
- package/base/Logs.d.ts +0 -22
- package/base/Logs.d.ts.map +0 -1
- package/base/Model.d.ts +0 -302
- package/base/Model.d.ts.map +0 -1
- package/base/ModelManager.d.ts +0 -2
- package/base/ModelManager.d.ts.map +0 -1
- package/base/NotFoundHttpException.d.ts +0 -6
- package/base/NotFoundHttpException.d.ts.map +0 -1
- package/base/NotFoundHttpException.js +0 -17
- package/base/RestClient.d.ts +0 -7
- package/base/RestClient.d.ts.map +0 -1
- package/base/RestClient.js +0 -25
- package/base/UnauthorizedHttpException.d.ts +0 -6
- package/base/UnauthorizedHttpException.d.ts.map +0 -1
- package/base/UnauthorizedHttpException.js +0 -17
- package/base/ValidationHttpException.d.ts +0 -6
- package/base/ValidationHttpException.d.ts.map +0 -1
- package/base/ValidationHttpException.js +0 -17
- package/base/db/Exception.d.ts +0 -7
- package/base/db/Exception.d.ts.map +0 -1
- package/base/db/Exception.js +0 -15
- package/base/index.d.ts +0 -20
- package/base/index.d.ts.map +0 -1
- package/base/index.js +0 -41
- package/decorator/Middleware.d.ts +0 -4
- package/decorator/Middleware.d.ts.map +0 -1
- package/decorator/Middleware.js +0 -11
- package/decorator/controller.d.ts +0 -3
- package/decorator/controller.d.ts.map +0 -1
- package/decorator/delete.d.ts +0 -3
- package/decorator/delete.d.ts.map +0 -1
- package/decorator/enums/ControllerDecoratorParams.d.ts +0 -6
- package/decorator/enums/ControllerDecoratorParams.d.ts.map +0 -1
- package/decorator/enums/ControllerDecoratorParams.js +0 -9
- package/decorator/get.d.ts +0 -3
- package/decorator/get.d.ts.map +0 -1
- package/decorator/index.d.ts +0 -6
- package/decorator/index.d.ts.map +0 -1
- package/decorator/index.js +0 -13
- package/decorator/post.d.ts +0 -3
- package/decorator/post.d.ts.map +0 -1
- package/decorator/put.d.ts +0 -3
- package/decorator/put.d.ts.map +0 -1
- package/filters/AccessControl.d.ts +0 -13
- package/filters/AccessControl.d.ts.map +0 -1
- package/filters/AccessControl.js +0 -83
- package/filters/AccessRule.d.ts +0 -83
- package/filters/AccessRule.d.ts.map +0 -1
- package/filters/AccessRule.js +0 -136
- package/filters/auth/AuthInterface.d.ts.map +0 -1
- package/filters/auth/AuthInterface.js +0 -2
- package/filters/auth/AuthMethod.d.ts +0 -38
- package/filters/auth/AuthMethod.d.ts.map +0 -1
- package/filters/auth/AuthMethod.js +0 -82
- package/filters/auth/HttpBasicAuth.d.ts +0 -24
- package/filters/auth/HttpBasicAuth.d.ts.map +0 -1
- package/filters/auth/HttpBasicAuth.js +0 -78
- package/filters/auth/HttpBearerAuth.d.ts +0 -18
- package/filters/auth/HttpBearerAuth.d.ts.map +0 -1
- package/filters/auth/HttpBearerAuth.js +0 -33
- package/filters/auth/HttpHeaderAuth.d.ts +0 -19
- package/filters/auth/HttpHeaderAuth.d.ts.map +0 -1
- package/filters/auth/HttpHeaderAuth.js +0 -64
- package/filters/auth/JwtHttpBearerAuth.d.ts +0 -21
- package/filters/auth/JwtHttpBearerAuth.d.ts.map +0 -1
- package/filters/auth/JwtHttpBearerAuth.js +0 -87
- package/filters/auth/KeyCloakHttpBearerAuth.d.ts +0 -23
- package/filters/auth/KeyCloakHttpBearerAuth.d.ts.map +0 -1
- package/filters/auth/KeyCloakHttpBearerAuth.js +0 -116
- package/filters/auth/index.d.ts +0 -5
- package/filters/auth/index.d.ts.map +0 -1
- package/filters/auth/index.js +0 -11
- package/filters/index.d.ts +0 -3
- package/filters/index.d.ts.map +0 -1
- package/filters/index.js +0 -7
- package/index.d.ts +0 -26
- package/index.d.ts.map +0 -1
- package/index.js +0 -72
- package/model/RouteDefinition.d.ts +0 -7
- package/model/RouteDefinition.d.ts.map +0 -1
- package/rbac/AuthAssignment.d.ts +0 -19
- package/rbac/AuthAssignment.d.ts.map +0 -1
- package/rbac/AuthAssignment.js +0 -44
- package/rbac/AuthItem.d.ts +0 -27
- package/rbac/AuthItem.d.ts.map +0 -1
- package/rbac/AuthItemChild.d.ts +0 -19
- package/rbac/AuthItemChild.d.ts.map +0 -1
- package/rbac/AuthManager.d.ts +0 -112
- package/rbac/AuthManager.d.ts.map +0 -1
- package/rbac/AuthManager.js +0 -356
- package/rbac/index.d.ts +0 -5
- package/rbac/index.d.ts.map +0 -1
- package/rbac/index.js +0 -28
- package/requiments/Glob.d.ts +0 -3
- package/requiments/Glob.d.ts.map +0 -1
- package/requiments/Glob.js +0 -10
- package/requiments/ReflectUtil.d.ts +0 -1
- package/requiments/ReflectUtil.d.ts.map +0 -1
- package/requiments/Utils.d.ts +0 -2
- package/requiments/Utils.d.ts.map +0 -1
- package/validators/BooleanValidator.d.ts +0 -1
- package/validators/BooleanValidator.d.ts.map +0 -1
- package/validators/BooleanValidator.js +0 -1
- package/validators/CompareValidator.d.ts +0 -1
- package/validators/CompareValidator.d.ts.map +0 -1
- package/validators/CompareValidator.js +0 -1
- package/validators/DateValidator.d.ts +0 -1
- package/validators/DateValidator.d.ts.map +0 -1
- package/validators/DateValidator.js +0 -1
- package/validators/EmailValidator.d.ts +0 -1
- package/validators/EmailValidator.d.ts.map +0 -1
- package/validators/EmailValidator.js +0 -1
- package/validators/Validator.d.ts +0 -18
- package/validators/Validator.d.ts.map +0 -1
- package/validators/Validator.js +0 -27
- package/web/IdentityInterface.d.ts.map +0 -1
- package/web/WebUser.d.ts +0 -72
- package/web/WebUser.d.ts.map +0 -1
- package/web/WebUser.js +0 -165
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {Request, Response} from "express";
|
|
9
|
+
import {WebUser} from "../../web/WebUser";
|
|
10
|
+
import {AuthMethod} from "./AuthMethod";
|
|
11
|
+
import {InvalidConfigException} from "../../base";
|
|
12
|
+
import BaseChyz from "../../BaseChyz";
|
|
13
|
+
|
|
14
|
+
export class HttpBasicAuth extends AuthMethod {
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @var string the HTTP header name
|
|
18
|
+
*/
|
|
19
|
+
public header = 'Authorization';
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @var string a pattern to use to extract the HTTP authentication value
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
public pattern = /^Basic\s+(.*?)$/;
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
public auth: any = null;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @throws InvalidConfigException
|
|
33
|
+
*/
|
|
34
|
+
public init(): void {
|
|
35
|
+
super.init();
|
|
36
|
+
|
|
37
|
+
if (!this.pattern) {
|
|
38
|
+
throw new InvalidConfigException('You must provide pattern to use to extract the HTTP authentication value!');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.user = BaseChyz.getComponent("user") ?? null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
async authenticate(user: WebUser, request: Request, response: Response) {
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
49
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
50
|
+
return this.fail(response);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let identity = null;
|
|
54
|
+
let token = null;
|
|
55
|
+
|
|
56
|
+
let buff = new Buffer(autHeader[1], "base64");
|
|
57
|
+
let basicauth = buff.toString().split(":");
|
|
58
|
+
|
|
59
|
+
if (this.auth != null) {
|
|
60
|
+
identity = await this.auth(autHeader[1], ...arguments, basicauth)
|
|
61
|
+
} else {
|
|
62
|
+
identity = await user.loginByAccessToken(basicauth, "HttpBasicAuth");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if (identity == null) this.fail(response)
|
|
67
|
+
return identity;
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @throws UnauthorizedHttpException
|
|
74
|
+
*/
|
|
75
|
+
public fail(response: Response): void {
|
|
76
|
+
this.challenge(response)
|
|
77
|
+
this.handleFailure(response);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {HttpHeaderAuth} from "./HttpHeaderAuth";
|
|
9
|
+
import {Response} from "express";
|
|
10
|
+
|
|
11
|
+
export class HttpBearerAuth extends HttpHeaderAuth {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* {@inheritdoc}
|
|
15
|
+
*/
|
|
16
|
+
public header = 'Authorization';
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
public pattern = /^Bearer\s+(.*?)$/;
|
|
19
|
+
/**
|
|
20
|
+
* @var string the HTTP authentication realm
|
|
21
|
+
*/
|
|
22
|
+
public realm = 'api';
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* {@inheritdoc}
|
|
27
|
+
*/
|
|
28
|
+
public challenge(response: Response):Response {
|
|
29
|
+
response.set('WWW-Authenticate', `Bearer realm="${this.realm}"`);
|
|
30
|
+
return response;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {AuthMethod} from "./AuthMethod";
|
|
9
|
+
import {WebUser} from "../../web/WebUser";
|
|
10
|
+
import {Utils} from "../../requiments/Utils";
|
|
11
|
+
import {Request, Response} from "express";
|
|
12
|
+
|
|
13
|
+
export class HttpHeaderAuth extends AuthMethod {
|
|
14
|
+
/**
|
|
15
|
+
* @var string the HTTP header name
|
|
16
|
+
*/
|
|
17
|
+
public header = 'X-Api-Key';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @var string a pattern to use to extract the HTTP authentication value
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
public pattern!: string;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async authenticate(user: WebUser, request:Request, response:Response) {
|
|
28
|
+
let key = Object.keys(request.headers).find(key => key.toLowerCase() === this.header.toLowerCase())
|
|
29
|
+
if (key) {
|
|
30
|
+
let authHeader:any = request.headers[key];
|
|
31
|
+
if (!Utils.isEmpty(authHeader)) {
|
|
32
|
+
if (this.pattern) {
|
|
33
|
+
//preg_match
|
|
34
|
+
let matches = authHeader.match(this.pattern)
|
|
35
|
+
if (matches && matches.length > 0) {
|
|
36
|
+
authHeader = matches[1];
|
|
37
|
+
} else {
|
|
38
|
+
return this.fail(response);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let identity = await user.loginByAccessToken(authHeader, "HttpHeaderAuth");
|
|
43
|
+
if (identity === null) {
|
|
44
|
+
this.challenge(response);
|
|
45
|
+
this.handleFailure(response);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return identity;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return this.fail(response);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @throws UnauthorizedHttpException
|
|
56
|
+
*/
|
|
57
|
+
public fail(response: Response): void {
|
|
58
|
+
this.challenge(response)
|
|
59
|
+
this.handleFailure(response);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
import BaseChyz from "../../BaseChyz";
|
|
8
|
+
import {HttpBearerAuth} from "./HttpBearerAuth";
|
|
9
|
+
import {InvalidConfigException} from "../../base";
|
|
10
|
+
import {Response, Request} from "express";
|
|
11
|
+
import {WebUser} from "../../web/WebUser";
|
|
12
|
+
|
|
13
|
+
const JsonWebToken = require("jsonwebtoken");
|
|
14
|
+
|
|
15
|
+
export class JwtHttpBearerAuth extends HttpBearerAuth {
|
|
16
|
+
/**
|
|
17
|
+
* @var string|array<string, mixed>|Jwt application component ID of the JWT handler, configuration array, or JWT handler object
|
|
18
|
+
* itself. By default it's assumes that component of ID "jwt" has been configured.
|
|
19
|
+
*/
|
|
20
|
+
public jwt = 'jwt'
|
|
21
|
+
public auth: any = null;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @throws InvalidConfigException
|
|
26
|
+
*/
|
|
27
|
+
public init(): void {
|
|
28
|
+
super.init();
|
|
29
|
+
|
|
30
|
+
if (!this.pattern) {
|
|
31
|
+
throw new InvalidConfigException('You must provide pattern to use to extract the HTTP authentication value!');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.user = BaseChyz.getComponent("user") ?? null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
public async authenticate(user: WebUser, request: Request, response: Response) // BC signature
|
|
39
|
+
{
|
|
40
|
+
let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
41
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
42
|
+
return null ;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let identity = null;
|
|
46
|
+
let token = null;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
50
|
+
if (!token) {
|
|
51
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
52
|
+
this.fail(response);
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.",autHeader,request.path);
|
|
56
|
+
this.fail(response);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
if (token !== null) {
|
|
61
|
+
if (this.auth != null) {
|
|
62
|
+
identity = await this.auth(autHeader[1], ...arguments)
|
|
63
|
+
} else {
|
|
64
|
+
identity = await user.loginByAccessToken(autHeader[1], "JwtHttpBearerAuth")
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (identity == null) this.fail(response)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
return identity;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @throws UnauthorizedHttpException
|
|
77
|
+
*/
|
|
78
|
+
public fail(response: Response): void {
|
|
79
|
+
this.challenge(response)
|
|
80
|
+
this.handleFailure(response);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
import BaseChyz from "../../BaseChyz";
|
|
8
|
+
import {HttpBearerAuth} from "./HttpBearerAuth";
|
|
9
|
+
import {InvalidConfigException} from "../../base/InvalidConfigException";
|
|
10
|
+
import {Request, Response} from "express";
|
|
11
|
+
import {WebUser} from "../../web/WebUser";
|
|
12
|
+
|
|
13
|
+
const JsonWebToken = require("jsonwebtoken");
|
|
14
|
+
|
|
15
|
+
export class KeyCloakHttpBearerAuth extends HttpBearerAuth {
|
|
16
|
+
/**
|
|
17
|
+
* @var string|array<string, mixed>|Jwt application component ID of the JWT handler, configuration array, or JWT handler object
|
|
18
|
+
* itself. By default it's assumes that component of ID "jwt" has been configured.
|
|
19
|
+
*/
|
|
20
|
+
public jwt = 'jwt'
|
|
21
|
+
public auth: any = null;
|
|
22
|
+
public keycloak: any = null;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @throws InvalidConfigException
|
|
27
|
+
*/
|
|
28
|
+
public init(): void {
|
|
29
|
+
super.init();
|
|
30
|
+
|
|
31
|
+
if (!this.pattern) {
|
|
32
|
+
throw new InvalidConfigException('You must provide pattern to use to extract the HTTP authentication value!');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.keycloak = BaseChyz.getMiddlewares("keycloak").keycloak ?? null;
|
|
36
|
+
this.user = BaseChyz.getComponent("user") ?? null;
|
|
37
|
+
this.auth = this.KeyCloakCheck;
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async KeyCloakCheck(token: string, request: Request, response: Response,) {
|
|
43
|
+
if (this.keycloak == null) return false;
|
|
44
|
+
// return await this.keycloak.protect('realm:user')(request, response, () => true /*next*/)
|
|
45
|
+
return await this.keycloak.protect()(request, response, () => true /*next*/);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
public async authenticate(user: WebUser, request: Request, response: Response) // BC signature
|
|
50
|
+
{
|
|
51
|
+
|
|
52
|
+
let identity = null;
|
|
53
|
+
let token = null;
|
|
54
|
+
|
|
55
|
+
let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
56
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
57
|
+
return this.fail(response);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
61
|
+
if (!token) {
|
|
62
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
63
|
+
this.fail(response);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (token !== null) {
|
|
67
|
+
identity = await this.KeyCloakCheck(autHeader[1], request, response)
|
|
68
|
+
BaseChyz.debug("KeyCloakCheck Result:", identity)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (identity == null || identity == false) this.fail(response)
|
|
72
|
+
|
|
73
|
+
return identity;
|
|
74
|
+
|
|
75
|
+
/* let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
76
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
BaseChyz.debug("JSON Web Token.",autHeader);
|
|
81
|
+
let identity = null;
|
|
82
|
+
let token = null;
|
|
83
|
+
|
|
84
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
85
|
+
if (!token) {
|
|
86
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
87
|
+
this.fail(response);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (token !== null) {
|
|
91
|
+
if (this.auth != null) {
|
|
92
|
+
identity = await this.auth(autHeader[1])
|
|
93
|
+
} else {
|
|
94
|
+
identity = await user.loginByAccessToken(autHeader[1], "JwtHttpBearerAuth")
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (identity == null) this.fail(response)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
return identity;*/
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @throws UnauthorizedHttpException
|
|
108
|
+
*/
|
|
109
|
+
public fail(response: Response): void {
|
|
110
|
+
// this.challenge(response)
|
|
111
|
+
this.handleFailure(response);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
}
|
package/filters/index.ts
ADDED
package/index.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
*
|
|
3
|
+
* Copyright (c) 2022.. Chy Bilgisayar Bilisim
|
|
4
|
+
* Author: Cihan Ozturk
|
|
5
|
+
* E-mail: cihan@chy.com.tr
|
|
6
|
+
* Github:https://github.com/cihan53/
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
declare global {
|
|
11
|
+
interface String {
|
|
12
|
+
tokenReplace(obj: any): string;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare namespace Express {
|
|
17
|
+
export interface Request {
|
|
18
|
+
identity?: string
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
import BaseChyz from "./BaseChyz";
|
|
23
|
+
import {RestClient} from "./base";
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
declare module "express-serve-static-core" {
|
|
27
|
+
interface Request {
|
|
28
|
+
identity: any;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
String.prototype.tokenReplace = function (obj) {
|
|
33
|
+
let retStr: any = this;
|
|
34
|
+
for (let x in obj) {
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
retStr = retStr.replace(new RegExp("\\:" + x, 'g'), obj[x]).toString();
|
|
37
|
+
}
|
|
38
|
+
return retStr;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
const Chyz = new BaseChyz();
|
|
46
|
+
const pjson = require('./package.json');
|
|
47
|
+
export const Chyz_Version = pjson.version;
|
|
48
|
+
export {Request, Response, NextFunction} from "./base/CRequest";
|
|
49
|
+
export {RouteDefinition} from "./model/RouteDefinition";
|
|
50
|
+
export {WebUser} from "./web/WebUser";
|
|
51
|
+
export {BaseChyz, RestClient}
|
|
52
|
+
export {
|
|
53
|
+
Logs,
|
|
54
|
+
ActionFilter,
|
|
55
|
+
BaseError,
|
|
56
|
+
Behavior,
|
|
57
|
+
Component,
|
|
58
|
+
Configurable,
|
|
59
|
+
CWebController,
|
|
60
|
+
DbConnection,
|
|
61
|
+
ForbiddenHttpException,
|
|
62
|
+
InvalidConfigException,
|
|
63
|
+
InvalidArgumentException,
|
|
64
|
+
NotFoundHttpException,
|
|
65
|
+
UnauthorizedHttpException,
|
|
66
|
+
DataErrorDbException,
|
|
67
|
+
ValidationHttpException,
|
|
68
|
+
Model,
|
|
69
|
+
ModelManager,
|
|
70
|
+
CEvents
|
|
71
|
+
} from "./base";
|
|
72
|
+
export * from "./filters";
|
|
73
|
+
export * from "./filters/auth";
|
|
74
|
+
export * from "./rbac/";
|
|
75
|
+
|
|
76
|
+
export * from "./decorator";
|
|
77
|
+
export * from "./requiments/Utils";
|
|
78
|
+
|
|
79
|
+
export default Chyz;
|
|
80
|
+
|
package/log/config/log4js.json
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
{
|
|
2
|
-
"appenders": {
|
|
3
|
-
"access": {
|
|
4
|
-
"type": "dateFile",
|
|
5
|
-
"filename": "log/access.log",
|
|
6
|
-
"pattern": "-yyyy-MM-dd",
|
|
7
|
-
"category": "http",
|
|
8
|
-
"layout": {
|
|
9
|
-
"type": "pattern",
|
|
10
|
-
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"app": {
|
|
14
|
-
"type": "file",
|
|
15
|
-
"filename": "log/app.log",
|
|
16
|
-
"maxLogSize": 10485760,
|
|
17
|
-
"numBackups": 3,
|
|
18
|
-
"layout": {
|
|
19
|
-
"type": "pattern",
|
|
20
|
-
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"errorFile": {
|
|
24
|
-
"type": "file",
|
|
25
|
-
"filename": "log/errors.log",
|
|
26
|
-
"layout": {
|
|
27
|
-
"type": "pattern",
|
|
28
|
-
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"errors": {
|
|
32
|
-
"type": "logLevelFilter",
|
|
33
|
-
"level": "ERROR",
|
|
34
|
-
"appender": "errorFile",
|
|
35
|
-
"layout": {
|
|
36
|
-
"type": "pattern",
|
|
37
|
-
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"console": {
|
|
41
|
-
"type": "stdout"
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
"categories": {
|
|
45
|
-
"default": {
|
|
46
|
-
"appenders": ["console","app" ,"errors" ],
|
|
47
|
-
"level": "INFO",
|
|
48
|
-
"enableCallStack": true
|
|
49
|
-
},
|
|
50
|
-
"http": {
|
|
51
|
-
"level": "INFO",
|
|
52
|
-
"appenders": [ "access" ]
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"appenders": {
|
|
3
|
+
"access": {
|
|
4
|
+
"type": "dateFile",
|
|
5
|
+
"filename": "log/access.log",
|
|
6
|
+
"pattern": "-yyyy-MM-dd",
|
|
7
|
+
"category": "http",
|
|
8
|
+
"layout": {
|
|
9
|
+
"type": "pattern",
|
|
10
|
+
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"app": {
|
|
14
|
+
"type": "file",
|
|
15
|
+
"filename": "log/app.log",
|
|
16
|
+
"maxLogSize": 10485760,
|
|
17
|
+
"numBackups": 3,
|
|
18
|
+
"layout": {
|
|
19
|
+
"type": "pattern",
|
|
20
|
+
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"errorFile": {
|
|
24
|
+
"type": "file",
|
|
25
|
+
"filename": "log/errors.log",
|
|
26
|
+
"layout": {
|
|
27
|
+
"type": "pattern",
|
|
28
|
+
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"errors": {
|
|
32
|
+
"type": "logLevelFilter",
|
|
33
|
+
"level": "ERROR",
|
|
34
|
+
"appender": "errorFile",
|
|
35
|
+
"layout": {
|
|
36
|
+
"type": "pattern",
|
|
37
|
+
"pattern": "[%d{yyyy-MM-ddThh.mm.ss.SSS}] [%p] %c [%l] [%f] - %m"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"console": {
|
|
41
|
+
"type": "stdout"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"categories": {
|
|
45
|
+
"default": {
|
|
46
|
+
"appenders": ["console","app" ,"errors" ],
|
|
47
|
+
"level": "INFO",
|
|
48
|
+
"enableCallStack": true
|
|
49
|
+
},
|
|
50
|
+
"http": {
|
|
51
|
+
"level": "INFO",
|
|
52
|
+
"appenders": [ "access" ]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface RouteDefinition {
|
|
9
|
+
//id
|
|
10
|
+
id:string;
|
|
11
|
+
// Path to our route
|
|
12
|
+
path: string;
|
|
13
|
+
// HTTP Request method (get, post, ...)
|
|
14
|
+
requestMethod: 'get' | 'post' | 'delete' | 'options' | 'put';
|
|
15
|
+
// Method name within our class responsible for this route
|
|
16
|
+
methodName: string;
|
|
17
|
+
}
|
|
18
|
+
|