Skip to content

Commit

Permalink
(feature) Add option to list and add api keys from api
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarmlessPyro committed Sep 28, 2022
1 parent 0fe61ca commit bc2d4db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions backend/src/api/keys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ApiResponseHandler from "api-response-handler"
import { Request, Response } from "express"
import { AppDataSource } from "data-source"
import { ApiKey } from "models"

export const listKeys = async (
req: Request,
res: Response,
): Promise<void> => {
const keys = await AppDataSource.getRepository(ApiKey).find()
return ApiResponseHandler.success(res, keys.map((v) => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" + v.apiKey.slice(-4)))
}

export const createKey = async (
req: Request,
res: Response,
): Promise<void> => {
const { name: keyName } = req.body
const newKey = ApiKey.create({ name: keyName })
const key = await AppDataSource.getRepository(ApiKey).save(
newKey,
)
return ApiResponseHandler.success(res, {
apiKey: key.apiKey
})
}
7 changes: 5 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +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"

const app: Express = express()
const port = process.env.PORT || 8080
Expand Down Expand Up @@ -130,6 +131,9 @@ app.delete("/api/v1/test/:uuid/delete", deleteTest)

app.get("/api/v1/attacks", getAttacksHandler)

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

const initInstanceSettings = async () => {
const settingRepository = AppDataSource.getRepository(InstanceSettings)
const numSettings = await settingRepository.count()
Expand All @@ -144,8 +148,7 @@ const main = async () => {
try {
const datasource = await AppDataSource.initialize()
console.log(
`Is AppDataSource Initialized? ${
datasource.isInitialized ? "Yes" : "No"
`Is AppDataSource Initialized? ${datasource.isInitialized ? "Yes" : "No"
}`,
)
await initInstanceSettings()
Expand Down

0 comments on commit bc2d4db

Please sign in to comment.