chyz 1.0.12-rc.24 → 1.0.12-rc.28

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.
Files changed (49) hide show
  1. package/BaseChyz.ts +46 -9
  2. package/Examples/Controllers/KeyCloakController.ts +102 -0
  3. package/Examples/Models/KeycloakUser.ts +66 -0
  4. package/Examples/index.ts +48 -8
  5. package/Examples/keycloak.json +7 -0
  6. package/Examples/log/app.log +5423 -0
  7. package/Examples/log/errors.log +1139 -0
  8. package/Examples/package.json +46 -45
  9. package/Examples/yarn.lock +354 -7
  10. package/base/BaseError.ts +2 -2
  11. package/base/DataErrorDbException.ts +1 -1
  12. package/base/ForbiddenHttpException.ts +1 -1
  13. package/base/InvalidConfigException.ts +1 -1
  14. package/base/Model.ts +13 -2
  15. package/base/NotFoundHttpException.ts +1 -1
  16. package/base/UnauthorizedHttpException.ts +2 -1
  17. package/base/ValidationHttpException.ts +1 -1
  18. package/base/index.ts +1 -1
  19. package/dist/BaseChyz.js +32 -3
  20. package/dist/BaseChyz.js.map +1 -1
  21. package/dist/base/BaseError.js +2 -2
  22. package/dist/base/BaseError.js.map +1 -1
  23. package/dist/base/DataErrorDbException.js +1 -1
  24. package/dist/base/DataErrorDbException.js.map +1 -1
  25. package/dist/base/ForbiddenHttpException.js +1 -1
  26. package/dist/base/ForbiddenHttpException.js.map +1 -1
  27. package/dist/base/InvalidConfigException.js +1 -1
  28. package/dist/base/InvalidConfigException.js.map +1 -1
  29. package/dist/base/Model.js +13 -0
  30. package/dist/base/Model.js.map +1 -1
  31. package/dist/base/NotFoundHttpException.js +1 -1
  32. package/dist/base/NotFoundHttpException.js.map +1 -1
  33. package/dist/base/UnauthorizedHttpException.js +1 -1
  34. package/dist/base/UnauthorizedHttpException.js.map +1 -1
  35. package/dist/base/ValidationHttpException.js +1 -1
  36. package/dist/base/ValidationHttpException.js.map +1 -1
  37. package/dist/base/index.js.map +1 -1
  38. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  39. package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
  40. package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
  41. package/dist/index.js +40 -8
  42. package/dist/index.js.map +1 -1
  43. package/dist/web/User.js +8 -3
  44. package/dist/web/User.js.map +1 -1
  45. package/filters/auth/JwtHttpBearerAuth.ts +1 -3
  46. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  47. package/index.ts +22 -6
  48. package/package.json +2 -1
  49. package/web/User.ts +9 -5
