-
Notifications
You must be signed in to change notification settings - Fork 0
[1b] Google OAuth sign-in + JWT session #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,196
−19
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
306d77a
feat(api): Google OAuth sign-in + JWT session
cursoragent 56972a2
fix(api): address CodeRabbit review — security hardening + contract f…
cursoragent 30d8d42
fix(api): address second CodeRabbit review — atomicity, PII, org uniq…
cursoragent 4513920
ci: trigger CI on develop branch as well as main
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Database | ||
| DATABASE_URL="postgresql://user:password@localhost:5432/cortex" | ||
|
|
||
| # Google OAuth 2.0 | ||
| # Create credentials at https://console.cloud.google.com/apis/credentials | ||
| GOOGLE_CLIENT_ID="your-google-client-id.apps.googleusercontent.com" | ||
| GOOGLE_CLIENT_SECRET="your-google-client-secret" | ||
| GOOGLE_CALLBACK_URL="http://localhost:4000/auth/google/callback" | ||
|
|
||
| # JWT | ||
| # Use a long, random secret in production: `openssl rand -base64 64` | ||
| JWT_SECRET="changeme-dev-secret" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * Jest manual mock for the Prisma generated client (`db/client`). | ||
| * | ||
| * Used in all unit and integration tests so that module resolution doesn't | ||
| * attempt to load the ESM-only generated Prisma files. | ||
| */ | ||
|
|
||
| export const mockPrismaClient = { | ||
| $connect: jest.fn().mockResolvedValue(undefined), | ||
| $disconnect: jest.fn().mockResolvedValue(undefined), | ||
| user: { | ||
| findUnique: jest.fn(), | ||
| create: jest.fn(), | ||
| upsert: jest.fn(), | ||
| }, | ||
| organization: { | ||
| findUnique: jest.fn(), | ||
| findFirst: jest.fn(), | ||
| }, | ||
| }; | ||
|
|
||
| export class PrismaClient { | ||
| $connect = mockPrismaClient.$connect; | ||
| $disconnect = mockPrismaClient.$disconnect; | ||
| user = mockPrismaClient.user; | ||
| organization = mockPrismaClient.organization; | ||
| } | ||
|
|
||
| export const Prisma = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { MCPModule } from "./mcp/mcp.module"; | ||
| import { AuthModule } from "./auth/auth.module"; | ||
| import { PrismaModule } from "./prisma/prisma.module"; | ||
|
|
||
| @Module({ | ||
| imports: [MCPModule], | ||
| imports: [PrismaModule, AuthModule, MCPModule], | ||
| }) | ||
| export class AppModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Req, | ||
| Res, | ||
| UseGuards, | ||
| HttpCode, | ||
| HttpStatus, | ||
| } from "@nestjs/common"; | ||
| import type { Request, Response } from "express"; | ||
| import { AuthService } from "./auth.service"; | ||
| import { UserService } from "./user.service"; | ||
| import { GoogleAuthGuard } from "./guards/google-auth.guard"; | ||
| import { JwtAuthGuard } from "./guards/jwt-auth.guard"; | ||
| import type { AuthenticatedUser } from "./auth.types"; | ||
|
|
||
| interface RequestWithUser extends Request { | ||
| user: AuthenticatedUser & { googleSub: string }; | ||
| } | ||
|
|
||
| interface RequestWithJwtUser extends Request { | ||
| user: AuthenticatedUser; | ||
| } | ||
|
|
||
| @Controller("auth") | ||
| export class AuthController { | ||
| constructor( | ||
| private readonly authService: AuthService, | ||
| private readonly userService: UserService, | ||
| ) {} | ||
|
|
||
| /** Initiates the Google OAuth consent screen redirect. */ | ||
| @Get("google") | ||
| @UseGuards(GoogleAuthGuard) | ||
| googleLogin(): void { | ||
| // Guard redirects to Google – no body needed. | ||
| } | ||
|
|
||
| /** | ||
| * Google calls back here after the user grants consent. | ||
| * findOrCreate throws UnauthorizedException when no organization is | ||
| * provisioned for the user's email domain, so a JWT is only issued after | ||
| * a valid organizationId is confirmed. | ||
| */ | ||
| @Get("google/callback") | ||
| @UseGuards(GoogleAuthGuard) | ||
| async googleCallback( | ||
| @Req() req: RequestWithUser, | ||
| @Res() res: Response, | ||
| ): Promise<void> { | ||
| const { googleSub, email } = req.user; | ||
|
|
||
| const dbUser = await this.userService.findOrCreate({ googleSub, email }); | ||
|
|
||
| const authenticatedUser: AuthenticatedUser = { | ||
| id: dbUser.id, | ||
| email: dbUser.email, | ||
| organizationId: dbUser.organizationId, | ||
| role: dbUser.role, | ||
| }; | ||
|
|
||
| const token = this.authService.issueToken(authenticatedUser); | ||
|
|
||
| res.json({ accessToken: token }); | ||
| } | ||
| } | ||
|
|
||
| @Controller("api") | ||
| export class MeController { | ||
| /** Returns the identity of the currently authenticated user. */ | ||
| @Get("me") | ||
| @UseGuards(JwtAuthGuard) | ||
| @HttpCode(HttpStatus.OK) | ||
| getMe(@Req() req: RequestWithJwtUser): AuthenticatedUser { | ||
| return req.user; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { JwtModule } from "@nestjs/jwt"; | ||
| import { PassportModule } from "@nestjs/passport"; | ||
| import { AuthService } from "./auth.service"; | ||
| import { UserService } from "./user.service"; | ||
| import { AuthController, MeController } from "./auth.controller"; | ||
| import { GoogleStrategy } from "./strategies/google.strategy"; | ||
| import { JwtStrategy } from "./strategies/jwt.strategy"; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| PassportModule.register({ defaultStrategy: "jwt" }), | ||
| JwtModule.registerAsync({ | ||
| useFactory: () => { | ||
| const secret = process.env["JWT_SECRET"]; | ||
| if (!secret) { | ||
| throw new Error("JWT_SECRET environment variable is required"); | ||
| } | ||
| return { secret, signOptions: { expiresIn: "8h" } }; | ||
| }, | ||
| }), | ||
| ], | ||
| controllers: [AuthController, MeController], | ||
| providers: [AuthService, UserService, GoogleStrategy, JwtStrategy], | ||
| exports: [AuthService, JwtModule], | ||
| }) | ||
| export class AuthModule {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.