chyz 2.0.1-rc.36 → 2.0.1-rc.37
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/dist/BaseChyz.d.ts +74 -0
- package/dist/BaseChyz.d.ts.map +1 -0
- package/dist/BaseChyz.js +586 -0
- package/dist/base/ActionFilter.d.ts +32 -0
- package/dist/base/ActionFilter.d.ts.map +1 -0
- package/dist/base/ActionFilter.js +90 -0
- package/dist/base/BadRequestHttpException.d.ts +6 -0
- package/dist/base/BadRequestHttpException.d.ts.map +1 -0
- package/dist/base/BadRequestHttpException.js +17 -0
- package/dist/base/BaseError.d.ts +44 -0
- package/dist/base/BaseError.d.ts.map +1 -0
- package/{base/BaseError.ts → dist/base/BaseError.js} +18 -26
- package/dist/base/Behavior.d.ts +4 -0
- package/dist/base/Behavior.d.ts.map +1 -0
- package/dist/base/Behavior.js +7 -0
- package/dist/base/CBaseObject.d.ts +5 -0
- package/dist/base/CBaseObject.d.ts.map +1 -0
- package/dist/base/CBaseObject.js +16 -0
- package/dist/base/CEvents.d.ts +13 -0
- package/dist/base/CEvents.d.ts.map +1 -0
- package/dist/base/CEvents.js +24 -0
- package/dist/base/CRequest.d.ts +2 -0
- package/dist/base/CRequest.d.ts.map +1 -0
- package/{base/CRequest.ts → dist/base/CRequest.js} +2 -6
- package/dist/base/CWebController.d.ts +44 -0
- package/dist/base/CWebController.d.ts.map +1 -0
- package/dist/base/CWebController.js +81 -0
- package/dist/base/Component.d.ts +21 -0
- package/dist/base/Component.d.ts.map +1 -0
- package/dist/base/Component.js +57 -0
- package/dist/base/Configurable.d.ts +3 -0
- package/dist/base/Configurable.d.ts.map +1 -0
- package/{requiments/Glob.ts → dist/base/Configurable.js} +2 -3
- package/dist/base/DataErrorDbException.d.ts +6 -0
- package/dist/base/DataErrorDbException.d.ts.map +1 -0
- package/dist/base/DataErrorDbException.js +17 -0
- package/dist/base/DbConnection.d.ts +13 -0
- package/dist/base/DbConnection.d.ts.map +1 -0
- package/dist/base/DbConnection.js +53 -0
- package/dist/base/ForbiddenHttpException.d.ts +5 -0
- package/dist/base/ForbiddenHttpException.d.ts.map +1 -0
- package/dist/base/ForbiddenHttpException.js +16 -0
- package/dist/base/InvalidArgumentException.d.ts +6 -0
- package/dist/base/InvalidArgumentException.d.ts.map +1 -0
- package/dist/base/InvalidArgumentException.js +17 -0
- package/dist/base/InvalidConfigException.d.ts +6 -0
- package/dist/base/InvalidConfigException.d.ts.map +1 -0
- package/dist/base/InvalidConfigException.js +17 -0
- package/dist/base/Logs.d.ts +22 -0
- package/dist/base/Logs.d.ts.map +1 -0
- package/{base/Logs.ts → dist/base/Logs.js} +24 -41
- package/dist/base/Model.d.ts +302 -0
- package/dist/base/Model.d.ts.map +1 -0
- package/{base/Model.ts → dist/base/Model.js} +158 -225
- package/dist/base/ModelManager.d.ts +2 -0
- package/dist/base/ModelManager.d.ts.map +1 -0
- package/{base/ModelManager.ts → dist/base/ModelManager.js} +7 -7
- package/dist/base/NotFoundHttpException.d.ts +6 -0
- package/dist/base/NotFoundHttpException.d.ts.map +1 -0
- package/dist/base/NotFoundHttpException.js +17 -0
- package/dist/base/RestClient.d.ts +7 -0
- package/dist/base/RestClient.d.ts.map +1 -0
- package/dist/base/RestClient.js +25 -0
- package/dist/base/UnauthorizedHttpException.d.ts +6 -0
- package/dist/base/UnauthorizedHttpException.d.ts.map +1 -0
- package/dist/base/UnauthorizedHttpException.js +17 -0
- package/dist/base/ValidationHttpException.d.ts +6 -0
- package/dist/base/ValidationHttpException.d.ts.map +1 -0
- package/dist/base/ValidationHttpException.js +17 -0
- package/dist/base/db/Exception.d.ts +7 -0
- package/dist/base/db/Exception.d.ts.map +1 -0
- package/dist/base/db/Exception.js +15 -0
- package/dist/base/index.d.ts +20 -0
- package/dist/base/index.d.ts.map +1 -0
- package/dist/base/index.js +41 -0
- package/dist/decorator/Middleware.d.ts +4 -0
- package/dist/decorator/Middleware.d.ts.map +1 -0
- package/dist/decorator/Middleware.js +10 -0
- package/dist/decorator/controller.d.ts +3 -0
- package/dist/decorator/controller.d.ts.map +1 -0
- package/{decorator/controller.ts → dist/decorator/controller.js} +9 -7
- package/dist/decorator/delete.d.ts +3 -0
- package/dist/decorator/delete.d.ts.map +1 -0
- package/{decorator/delete.ts → dist/decorator/delete.js} +9 -9
- package/dist/decorator/enums/ControllerDecoratorParams.d.ts +6 -0
- package/dist/decorator/enums/ControllerDecoratorParams.d.ts.map +1 -0
- package/dist/decorator/enums/ControllerDecoratorParams.js +9 -0
- package/dist/decorator/get.d.ts +3 -0
- package/dist/decorator/get.d.ts.map +1 -0
- package/{decorator/get.ts → dist/decorator/get.js} +10 -11
- package/dist/decorator/index.d.ts +6 -0
- package/dist/decorator/index.d.ts.map +1 -0
- package/dist/decorator/index.js +13 -0
- package/dist/decorator/post.d.ts +3 -0
- package/dist/decorator/post.d.ts.map +1 -0
- package/{decorator/post.ts → dist/decorator/post.js} +10 -10
- package/dist/decorator/put.d.ts +3 -0
- package/dist/decorator/put.d.ts.map +1 -0
- package/{decorator/put.ts → dist/decorator/put.js} +9 -9
- package/dist/filters/AccessControl.d.ts +12 -0
- package/dist/filters/AccessControl.d.ts.map +1 -0
- package/dist/filters/AccessControl.js +83 -0
- package/dist/filters/AccessRule.d.ts +83 -0
- package/dist/filters/AccessRule.d.ts.map +1 -0
- package/dist/filters/AccessRule.js +136 -0
- package/{filters/auth/AuthInterface.ts → dist/filters/auth/AuthInterface.d.ts} +7 -15
- package/dist/filters/auth/AuthInterface.d.ts.map +1 -0
- package/dist/filters/auth/AuthInterface.js +2 -0
- package/dist/filters/auth/AuthMethod.d.ts +38 -0
- package/dist/filters/auth/AuthMethod.d.ts.map +1 -0
- package/dist/filters/auth/AuthMethod.js +82 -0
- package/dist/filters/auth/HttpBasicAuth.d.ts +24 -0
- package/dist/filters/auth/HttpBasicAuth.d.ts.map +1 -0
- package/dist/filters/auth/HttpBasicAuth.js +79 -0
- package/dist/filters/auth/HttpBearerAuth.d.ts +18 -0
- package/dist/filters/auth/HttpBearerAuth.d.ts.map +1 -0
- package/dist/filters/auth/HttpBearerAuth.js +33 -0
- package/dist/filters/auth/HttpHeaderAuth.d.ts +19 -0
- package/dist/filters/auth/HttpHeaderAuth.d.ts.map +1 -0
- package/dist/filters/auth/HttpHeaderAuth.js +64 -0
- package/dist/filters/auth/JwtHttpBearerAuth.d.ts +21 -0
- package/dist/filters/auth/JwtHttpBearerAuth.d.ts.map +1 -0
- package/dist/filters/auth/JwtHttpBearerAuth.js +88 -0
- package/dist/filters/auth/KeyCloakHttpBearerAuth.d.ts +23 -0
- package/dist/filters/auth/KeyCloakHttpBearerAuth.d.ts.map +1 -0
- package/dist/filters/auth/KeyCloakHttpBearerAuth.js +116 -0
- package/dist/filters/auth/index.d.ts +5 -0
- package/dist/filters/auth/index.d.ts.map +1 -0
- package/dist/filters/auth/index.js +11 -0
- package/dist/filters/index.d.ts +3 -0
- package/dist/filters/index.d.ts.map +1 -0
- package/dist/filters/index.js +7 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/log/log/config/log4js.json +55 -0
- package/dist/model/RouteDefinition.d.ts +7 -0
- package/dist/model/RouteDefinition.d.ts.map +1 -0
- package/{base/Configurable.ts → dist/model/RouteDefinition.js} +2 -4
- package/dist/package.json +63 -0
- package/dist/rbac/AuthAssignment.d.ts +19 -0
- package/dist/rbac/AuthAssignment.d.ts.map +1 -0
- package/dist/rbac/AuthAssignment.js +44 -0
- package/dist/rbac/AuthItem.d.ts +27 -0
- package/dist/rbac/AuthItem.d.ts.map +1 -0
- package/{rbac/AuthItem.ts → dist/rbac/AuthItem.js} +16 -22
- package/dist/rbac/AuthItemChild.d.ts +19 -0
- package/dist/rbac/AuthItemChild.d.ts.map +1 -0
- package/{rbac/AuthItemChild.ts → dist/rbac/AuthItemChild.js} +14 -21
- package/dist/rbac/AuthManager.d.ts +112 -0
- package/dist/rbac/AuthManager.d.ts.map +1 -0
- package/dist/rbac/AuthManager.js +356 -0
- package/dist/rbac/index.d.ts +5 -0
- package/dist/rbac/index.d.ts.map +1 -0
- package/dist/rbac/index.js +28 -0
- package/dist/requiments/Glob.d.ts +3 -0
- package/dist/requiments/Glob.d.ts.map +1 -0
- package/dist/requiments/Glob.js +10 -0
- package/dist/requiments/ReflectUtil.d.ts +1 -0
- package/dist/requiments/ReflectUtil.d.ts.map +1 -0
- package/{requiments/ReflectUtil.ts → dist/requiments/ReflectUtil.js} +4 -6
- package/dist/requiments/Utils.d.ts +2 -0
- package/dist/requiments/Utils.d.ts.map +1 -0
- package/{requiments/Utils.ts → dist/requiments/Utils.js} +34 -40
- package/dist/validators/BooleanValidator.d.ts +1 -0
- package/dist/validators/BooleanValidator.d.ts.map +1 -0
- package/dist/validators/BooleanValidator.js +1 -0
- package/dist/validators/CompareValidator.d.ts +1 -0
- package/dist/validators/CompareValidator.d.ts.map +1 -0
- package/dist/validators/CompareValidator.js +1 -0
- package/dist/validators/DateValidator.d.ts +1 -0
- package/dist/validators/DateValidator.d.ts.map +1 -0
- package/dist/validators/DateValidator.js +1 -0
- package/dist/validators/EmailValidator.d.ts +1 -0
- package/dist/validators/EmailValidator.d.ts.map +1 -0
- package/dist/validators/EmailValidator.js +1 -0
- package/dist/validators/Validator.d.ts +18 -0
- package/dist/validators/Validator.d.ts.map +1 -0
- package/dist/validators/Validator.js +27 -0
- package/{web/IdentityInterface.ts → dist/web/IdentityInterface.d.ts} +2 -15
- package/dist/web/IdentityInterface.d.ts.map +1 -0
- package/dist/web/IdentityInterface.js +8 -0
- package/dist/web/WebUser.d.ts +72 -0
- package/dist/web/WebUser.d.ts.map +1 -0
- package/dist/web/WebUser.js +165 -0
- package/package.json +16 -56
- package/BaseChyz.ts +0 -623
- package/base/ActionFilter.ts +0 -88
- package/base/BadRequestHttpException.ts +0 -14
- package/base/Behavior.ts +0 -6
- package/base/CBaseObject.ts +0 -17
- package/base/CEvents.ts +0 -24
- package/base/CWebController.ts +0 -90
- package/base/Component.ts +0 -66
- package/base/DataErrorDbException.ts +0 -16
- package/base/DbConnection.ts +0 -55
- package/base/ForbiddenHttpException.ts +0 -15
- package/base/InvalidArgumentException.ts +0 -16
- package/base/InvalidConfigException.ts +0 -16
- package/base/NotFoundHttpException.ts +0 -14
- package/base/RestClient.ts +0 -26
- package/base/UnauthorizedHttpException.ts +0 -17
- package/base/ValidationHttpException.ts +0 -14
- package/base/db/Exception.ts +0 -14
- package/base/index.ts +0 -19
- package/decorator/Middleware.ts +0 -9
- package/decorator/enums/ControllerDecoratorParams.ts +0 -5
- package/decorator/index.ts +0 -5
- package/filters/AccessControl.ts +0 -74
- package/filters/AccessRule.ts +0 -182
- package/filters/auth/AuthMethod.ts +0 -100
- package/filters/auth/HttpBasicAuth.ts +0 -79
- package/filters/auth/HttpBearerAuth.ts +0 -34
- package/filters/auth/HttpHeaderAuth.ts +0 -61
- package/filters/auth/JwtHttpBearerAuth.ts +0 -83
- package/filters/auth/KeyCloakHttpBearerAuth.ts +0 -114
- package/filters/auth/index.ts +0 -4
- package/filters/index.ts +0 -2
- package/index.ts +0 -80
- package/model/RouteDefinition.ts +0 -18
- package/rbac/AuthAssignment.ts +0 -50
- package/rbac/AuthManager.ts +0 -399
- package/rbac/index.ts +0 -12
- 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 +0 -27
- package/web/WebUser.ts +0 -187
- /package/{README.md → dist/README.md} +0 -0
- /package/{log → dist/log}/config/log4js.json +0 -0
package/filters/AccessRule.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
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 {WebUser} from "../web/WebUser";
|
|
8
|
-
import {Component} from "../base";
|
|
9
|
-
import {InvalidConfigException} from "../base";
|
|
10
|
-
import {Request} from "express";
|
|
11
|
-
import {Utils} from "../requiments/Utils";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export class AccessRule extends Component {
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @var allow whether this is an 'allow' rule or 'deny' rule.
|
|
18
|
-
*/
|
|
19
|
-
public allow: any;
|
|
20
|
-
/**
|
|
21
|
-
* @var array list of action IDs that this rule applies to. The comparison is case-sensitive.
|
|
22
|
-
* If not set or empty, it means this rule applies to all actions.
|
|
23
|
-
*/
|
|
24
|
-
public actions: any;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @var array list of the controller IDs that this rule applies to.
|
|
28
|
-
*/
|
|
29
|
-
public controllers: any;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* - `?`: matches a guest user (not authenticated yet)
|
|
33
|
-
* - `@`: matches an authenticated user
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
public roles: any;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @var array list of RBAC (Role-Based Access Control) permissions that this rules applies to.
|
|
40
|
-
*/
|
|
41
|
-
public permissions: any;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @var array|Closure parameters to pass to the [[User::can()]] function for evaluating
|
|
45
|
-
* user permissions in [[$roles]].
|
|
46
|
-
*
|
|
47
|
-
* If this is an array, it will be passed directly to [[User::can()]]. For example for passing an
|
|
48
|
-
* ID from the current request, you may use the following:
|
|
49
|
-
*
|
|
50
|
-
* ```php
|
|
51
|
-
* ['postId' => Yii::$app->request->get('id')]
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* You may also specify a closure that returns an array. This can be used to
|
|
55
|
-
* evaluate the array values only if they are needed, for example when a model needs to be
|
|
56
|
-
* loaded like in the following code:
|
|
57
|
-
*
|
|
58
|
-
* ```php
|
|
59
|
-
* 'rules' => [
|
|
60
|
-
* [
|
|
61
|
-
* 'allow' => true,
|
|
62
|
-
* 'actions' => ['update'],
|
|
63
|
-
* 'roles' => ['updatePost'],
|
|
64
|
-
* 'roleParams' => function($rule) {
|
|
65
|
-
* return ['post' => Post::findOne(Yii::$app->request->get('id'))];
|
|
66
|
-
* },
|
|
67
|
-
* ],
|
|
68
|
-
* ],
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* A reference to the [[AccessRule]] instance will be passed to the closure as the first parameter.
|
|
72
|
-
*
|
|
73
|
-
* @see roles
|
|
74
|
-
* @since 2.0.12
|
|
75
|
-
*/
|
|
76
|
-
public roleParams: any = [];
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @var array list of user IP addresses that this rule applies to. An IP address
|
|
81
|
-
* can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix.
|
|
82
|
-
* For example, '192.168.*' matches all IP addresses in the segment '192.168.'.
|
|
83
|
-
* It may also contain a pattern/mask like '172.16.0.0/12' which would match all IPs from the
|
|
84
|
-
* 20-bit private network block in RFC1918.
|
|
85
|
-
* If not set or empty, it means this rule applies to all IP addresses.
|
|
86
|
-
*/
|
|
87
|
-
public ips: any;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
public async allows(action: any, user: WebUser, request: Request) {
|
|
91
|
-
if (
|
|
92
|
-
this.matchAction(action)
|
|
93
|
-
&& await this.matchRole(user)
|
|
94
|
-
) {
|
|
95
|
-
return this.allow
|
|
96
|
-
}
|
|
97
|
-
// if (this.matchAction($action)
|
|
98
|
-
// && this.matchRole($user)
|
|
99
|
-
// && this.matchIP($request->getUserIP())
|
|
100
|
-
// && this.matchVerb($request->getMethod())
|
|
101
|
-
// && this.matchController($action->controller)
|
|
102
|
-
// && this.matchCustom($action)
|
|
103
|
-
// ) {
|
|
104
|
-
// return $this->allow ? true : false;
|
|
105
|
-
// }
|
|
106
|
-
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @param action $action the action
|
|
112
|
-
* @return bool whether the rule applies to the action
|
|
113
|
-
*/
|
|
114
|
-
protected matchAction(action: any) {
|
|
115
|
-
return Utils.isEmpty(this.actions) || this.actions.includes(action.id);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @param controller $controller the controller
|
|
120
|
-
* @return bool whether the rule applies to the controller
|
|
121
|
-
*/
|
|
122
|
-
protected matchController(controller: any) {
|
|
123
|
-
// if (empty($this->controllers)) {
|
|
124
|
-
// return true;
|
|
125
|
-
// }
|
|
126
|
-
//
|
|
127
|
-
// $id = $controller->getUniqueId();
|
|
128
|
-
// foreach ($this->controllers as $pattern) {
|
|
129
|
-
// if (StringHelper::matchWildcard($pattern, $id)) {
|
|
130
|
-
// return true;
|
|
131
|
-
// }
|
|
132
|
-
// }
|
|
133
|
-
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
protected async matchRole(user: WebUser) {
|
|
138
|
-
let items = Utils.isEmpty(this.roles) ? [] : this.roles;
|
|
139
|
-
|
|
140
|
-
if (!Utils.isEmpty(this.permissions)) {
|
|
141
|
-
items = Utils.merge(items, this.permissions);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (Utils.isEmpty(items)) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (!user) {
|
|
150
|
-
throw new InvalidConfigException('The user application component must be available to specify roles in AccessRule.');
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// @ts-ignore
|
|
154
|
-
let roleParams: any = [];
|
|
155
|
-
for (const itemsKey in items) {
|
|
156
|
-
let item = items[itemsKey];
|
|
157
|
-
if (item === '?') {
|
|
158
|
-
if (user.getIsGuest()) {
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
} else if (item === '@') {
|
|
162
|
-
if (!user.getIsGuest()) {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
} else {
|
|
166
|
-
//role-params
|
|
167
|
-
if (!Utils.isEmpty(this.roleParams)) {
|
|
168
|
-
roleParams = !Utils.isArray(this.roleParams) ? this.roleParams.apply(this) : this.roleParams;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (await user.can(item, this.roleParams)) {
|
|
172
|
-
return true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
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";
|
|
9
|
-
import {AuthInterface} from "./AuthInterface";
|
|
10
|
-
import {UnauthorizedHttpException} from "../../base";
|
|
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
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
* @param action
|
|
37
|
-
* @param request
|
|
38
|
-
* @param response
|
|
39
|
-
*/
|
|
40
|
-
public async beforeAction(action: any, request: Request, response: Response) {
|
|
41
|
-
let identity =await this.authenticate(
|
|
42
|
-
this.user ?? new WebUser(),
|
|
43
|
-
request,
|
|
44
|
-
response
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
request.identity = identity;
|
|
49
|
-
|
|
50
|
-
if (identity !== null) {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.challenge(response);
|
|
55
|
-
this.handleFailure(response);
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
* @param user
|
|
62
|
-
* @param request
|
|
63
|
-
* @param response
|
|
64
|
-
*/
|
|
65
|
-
authenticate(user: WebUser, request: Request, response: Response) {
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
challenge(response: Response): Response {
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// @ts-ignore
|
|
75
|
-
handleFailure(response: Response): Response {
|
|
76
|
-
throw new UnauthorizedHttpException('Your request was made with invalid credentials.');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
getHeaderByKey(headers: any, findKey: any) {
|
|
80
|
-
let key = Object.keys(headers).find(key => key.toLowerCase() === findKey.toLowerCase())
|
|
81
|
-
if (key) {
|
|
82
|
-
return headers[key];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
patternCheck(headerText: any, pattern: RegExp) {
|
|
89
|
-
if (pattern) {
|
|
90
|
-
let matches = headerText.match(pattern)
|
|
91
|
-
if (matches && matches.length > 0) {
|
|
92
|
-
return matches;
|
|
93
|
-
} else {
|
|
94
|
-
return null
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return null
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
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/auth/index.ts
DELETED
package/filters/index.ts
DELETED