package/BaseChyz.ts CHANGED
@@ -25,8 +25,7 @@ export default class BaseChyz {
25
25
  private _controllerpath: string = "Controllers"
26
26
  private static controllers: Array<Controller> = []
27
27
  public static components: any = {}
28
-
29
-
28
+ public static middlewares: any = {}
30
29
 
31
30
 
32
31
  get logConfig(): any {
@@ -64,7 +63,6 @@ export default class BaseChyz {
64
63
  writable: true
65
64
  })
66
65
 
67
-
68
66
  Object.defineProperty(BaseChyz.express.request, 'identity', {
69
67
  configurable: true,
70
68
  enumerable: true,
@@ -123,7 +121,6 @@ export default class BaseChyz {
123
121
  */
124
122
  this.config = config;
125
123
 
126
- this.init();
127
124
 
128
125
  let components = Utils.findKeyValue(config, "components")
129
126
  if (components) {
@@ -135,6 +132,21 @@ export default class BaseChyz {
135
132
  }
136
133
  }
137
134
 
135
+
136
+ let middlewares = Utils.findKeyValue(config, "middlewares")
137
+ if (middlewares) {
138
+ for (const middlewareKey in middlewares) {
139
+ let middleware1 = middlewares[middlewareKey];
140
+ BaseChyz.debug("Create middlewares ", middlewareKey)
141
+ BaseChyz.middlewares[middlewareKey] = middleware1;
142
+ // BaseChyz.middlewares[middlewareKey] = Utils.createObject(new middleware1.class, middleware1);
143
+ }
144
+ }
145
+
146
+
147
+
148
+ this.init();
149
+
138
150
  return this;
139
151
  }
140
152
 
@@ -143,7 +155,7 @@ export default class BaseChyz {
143
155
  return log4js;
144
156
  }
145
157
 
146
- public getLogger(){
158
+ public getLogger() {
147
159
  return this.logProvider().getLogger(this.constructor.name);
148
160
  }
149
161
 
@@ -154,12 +166,15 @@ export default class BaseChyz {
154
166
  public static trace(...args: any[]) {
155
167
  BaseChyz.logs().fatal(...arguments)
156
168
  }
169
+
157
170
  public static debug(...args: any[]) {
158
171
  BaseChyz.logs().debug(...arguments)
159
172
  }
173
+
160
174
  public static info(...args: any[]) {
161
175
  BaseChyz.logs().info(...arguments)
162
176
  }
177
+
163
178
  public static warn(...args: any[]) {
164
179
  BaseChyz.logs().warn(...arguments)
165
180
  }
@@ -173,8 +188,6 @@ export default class BaseChyz {
173
188
  }
174
189
 
175
190
 
176
-
177
-
178
191
  public static warning(...args: any[]) {
179
192
  BaseChyz.logs().warn(...arguments)
180
193
  }
@@ -211,6 +224,12 @@ export default class BaseChyz {
211
224
  return BaseChyz.components[key] ?? null
212
225
  }
213
226
 
227
+
228
+ public static getMiddlewares(key: any) {
229
+ return BaseChyz.middlewares[key] ?? null
230
+ }
231
+
232
+
214
233
  /**
215
234
  * load contoller
216
235
  */
@@ -280,8 +299,8 @@ export default class BaseChyz {
280
299
  BaseChyz.express.use(bodyParser.json())
281
300
  BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
282
301
  BaseChyz.express.use(methodOverride());
283
- BaseChyz.express.use(this.errorResponder)
284
- BaseChyz.express.use(this.errorHandler)
302
+ BaseChyz.express.use(methodOverride());
303
+
285
304
 
286
305
 
287
306
  // CORS
@@ -296,7 +315,20 @@ export default class BaseChyz {
296
315
  next();
297
316
  });
298
317
 
318
+ //Middlewares
319
+ for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
320
+ if (!Utils.isFunction(middleware1)) {
321
+ let keycloak = BaseChyz.middlewares[middleware1].keycloak;
322
+ BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
323
+ } else {
324
+ BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
325
+ }
326
+
327
+ }
328
+
299
329
 
330
+ BaseChyz.express.use(this.errorResponder)
331
+ BaseChyz.express.use(this.errorHandler)
300
332
  }
301
333
 
302
334
 
@@ -316,3 +348,8 @@ export default class BaseChyz {
316
348
  }
317
349
 
318
350
 
351
+ process.on('uncaughtException', err => {
352
+ BaseChyz.error('There was an uncaught error', err)
353
+ process.exit(1) //mandatory (as per the Node.js docs)
354
+ })
355
+
@@ -0,0 +1,102 @@
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 {Controller} from "../../base/Controller";
9
+ import BaseChyz from "../../BaseChyz";
10
+ // @ts-ignore
11
+ import {Request, Response} from "express";
12
+ import {get} from "../../decorator/get";
13
+ import {post} from "../../decorator/post";
14
+ import {controller} from "../../decorator/controller";
15
+ import {AccessControl} from "../../filters/AccessControl";
16
+ import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
+ import {Order} from "../Models/Order";
18
+ import {Customer} from "../Models/Customer";
19
+ import {ValidationHttpException} from "../../base/ValidationHttpException";
20
+ import {ValidationError} from "sequelize";
21
+ import {ForbiddenHttpException} from "../../base";
22
+ import {KeyCloakHttpBearerAuth} from "../../filters/auth/KeyCloakHttpBearerAuth";
23
+
24
+ @controller("/oauth2.0")
25
+ class ApiController extends Controller {
26
+
27
+ public behaviors(): any[] {
28
+
29
+ return [{
30
+ 'authenticator': {
31
+ "class": KeyCloakHttpBearerAuth,
32
+ // "auth": this.myCheck
33
+ }
34
+ }]
35
+ }
36
+
37
+ @get("/")
38
+ Index(req: Request, res: Response) {
39
+
40
+ BaseChyz.logs().info("Site Controller Burası", this.id)
41
+ return res.json({message: "index sayfası"})
42
+ }
43
+
44
+ @post("orderCreate")
45
+ async Login(req: Request, res: Response) {
46
+ let data = req.body;
47
+ data.Customer.status = "true";
48
+ data.Customer["2fa"] = "true";
49
+
50
+ //Customer Model Create
51
+ let customer: Customer = new Customer();
52
+ //Order Model Create
53
+ let order: Order = new Order();
54
+
55
+
56
+ let transaction
57
+ try {
58
+ // get transaction
59
+ transaction = await BaseChyz.getComponent("db").transaction();
60
+
61
+
62
+ customer.load(data, "Customer");//load customer data
63
+ let cus: any = await customer.save({}, {transaction});
64
+
65
+ if (!cus) {
66
+ throw new ValidationHttpException(customer.errors);
67
+ }
68
+
69
+ data.Order.customer_id = cus.id;
70
+ // data.Order.total = 0;
71
+ // data.Order.status = true;
72
+ order.load(data, "Order");
73
+ let res1 = await order.save({}, {transaction});
74
+ if (!res1) {
75
+ throw new ValidationHttpException(order.errors);
76
+ }
77
+
78
+ // commit
79
+ await transaction.commit();
80
+
81
+ } catch (e) {
82
+ if (transaction) {
83
+ await transaction.rollback();
84
+ BaseChyz.warn("Rollback transaction")
85
+ }
86
+
87
+ if (e instanceof ValidationHttpException)
88
+ throw new ValidationHttpException(e.message)
89
+ else
90
+ throw new ForbiddenHttpException(e.message)
91
+ }
92
+ return res.send("Post Controller")
93
+ }
94
+
95
+
96
+ error(req: Request, res: Response) {
97
+ BaseChyz.logs().info("Error Sayfası")
98
+ return res.send("Post Controller")
99
+ }
100
+ }
101
+
102
+ module.exports = ApiController
@@ -0,0 +1,66 @@
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 {IdentityInterface} from "../../web/IdentityInterface";
9
+ import {DataTypes, Model} from "../../base";
10
+ import BaseChyz from "../../BaseChyz";
11
+
12
+ const JsonWebToken = require("jsonwebtoken");
13
+
14
+ export class KeycloakUser implements IdentityInterface {
15
+
16
+ [x: string]: any;
17
+
18
+
19
+ findIdentity(id: number) {
20
+ throw new Error("Method not implemented.");
21
+ }
22
+
23
+ getId(): number {
24
+ throw new Error("Method not implemented.");
25
+ }
26
+
27
+ getAuthKey(): string {
28
+ throw new Error("Method not implemented.");
29
+ }
30
+
31
+ validateAuthKey(authKey: string): boolean {
32
+ throw new Error("Method not implemented.");
33
+ }
34
+
35
+ public attributes() {
36
+ return {
37
+ // Model attributes are defined here
38
+ username: {
39
+ type: DataTypes.STRING,
40
+ allowNull: false
41
+ },
42
+ password: {
43
+ type: DataTypes.STRING,
44
+ allowNull: false
45
+ },
46
+ user_role: {
47
+ type: DataTypes.STRING,
48
+ allowNull: false
49
+ },
50
+ salt_text: {
51
+ type: DataTypes.STRING
52
+ // allowNull defaults to true
53
+ }
54
+ }
55
+ }
56
+
57
+ async findIdentityByAccessToken(token, type) {
58
+ console.log(token,type)
59
+ console.log(this)
60
+ console.log(BaseChyz)
61
+ return null;
62
+ // return keycloak.protect('realm:user');
63
+ }
64
+ }
65
+
66
+
package/Examples/index.ts CHANGED
@@ -5,24 +5,56 @@
5
5
  * Github:https://github.com/cihan53/
6
6
  */
7
7
 
8
+
8
9
  require('dotenv-flow').config();
10
+
11
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
12
+
9
13
  import Chyz from "../Chyz";
10
14
  import {User} from "../web/User";
11
- import {User as Identity } from "./Models/User";
15
+ import {KeycloakUser as Identity} from "./Models/KeycloakUser";
12
16
  // @ts-ignore
13
17
  import {DbConnection} from "../base/DbConnection";
14
18
 
19
+ var Keycloak = require('keycloak-connect');
20
+ var session = require('express-session');
21
+ var memoryStore = new session.MemoryStore();
22
+
23
+
24
+ var keycloakConfig = {
25
+ "realm": "CameraBox",
26
+ "auth-server-url": "https://keycloak.hubbox.io:8080/auth/",
27
+ //"ssl-required": "external",
28
+ "resource": "izanami",
29
+ "verify-token-audience": true,
30
+ "bearerOnly": true,
31
+ "confidential-port":0,
32
+ "policy-enforcer":{},
33
+ "credentials": {
34
+ "secret": "0b476571-28ab-49b1-9968-90fce6294d5a"
35
+ }
36
+
37
+ };
38
+
39
+
40
+
41
+ Keycloak.prototype.accessDenied = function () {
42
+ return null;
43
+ }
44
+
45
+ var keycloak = new Keycloak({scope: 'offline_access'}, keycloakConfig);
46
+
15
47
 
16
48
  let config = {
17
49
  port: 3000,
18
- controllerpath:"C:\\PROJELER\\github\\Chy-Nodejs-Framework\\Examples\\Controllers",
50
+ controllerpath: "C:\\PROJELER\\github\\Chy-Nodejs-Framework\\Examples\\Controllers",
19
51
  components: {
20
- db:{
21
- class:DbConnection,
52
+ db: {
53
+ class: DbConnection,
22
54
  database: process.env.DBDATABASE,
23
- username:process.env.DBUSER,
24
- password:process.env.DBPASS,
25
- options:{
55
+ username: process.env.DBUSER,
56
+ password: process.env.DBPASS,
57
+ options: {
26
58
  host: process.env.DBHOST,
27
59
  dialect: 'postgres', /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
28
60
  // disable logging; default: console.log
@@ -31,7 +63,15 @@ let config = {
31
63
  },
32
64
  user: {
33
65
  'class': User,
34
- 'identityClass':Identity
66
+ 'identityClass': Identity
67
+ }
68
+ },
69
+ middlewares: {
70
+ keycloak: {
71
+ keycloak: keycloak,
72
+ config: {
73
+ logout: '/logout'
74
+ }
35
75
  }
36
76
  }
37
77
  }
@@ -0,0 +1,7 @@
1
+ {
2
+ "realm": "quickstart",
3
+ "bearer-only": true,
4
+ "auth-server-url": "http://localhost:8180/auth",
5
+ "ssl-required": "external",
6
+ "resource": "service-nodejs"
7
+ }