From eb263a470d6062aacb81751cac948bf3db302388 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 16 Apr 2025 13:29:40 +0200 Subject: [PATCH] feat: Move api token authentication to .ee files --- .../src/helpers/authenticate-api-token.ee.js | 27 +++++++++++++++++++ .../helpers/authenticate-api-token.ee.test.js | 22 +++++++++++++++ .../backend/src/helpers/authentication.js | 27 ------------------- .../src/helpers/authentication.test.js | 22 +-------------- 4 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 packages/backend/src/helpers/authenticate-api-token.ee.js create mode 100644 packages/backend/src/helpers/authenticate-api-token.ee.test.js diff --git a/packages/backend/src/helpers/authenticate-api-token.ee.js b/packages/backend/src/helpers/authenticate-api-token.ee.js new file mode 100644 index 00000000..546ddf14 --- /dev/null +++ b/packages/backend/src/helpers/authenticate-api-token.ee.js @@ -0,0 +1,27 @@ +import ApiToken from '../models/api-token.ee.js'; + +export const isApiTokenAuthenticated = async (request) => { + const token = request.headers['x-api-token']; + + if (token == null) return false; + + try { + const apiToken = await ApiToken.query().findOne({ + token, + }); + + if (apiToken == null) return false; + + return true; + } catch (error) { + return false; + } +}; + +export const authenticateApiToken = async (request, response, next) => { + if (await isApiTokenAuthenticated(request)) { + next(); + } else { + return response.status(401).end(); + } +}; diff --git a/packages/backend/src/helpers/authenticate-api-token.ee.test.js b/packages/backend/src/helpers/authenticate-api-token.ee.test.js new file mode 100644 index 00000000..91d7d714 --- /dev/null +++ b/packages/backend/src/helpers/authenticate-api-token.ee.test.js @@ -0,0 +1,22 @@ +import { describe, it, expect } from 'vitest'; +import { isApiTokenAuthenticated } from './authenticate-api-token.ee.js'; +import { createApiToken } from '../../test/factories/api-token.js'; + +describe('isApiTokenAuthenticated', () => { + it('should return false if no token is provided', async () => { + const req = { headers: {} }; + expect(await isApiTokenAuthenticated(req)).toBe(false); + }); + + it('should return false if token is invalid', async () => { + const req = { headers: { 'x-api-token': 'invalidToken' } }; + expect(await isApiTokenAuthenticated(req)).toBe(false); + }); + + it('should return true if token is valid', async () => { + const apiToken = await createApiToken(); + + const req = { headers: { 'x-api-token': apiToken.token } }; + expect(await isApiTokenAuthenticated(req)).toBe(true); + }); +}); diff --git a/packages/backend/src/helpers/authentication.js b/packages/backend/src/helpers/authentication.js index 26851600..cfbb20d4 100644 --- a/packages/backend/src/helpers/authentication.js +++ b/packages/backend/src/helpers/authentication.js @@ -1,6 +1,5 @@ import User from '../models/user.js'; import AccessToken from '../models/access-token.js'; -import ApiToken from '../models/api-token.ee.js'; export const isAuthenticated = async (req) => { const token = req.headers['authorization']; @@ -47,29 +46,3 @@ export const authenticateUser = async (request, response, next) => { return response.status(401).end(); } }; - -export const isApiTokenAuthenticated = async (request) => { - const token = request.headers['x-api-token']; - - if (token == null) return false; - - try { - const apiToken = await ApiToken.query().findOne({ - token, - }); - - if (apiToken == null) return false; - - return true; - } catch (error) { - return false; - } -}; - -export const authenticateApiToken = async (request, response, next) => { - if (await isApiTokenAuthenticated(request)) { - next(); - } else { - return response.status(401).end(); - } -}; diff --git a/packages/backend/src/helpers/authentication.test.js b/packages/backend/src/helpers/authentication.test.js index 024145c6..1b5f1620 100644 --- a/packages/backend/src/helpers/authentication.test.js +++ b/packages/backend/src/helpers/authentication.test.js @@ -1,7 +1,6 @@ import { describe, it, expect } from 'vitest'; -import { isAuthenticated, isApiTokenAuthenticated } from './authentication.js'; +import { isAuthenticated } from './authentication.js'; import { createUser } from '../../test/factories/user.js'; -import { createApiToken } from '../../test/factories/api-token.js'; import createAuthTokenByUserId from '../helpers/create-auth-token-by-user-id.js'; describe('isAuthenticated', () => { @@ -32,22 +31,3 @@ describe('isAuthenticated', () => { expect(await isAuthenticated(req)).toBe(false); }); }); - -describe('isApiTokenAuthenticated', () => { - it('should return false if no token is provided', async () => { - const req = { headers: {} }; - expect(await isApiTokenAuthenticated(req)).toBe(false); - }); - - it('should return false if token is invalid', async () => { - const req = { headers: { 'x-api-token': 'invalidToken' } }; - expect(await isApiTokenAuthenticated(req)).toBe(false); - }); - - it('should return true if token is valid', async () => { - const apiToken = await createApiToken(); - - const req = { headers: { 'x-api-token': apiToken.token } }; - expect(await isApiTokenAuthenticated(req)).toBe(true); - }); -});