@igea/oac_backend 1.0.7 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igea/oac_backend",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Backend service for the OAC project",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -18,7 +18,9 @@
18
18
  },
19
19
  "homepage": "https://github.com/chpigea/oac_backend#readme",
20
20
  "dependencies": {
21
+ "@igea/oac_jwt_helpers": "1.0.6",
21
22
  "axios": "1.10.0",
23
+ "cookie-parser": "1.4.7",
22
24
  "express": "5.1.0",
23
25
  "get-port": "7.1.0",
24
26
  "knex": "3.1.0",
@@ -3,5 +3,14 @@
3
3
  "url_register": "http://localhost:4001/register",
4
4
  "port_range": {
5
5
  "min": 5000, "max": 6000
6
- }
6
+ },
7
+ "database": {
8
+ "host": "127.0.0.1",
9
+ "port": "5432",
10
+ "user": "postgres",
11
+ "password": "postgres",
12
+ "database": "oac",
13
+ "schema": "public"
14
+ },
15
+ "jwt_secret": "@igea#"
7
16
  }
@@ -3,5 +3,14 @@
3
3
  "url_register": "http://localhost:4001/register",
4
4
  "port_range": {
5
5
  "min": 5000, "max": 6000
6
- }
6
+ },
7
+ "database": {
8
+ "host": "127.0.0.1",
9
+ "port": "5432",
10
+ "user": "postgres",
11
+ "password": "postgres",
12
+ "database": "oac",
13
+ "schema": "public"
14
+ },
15
+ "jwt_secret": "@igea#"
7
16
  }
@@ -0,0 +1,73 @@
1
+ const express = require('express');
2
+ const router = express.Router();
3
+ const Users = require('../models/users');
4
+
5
+ module.exports = function(jwtLib){
6
+ /**
7
+ curl -X POST \
8
+ -H "Content-Type: application/json" \
9
+ -d '{"type": "login","user":"igea","password":"@igea#"}' \
10
+ http://localhost:4000/backend/auth/authenticate
11
+ */
12
+
13
+ const manageUser = function(res, user){
14
+ let token = jwtLib.createToken(user)
15
+ res.cookie('token', token, {
16
+ httpOnly: true,
17
+ secure: false,
18
+ sameSite: 'lax'
19
+ })
20
+ res.json({
21
+ success: true,
22
+ data: user,
23
+ message: null
24
+ });
25
+ }
26
+
27
+ router.post('/authenticate', (req, res) => {
28
+ console.log("here authenticate")
29
+ let body = req.body
30
+ let type = body.type
31
+
32
+ if(type.toLowerCase()=="login"){
33
+ let user = body.user
34
+ let password = body.password
35
+ Users.fromAuthentication(user, password)
36
+ .then(response => {
37
+ response["is_anonymous"] = false
38
+ manageUser(res, response)
39
+ }).catch(err => {
40
+ res.json({
41
+ success: false,
42
+ data: null,
43
+ message: `${ err }`
44
+ });
45
+ })
46
+ }else{
47
+ manageUser(res, {
48
+ id: -1,
49
+ name: 'anonymous',
50
+ surname: 'anonymous',
51
+ username: 'anonymous',
52
+ email: 'anonymous@igea-soluzioni.it',
53
+ mobile: null,
54
+ is_admin: false,
55
+ is_active: true,
56
+ is_anonymous: true
57
+ })
58
+ }
59
+ });
60
+
61
+ router.get('/echo', (req, res) => {
62
+ console.log("here ECHO")
63
+ res.json({
64
+ success: true,
65
+ data: "echo",
66
+ message: null
67
+ });
68
+ });
69
+
70
+ return router
71
+
72
+ }
73
+
package/src/index.js CHANGED
@@ -2,10 +2,23 @@ const config = require('./config.js');
2
2
  const express = require("express");
3
3
  const axios = require("axios");
4
4
  const getPort = require('get-port');
5
+ const cookieParser = require('cookie-parser');
5
6
  const serviceName = "backend"
7
+ const jwtLibFactory = require('@igea/oac_jwt_helpers')
8
+ const jwtLib = jwtLibFactory({
9
+ secret: process.env.JWT_SECRET || config.jwt_secret,
10
+ excludePaths: [
11
+ `/${serviceName}/auth/authenticate`,
12
+ `/${serviceName}/auth/echo`,
13
+ `/${serviceName}/health`
14
+ ],
15
+ signOptions: { expiresIn: '1h' }
16
+ });
6
17
 
7
18
  const app = express();
19
+ app.use(cookieParser());
8
20
  app.use(express.json());
21
+ app.use(jwtLib.middleware);
9
22
 
10
23
  let newPort = null
11
24
 
@@ -27,9 +40,13 @@ getPort.default({
27
40
  port: getPort.portNumbers(config.port_range.min, config.port_range.max) }
28
41
  ).then((port)=>{
29
42
  newPort = port
43
+
30
44
  const healthRouter = require('./controllers/health.js')
31
45
  app.use(`/${serviceName}/health`, healthRouter)
32
46
 
47
+ const authRouter = require('./controllers/auth.js')(jwtLib)
48
+ app.use(`/${serviceName}/auth`, authRouter)
49
+
33
50
  app.listen(port, async () => {
34
51
  console.log(`${serviceName} listening on port ${port}`)
35
52
  await register()
@@ -0,0 +1,10 @@
1
+ const knex = require('knex');
2
+ const config = require('../config')
3
+
4
+ const db = knex({
5
+ client: 'pg',
6
+ connection: config.database,
7
+ pool: { min: 2, max: 10 }
8
+ });
9
+
10
+ module.exports = { db, schema: config.schema || 'public' };
@@ -0,0 +1,39 @@
1
+ const config = require('../config')
2
+ const {db, schema } = require('./db')
3
+ const table = `${schema}.users`
4
+
5
+ class Users {
6
+
7
+ static fromAuthentication(user, password){
8
+ console.log(user, password)
9
+ return new Promise(async (resolve, reject) => {
10
+ try{
11
+ let userFound = await db(table)
12
+ .where(function() {
13
+ this.whereRaw(
14
+ '(username = ? AND password = md5(?))',
15
+ [user, password]
16
+ );
17
+ })
18
+ .orWhere(function() {
19
+ this.whereRaw(
20
+ '(email = ? AND password = md5(?))',
21
+ [user, password]
22
+ );
23
+ })
24
+ .first();
25
+ if(userFound && userFound["is_active"]) {
26
+ delete userFound["password"]
27
+ resolve(userFound)
28
+ }else{
29
+ reject(new Error("Invalid credentials"))
30
+ }
31
+ }catch(e){
32
+ reject(e)
33
+ }
34
+ });
35
+ }
36
+
37
+ }
38
+
39
+ module.exports = Users