-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding support for
hopp-cli
in self-host Hoppscotch (#4079)
* feat: created a new table to store user PATs * chore: renamed UserTokens table name to PersonalAccessToken * chore: added unique property to token column * chore: made expiresOn field optional * chore: created access-token module * feat: created access-token rest routes * chore: created a new auth guard for PATs * chore: scaffolded routes in team collection and environments modules for CLI * chore: created method to update lastUsedOn property for accessTokens * chore: created interceptor to update lastUsedOn property of PAT * feat: interceptor to update lastUpdatedOn property complete * chore: removed unused imports in access-token interceptor * chore: moved routes to fetch collection and environment into access-token module * feat: added routes to fetch collections and environments for CLI * chore: modified access-token interceptor * chore: removed log statement from interceptor * chore: added team member checking logic to ForCLI methods in team collection and environments module * chore: changed return error messages to those defined in spec * chore: added comments to all service methods * chore: removed unused imports * chore: updated testcases for team-environments module service file * chore: added and updated testcases * chore: removed unneseccary SQL from auto-generated migration sql for PAT * chore: remobed JWTAuthGuard from relevant routes in PAT controllers file * chore: modified token for auth in PATAuthGuard * chore: changed error codes in some certain service methods in access-token module * feat: worked on feedback for PR review * chore: renamed service method in access-token module * chore: removed console log statements * chore: modified cli error type * test: fix broken test case * chore: changed target of hopp-old-backend to prod --------- Co-authored-by: mirarifhasan <arif.ishan05@gmail.com>
- Loading branch information
1 parent
f4f3fdf
commit 4bd23a8
Showing
20 changed files
with
983 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
19 changes: 19 additions & 0 deletions
19
.../hoppscotch-backend/prisma/migrations/20240520091033_personal_access_tokens/migration.sql
This file contains 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,19 @@ | ||
|
||
-- CreateTable | ||
CREATE TABLE "PersonalAccessToken" ( | ||
"id" TEXT NOT NULL, | ||
"userUid" TEXT NOT NULL, | ||
"label" TEXT NOT NULL, | ||
"token" TEXT NOT NULL, | ||
"expiresOn" TIMESTAMP(3), | ||
"createdOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedOn" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "PersonalAccessToken_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "PersonalAccessToken_token_key" ON "PersonalAccessToken"("token"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "PersonalAccessToken" ADD CONSTRAINT "PersonalAccessToken_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains 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
107 changes: 107 additions & 0 deletions
107
packages/hoppscotch-backend/src/access-token/access-token.controller.ts
This file contains 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,107 @@ | ||
import { | ||
BadRequestException, | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpStatus, | ||
Param, | ||
ParseIntPipe, | ||
Post, | ||
Query, | ||
UseGuards, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { AccessTokenService } from './access-token.service'; | ||
import { CreateAccessTokenDto } from './dto/create-access-token.dto'; | ||
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard'; | ||
import * as E from 'fp-ts/Either'; | ||
import { throwHTTPErr } from 'src/utils'; | ||
import { GqlUser } from 'src/decorators/gql-user.decorator'; | ||
import { AuthUser } from 'src/types/AuthUser'; | ||
import { ThrottlerBehindProxyGuard } from 'src/guards/throttler-behind-proxy.guard'; | ||
import { PATAuthGuard } from 'src/guards/rest-pat-auth.guard'; | ||
import { AccessTokenInterceptor } from 'src/interceptors/access-token.interceptor'; | ||
import { TeamEnvironmentsService } from 'src/team-environments/team-environments.service'; | ||
import { TeamCollectionService } from 'src/team-collection/team-collection.service'; | ||
import { ACCESS_TOKENS_INVALID_DATA_ID } from 'src/errors'; | ||
import { createCLIErrorResponse } from './helper'; | ||
|
||
@UseGuards(ThrottlerBehindProxyGuard) | ||
@Controller({ path: 'access-tokens', version: '1' }) | ||
export class AccessTokenController { | ||
constructor( | ||
private readonly accessTokenService: AccessTokenService, | ||
private readonly teamCollectionService: TeamCollectionService, | ||
private readonly teamEnvironmentsService: TeamEnvironmentsService, | ||
) {} | ||
|
||
@Post('create') | ||
@UseGuards(JwtAuthGuard) | ||
async createPAT( | ||
@GqlUser() user: AuthUser, | ||
@Body() createAccessTokenDto: CreateAccessTokenDto, | ||
) { | ||
const result = await this.accessTokenService.createPAT( | ||
createAccessTokenDto, | ||
user, | ||
); | ||
if (E.isLeft(result)) throwHTTPErr(result.left); | ||
return result.right; | ||
} | ||
|
||
@Delete('revoke') | ||
@UseGuards(JwtAuthGuard) | ||
async deletePAT(@Query('id') id: string) { | ||
const result = await this.accessTokenService.deletePAT(id); | ||
|
||
if (E.isLeft(result)) throwHTTPErr(result.left); | ||
return result.right; | ||
} | ||
|
||
@Get('list') | ||
@UseGuards(JwtAuthGuard) | ||
async listAllUserPAT( | ||
@GqlUser() user: AuthUser, | ||
@Query('offset', ParseIntPipe) offset: number, | ||
@Query('limit', ParseIntPipe) limit: number, | ||
) { | ||
return await this.accessTokenService.listAllUserPAT( | ||
user.uid, | ||
offset, | ||
limit, | ||
); | ||
} | ||
|
||
@Get('collection/:id') | ||
@UseGuards(PATAuthGuard) | ||
@UseInterceptors(AccessTokenInterceptor) | ||
async fetchCollection(@GqlUser() user: AuthUser, @Param('id') id: string) { | ||
const res = await this.teamCollectionService.getCollectionForCLI( | ||
id, | ||
user.uid, | ||
); | ||
|
||
if (E.isLeft(res)) | ||
throw new BadRequestException( | ||
createCLIErrorResponse(ACCESS_TOKENS_INVALID_DATA_ID), | ||
); | ||
return res.right; | ||
} | ||
|
||
@Get('environment/:id') | ||
@UseGuards(PATAuthGuard) | ||
@UseInterceptors(AccessTokenInterceptor) | ||
async fetchEnvironment(@GqlUser() user: AuthUser, @Param('id') id: string) { | ||
const res = await this.teamEnvironmentsService.getTeamEnvironmentForCLI( | ||
id, | ||
user.uid, | ||
); | ||
|
||
if (E.isLeft(res)) | ||
throw new BadRequestException( | ||
createCLIErrorResponse(ACCESS_TOKENS_INVALID_DATA_ID), | ||
); | ||
return res.right; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/hoppscotch-backend/src/access-token/access-token.module.ts
This file contains 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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AccessTokenController } from './access-token.controller'; | ||
import { PrismaModule } from 'src/prisma/prisma.module'; | ||
import { AccessTokenService } from './access-token.service'; | ||
import { TeamCollectionModule } from 'src/team-collection/team-collection.module'; | ||
import { TeamEnvironmentsModule } from 'src/team-environments/team-environments.module'; | ||
import { TeamModule } from 'src/team/team.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
PrismaModule, | ||
TeamCollectionModule, | ||
TeamEnvironmentsModule, | ||
TeamModule, | ||
], | ||
controllers: [AccessTokenController], | ||
providers: [AccessTokenService], | ||
exports: [AccessTokenService], | ||
}) | ||
export class AccessTokenModule {} |
Oops, something went wrong.