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: support for auth with token #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
78 changes: 53 additions & 25 deletions middleware/authServiceMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,68 @@ const axios = require('axios');

exports.authMiddleware = async (req, res, next) => {
try {
// Extract the session cookie from the incoming request
const sessionCookie = req.headers.cookie;
const authHeader = req.headers['authorization'];

if (!sessionCookie) {
return res
.status(401)
.json({
if (authHeader && authHeader.startsWith('Bearer ')) {
// Bearer token is present
const token = authHeader.split(' ')[1];

if (!token) {
return res.status(401).json({
authenticated: false,
message: 'No session cookie found'
message: 'Invalid Bearer token format'
});
}

// Make a request to the Auth service to verify the session
const authResponse = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/check`,
{
headers: {
Cookie: sessionCookie // Forward the session cookie to the Auth service
},
withCredentials: true // Include credentials in the request
}
);

// Handle the Auth service response
if (authResponse.data.authenticated) {
req.user = authResponse.data.user; // Attach the user data to the request object
next(); // Continue to the next middleware or route handler
const authResponse = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/check`,
{
headers: {
Authorization: `Bearer ${token}`
},
withCredentials: true
}
);

if (authResponse.data.authenticated) {
req.user = authResponse.data.user;
return next();
} else {
return res.status(401).json({
authenticated: false,
message: 'User not authenticated'
});
}
} else {
return res
.status(401)
.json({
// Bearer token not present, check for session cookie
const sessionCookie = req.headers.cookie;

if (!sessionCookie) {
return res.status(401).json({
authenticated: false,
message: 'No session cookie found'
});
}

const authResponse = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/check`,
{
headers: {
Cookie: sessionCookie
},
withCredentials: true
}
);

if (authResponse.data.authenticated) {
req.user = authResponse.data.user;
next();
} else {
return res.status(401).json({
authenticated: false,
message: 'User not authenticated'
});
}
}
} catch (error) {
console.error('Error during authentication:', error);
Expand Down
63 changes: 44 additions & 19 deletions services/kMiningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ const FormData = require('form-data');
const fs = require('fs');
const datasetService = require('./datasetService.js');

exports.defineProcessingPipelineId = async (req) => {
exports.defineProcessingPipelineId = async req => {
const kmining_json_pipeline_id = req.user.config.find(
(item) => item.option === 'kmining_json_pipeline_id'
item => item.option === 'kmining_json_pipeline_id'
)?.value;
const kmining_pdf_pipeline_id = req.user.config.find(
(item) => item.option === 'kmining_pdf_pipeline_id'
item => item.option === 'kmining_pdf_pipeline_id'
)?.value;
const kmining_csv_pipeline_id = req.user.config.find(
(item) => item.option === 'kmining_csv_pipeline_id'
item => item.option === 'kmining_csv_pipeline_id'
)?.value;


if (req.file.mimetype === 'application/ld+json') {
return "simple_json_to_jsonld";
return 'simple_json_to_jsonld';
}
if (req.file.mimetype === 'application/json') {
return kmining_json_pipeline_id;
Expand Down Expand Up @@ -44,25 +43,51 @@ exports.triggerPipeline = async (
formData.append('pipelineId', kMiningPipelineId);
formData.append(
'fileFormat',
file.mimetype === 'application/json'
|| file.mimetype === 'application/ld+json'
file.mimetype === 'application/json' ||
file.mimetype === 'application/ld+json'
? 'json'
: file.mimetype === 'application/pdf'
? 'pdf'
: 'csv'
);

let result = await axios.post(
`${kMiningEndpoint}/trigger_pipeline`,
formData,
{
withCredentials: true,
headers: {
Cookie: sessionCookie,
...formData.getHeaders() // Include multipart/form-data headers
}
let result = null;

const authHeader = req.headers['authorization'];

if (authHeader && authHeader.startsWith('Bearer ')) {
// Bearer token is present
const token = authHeader.split(' ')[1];

if (!token) {
throw Error('Invalid Bearer token format');
}
);

result = await axios.post(
`${kMiningEndpoint}/trigger_pipeline`,
formData,
{
withCredentials: true,
headers: {
Authorization: `Bearer ${token}`,
...formData.getHeaders() // Include multipart/form-data headers
}
}
);
} else {
const sessionCookie = req.headers.cookie;
result = await axios.post(
`${kMiningEndpoint}/trigger_pipeline`,
formData,
{
withCredentials: true,
headers: {
Cookie: sessionCookie,
...formData.getHeaders() // Include multipart/form-data headers
}
}
);
}

if (result.data.message === 'DAG triggered') {
const pipelineId = result.data.pipeline_id;
Expand Down Expand Up @@ -106,5 +131,5 @@ exports.triggerPipeline = async (
};

function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
}
46 changes: 37 additions & 9 deletions services/publishService.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,44 @@ class PublishService {
}

async getWallets(sessionCookie) {
const wallets = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/wallets`,
{
headers: {
Cookie: sessionCookie
},
withCredentials: true
try {
const authHeader = req.headers['authorization'];

if (authHeader && authHeader.startsWith('Bearer ')) {
// Bearer token is present
const token = authHeader.split(' ')[1];

if (!token) {
throw Error('Invalid token format');
}

const wallets = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/wallets`,
{
headers: {
Authorization: `Bearer ${token}`
},
withCredentials: true
}
);
return wallets.data.wallets;
} else {
const sessionCookie = req.headers.cookie;

const wallets = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/wallets`,
{
headers: {
Cookie: sessionCookie
},
withCredentials: true
}
);
return wallets.data.wallets;
}
);
return wallets.data.wallets;
} catch (e) {
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe log something that error happened, if were not gonna add guards

}
}

async defineNextWallet(wallets) {
Expand Down