Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: oauth

Nevo Davide4c4d6470c69
diff --git a/apps/backend/src/api/api.module.ts b/apps/backend/src/api/api.module.ts
index 806fb662d..1f89b42c8 100644
--- a/apps/backend/src/api/api.module.ts
+++ b/apps/backend/src/api/api.module.ts
@@ -35,7 +35,7 @@ import { NoAuthIntegrationsController } from '@gitroom/backend/api/routes/no.aut
import { EnterpriseController } from '@gitroom/backend/api/routes/enterprise.controller';
import { OAuthAppController } from '@gitroom/backend/api/routes/oauth-app.controller';
import { ApprovedAppsController } from '@gitroom/backend/api/routes/approved-apps.controller';
-import { OAuthController } from '@gitroom/backend/api/routes/oauth.controller';
+import { OAuthController, OAuthAuthorizedController } from '@gitroom/backend/api/routes/oauth.controller';
const authenticatedController = [
UsersController,
@@ -54,6 +54,7 @@ const authenticatedController = [
ThirdPartyController,
OAuthAppController,
ApprovedAppsController,
+ OAuthAuthorizedController,
];
@Module({
imports: [UploadModule],
diff --git a/apps/backend/src/api/routes/oauth.controller.ts b/apps/backend/src/api/routes/oauth.controller.ts
index c10bf6c07..080558f87 100644
--- a/apps/backend/src/api/routes/oauth.controller.ts
+++ b/apps/backend/src/api/routes/oauth.controller.ts
@@ -6,25 +6,19 @@ import {
HttpStatus,
Post,
Query,
- Req,
- Res,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
-import { Request, Response } from 'express';
import { OAuthService } from '@gitroom/nestjs-libraries/database/prisma/oauth/oauth.service';
-import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
-import { AuthService } from '@gitroom/helpers/auth/auth.service';
-import { User } from '@prisma/client';
+import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request';
+import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
+import { User, Organization } from '@prisma/client';
import { AuthorizeOAuthQueryDto, ApproveOAuthDto } from '@gitroom/nestjs-libraries/dtos/oauth/authorize-oauth.dto';
import { TokenExchangeDto } from '@gitroom/nestjs-libraries/dtos/oauth/token-exchange.dto';
@ApiTags('OAuth')
@Controller('/oauth')
export class OAuthController {
- constructor(
- private _oauthService: OAuthService,
- private _organizationService: OrganizationService
- ) {}
+ constructor(private _oauthService: OAuthService) {}
@Get('/authorize')
async authorize(@Query() query: AuthorizeOAuthQueryDto) {
@@ -44,27 +38,34 @@ export class OAuthController {
};
}
+ @Post('/token')
+ async token(@Body() body: TokenExchangeDto) {
+ if (body.grant_type !== 'authorization_code') {
+ throw new HttpException(
+ { error: 'unsupported_grant_type' },
+ HttpStatus.BAD_REQUEST
+ );
+ }
+
+ return this._oauthService.exchangeCodeForToken(
+ body.code,
+ body.client_id,
+ body.client_secret
+ );
+ }
+}
+
+@ApiTags('OAuth')
+@Controller('/oauth')
+export class OAuthAuthorizedController {
+ constructor(private _oauthService: OAuthService) {}
+
@Post('/authorize')
async approveOrDeny(
@Body() body: ApproveOAuthDto,
- @Req() req: Request,
- @Res({ passthrough: true }) res: Response
+ @GetUserFromRequest() user: User,
+ @GetOrgFromRequest() org: Organization
) {
- const auth = (req.headers as any).auth || req.cookies.auth;
- if (!auth) {
- throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
- }
-
- let user: User;
- try {
- user = AuthService.verifyJWT(auth) as User;
- if (!user) {
- throw new Error();
- }
- } catch {
- throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
- }
-
const app = await this._oauthService.validateAuthorizationRequest(
body.client_id
);
@@ -78,15 +79,6 @@ export class OAuthController {
return { redirect: redirectUrl.toString() };
}
- const orgs = await this._organizationService.getOrgsByUserId(user.id);
- const org = orgs.find((o: any) => !o.users?.[0]?.disabled);
- if (!org) {
- throw new HttpException(
- 'No active organization found',
- HttpStatus.BAD_REQUEST
- );
- }
-
const code = await this._oauthService.createAuthorizationCode(
app.id,
user.id,
@@ -100,20 +92,4 @@ export class OAuthController {
}
return { redirect: redirectUrl.toString() };
}
-
- @Post('/token')
- async token(@Body() body: TokenExchangeDto) {
- if (body.grant_type !== 'authorization_code') {
- throw new HttpException(
- { error: 'unsupported_grant_type' },
- HttpStatus.BAD_REQUEST
- );
- }
-
- return this._oauthService.exchangeCodeForToken(
- body.code,
- body.client_id,
- body.client_secret
- );
- }
}