Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add permission check #395

Merged
merged 10 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SCICAT_API_BASE_PATH=
SCICAT_API_ACCESS_TOKEN=
bolmsten marked this conversation as resolved.
Show resolved Hide resolved
164 changes: 164 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint": "eslint --fix ."
},
"dependencies": {
"@scicatproject/scicat-ts-fetch-test": "^2.0.0",
"@types/archiver": "^5.3.1",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.12",
Expand All @@ -27,6 +28,7 @@
"cookie-parser": "~1.4.6",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"ejs": "^3.1.8",
"express": "~4.19.2",
"express-fileupload": "^1.4.0",
Expand Down
1 change: 1 addition & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { router as indexRouter } from "./routes/index";
import { router as uploadRouter } from "./routes/upload";
import { logger } from "@user-office-software/duo-logger";
import { configureLogger } from "./common/configureLogger";
import 'dotenv/config'
bolmsten marked this conversation as resolved.
Show resolved Hide resolved

const app = express();
app.set("views", path.join(__dirname, "views"));
Expand Down
69 changes: 37 additions & 32 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import express from "express";
import { config } from "./common/config";
import jwtLib from "jsonwebtoken";
import * as fs from "fs";
import jwtLib from "jsonwebtoken";

import { logger } from "@user-office-software/duo-logger";
import { scicatDataSetAPI } from "./common/scicatAPI"

export const hasFileAccess = (
export const hasFileAccess = async (
req: express.Request,
directory: string,
fileNames: string[]
): Global.AuthResponse => {
const { jwtSecret, facility } = config;
fileNames: string[],
dataset: string
): Promise<Global.AuthResponse> => {

const { jwtSecret } = config;
const dataSetAPI = scicatDataSetAPI();

if (!jwtSecret) {
return {
hasAccess: false,
Expand Down Expand Up @@ -41,7 +47,10 @@ export const hasFileAccess = (
httpMethod: req.method,
directory,
fileNames,
dataset
};


if (!authRequest.directory) {
return {
hasAccess: false,
Expand Down Expand Up @@ -79,32 +88,28 @@ export const hasFileAccess = (
fileNames: [],
};
}
// Evaluate access rights based on institution specific logic
switch (facility) {
case "maxiv": {
return authMAXIV(authRequest);
}
default:
return {
hasAccess: true,
statusCode: 200,
directory: authRequest.directory,
fileNames: authRequest.fileNames,
};
}
};

const authMAXIV = (authRequest: Global.AuthRequest): Global.AuthResponse => {
const valid =
authRequest.jwt.groups.filter(
(group) => group.trim() && authRequest.directory.indexOf(group) > -1
).length > 0;

return {
hasAccess: valid,
statusCode: valid ? 200 : 403,
error: valid ? "" : "You do not have access to this resource",
directory: valid ? authRequest.directory : undefined,
fileNames: valid ? authRequest.fileNames : [],
};
};

const valid = await dataSetAPI.datasetsControllerFindById({pid: authRequest.dataset}).then(
(value) =>
{
if(value.isPublished || value.accessGroups.some(item => new Set(authRequest.jwt.groups).has(item)) || authRequest.jwt.groups.indexOf(value.ownerGroup) > -1){
return true
Junjiequan marked this conversation as resolved.
Show resolved Hide resolved
}else{
false
}
bolmsten marked this conversation as resolved.
Show resolved Hide resolved
}
).catch((e) => {

return false
});

return {
hasAccess: valid,
statusCode: valid ? 200 : 403,
error: valid ? "" : "You do not have access to this resource",
directory: valid ? authRequest.directory : undefined,
fileNames: valid ? authRequest.fileNames : [],
};
}
23 changes: 23 additions & 0 deletions src/common/scicatAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Configuration, DatasetsApi } from "@scicatproject/scicat-ts-fetch-test";

let datasetsApiInstance: DatasetsApi | null = null;

export function scicatDataSetAPI(): DatasetsApi {
if (!datasetsApiInstance) {
const basePath = process.env.SCICAT_API_BASE_PATH;
const accessToken = process.env.SCICAT_API_ACCESS_TOKEN;
bolmsten marked this conversation as resolved.
Show resolved Hide resolved

if (!basePath || !accessToken) {
throw new Error("SciCat API configuration is missing: Check SCICAT_API_BASE_PATH and SCICAT_API_ACCESS_TOKEN.");
}

const apiConfig = new Configuration({
basePath,
accessToken,
});

datasetsApiInstance = new DatasetsApi(apiConfig);
}

return datasetsApiInstance;
}
1 change: 1 addition & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ router.get("/", function (req, res) {
res.render("index", {
jwt: config.testData.jwt || "",
directory: config.testData.directory || "",
dataset: "",
file0: config.testData?.files[0] || "",
file1: config.testData?.files[1] || "",
file2: config.testData?.files[2] || "",
bolmsten marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading