Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Merge crud cli api and cli changes

Maidul Islam7e026e82bbfd
diff --cc backend/package-lock.json
index fc03ec328d,95f1468120..6b1c1f97ab
--- a/backend/package-lock.json
+++ b/backend/package-lock.json
@@@ -15,8 -15,8 +15,9 @@@
"@sentry/tracing": "^7.19.0",
"@types/crypto-js": "^4.1.1",
"@types/libsodium-wrappers": "^0.7.10",
+ "await-to-js": "^3.0.0",
"axios": "^1.1.3",
+ "bcrypt": "^5.1.0",
"bigint-conversion": "^2.2.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
@@@ -17175,9 -16799,9 +17198,9 @@@
"dev": true
},
"json5": {
- "version": "2.2.2",
- "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.2.tgz",
- "integrity": "sha512-46Tk9JiOL2z7ytNQWFLpj99RZkVgeHf87yGQKsIkaPz1qSH9UczKH1rO7K3wgRselo0tYMUNfecYpm/p1vC7tQ==",
- "version": "2.2.3",
- "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
- "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
++ "version": "2.2.1",
++ "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
++ "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
"dev": true
},
"jsonwebtoken": {
diff --cc backend/package.json
index 0bcb36e496,1eb5e3b456..56009e3e91
--- a/backend/package.json
+++ b/backend/package.json
@@@ -6,8 -6,8 +6,9 @@@
"@sentry/tracing": "^7.19.0",
"@types/crypto-js": "^4.1.1",
"@types/libsodium-wrappers": "^0.7.10",
+ "await-to-js": "^3.0.0",
"axios": "^1.1.3",
+ "bcrypt": "^5.1.0",
"bigint-conversion": "^2.2.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
diff --cc backend/src/controllers/v2/index.ts
index e867f76923,24ca015ac7..2c5cce6600
--- a/backend/src/controllers/v2/index.ts
+++ b/backend/src/controllers/v2/index.ts
@@@ -1,9 -1,7 +1,11 @@@
import * as workspaceController from './workspaceController';
+import * as serviceTokenDataController from './serviceTokenDataController';
+import * as apiKeyDataController from './apiKeyDataController';
+ import * as secretController from './secretController';
- export {
+ export {
workspaceController,
+ serviceTokenDataController,
- apiKeyDataController
++ apiKeyDataController,
+ secretController
}
diff --cc backend/src/middleware/requireAuth.ts
index 5d95883a5b,172f7b68e1..3be8418eed
--- a/backend/src/middleware/requireAuth.ts
+++ b/backend/src/middleware/requireAuth.ts
@@@ -16,58 -11,34 +16,58 @@@ declare module 'jsonwebtoken'
}
/**
- * Validate if JWT (auth) token on request is valid (e.g. not expired),
- * if there is an associated user, and if that user is fully setup.
- * @param req - express request object
- * @param res - express response object
- * @param next - express next function
+ * Validate if token on request is valid (e.g. not expired) for various auth modes:
+ * - If token is a JWT token, then check if there is an associated user
+ * and if user is fully setup.
+ * - If token is a service token (st), then check if there is associated
+ * service token data.
+ * @param {Object} obj
+ * @param {String[]} obj.acceptedAuthModes - accepted modes of authentication (jwt/st)
* @returns
*/
- const requireAuth = ({
-const requireAuth = async (req: Request, res: Response, next: NextFunction) => {
- // JWT authentication middleware
- const [AUTH_TOKEN_TYPE, AUTH_TOKEN_VALUE] = <[string, string]>req.headers['authorization']?.split(' ', 2) ?? [null, null]
- if (AUTH_TOKEN_TYPE === null) return next(BadRequestError({ message: `Missing Authorization Header in the request header.` }))
- if (AUTH_TOKEN_TYPE.toLowerCase() !== 'bearer') return next(BadRequestError({ message: `The provided authentication type '${AUTH_TOKEN_TYPE}' is not supported.` }))
- if (AUTH_TOKEN_VALUE === null) return next(BadRequestError({ message: 'Missing Authorization Body in the request header' }))
++const requireAuth = ({
+ acceptedAuthModes = ['jwt']
+}: {
+ acceptedAuthModes: string[];
+}) => {
+ return async (req: Request, res: Response, next: NextFunction) => {
- const [ AUTH_TOKEN_TYPE, AUTH_TOKEN_VALUE ] = <[string, string]>req.headers['authorization']?.split(' ', 2) ?? [null, null]
- if(AUTH_TOKEN_TYPE === null)
- return next(BadRequestError({message: `Missing Authorization Header in the request header.`}))
- if(AUTH_TOKEN_TYPE.toLowerCase() !== 'bearer')
- return next(BadRequestError({message: `The provided authentication type '${AUTH_TOKEN_TYPE}' is not supported.`}))
- if(AUTH_TOKEN_VALUE === null)
- return next(BadRequestError({message: 'Missing Authorization Body in the request header'}))
-
++ const [AUTH_TOKEN_TYPE, AUTH_TOKEN_VALUE] = <[string, string]>req.headers['authorization']?.split(' ', 2) ?? [null, null]
++ if (AUTH_TOKEN_TYPE === null)
++ return next(BadRequestError({ message: `Missing Authorization Header in the request header.` }))
++ if (AUTH_TOKEN_TYPE.toLowerCase() !== 'bearer')
++ return next(BadRequestError({ message: `The provided authentication type '${AUTH_TOKEN_TYPE}' is not supported.` }))
++ if (AUTH_TOKEN_VALUE === null)
++ return next(BadRequestError({ message: 'Missing Authorization Body in the request header' }))
+
- const decodedToken = <jwt.UserIDJwtPayload>(
- jwt.verify(AUTH_TOKEN_VALUE, JWT_AUTH_SECRET)
- );
+ // validate auth token against
+ const authMode = validateAuthMode({
+ authTokenValue: AUTH_TOKEN_VALUE,
+ acceptedAuthModes
+ });
-
+
- const user = await User.findOne({
- _id: decodedToken.userId
- }).select('+publicKey');
+ if (!acceptedAuthModes.includes(authMode)) throw new Error('Failed to validate auth mode');
-
+
- if (!user) return next(AccountNotFoundError({ message: 'Failed to locate User account' }))
- if (!user?.publicKey)
- return next(UnauthorizedRequestError({ message: 'Unable to authenticate due to partially set up account' }))
+ // attach auth payloads
+ switch (authMode) {
+ case 'serviceToken':
+ req.serviceTokenData = await getAuthSTDPayload({
+ authTokenValue: AUTH_TOKEN_VALUE
+ });
+ break;
+ case 'apiKey':
+ req.user = await getAuthAPIKeyPayload({
+ authTokenValue: AUTH_TOKEN_VALUE
+ });
+ break;
+ default:
+ req.user = await getAuthUserPayload({
+ authTokenValue: AUTH_TOKEN_VALUE
+ });
+ break;
+ }
- req.user = user;
- return next();
-};
+ return next();
+ }
+}
export default requireAuth;