Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Modify service token format
Tuan Dange99ee94a7b5b
diff --git a/backend/src/controllers/v1/serviceTokenDataController.ts b/backend/src/controllers/v1/serviceTokenDataController.tsindex 3a4b94a83c..d8f4d4eeac 100644--- a/backend/src/controllers/v1/serviceTokenDataController.ts+++ b/backend/src/controllers/v1/serviceTokenDataController.ts@@ -15,7 +15,7 @@ import {* @param res* @returns*/-export const getServiceTokenData = async (req: Request, res: Response) => ({+export const getServiceTokenData = async (req: Request, res: Response) => res.status(200).send({serviceTokenData: req.serviceTokenData});@@ -38,35 +38,32 @@ export const createServiceTokenData = async (req: Request, res: Response) => {tag,expiresIn} = req.body;-- // create 41-char service token with first 9-char being the prefix- serviceToken = `st.${crypto.randomBytes(19).toString('hex')}`;-- const serviceTokenHash = await bcrypt.hash(serviceToken, SALT_ROUNDS);- // compute access token expiration date+ const secret = crypto.randomBytes(16).toString('hex');+ const secretHash = await bcrypt.hash(secret, SALT_ROUNDS);+const expiresAt = new Date();expiresAt.setSeconds(expiresAt.getSeconds() + expiresIn);- // create service token data- serviceTokenData = new ServiceTokenData({+ serviceTokenData = await new ServiceTokenData({name,workspace: workspaceId,environment,user: req.user._id,expiresAt,- prefix: serviceToken.substring(0, 9),- serviceTokenHash,+ secretHash,encryptedKey,iv,tag- })+ }).save();- await serviceTokenData.save();-// return service token data without sensitive dataserviceTokenData = await ServiceTokenData.findById(serviceTokenData._id);+ if (!serviceTokenData) throw new Error('Failed to find service token data');++ serviceToken = `st.${serviceTokenData._id.toString()}.${secret}`;+} catch (err) {Sentry.setUser({ email: req.user.email });Sentry.captureException(err);diff --git a/backend/src/helpers/auth.ts b/backend/src/helpers/auth.tsindex c3b2129162..ad63d41b4c 100644--- a/backend/src/helpers/auth.ts+++ b/backend/src/helpers/auth.ts@@ -15,7 +15,8 @@ import {import {AccountNotFoundError,ServiceTokenDataNotFoundError,- UnauthorizedRequestError+ UnauthorizedRequestError,+ BadRequestError} from '../utils/errors';/**@@ -101,16 +102,31 @@ const getAuthSTDPayload = async ({}) => {let serviceTokenData;try {- const serviceTokenHash = await bcrypt.hash(authTokenValue, SALT_ROUNDS);+ const [_, TOKEN_IDENTIFIER, TOKEN_SECRET] = <[string, string, string]>authTokenValue.split('.', 3);+ // TODO: optimize double queryserviceTokenData = await ServiceTokenData- .findOne({- serviceTokenHash- })- .select('+encryptedKey +iv +tag');+ .findById(TOKEN_IDENTIFIER, 'secretHash expiresAt');+ if (serviceTokenData?.expiresAt && new Date(serviceTokenData.expiresAt) < new Date()) {+ // case: service token expired+ await ServiceTokenData.findByIdAndDelete(serviceTokenData._id);+ throw UnauthorizedRequestError({+ message: 'Failed to authenticate expired service token'+ });+ }+if (!serviceTokenData) throw ServiceTokenDataNotFoundError({ message: 'Failed to find service token data' });+ const isMatch = await bcrypt.compare(TOKEN_SECRET, serviceTokenData.secretHash);+ if (!isMatch) throw UnauthorizedRequestError({+ message: 'Failed to authenticate service token'+ });++ serviceTokenData = await ServiceTokenData+ .findById(TOKEN_IDENTIFIER)+ .select('+encryptedKey +iv +tag');+} catch (err) {throw UnauthorizedRequestError({message: 'Failed to authenticate service token'diff --git a/backend/src/middleware/requireAuth.ts b/backend/src/middleware/requireAuth.tsindex b91d2cd09f..8253cb64eb 100644--- a/backend/src/middleware/requireAuth.ts+++ b/backend/src/middleware/requireAuth.ts@@ -6,8 +6,7 @@ import {getAuthUserPayload,getAuthSTDPayload} from '../helpers/auth';-import { JWT_AUTH_SECRET } from '../config';-import { AccountNotFoundError, BadRequestError, UnauthorizedRequestError } from '../utils/errors';+import { BadRequestError } from '../utils/errors';declare module 'jsonwebtoken' {export interface UserIDJwtPayload extends jwt.JwtPayload {diff --git a/backend/src/middleware/requireWorkspaceAuth.ts b/backend/src/middleware/requireWorkspaceAuth.tsindex 2a9110c2c6..68edec6a4b 100644--- a/backend/src/middleware/requireWorkspaceAuth.ts+++ b/backend/src/middleware/requireWorkspaceAuth.ts@@ -40,10 +40,10 @@ const requireWorkspaceAuth = ({if (req.serviceTokenData&& req.serviceTokenData.workspace !== workspaceId- && req.serviceTokenData.environment !== req.body.environment- )- // case: st auth+ && req.serviceTokenData.environment !== req.query.environment+ ) {next(UnauthorizedRequestError({message: 'Unable to authenticate workspace'}))+ }return next();} catch (err) {diff --git a/backend/src/models/serviceTokenData .ts b/backend/src/models/serviceTokenData .tsindex 1faecfaffe..8e8ae5eaba 100644--- a/backend/src/models/serviceTokenData .ts+++ b/backend/src/models/serviceTokenData .ts@@ -7,8 +7,7 @@ export interface IServiceTokenData {environment: string; // TODO: adapt to upcoming environment iduser: Types.ObjectId;expiresAt: Date;- prefix: string;- serviceTokenHash: string;+ secretHash: string;encryptedKey: string;iv: string;tag: string;@@ -37,11 +36,7 @@ const serviceTokenDataSchema = new Schema<IServiceTokenData>(expiresAt: {type: Date},- prefix: {- type: String,- required: true- },- serviceTokenHash: {+ secretHash: {type: String,unique: true,required: true,diff --git a/backend/src/routes/v1/serviceTokenData.ts b/backend/src/routes/v1/serviceTokenData.tsindex 4223172d35..1d77006158 100644--- a/backend/src/routes/v1/serviceTokenData.ts+++ b/backend/src/routes/v1/serviceTokenData.ts@@ -1,4 +1,4 @@-import express, { Request, Response } from 'express';+import express from 'express';const router = express.Router();import {requireAuth,@@ -20,8 +20,6 @@ router.get(requireAuth({acceptedAuthModes: ['serviceToken']}),- param('serviceTokenDataId').exists().trim(),- validateRequest,serviceTokenDataController.getServiceTokenData);diff --git a/backend/src/routes/v2/workspace.ts b/backend/src/routes/v2/workspace.tsindex df26749f3f..b2c91bd1b8 100644--- a/backend/src/routes/v2/workspace.ts+++ b/backend/src/routes/v2/workspace.ts@@ -4,11 +4,9 @@ import { body, param, query } from 'express-validator';import {requireAuth,requireWorkspaceAuth,- requireServiceTokenAuth,validateRequest} from '../../middleware';import { ADMIN, MEMBER, COMPLETED, GRANTED } from '../../variables';-import { membershipController } from '../../controllers/v1';import { workspaceController } from '../../controllers/v2';router.post(