Skip to content

Commit

Permalink
chore(api): Fix inconsistencies in zod schema (keyshade-xyz#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritika1705 authored May 27, 2024
1 parent 6107e7d commit f3a3632
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
15 changes: 11 additions & 4 deletions apps/api/src/auth/guard/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PrismaService } from '../../../prisma/prisma.service'
import { ONBOARDING_BYPASSED } from '../../../decorators/bypass-onboarding.decorator'
import { AuthenticatedUserContext } from '../../auth.types'
import { toSHA256 } from '../../../common/to-sha256'
import { EnvSchema } from '../../../common/env/env.schema'

const X_E2E_USER_EMAIL = 'x-e2e-user-email'
const X_KEYSHADE_TOKEN = 'x-keyshade-token'
Expand Down Expand Up @@ -41,17 +42,23 @@ export class AuthGuard implements CanActivate {
let user: AuthenticatedUserContext | null = null
const request = context.switchToHttp().getRequest()
const authType = this.getAuthType(request)
const parsedEnv = EnvSchema.safeParse(process.env)
let nodeEnv

//@ts-expect-error process.env.NODE_ENV parses to 'dev'
if (process.env.NODE_ENV !== 'e2e' && authType === 'NONE') {
if (!parsedEnv.success) {
nodeEnv = 'dev' // Default to a valid value or handle appropriately
} else {
nodeEnv = parsedEnv.data.NODE_ENV
}

if (nodeEnv !== 'e2e' && authType === 'NONE') {
throw new ForbiddenException('No authentication provided')
}

// In case the environment is e2e, we want to authenticate the user using the email
// else we want to authenticate the user using the JWT token.

// @ts-expect-error process.env.NODE_ENV parses to 'dev'
if (authType !== 'API_KEY' && process.env.NODE_ENV === 'e2e') {
if (authType !== 'API_KEY' && nodeEnv === 'e2e') {
const email = request.headers[X_E2E_USER_EMAIL]
if (!email) {
throw new ForbiddenException()
Expand Down
13 changes: 8 additions & 5 deletions apps/api/src/common/env/env.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const e2eEnvSchema = z.object({
const generalSchema = z.object({
NODE_ENV: z.literal('dev'),
DATABASE_URL: z.string(),
ADMIN_EMAIL: z.string(),
ADMIN_EMAIL: z.string().email(),

REDIS_URL: z.string(),
REDIS_PASSWORD: z.string().optional(),
Expand Down Expand Up @@ -56,10 +56,13 @@ const generalSchema = z.object({

SMTP_HOST: z.string(),
SMTP_PORT: z.string(),
SMTP_EMAIL_ADDRESS: z.string(),
SMTP_EMAIL_ADDRESS: z.string().email(),
SMTP_PASSWORD: z.string(),
// TODO: add regex check for FORM_EMAIL value as represented in .env.example (your-name <your-name@email.com>)
FROM_EMAIL: z.string(),
FROM_EMAIL: z
.string()
.regex(
/^[a-zA-Z0-9._%+-]+(?: [a-zA-Z0-9._%+-]+)* <[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}>$/
),

JWT_SECRET: z.string(),

Expand All @@ -75,7 +78,7 @@ const generalSchema = z.object({
MINIO_BUCKET_NAME: z.string().optional(),
MINIO_USE_SSL: z.string().optional(),

FEEDBACK_FORWARD_EMAIL: z.string()
FEEDBACK_FORWARD_EMAIL: z.string().email()
})

export type EnvSchemaType = z.infer<typeof generalSchema>
Expand Down
14 changes: 11 additions & 3 deletions apps/api/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../mail/services/interface.service'
import createUser from '../../common/create-user'
import generateOtp from '../../common/generate-otp'
import { EnvSchema } from '../../common/env/env.schema'

@Injectable()
export class UserService {
Expand Down Expand Up @@ -294,9 +295,16 @@ export class UserService {
}

private async checkIfAdminExistsOrCreate() {
// @ts-expect-error process.env.NODE_ENV parses to 'dev'
// FIXME
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'e2e') {
const parsedEnv = EnvSchema.safeParse(process.env)
let nodeEnv

if (!parsedEnv.success) {
nodeEnv = 'dev' // Default to a valid value or handle appropriately
} else {
nodeEnv = parsedEnv.data.NODE_ENV
}

if (nodeEnv === 'test' || nodeEnv === 'e2e') {
return
}

Expand Down

0 comments on commit f3a3632

Please sign in to comment.