Skip to content
Snippets Groups Projects
Commit 5ff80cbf authored by Embruch, Gerd's avatar Embruch, Gerd
Browse files

moved REST-Client variables into shared store; finished embeddings/status

parent f329df3b
No related branches found
No related tags found
No related merge requests found
...@@ -125,7 +125,7 @@ Accept: application/json ...@@ -125,7 +125,7 @@ Accept: application/json
Content-Type: application/json Content-Type: application/json
{ {
"input": "John has five apples. He eats one of them himself. How many apples does John have now?", "input": "Was muss getan werden um mit den Druckern aus der GUI zu drucken?",
"model": "llama3" "model": "llama3"
} }
......
...@@ -7,37 +7,17 @@ ...@@ -7,37 +7,17 @@
################# #################
# SET VARS # SET VARS
################# #################
@host = https://localhost:8080 # in VSCode press ctrl+alt+e to switch between admin and user
# vars are stored in /.vscode/settings.json
#user @token = {{login.response.body.accessToken}}
@email = embruch@zbh.uni-hamburg.de
@password = 8z44tcZa!
# admin
@email_admin = ge@reigncode.de
@password_admin = A9z44tcZa!
# JWT
@token = {{login.response.body.token}}
################# #################
# HANDLE LOGIN # HANDLE LOGIN
################# #################
### admin login
# @name adminLogin
POST {{host}}/users/adminlogin
Accept: application/json
Content-Type: application/json
{
"password": "{{password_admin}}",
"email": "{{email_admin}}"
}
### login ### login
# @name login # @name login
POST {{host}}/users/login POST {{host}}/auth/login
Accept: application/json Accept: application/json
Content-Type: application/json Content-Type: application/json
......
...@@ -10,23 +10,10 @@ ...@@ -10,23 +10,10 @@
################# #################
# SET VARS # SET VARS
################# #################
@host = https://localhost:8080 # in VSCode press ctrl+alt+e to switch between admin and user
# user # vars are stored in /.vscode/settings.json
@name = Carl Benz @token = {{login.response.body.accessToken}}
@username = cbenz @token = {{refreshJWT.response.body.accessToken}}
@password = 8z55tcZa!
@email = embruch@zbh.uni-hamburg.de
@newEmail = gerd.embruch@uni-hamburg.de
# admin
@email_admin = ge@reigncode.de
@password_admin = A9z44tcZa!
# token
@confirmToken = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY2YTE1NGQ2MDVhMDBkNzFlODAwYTVhNSIsImVtYWlsIjoiZW1icnVjaEB6YmgudW5pLWhhbWJ1cmcuZGUiLCJpYXQiOjE3MjE4ODkxMjUsImV4cCI6MTcyMTg5MjcyNX0.6sus-EudvmU_h5hlz0I74XyjJf7_xe9_VztUqgV1Zpw
@token = {{login.response.body.token}}
@token = {{adminLogin.response.body.token}}
@token = {{refreshJWT.response.body.token}}
################# #################
# HANDLE SIGNUP # HANDLE SIGNUP
...@@ -81,17 +68,6 @@ Content-Type: application/json ...@@ -81,17 +68,6 @@ Content-Type: application/json
"email": "{{email}}" "email": "{{email}}"
} }
### admin login
# @name adminLogin
POST {{host}}/auth/login
Accept: application/json
Content-Type: application/json
{
"password": "{{password_admin}}",
"email": "{{email_admin}}"
}
### refresh jwt ### refresh jwt
# @name refreshJWT # @name refreshJWT
GET {{host}}/auth GET {{host}}/auth
......
...@@ -182,7 +182,28 @@ export const passwordReset = async (req, res, next) => { ...@@ -182,7 +182,28 @@ export const passwordReset = async (req, res, next) => {
} catch (error) { } catch (error) {
next(error); next(error);
} }
} };
/**
* VERIFY ACCESS RIGHTS
* check if user is alllowed to access route
*/
export const gateKeeper = async (req, res, next) => {
// admins are allowed to access anything
if (global.currentUserRole >= 4) return next();
// FEATURE
// - check for custom field role (which has to be created via settings before)
// - create a access config, which exports an array of allowed roles for each route
// - fetch array of called route an compare
// const allowed = [];
// if (allowed.includes(pb.authStore.model.role)) {
// return next();
// }
// deny access for others
return res.status(403).json({ message: 'Access Forbidden' });
};
...@@ -99,9 +99,9 @@ export const getStatus = async (req, res) => { ...@@ -99,9 +99,9 @@ export const getStatus = async (req, res) => {
} }
// get collection count // get collection count
const itemCount = await collection.count(); const itemCount = await collection.count();
const items = await collection.get(); // const items = await collection.get();
// return status // return status
return res.json({ vectorDBrunning, collection, itemCount, items }); return res.json({ vectorDBrunning, collection, itemCount });
}; };
/** ******************************************************* /** *******************************************************
......
import { Router } from "express"; import { Router } from "express";
import { getStatus, getModel, getModels, deleteModel, installModel, checkRequestedModel, getChat, getChats } from "../controllers/AI.js"; import { getStatus, getModel, getModels, deleteModel, installModel, checkRequestedModel, getChat, getChats } from "../controllers/AI.js";
import { chat } from "../utils/handleAI.js"; import { chat } from "../utils/handleAI.js";
import { pbVerifyAccessToken, gateKeeper } from "../utils/pocketbase/handlePocketBase.js"; import { pbVerifyAccessToken } from "../utils/pocketbase/handlePocketBase.js";
import { chatSchema, deleteModelSchema, getModelSchema, getModelsSchema, installModelSchema } from "../validationSchemes/AI.js"; import { chatSchema, deleteModelSchema, getModelSchema, getModelsSchema, installModelSchema } from "../validationSchemes/AI.js";
import { validate } from "../utils/handleValidations.js"; import { validate } from "../utils/handleValidations.js";
import { gateKeeper } from "../controllers/Auth.js";
const router = Router(); const router = Router();
......
import { Router } from "express"; import { Router } from "express";
import { removeVectorDb, getStatus, createEmbeddings, updateEmbeddings } from "../controllers/Embeddings.js"; import { removeVectorDb, getStatus, createEmbeddings, updateEmbeddings } from "../controllers/Embeddings.js";
import { gateKeeper, pbVerifyAccessToken } from "../utils/pocketbase/handlePocketBase.js"; import { pbVerifyAccessToken } from "../utils/pocketbase/handlePocketBase.js";
import { verifyAccessToken } from "../utils/handleTokens.js";
import { gateKeeper } from "../controllers/Auth.js";
const router = Router(); const router = Router();
// remove Vector DB // remove Vector DB
router.delete('/', pbVerifyAccessToken, gateKeeper, removeVectorDb); // router.delete('/', verifyAccessToken, gateKeeper, removeVectorDb);
// Vector DB status // Vector DB status
router.get('/', getStatus); router.get('/', verifyAccessToken, gateKeeper, getStatus);
// createEmbeddings // createEmbeddings
router.put('/', pbVerifyAccessToken, gateKeeper, createEmbeddings); // router.put('/', pbVerifyAccessToken, gateKeeper, createEmbeddings);
// update embeddings // update embeddings
// TODO test update & delete https://python.langchain.com/v0.1/docs/integrations/vectorstores/chroma/#update-and-delete // TODO test update & delete https://python.langchain.com/v0.1/docs/integrations/vectorstores/chroma/#update-and-delete
// https://js.langchain.com/v0.1/docs/integrations/vectorstores/chroma/#usage-delete-docs // https://js.langchain.com/v0.1/docs/integrations/vectorstores/chroma/#usage-delete-docs
router.patch('/', pbVerifyAccessToken, gateKeeper, updateEmbeddings); // router.patch('/', pbVerifyAccessToken, gateKeeper, updateEmbeddings);
......
...@@ -19,7 +19,7 @@ export const dbConnection = async () => { ...@@ -19,7 +19,7 @@ export const dbConnection = async () => {
console.log(chalk.green('DB connected successfully')); console.log(chalk.green('DB connected successfully'));
}); });
dbConnection.on("error", (err) => { dbConnection.on("error", (error) => {
console.error(chalk.red(error)); console.error(chalk.red(error));
}); });
return dbConnection; return dbConnection;
......
...@@ -45,6 +45,7 @@ export const verifyVerificationToken = async (req, res, next) => { ...@@ -45,6 +45,7 @@ export const verifyVerificationToken = async (req, res, next) => {
* @return {token} * @return {token}
*/ */
export const createAccessToken = (payload) => { export const createAccessToken = (payload) => {
console.log('create JWT payload', payload);
return jwt.sign(payload, process.env.JWT_SECRET_KEY, { expiresIn: process.env.JWT_TTL }); return jwt.sign(payload, process.env.JWT_SECRET_KEY, { expiresIn: process.env.JWT_TTL });
}; };
...@@ -139,7 +140,7 @@ export const verifyAccessToken = async (req, res, next) => { ...@@ -139,7 +140,7 @@ export const verifyAccessToken = async (req, res, next) => {
if (error) return res.status(403).json({ message: 'Access token is no longer valid. Access denied.' }); if (error) return res.status(403).json({ message: 'Access token is no longer valid. Access denied.' });
// if valid: remember current user id & role and go on // if valid: remember current user id & role and go on
global.currentUserId = payload.id; global.currentUserId = payload.id;
global.currentUserRole = payload.role;; global.currentUserRole = payload.role;
next(); next();
}); });
}; };
......
...@@ -225,33 +225,6 @@ export const pbVerifyAccessToken = async (req, res, next) => { ...@@ -225,33 +225,6 @@ export const pbVerifyAccessToken = async (req, res, next) => {
next(); next();
}; };
/**
* VERIFY ACCESS RIGHTS
* check if user is alllowed to access route
* if allowed = [] only admins are allowed
*
* This setup is called "configurable middleware"
* https://expressjs.com/en/guide/writing-middleware.html
*/
export const gateKeeper = async (req, res, next) => {
// admins are allowed to access anything
if (pb.authStore.model.isAdmin) return next();
// FEATURE
// - check for custom field role (which has to be created via settings before)
// - create a access config, which exports an array of allowed roles for each route
// - fetch array of called route an compare
// const allowed = [];
// if (allowed.includes(pb.authStore.model.role)) {
// return next();
// }
// deny access for others
return res.status(403).json({ message: 'Access Forbidden' });
};
/** /**
* CREATE RECORD IN PB * CREATE RECORD IN PB
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment