chyz 1.0.13-rc.4 → 1.0.13-rc.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.
- package/BaseChyz.ts +384 -0
- package/Chyz.ts +11 -0
- package/Examples/Controllers/ApiController.ts +140 -0
- package/Examples/Controllers/KeyCloakController.ts +100 -0
- package/Examples/Controllers/PublicController.ts +42 -0
- package/Examples/Controllers/SiteController.ts +139 -0
- package/Examples/Models/Categories.ts +48 -0
- package/Examples/Models/Customer.ts +115 -0
- package/Examples/Models/KeycloakUser.ts +66 -0
- package/Examples/Models/Order.ts +64 -0
- package/Examples/Models/OrderItem.ts +27 -0
- package/Examples/Models/ProductModels.ts +49 -0
- package/Examples/Models/ProductToCategories.ts +46 -0
- package/Examples/Models/Products.ts +50 -0
- package/Examples/Models/Stocks.ts +60 -0
- package/Examples/Models/User.ts +100 -0
- package/Examples/Models/UserPermission.ts +37 -0
- package/Examples/Models/index.ts +19 -0
- package/Examples/index-keycloack.ts +78 -0
- package/Examples/index.ts +40 -0
- package/Examples/keycloak.json +7 -0
- package/Examples/log/access.log +0 -0
- package/Examples/log/app.log +6057 -0
- package/Examples/log/errors.log +1526 -0
- package/Examples/package.json +46 -0
- package/Examples/tsconfig.json +21 -0
- package/base/ActionFilter.ts +71 -0
- package/base/BaseError.ts +26 -0
- package/base/BaseObject.ts +19 -0
- package/base/Behavior.ts +6 -0
- package/base/Component.ts +66 -0
- package/base/Configurable.ts +10 -0
- package/base/Controller.ts +84 -0
- package/base/DataErrorDbException.ts +16 -0
- package/base/DbConnection.ts +51 -0
- package/base/ForbiddenHttpException.ts +15 -0
- package/base/InvalidConfigException.ts +16 -0
- package/base/Model.ts +358 -0
- package/base/ModelManager.ts +14 -0
- package/base/NotFoundHttpException.ts +14 -0
- package/base/RestClient.ts +28 -0
- package/base/UnauthorizedHttpException.ts +17 -0
- package/base/ValidationHttpException.ts +14 -0
- package/base/db/Exception.ts +14 -0
- package/base/index.ts +17 -0
- package/decorator/Middleware.ts +9 -0
- package/decorator/controller.ts +22 -0
- package/decorator/enums/ControllerDecoratorParams.ts +5 -0
- package/decorator/get.ts +37 -0
- package/decorator/index.ts +3 -0
- package/decorator/post.ts +36 -0
- package/{BaseChyz.js → dist/BaseChyz.js} +29 -4
- package/dist/BaseChyz.js.map +1 -0
- package/{Chyz.js → dist/Chyz.js} +0 -0
- package/{Chyz.js.map → dist/Chyz.js.map} +0 -0
- package/{base → dist/base}/ActionFilter.js +0 -0
- package/{base → dist/base}/ActionFilter.js.map +0 -0
- package/{base → dist/base}/BaseError.js +0 -0
- package/{base → dist/base}/BaseError.js.map +0 -0
- package/{base → dist/base}/BaseObject.js +0 -0
- package/{base → dist/base}/BaseObject.js.map +0 -0
- package/{base → dist/base}/Behavior.js +0 -0
- package/{base → dist/base}/Behavior.js.map +0 -0
- package/{base → dist/base}/Component.js +0 -0
- package/{base → dist/base}/Component.js.map +0 -0
- package/{base → dist/base}/Configurable.js +0 -0
- package/{base → dist/base}/Configurable.js.map +0 -0
- package/{base → dist/base}/Controller.js +0 -0
- package/{base → dist/base}/Controller.js.map +0 -0
- package/{base → dist/base}/DataErrorDbException.js +0 -0
- package/{base → dist/base}/DataErrorDbException.js.map +0 -0
- package/{base → dist/base}/DbConnection.js +0 -0
- package/{base → dist/base}/DbConnection.js.map +0 -0
- package/{base → dist/base}/ForbiddenHttpException.js +0 -0
- package/{base → dist/base}/ForbiddenHttpException.js.map +0 -0
- package/{base → dist/base}/InvalidConfigException.js +0 -0
- package/{base → dist/base}/InvalidConfigException.js.map +0 -0
- package/{base → dist/base}/Model.js +32 -28
- package/dist/base/Model.js.map +1 -0
- package/dist/base/ModelManager.js +17 -0
- package/dist/base/ModelManager.js.map +1 -0
- package/{base → dist/base}/NotFoundHttpException.js +0 -0
- package/{base → dist/base}/NotFoundHttpException.js.map +0 -0
- package/{base → dist/base}/RestClient.js +0 -0
- package/{base → dist/base}/RestClient.js.map +0 -0
- package/{base → dist/base}/UnauthorizedHttpException.js +0 -0
- package/{base → dist/base}/UnauthorizedHttpException.js.map +0 -0
- package/{base → dist/base}/ValidationHttpException.js +0 -0
- package/{base → dist/base}/ValidationHttpException.js.map +0 -0
- package/{base → dist/base}/db/Exception.js +0 -0
- package/{base → dist/base}/db/Exception.js.map +0 -0
- package/{base → dist/base}/index.js +1 -0
- package/{base → dist/base}/index.js.map +1 -1
- package/{decorator → dist/decorator}/Middleware.js +0 -0
- package/{decorator → dist/decorator}/Middleware.js.map +0 -0
- package/{decorator → dist/decorator}/controller.js +0 -0
- package/{decorator → dist/decorator}/controller.js.map +0 -0
- package/{decorator → dist/decorator}/enums/ControllerDecoratorParams.js +0 -0
- package/{decorator → dist/decorator}/enums/ControllerDecoratorParams.js.map +0 -0
- package/{decorator → dist/decorator}/get.js +0 -0
- package/{decorator → dist/decorator}/get.js.map +0 -0
- package/{decorator → dist/decorator}/index.js +0 -0
- package/{decorator → dist/decorator}/index.js.map +0 -0
- package/{decorator → dist/decorator}/post.js +0 -0
- package/{decorator → dist/decorator}/post.js.map +0 -0
- package/{filters → dist/filters}/AccessControl.js +0 -0
- package/{filters → dist/filters}/AccessControl.js.map +0 -0
- package/{filters → dist/filters}/AccessRule.js +0 -0
- package/{filters → dist/filters}/AccessRule.js.map +0 -0
- package/{filters → dist/filters}/auth/AuthInterface.js +0 -0
- package/{filters → dist/filters}/auth/AuthInterface.js.map +0 -0
- package/{filters → dist/filters}/auth/AuthMethod.js +0 -0
- package/{filters → dist/filters}/auth/AuthMethod.js.map +0 -0
- package/{filters → dist/filters}/auth/HttpBasicAuth.js +0 -0
- package/{filters → dist/filters}/auth/HttpBasicAuth.js.map +0 -0
- package/{filters → dist/filters}/auth/HttpBearerAuth.js +0 -0
- package/{filters → dist/filters}/auth/HttpBearerAuth.js.map +0 -0
- package/{filters → dist/filters}/auth/HttpHeaderAuth.js +0 -0
- package/{filters → dist/filters}/auth/HttpHeaderAuth.js.map +0 -0
- package/{filters → dist/filters}/auth/JwtHttpBearerAuth.js +0 -0
- package/{filters → dist/filters}/auth/JwtHttpBearerAuth.js.map +0 -0
- package/{filters → dist/filters}/auth/KeyCloakHttpBearerAuth.js +0 -0
- package/{filters → dist/filters}/auth/KeyCloakHttpBearerAuth.js.map +0 -0
- package/{filters → dist/filters}/auth/index.js +0 -0
- package/{filters → dist/filters}/auth/index.js.map +0 -0
- package/{filters → dist/filters}/index.js +0 -0
- package/{filters → dist/filters}/index.js.map +0 -0
- package/{index.js → dist/index.js} +0 -0
- package/{index.js.map → dist/index.js.map} +0 -0
- package/dist/log/config/log4js.json +55 -0
- package/{model → dist/model}/RouteDefinition.js +0 -0
- package/{model → dist/model}/RouteDefinition.js.map +0 -0
- package/{requiments → dist/requiments}/Glob.js +0 -0
- package/{requiments → dist/requiments}/Glob.js.map +0 -0
- package/{requiments → dist/requiments}/ReflectUtil.js +0 -0
- package/{requiments → dist/requiments}/ReflectUtil.js.map +0 -0
- package/{requiments → dist/requiments}/Utils.js +0 -0
- package/{requiments → dist/requiments}/Utils.js.map +0 -0
- package/{validators → dist/validators}/BooleanValidator.js +0 -0
- package/{validators → dist/validators}/BooleanValidator.js.map +0 -0
- package/{validators → dist/validators}/CompareValidator.js +0 -0
- package/{validators → dist/validators}/CompareValidator.js.map +0 -0
- package/{validators → dist/validators}/DateValidator.js +0 -0
- package/{validators → dist/validators}/DateValidator.js.map +0 -0
- package/{validators → dist/validators}/EmailValidator.js +0 -0
- package/{validators → dist/validators}/EmailValidator.js.map +0 -0
- package/{validators → dist/validators}/Validator.js +0 -0
- package/{validators → dist/validators}/Validator.js.map +0 -0
- package/{web → dist/web}/IdentityInterface.js +0 -0
- package/{web → dist/web}/IdentityInterface.js.map +0 -0
- package/{web → dist/web}/WebUser.js +0 -0
- package/{web → dist/web}/WebUser.js.map +0 -0
- package/filters/AccessControl.ts +64 -0
- package/filters/AccessRule.ts +137 -0
- package/filters/auth/AuthInterface.ts +34 -0
- package/filters/auth/AuthMethod.ts +88 -0
- package/filters/auth/HttpBasicAuth.ts +7 -0
- package/filters/auth/HttpBearerAuth.ts +31 -0
- package/filters/auth/HttpHeaderAuth.ts +53 -0
- package/filters/auth/JwtHttpBearerAuth.ts +80 -0
- package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
- package/filters/auth/index.ts +3 -0
- package/filters/index.ts +2 -0
- package/index.ts +14 -0
- package/model/RouteDefinition.ts +18 -0
- package/package-lock.json +5259 -0
- package/package.json +3 -3
- package/requiments/Glob.ts +9 -0
- package/requiments/ReflectUtil.ts +26 -0
- package/requiments/Utils.ts +57 -0
- package/tsconfig.json +18 -0
- 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.ts +62 -0
- package/web/WebUser.ts +100 -0
- package/BaseChyz.js.map +0 -1
- package/base/Model.js.map +0 -1
|
@@ -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
|
+
import {Request, Response} from "express";
|
|
8
|
+
|
|
9
|
+
export interface AuthInterface{
|
|
10
|
+
/**
|
|
11
|
+
* Authenticates the current user.
|
|
12
|
+
* @param User $user
|
|
13
|
+
* @param Request $request
|
|
14
|
+
* @param Response $response
|
|
15
|
+
* @return IdentityInterface the authenticated user identity. If authentication information is not provided, null will be returned.
|
|
16
|
+
* @throws UnauthorizedHttpException if authentication information is provided but is invalid.
|
|
17
|
+
*/
|
|
18
|
+
authenticate(user:any, request:Request, response:Response):any;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Generates challenges upon authentication failure.
|
|
22
|
+
* For example, some appropriate HTTP headers may be generated.
|
|
23
|
+
* @param Response $response
|
|
24
|
+
*/
|
|
25
|
+
challenge(response:Response):Response;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Handles authentication failure.
|
|
29
|
+
* The implementation should normally throw UnauthorizedHttpException to indicate authentication failure.
|
|
30
|
+
* @param Response $response
|
|
31
|
+
* @throws UnauthorizedHttpException
|
|
32
|
+
*/
|
|
33
|
+
handleFailure(response:Response):Response;
|
|
34
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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 {ActionFilter} from "../../base/ActionFilter";
|
|
9
|
+
import {AuthInterface} from "./AuthInterface";
|
|
10
|
+
import {UnauthorizedHttpException} from "../../base/UnauthorizedHttpException";
|
|
11
|
+
import {WebUser} from "../../web/WebUser";
|
|
12
|
+
import {Request, Response} from "express";
|
|
13
|
+
|
|
14
|
+
export abstract class AuthMethod extends ActionFilter implements AuthInterface {
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @var User the user object representing the user authentication status. If not set, the `user` application component will be used.
|
|
18
|
+
*/
|
|
19
|
+
public user: WebUser | undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @var Request the current request. If not set, the `request` application component will be used.
|
|
23
|
+
*/
|
|
24
|
+
public request: Request | undefined;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @var Response the response to be sent. If not set, the `response` application component will be used.
|
|
28
|
+
*/
|
|
29
|
+
public response: Response | undefined;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
public optional = [];
|
|
33
|
+
|
|
34
|
+
public async beforeAction(action: any, request: Request, response: Response) {
|
|
35
|
+
let identity = await this.authenticate(
|
|
36
|
+
this.user ?? new WebUser(),
|
|
37
|
+
request,
|
|
38
|
+
response
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
request.identity = identity;
|
|
44
|
+
|
|
45
|
+
if (identity !== null) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.challenge(response);
|
|
50
|
+
this.handleFailure(response);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
authenticate(user: WebUser, request: Request, response: Response) {
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
challenge(response: Response): void {
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
handleFailure(response: Response) {
|
|
64
|
+
throw new UnauthorizedHttpException('Your request was made with invalid credentials.');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getHeaderByKey(headers: any, findKey: any) {
|
|
68
|
+
let key = Object.keys(headers).find(key => key.toLowerCase() === findKey.toLowerCase())
|
|
69
|
+
if (key) {
|
|
70
|
+
return headers[key];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return null
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
patternCheck(headerText:any, pattern:RegExp) {
|
|
77
|
+
if (pattern) {
|
|
78
|
+
let matches = headerText.match(pattern)
|
|
79
|
+
if (matches && matches.length > 0) {
|
|
80
|
+
return matches;
|
|
81
|
+
} else {
|
|
82
|
+
return null
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 {Request, 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) {
|
|
29
|
+
response.set('WWW-Authenticate', `Bearer realm="${this.realm}"`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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 null;
|
|
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 null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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 {UnauthorizedHttpException} from "../../base/UnauthorizedHttpException";
|
|
11
|
+
import {Response,Request} from "express";
|
|
12
|
+
import {WebUser} from "../../web/WebUser";
|
|
13
|
+
|
|
14
|
+
const JsonWebToken = require("jsonwebtoken");
|
|
15
|
+
|
|
16
|
+
export class JwtHttpBearerAuth extends HttpBearerAuth {
|
|
17
|
+
/**
|
|
18
|
+
* @var string|array<string, mixed>|Jwt application component ID of the JWT handler, configuration array, or JWT handler object
|
|
19
|
+
* itself. By default it's assumes that component of ID "jwt" has been configured.
|
|
20
|
+
*/
|
|
21
|
+
public jwt = 'jwt'
|
|
22
|
+
public auth: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.user = BaseChyz.getComponent("user") ?? null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
public async authenticate(user:WebUser, request:Request, response:Response) // BC signature
|
|
40
|
+
{
|
|
41
|
+
|
|
42
|
+
let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
43
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
BaseChyz.debug("JSON Web Token.",autHeader);
|
|
48
|
+
let identity = null;
|
|
49
|
+
let token = null;
|
|
50
|
+
|
|
51
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
52
|
+
if (!token) {
|
|
53
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
54
|
+
this.fail(response);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (token !== null) {
|
|
58
|
+
if (this.auth != null) {
|
|
59
|
+
identity = await this.auth(autHeader[1])
|
|
60
|
+
} else {
|
|
61
|
+
identity = await user.loginByAccessToken(autHeader[1], "JwtHttpBearerAuth")
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (identity == null) this.fail(response)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
return identity;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @throws UnauthorizedHttpException
|
|
74
|
+
*/
|
|
75
|
+
public fail(response:Response): void {
|
|
76
|
+
this.challenge(response)
|
|
77
|
+
this.handleFailure(response);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
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 {UnauthorizedHttpException} from "../../base/UnauthorizedHttpException";
|
|
11
|
+
import {Response, Request} from "express";
|
|
12
|
+
import {WebUser} from "../../web/WebUser";
|
|
13
|
+
|
|
14
|
+
const JsonWebToken = require("jsonwebtoken");
|
|
15
|
+
|
|
16
|
+
export class KeyCloakHttpBearerAuth extends HttpBearerAuth {
|
|
17
|
+
/**
|
|
18
|
+
* @var string|array<string, mixed>|Jwt application component ID of the JWT handler, configuration array, or JWT handler object
|
|
19
|
+
* itself. By default it's assumes that component of ID "jwt" has been configured.
|
|
20
|
+
*/
|
|
21
|
+
public jwt = 'jwt'
|
|
22
|
+
public auth: any = null;
|
|
23
|
+
public keycloak: any = null;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @throws InvalidConfigException
|
|
28
|
+
*/
|
|
29
|
+
public init(): void {
|
|
30
|
+
super.init();
|
|
31
|
+
|
|
32
|
+
if (!this.pattern) {
|
|
33
|
+
throw new InvalidConfigException('You must provide pattern to use to extract the HTTP authentication value!');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.keycloak = BaseChyz.getMiddlewares("keycloak").keycloak ?? null;
|
|
37
|
+
this.user = BaseChyz.getComponent("user") ?? null;
|
|
38
|
+
this.auth = this.KeyCloakCheck;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async KeyCloakCheck(token: string, request: Request, response: Response,) {
|
|
44
|
+
if (this.keycloak == null) return false;
|
|
45
|
+
// return await this.keycloak.protect('realm:user')(request, response, () => true /*next*/)
|
|
46
|
+
return await this.keycloak.protect()(request, response, () => true /*next*/);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
public async authenticate(user: WebUser, request: Request, response: Response) // BC signature
|
|
51
|
+
{
|
|
52
|
+
|
|
53
|
+
let identity = null;
|
|
54
|
+
let token = null;
|
|
55
|
+
|
|
56
|
+
let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
57
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
62
|
+
if (!token) {
|
|
63
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
64
|
+
this.fail(response);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (token !== null) {
|
|
68
|
+
identity = await this.KeyCloakCheck(autHeader[1], request, response)
|
|
69
|
+
BaseChyz.debug("KeyCloakCheck Result:", identity)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (identity == null || identity == false) this.fail(response)
|
|
73
|
+
|
|
74
|
+
return identity;
|
|
75
|
+
|
|
76
|
+
/* let autHeader = this.getHeaderByKey(request.headers, this.header)
|
|
77
|
+
if (autHeader == null || (autHeader = this.patternCheck(autHeader, this.pattern)) == null) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
BaseChyz.debug("JSON Web Token.",autHeader);
|
|
82
|
+
let identity = null;
|
|
83
|
+
let token = null;
|
|
84
|
+
|
|
85
|
+
token = JsonWebToken.decode(autHeader[1], {complete: true})
|
|
86
|
+
if (!token) {
|
|
87
|
+
BaseChyz.warning("Your request was made with invalid or expired JSON Web Token.");
|
|
88
|
+
this.fail(response);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (token !== null) {
|
|
92
|
+
if (this.auth != null) {
|
|
93
|
+
identity = await this.auth(autHeader[1])
|
|
94
|
+
} else {
|
|
95
|
+
identity = await user.loginByAccessToken(autHeader[1], "JwtHttpBearerAuth")
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (identity == null) this.fail(response)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
return identity;*/
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @throws UnauthorizedHttpException
|
|
109
|
+
*/
|
|
110
|
+
public fail(response: Response): void {
|
|
111
|
+
// this.challenge(response)
|
|
112
|
+
// this.handleFailure(response);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
package/filters/index.ts
ADDED
package/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {Request, Response, NextFunction} from "express";
|
|
2
|
+
export * from "./base";
|
|
3
|
+
export * from "./filters";
|
|
4
|
+
export * from "./filters/auth";
|
|
5
|
+
export * from "./decorator";
|
|
6
|
+
export * from "./requiments/Utils";
|
|
7
|
+
export {RouteDefinition} from "./model/RouteDefinition";
|
|
8
|
+
export {WebUser} from "./web/WebUser";
|
|
9
|
+
|
|
10
|
+
import BaseChyz from "./BaseChyz";
|
|
11
|
+
export {BaseChyz}
|
|
12
|
+
|
|
13
|
+
const Chyz = new BaseChyz();
|
|
14
|
+
export default Chyz;
|
|
@@ -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
|
+
|