Skip to content

Commit

Permalink
(feature) add delete key handler and endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarmlessPyro committed Sep 28, 2022
1 parent c8ef135 commit 453f529
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
25 changes: 25 additions & 0 deletions backend/src/api/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiResponseHandler from "api-response-handler"
import { Request, Response } from "express"
import { AppDataSource } from "data-source"
import { ApiKey } from "models"
import Error404NotFound from "errors/error-404-not-found"

export const listKeys = async (
req: Request,
Expand All @@ -23,4 +24,28 @@ export const createKey = async (
return ApiResponseHandler.success(res, {
apiKey: key.apiKey
})
}

export const deleteKey = async (
req: Request,
res: Response,
): Promise<void> => {
const { name: keyName } = req.params

let del_resp = await AppDataSource
.createQueryBuilder()
.delete()
.from(ApiKey)
.where("name = :name", { name: keyName })
.execute()

if (del_resp.affected != 0) {
return ApiResponseHandler.success(res, {
"status": "OK"
})
} else {
return ApiResponseHandler.error(res, new Error404NotFound(`Did not find any matching keys for ${keyName}`))
}


}
3 changes: 2 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { getSensitiveDataSummaryHandler } from "api/data-field/sensitive-data"
import { getVulnerabilitySummaryHandler } from "api/alert/vulnerability"
import { getAttacksHandler } from "api/attacks"
import { inSandboxMode } from "utils"
import { createKey, listKeys } from "api/keys"
import { createKey, deleteKey, listKeys } from "api/keys"

const app: Express = express()
const port = process.env.PORT || 8080
Expand Down Expand Up @@ -133,6 +133,7 @@ app.get("/api/v1/attacks", getAttacksHandler)

app.get("/api/v1/keys/list", listKeys)
app.post("/api/v1/keys/create", createKey)
app.delete("/api/v1/keys/:name/delete", deleteKey)

const initInstanceSettings = async () => {
const settingRepository = AppDataSource.getRepository(InstanceSettings)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ApiKey extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
uuid: string

@Column({ type: "text" })
@Column({ type: "text", unique: true })
name: string

@Column({ type: "uuid", nullable: false, unique: true })
Expand Down

0 comments on commit 453f529

Please sign in to comment.