mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 12:38:36 +00:00
Add eslint rule and fix as many issues as possible
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
import requireAuth from './requireAuth';
|
||||
import requireMfaAuth from './requireMfaAuth';
|
||||
import requireBotAuth from './requireBotAuth';
|
||||
import requireSignupAuth from './requireSignupAuth';
|
||||
import requireWorkspaceAuth from './requireWorkspaceAuth';
|
||||
import requireMembershipAuth from './requireMembershipAuth';
|
||||
import requireMembershipOrgAuth from './requireMembershipOrgAuth';
|
||||
import requireOrganizationAuth from './requireOrganizationAuth';
|
||||
import requireIntegrationAuth from './requireIntegrationAuth';
|
||||
import requireIntegrationAuthorizationAuth from './requireIntegrationAuthorizationAuth';
|
||||
import requireServiceTokenAuth from './requireServiceTokenAuth';
|
||||
import requireServiceTokenDataAuth from './requireServiceTokenDataAuth';
|
||||
import requireServiceAccountAuth from './requireServiceAccountAuth';
|
||||
import requireServiceAccountWorkspacePermissionAuth from './requireServiceAccountWorkspacePermissionAuth';
|
||||
import requireSecretAuth from './requireSecretAuth';
|
||||
import requireSecretsAuth from './requireSecretsAuth';
|
||||
import validateRequest from './validateRequest';
|
||||
import requireAuth from "./requireAuth";
|
||||
import requireMfaAuth from "./requireMfaAuth";
|
||||
import requireBotAuth from "./requireBotAuth";
|
||||
import requireSignupAuth from "./requireSignupAuth";
|
||||
import requireWorkspaceAuth from "./requireWorkspaceAuth";
|
||||
import requireMembershipAuth from "./requireMembershipAuth";
|
||||
import requireMembershipOrgAuth from "./requireMembershipOrgAuth";
|
||||
import requireOrganizationAuth from "./requireOrganizationAuth";
|
||||
import requireIntegrationAuth from "./requireIntegrationAuth";
|
||||
import requireIntegrationAuthorizationAuth from "./requireIntegrationAuthorizationAuth";
|
||||
import requireServiceTokenAuth from "./requireServiceTokenAuth";
|
||||
import requireServiceTokenDataAuth from "./requireServiceTokenDataAuth";
|
||||
import requireServiceAccountAuth from "./requireServiceAccountAuth";
|
||||
import requireServiceAccountWorkspacePermissionAuth from "./requireServiceAccountWorkspacePermissionAuth";
|
||||
import requireSecretAuth from "./requireSecretAuth";
|
||||
import requireSecretsAuth from "./requireSecretsAuth";
|
||||
import validateRequest from "./validateRequest";
|
||||
|
||||
export {
|
||||
requireAuth,
|
||||
@@ -33,5 +33,5 @@ export {
|
||||
requireServiceAccountWorkspacePermissionAuth,
|
||||
requireSecretAuth,
|
||||
requireSecretsAuth,
|
||||
validateRequest
|
||||
validateRequest,
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as Sentry from '@sentry/node';
|
||||
import { ErrorRequestHandler } from 'express';
|
||||
import { InternalServerError } from '../utils/errors';
|
||||
import { getLogger } from '../utils/logger';
|
||||
import RequestError, { LogLevel } from '../utils/requestError';
|
||||
import { getNodeEnv } from '../config';
|
||||
import * as Sentry from "@sentry/node";
|
||||
import { ErrorRequestHandler } from "express";
|
||||
import { InternalServerError } from "../utils/errors";
|
||||
import { getLogger } from "../utils/logger";
|
||||
import RequestError, { LogLevel } from "../utils/requestError";
|
||||
import { getNodeEnv } from "../config";
|
||||
|
||||
export const requestErrorHandler: ErrorRequestHandler = async (
|
||||
error: RequestError | Error,
|
||||
@@ -24,7 +24,7 @@ export const requestErrorHandler: ErrorRequestHandler = async (
|
||||
context: { exception: error.message },
|
||||
stack: error.stack,
|
||||
});
|
||||
(await getLogger('backend-main')).log(
|
||||
(await getLogger("backend-main")).log(
|
||||
(<RequestError>error).levelName.toLowerCase(),
|
||||
(<RequestError>error).message
|
||||
);
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import jwt from "jsonwebtoken";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import {
|
||||
validateAuthMode,
|
||||
getAuthUserPayload,
|
||||
getAuthSTDPayload,
|
||||
getAuthAPIKeyPayload,
|
||||
getAuthSAAKPayload
|
||||
} from '../helpers/auth';
|
||||
getAuthSAAKPayload,
|
||||
getAuthSTDPayload,
|
||||
getAuthUserPayload,
|
||||
validateAuthMode,
|
||||
} from "../helpers/auth";
|
||||
import {
|
||||
IUser,
|
||||
IServiceAccount,
|
||||
IServiceTokenData
|
||||
} from '../models';
|
||||
IServiceTokenData,
|
||||
IUser,
|
||||
} from "../models";
|
||||
import {
|
||||
AUTH_MODE_API_KEY,
|
||||
AUTH_MODE_JWT,
|
||||
AUTH_MODE_SERVICE_ACCOUNT,
|
||||
AUTH_MODE_SERVICE_TOKEN,
|
||||
AUTH_MODE_API_KEY
|
||||
} from '../variables';
|
||||
import { getChannelFromUserAgent } from '../utils/posthog';
|
||||
} from "../variables";
|
||||
import { getChannelFromUserAgent } from "../utils/posthog";
|
||||
|
||||
declare module 'jsonwebtoken' {
|
||||
declare module "jsonwebtoken" {
|
||||
export interface UserIDJwtPayload extends jwt.JwtPayload {
|
||||
userId: string;
|
||||
}
|
||||
@@ -47,32 +47,32 @@ const requireAuth = ({
|
||||
// and return token type [authTokenType] and value [authTokenValue]
|
||||
const { authMode, authTokenValue } = validateAuthMode({
|
||||
headers: req.headers,
|
||||
acceptedAuthModes
|
||||
acceptedAuthModes,
|
||||
});
|
||||
|
||||
let authPayload: IUser | IServiceAccount | IServiceTokenData;
|
||||
switch (authMode) {
|
||||
case AUTH_MODE_SERVICE_ACCOUNT:
|
||||
authPayload = await getAuthSAAKPayload({
|
||||
authTokenValue
|
||||
authTokenValue,
|
||||
});
|
||||
req.serviceAccount = authPayload;
|
||||
break;
|
||||
case AUTH_MODE_SERVICE_TOKEN:
|
||||
authPayload = await getAuthSTDPayload({
|
||||
authTokenValue
|
||||
authTokenValue,
|
||||
});
|
||||
req.serviceTokenData = authPayload;
|
||||
break;
|
||||
case AUTH_MODE_API_KEY:
|
||||
authPayload = await getAuthAPIKeyPayload({
|
||||
authTokenValue
|
||||
authTokenValue,
|
||||
});
|
||||
req.user = authPayload;
|
||||
break;
|
||||
default:
|
||||
const { user, tokenVersionId } = await getAuthUserPayload({
|
||||
authTokenValue
|
||||
authTokenValue,
|
||||
});
|
||||
authPayload = user;
|
||||
req.user = user;
|
||||
@@ -89,10 +89,10 @@ const requireAuth = ({
|
||||
req.authData = {
|
||||
authMode,
|
||||
authPayload, // User, ServiceAccount, ServiceTokenData
|
||||
authChannel: getChannelFromUserAgent(req.headers['user-agent']),
|
||||
authChannel: getChannelFromUserAgent(req.headers["user-agent"]),
|
||||
authIP: req.realIP,
|
||||
authUserAgent: req.headers['user-agent'] ?? 'other',
|
||||
tokenVersionId: req.tokenVersionId
|
||||
authUserAgent: req.headers["user-agent"] ?? "other",
|
||||
tokenVersionId: req.tokenVersionId,
|
||||
}
|
||||
|
||||
return next();
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForBot } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForBot } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
const requireBotAuth = ({
|
||||
acceptedRoles,
|
||||
locationBotId = 'params'
|
||||
locationBotId = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
locationBotId?: req;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -17,7 +17,7 @@ const requireBotAuth = ({
|
||||
req.bot = await validateClientForBot({
|
||||
authData: req.authData,
|
||||
botId: new Types.ObjectId(botId),
|
||||
acceptedRoles
|
||||
acceptedRoles,
|
||||
});
|
||||
|
||||
next();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForIntegration } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForIntegration } from "../validation";
|
||||
|
||||
/**
|
||||
* Validate if user on request is a member of workspace with proper roles associated
|
||||
@@ -9,9 +9,9 @@ import { validateClientForIntegration } from '../validation';
|
||||
* @param {String[]} obj.acceptedRoles - accepted workspace roles
|
||||
*/
|
||||
const requireIntegrationAuth = ({
|
||||
acceptedRoles
|
||||
acceptedRoles,
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
const { integrationId } = req.params;
|
||||
@@ -19,7 +19,7 @@ const requireIntegrationAuth = ({
|
||||
const { integration, accessToken } = await validateClientForIntegration({
|
||||
authData: req.authData,
|
||||
integrationId: new Types.ObjectId(integrationId),
|
||||
acceptedRoles
|
||||
acceptedRoles,
|
||||
});
|
||||
|
||||
if (integration) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Types } from 'mongoose';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { validateClientForIntegrationAuth } from '../validation';
|
||||
import { Types } from "mongoose";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { validateClientForIntegrationAuth } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
/**
|
||||
* Validate if user on request is a member of workspace with proper roles associated
|
||||
@@ -14,9 +14,9 @@ type req = 'params' | 'body' | 'query';
|
||||
const requireIntegrationAuthorizationAuth = ({
|
||||
acceptedRoles,
|
||||
attachAccessToken = true,
|
||||
location = 'params'
|
||||
location = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
attachAccessToken?: boolean;
|
||||
location?: req;
|
||||
}) => {
|
||||
@@ -27,7 +27,7 @@ const requireIntegrationAuthorizationAuth = ({
|
||||
authData: req.authData,
|
||||
integrationAuthId: new Types.ObjectId(integrationAuthId),
|
||||
acceptedRoles,
|
||||
attachAccessToken
|
||||
attachAccessToken,
|
||||
});
|
||||
|
||||
if (integrationAuth) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Types } from 'mongoose';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { validateClientForMembership } from '../validation';
|
||||
import { Types } from "mongoose";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { validateClientForMembership } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
/**
|
||||
* Validate membership with id [membershipId] and that user with id
|
||||
@@ -13,9 +13,9 @@ type req = 'params' | 'body' | 'query';
|
||||
*/
|
||||
const requireMembershipAuth = ({
|
||||
acceptedRoles,
|
||||
locationMembershipId = 'params'
|
||||
locationMembershipId = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
locationMembershipId: req
|
||||
}) => {
|
||||
return async (
|
||||
@@ -28,7 +28,7 @@ const requireMembershipAuth = ({
|
||||
req.targetMembership = await validateClientForMembership({
|
||||
authData: req.authData,
|
||||
membershipId: new Types.ObjectId(membershipId),
|
||||
acceptedRoles
|
||||
acceptedRoles,
|
||||
});
|
||||
|
||||
return next();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Types } from 'mongoose';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { validateClientForMembershipOrg } from '../validation';
|
||||
import { Types } from "mongoose";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { validateClientForMembershipOrg } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
/**
|
||||
* Validate (organization) membership id [membershipId] and that user with id
|
||||
@@ -14,10 +14,10 @@ type req = 'params' | 'body' | 'query';
|
||||
const requireMembershipOrgAuth = ({
|
||||
acceptedRoles,
|
||||
acceptedStatuses,
|
||||
locationMembershipOrgId = 'params'
|
||||
locationMembershipOrgId = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'owner' | 'admin' | 'member'>;
|
||||
acceptedStatuses: Array<'invited' | 'accepted'>;
|
||||
acceptedRoles: Array<"owner" | "admin" | "member">;
|
||||
acceptedStatuses: Array<"invited" | "accepted">;
|
||||
locationMembershipOrgId?: req;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -27,7 +27,7 @@ const requireMembershipOrgAuth = ({
|
||||
authData: req.authData,
|
||||
membershipOrgId: new Types.ObjectId(membershipId),
|
||||
acceptedRoles,
|
||||
acceptedStatuses
|
||||
acceptedStatuses,
|
||||
});
|
||||
|
||||
return next();
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { User } from '../models';
|
||||
import { BadRequestError, UnauthorizedRequestError } from '../utils/errors';
|
||||
import { getJwtMfaSecret } from '../config';
|
||||
import jwt from "jsonwebtoken";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { User } from "../models";
|
||||
import { BadRequestError, UnauthorizedRequestError } from "../utils/errors";
|
||||
import { getJwtMfaSecret } from "../config";
|
||||
|
||||
declare module 'jsonwebtoken' {
|
||||
declare module "jsonwebtoken" {
|
||||
export interface UserIDJwtPayload extends jwt.JwtPayload {
|
||||
userId: string;
|
||||
}
|
||||
@@ -20,21 +20,21 @@ const requireMfaAuth = async (
|
||||
next: NextFunction
|
||||
) => {
|
||||
// JWT (temporary) authentication middleware for complete signup
|
||||
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, await getJwtMfaSecret())
|
||||
);
|
||||
|
||||
const user = await User.findOne({
|
||||
_id: decodedToken.userId
|
||||
}).select('+publicKey');
|
||||
_id: decodedToken.userId,
|
||||
}).select("+publicKey");
|
||||
|
||||
if (!user)
|
||||
return next(UnauthorizedRequestError({message: 'Unable to authenticate for User account completion. Try logging in again'}))
|
||||
return next(UnauthorizedRequestError({message: "Unable to authenticate for User account completion. Try logging in again"}))
|
||||
|
||||
req.user = user;
|
||||
return next();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForOrganization } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForOrganization } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
/**
|
||||
* Validate if user on request is a member with proper roles for organization
|
||||
@@ -14,10 +14,10 @@ type req = 'params' | 'body' | 'query';
|
||||
const requireOrganizationAuth = ({
|
||||
acceptedRoles,
|
||||
acceptedStatuses,
|
||||
locationOrganizationId = 'params'
|
||||
locationOrganizationId = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'owner' | 'admin' | 'member'>;
|
||||
acceptedStatuses: Array<'invited' | 'accepted'>;
|
||||
acceptedRoles: Array<"owner" | "admin" | "member">;
|
||||
acceptedStatuses: Array<"invited" | "accepted">;
|
||||
locationOrganizationId?: req;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -27,7 +27,7 @@ const requireOrganizationAuth = ({
|
||||
authData: req.authData,
|
||||
organizationId: new Types.ObjectId(organizationId),
|
||||
acceptedRoles,
|
||||
acceptedStatuses
|
||||
acceptedStatuses,
|
||||
});
|
||||
|
||||
if (organization) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForSecret } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForSecret } from "../validation";
|
||||
|
||||
// note: used for old /v1/secret and /v2/secret routes.
|
||||
// newer /v2/secrets routes use [requireSecretsAuth] middleware with the exception
|
||||
@@ -14,9 +14,9 @@ import { validateClientForSecret } from '../validation';
|
||||
*/
|
||||
const requireSecretAuth = ({
|
||||
acceptedRoles,
|
||||
requiredPermissions
|
||||
requiredPermissions,
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
requiredPermissions: string[];
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -26,7 +26,7 @@ const requireSecretAuth = ({
|
||||
authData: req.authData,
|
||||
secretId: new Types.ObjectId(secretId),
|
||||
acceptedRoles,
|
||||
requiredPermissions
|
||||
requiredPermissions,
|
||||
});
|
||||
|
||||
req._secret = secret;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForSecrets } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForSecrets } from "../validation";
|
||||
|
||||
const requireSecretsAuth = ({
|
||||
acceptedRoles,
|
||||
requiredPermissions = []
|
||||
requiredPermissions = [],
|
||||
}: {
|
||||
acceptedRoles: string[];
|
||||
requiredPermissions?: string[];
|
||||
@@ -13,18 +13,18 @@ const requireSecretsAuth = ({
|
||||
let secretIds = [];
|
||||
if (Array.isArray(req.body.secrets)) {
|
||||
secretIds = req.body.secrets.map((s: any) => s.id);
|
||||
} else if (typeof req.body.secrets === 'object') {
|
||||
} else if (typeof req.body.secrets === "object") {
|
||||
secretIds = [req.body.secrets.id];
|
||||
} else if (Array.isArray(req.body.secretIds)) {
|
||||
secretIds = req.body.secretIds;
|
||||
} else if (typeof req.body.secretIds === 'string') {
|
||||
} else if (typeof req.body.secretIds === "string") {
|
||||
secretIds = [req.body.secretIds];
|
||||
}
|
||||
|
||||
req.secrets = await validateClientForSecrets({
|
||||
authData: req.authData,
|
||||
secretIds: secretIds.map((secretId: string) => new Types.ObjectId(secretId)),
|
||||
requiredPermissions
|
||||
requiredPermissions,
|
||||
});
|
||||
|
||||
return next();
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForServiceAccount } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForServiceAccount } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
const requireServiceAccountAuth = ({
|
||||
acceptedRoles,
|
||||
acceptedStatuses,
|
||||
locationServiceAccountId = 'params',
|
||||
requiredPermissions = []
|
||||
locationServiceAccountId = "params",
|
||||
requiredPermissions = [],
|
||||
}: {
|
||||
acceptedRoles: string[];
|
||||
acceptedStatuses: string[];
|
||||
@@ -21,7 +21,7 @@ const requireServiceAccountAuth = ({
|
||||
req.serviceAccount = await validateClientForServiceAccount({
|
||||
authData: req.authData,
|
||||
serviceAccountId: new Types.ObjectId(serviceAccountId),
|
||||
requiredPermissions
|
||||
requiredPermissions,
|
||||
});
|
||||
|
||||
next();
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { ServiceAccount, ServiceAccountWorkspacePermission } from '../models';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { ServiceAccount, ServiceAccountWorkspacePermission } from "../models";
|
||||
import {
|
||||
ServiceAccountNotFoundError
|
||||
} from '../utils/errors';
|
||||
ServiceAccountNotFoundError,
|
||||
} from "../utils/errors";
|
||||
import {
|
||||
validateMembershipOrg
|
||||
} from '../helpers/membershipOrg';
|
||||
validateMembershipOrg,
|
||||
} from "../helpers/membershipOrg";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
const requireServiceAccountWorkspacePermissionAuth = ({
|
||||
acceptedRoles,
|
||||
acceptedStatuses,
|
||||
location = 'params'
|
||||
location = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'owner' | 'admin' | 'member'>;
|
||||
acceptedStatuses: Array<'invited' | 'accepted'>;
|
||||
acceptedRoles: Array<"owner" | "admin" | "member">;
|
||||
acceptedStatuses: Array<"invited" | "accepted">;
|
||||
location?: req;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -23,13 +23,13 @@ const requireServiceAccountWorkspacePermissionAuth = ({
|
||||
const serviceAccountWorkspacePermission = await ServiceAccountWorkspacePermission.findById(serviceAccountWorkspacePermissionId);
|
||||
|
||||
if (!serviceAccountWorkspacePermission) {
|
||||
return next(ServiceAccountNotFoundError({ message: 'Failed to locate Service Account workspace permission' }));
|
||||
return next(ServiceAccountNotFoundError({ message: "Failed to locate Service Account workspace permission" }));
|
||||
}
|
||||
|
||||
const serviceAccount = await ServiceAccount.findById(serviceAccountWorkspacePermission.serviceAccount);
|
||||
|
||||
if (!serviceAccount) {
|
||||
return next(ServiceAccountNotFoundError({ message: 'Failed to locate Service Account' }));
|
||||
return next(ServiceAccountNotFoundError({ message: "Failed to locate Service Account" }));
|
||||
}
|
||||
|
||||
if (serviceAccount.user.toString() !== req.user.id.toString()) {
|
||||
@@ -39,7 +39,7 @@ const requireServiceAccountWorkspacePermissionAuth = ({
|
||||
userId: req.user._id,
|
||||
organizationId: serviceAccount.organization,
|
||||
acceptedRoles,
|
||||
acceptedStatuses
|
||||
acceptedStatuses,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { ServiceToken } from '../models';
|
||||
import { BadRequestError, UnauthorizedRequestError } from '../utils/errors';
|
||||
import { getJwtServiceSecret } from '../config';
|
||||
import jwt from "jsonwebtoken";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { ServiceToken } from "../models";
|
||||
import { BadRequestError, UnauthorizedRequestError } from "../utils/errors";
|
||||
import { getJwtServiceSecret } from "../config";
|
||||
|
||||
// TODO: deprecate
|
||||
declare module 'jsonwebtoken' {
|
||||
declare module "jsonwebtoken" {
|
||||
export interface UserIDJwtPayload extends jwt.JwtPayload {
|
||||
userId: string;
|
||||
}
|
||||
@@ -26,23 +26,23 @@ const requireServiceTokenAuth = async (
|
||||
) => {
|
||||
// JWT service token 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.`}))
|
||||
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."}))
|
||||
//TODO: Determine what is the actual Token Type for Service Token Authentication (ex. Bearer)
|
||||
//if(AUTH_TOKEN_TYPE.toLowerCase() !== 'bearer') return next(UnauthorizedRequestError({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'}))
|
||||
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, await getJwtServiceSecret())
|
||||
);
|
||||
|
||||
const serviceToken = await ServiceToken.findOne({
|
||||
_id: decodedToken.serviceTokenId
|
||||
_id: decodedToken.serviceTokenId,
|
||||
})
|
||||
.populate('user', '+publicKey')
|
||||
.select('+encryptedKey +publicKey +nonce');
|
||||
.populate("user", "+publicKey")
|
||||
.select("+encryptedKey +publicKey +nonce");
|
||||
|
||||
if (!serviceToken) return next(UnauthorizedRequestError({message: 'The service token does not match the record in the database'}))
|
||||
if (!serviceToken) return next(UnauthorizedRequestError({message: "The service token does not match the record in the database"}))
|
||||
|
||||
req.serviceToken = serviceToken;
|
||||
return next();
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForServiceTokenData } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForServiceTokenData } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
const requireServiceTokenDataAuth = ({
|
||||
acceptedRoles,
|
||||
location = 'params'
|
||||
location = "params",
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
location?: req;
|
||||
}) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
@@ -17,7 +17,7 @@ const requireServiceTokenDataAuth = ({
|
||||
req.serviceTokenData = await validateClientForServiceTokenData({
|
||||
authData: req.authData,
|
||||
serviceTokenDataId: new Types.ObjectId(serviceTokenDataId),
|
||||
acceptedRoles
|
||||
acceptedRoles,
|
||||
});
|
||||
|
||||
next();
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { User } from '../models';
|
||||
import { BadRequestError, UnauthorizedRequestError } from '../utils/errors';
|
||||
import { getJwtSignupSecret } from '../config';
|
||||
import jwt from "jsonwebtoken";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { User } from "../models";
|
||||
import { BadRequestError, UnauthorizedRequestError } from "../utils/errors";
|
||||
import { getJwtSignupSecret } from "../config";
|
||||
|
||||
declare module 'jsonwebtoken' {
|
||||
declare module "jsonwebtoken" {
|
||||
export interface UserIDJwtPayload extends jwt.JwtPayload {
|
||||
userId: string;
|
||||
}
|
||||
@@ -21,21 +21,21 @@ const requireSignupAuth = async (
|
||||
) => {
|
||||
// JWT (temporary) authentication middleware for complete signup
|
||||
|
||||
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, await getJwtSignupSecret())
|
||||
);
|
||||
|
||||
const user = await User.findOne({
|
||||
_id: decodedToken.userId
|
||||
}).select('+publicKey');
|
||||
_id: decodedToken.userId,
|
||||
}).select("+publicKey");
|
||||
|
||||
if (!user)
|
||||
return next(UnauthorizedRequestError({message: 'Unable to authenticate for User account completion. Try logging in again'}))
|
||||
return next(UnauthorizedRequestError({message: "Unable to authenticate for User account completion. Try logging in again"}))
|
||||
|
||||
req.user = user;
|
||||
return next();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Types } from 'mongoose';
|
||||
import { validateClientForWorkspace } from '../validation';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { validateClientForWorkspace } from "../validation";
|
||||
|
||||
type req = 'params' | 'body' | 'query';
|
||||
type req = "params" | "body" | "query";
|
||||
|
||||
/**
|
||||
* Validate if user on request is a member with proper roles for workspace
|
||||
@@ -17,9 +17,9 @@ const requireWorkspaceAuth = ({
|
||||
locationEnvironment = undefined,
|
||||
requiredPermissions = [],
|
||||
requireBlindIndicesEnabled = false,
|
||||
requireE2EEOff = false
|
||||
requireE2EEOff = false,
|
||||
}: {
|
||||
acceptedRoles: Array<'admin' | 'member'>;
|
||||
acceptedRoles: Array<"admin" | "member">;
|
||||
locationWorkspaceId: req;
|
||||
locationEnvironment?: req | undefined;
|
||||
requiredPermissions?: string[];
|
||||
@@ -38,7 +38,7 @@ const requireWorkspaceAuth = ({
|
||||
acceptedRoles,
|
||||
requiredPermissions,
|
||||
requireBlindIndicesEnabled,
|
||||
requireE2EEOff
|
||||
requireE2EEOff,
|
||||
});
|
||||
|
||||
if (membership) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { validationResult } from 'express-validator';
|
||||
import { BadRequestError, UnauthorizedRequestError, ValidationError } from '../utils/errors';
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { validationResult } from "express-validator";
|
||||
import { UnauthorizedRequestError, ValidationError } from "../utils/errors";
|
||||
|
||||
/**
|
||||
* Validate intended inputs on [req] via express-validator
|
||||
@@ -20,7 +20,7 @@ const validate = (req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
return next();
|
||||
} catch (err) {
|
||||
return next(UnauthorizedRequestError({ message: 'Unauthenticated requests are not allowed. Try logging in' }))
|
||||
return next(UnauthorizedRequestError({ message: "Unauthenticated requests are not allowed. Try logging in" }))
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user