chore: auth on api-v2 with api-key (#15455)
* chore: enable apiv2 auth with api keys * chore: enable apiv2 auth with api keys * chore: enable apiv2 auth with api keys * chore: enable apiv2 auth with api keys * chore: enable apiv2 auth with api keys * fixup! chore: enable apiv2 auth with api keys * fixup! Merge branch 'chore-apiv2-auth-api-key' of github.com:calcom/cal.com into chore-apiv2-auth-api-key * fixup! fixup! Merge branch 'chore-apiv2-auth-api-key' of github.com:calcom/cal.com into chore-apiv2-auth-api-key * fixup! fixup! fixup! Merge branch 'chore-apiv2-auth-api-key' of github.com:calcom/cal.com into chore-apiv2-auth-api-key
This commit is contained in:
1 parent
c5a83f4df2
commit
9a473d5b55
49 files changed
+583
-430
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { PrismaReadService } from "@/modules/prisma/prisma-read.service";
|
||||
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
|
||||
import { TestingModule } from "@nestjs/testing";
|
||||
import { randomBytes, createHash } from "crypto";
|
||||
|
||||
export class ApiKeysRepositoryFixture {
|
||||
private primaReadClient: PrismaReadService["prisma"];
|
||||
private prismaWriteClient: PrismaWriteService["prisma"];
|
||||
|
||||
constructor(private readonly module: TestingModule) {
|
||||
this.primaReadClient = module.get(PrismaReadService).prisma;
|
||||
this.prismaWriteClient = module.get(PrismaWriteService).prisma;
|
||||
}
|
||||
|
||||
async createApiKey(userId: number, expiresAt: Date | null, teamId?: number) {
|
||||
const keyString = randomBytes(16).toString("hex");
|
||||
const apiKey = await this.prismaWriteClient.apiKey.create({
|
||||
data: {
|
||||
userId,
|
||||
teamId,
|
||||
hashedKey: createHash("sha256").update(keyString).digest("hex"),
|
||||
expiresAt: expiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
return { apiKey, keyString };
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ import { Injectable } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class AccessTokenMockStrategy extends PassportStrategy(BaseStrategy, "access-token") {
|
||||
export class ApiAuthMockStrategy extends PassportStrategy(BaseStrategy, "api-auth") {
|
||||
constructor(private readonly email: string, private readonly usersRepository: UsersRepository) {
|
||||
super();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { RedisService } from "@/modules/redis/redis.service";
|
||||
import { Provider } from "@nestjs/common";
|
||||
|
||||
export const MockedRedisService = {
|
||||
provide: RedisService,
|
||||
useValue: {
|
||||
redis: {
|
||||
get: jest.fn(),
|
||||
hgetall: jest.fn(),
|
||||
set: jest.fn(),
|
||||
hmset: jest.fn(),
|
||||
expireat: jest.fn(),
|
||||
},
|
||||
},
|
||||
} as Provider;
|
||||
@@ -1,15 +1,21 @@
|
||||
import type { Environment } from "@/env";
|
||||
|
||||
const env: Partial<Omit<Environment, "NODE_ENV">> = {
|
||||
API_URL: "http://localhost",
|
||||
API_PORT: "5555",
|
||||
DATABASE_URL: "postgresql://postgres:@localhost:5450/calendso",
|
||||
DATABASE_READ_URL: "postgresql://postgres:@localhost:5450/calendso",
|
||||
DATABASE_WRITE_URL: "postgresql://postgres:@localhost:5450/calendso",
|
||||
NEXTAUTH_SECRET: "XF+Hws3A5g2eyWA5uGYYVJ74X+wrCWJ8oWo6kAfU6O8=",
|
||||
JWT_SECRET: "XF+Hws3A5g2eyWA5uGYYVJ74X+wrCWJ8oWo6kAfU6O8=",
|
||||
LOG_LEVEL: "trace",
|
||||
REDIS_URL: "redis://localhost:9199",
|
||||
REDIS_URL: "redis://localhost:6379",
|
||||
STRIPE_API_KEY: "sk_test_51J4",
|
||||
STRIPE_WEBHOOK_SECRET: "whsec_51J4",
|
||||
IS_E2E: true,
|
||||
API_KEY_PREFIX: "cal_test_",
|
||||
GET_LICENSE_KEY_URL: " https://console.cal.com/api/license",
|
||||
CALCOM_LICENSE_KEY: "c4234812-12ab-42s6-a1e3-55bedd4a5bb7",
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { AccessTokenStrategy } from "@/modules/auth/strategies/access-token/access-token.strategy";
|
||||
import { UsersRepository } from "@/modules/users/users.repository";
|
||||
import { TestingModuleBuilder } from "@nestjs/testing";
|
||||
import { AccessTokenMockStrategy } from "test/mocks/access-token-mock.strategy";
|
||||
|
||||
export const withAccessTokenAuth = (email: string, module: TestingModuleBuilder) =>
|
||||
module.overrideProvider(AccessTokenStrategy).useFactory({
|
||||
factory: (usersRepository: UsersRepository) => new AccessTokenMockStrategy(email, usersRepository),
|
||||
inject: [UsersRepository],
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ApiAuthStrategy } from "@/modules/auth/strategies/api-auth/api-auth.strategy";
|
||||
import { UsersRepository } from "@/modules/users/users.repository";
|
||||
import { TestingModuleBuilder } from "@nestjs/testing";
|
||||
import { ApiAuthMockStrategy } from "test/mocks/api-auth-mock.strategy";
|
||||
|
||||
export const withApiAuth = (email: string, module: TestingModuleBuilder) =>
|
||||
module.overrideProvider(ApiAuthStrategy).useFactory({
|
||||
factory: (usersRepository: UsersRepository) => new ApiAuthMockStrategy(email, usersRepository),
|
||||
inject: [UsersRepository],
|
||||
});
|
||||
Reference in new issue
Block a user