Skip to content

Commit

Permalink
add user set authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Oct 1, 2022
1 parent 1d7e7c3 commit 2837a30
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
17 changes: 17 additions & 0 deletions backend/src/api/get-endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ export const getUsageHandler = async (
await ApiResponseHandler.error(res, err)
}
}

export const updateEndpointIsAuthenticated = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const { endpointId } = req.params
const params: { authenticated: boolean } = req.body
await GetEndpointsService.updateIsAuthenticated(
endpointId,
params.authenticated,
)
await ApiResponseHandler.success(res)
} catch (err) {
await ApiResponseHandler.error(res, err)
}
}
5 changes: 5 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getEndpointsHandler,
getHostsHandler,
getUsageHandler,
updateEndpointIsAuthenticated,
} from "api/get-endpoints"
import {
deleteSpecHandler,
Expand Down Expand Up @@ -97,6 +98,10 @@ app.get("/api/v1/endpoints/hosts", getHostsHandler)
app.get("/api/v1/endpoints", getEndpointsHandler)
app.get("/api/v1/endpoint/:endpointId", getEndpointHandler)
app.get("/api/v1/endpoint/:endpointId/usage", getUsageHandler)
app.put(
"/api/v1/endpoint/:endpointId/authenticated",
updateEndpointIsAuthenticated,
)

app.post("/api/v1/spec/new", MulterSource.single("file"), uploadNewSpecHandler)
app.delete("/api/v1/spec/:specFileName", deleteSpecHandler)
Expand Down
3 changes: 3 additions & 0 deletions backend/src/models/api-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class ApiEndpoint extends BaseEntity {
@Column({ type: "bool", default: true })
isAuthenticatedDetected: boolean

@Column({ type: "bool", nullable: true })
isAuthenticatedUserSet: boolean

@Column({ nullable: true })
openapiSpecName: string

Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/openapi-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class OpenApiSpec extends BaseEntity {
@UpdateDateColumn({ type: "timestamptz" })
updatedAt: Date

@Column({ type: "timestamptz" })
@Column({ type: "timestamptz", nullable: true })
specUpdatedAt: Date

@Column({ type: "enum", enum: SpecExtension, default: SpecExtension.JSON })
Expand Down
11 changes: 11 additions & 0 deletions backend/src/services/get-endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import Error404NotFound from "errors/error-404-not-found"
import { getRiskScore } from "utils"

export class GetEndpointsService {
static async updateIsAuthenticated(
apiEndpointUuid: string,
authenticated: boolean,
): Promise<void> {
await AppDataSource.createQueryBuilder()
.update(ApiEndpoint)
.set({ isAuthenticatedUserSet: authenticated })
.where("uuid = :id", { id: apiEndpointUuid })
.execute()
}

static async updateEndpointRiskScore(
apiEndpointUuid: string,
): Promise<ApiEndpoint> {
Expand Down
1 change: 1 addition & 0 deletions common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export interface ApiEndpoint {
riskScore: RiskScore
openapiSpecName: string
isAuthenticatedDetected: boolean
isAuthenticatedUserSet: boolean
}

export interface ApiEndpointDetailed extends ApiEndpoint {
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ export const getUsage = async (endpointId: string): Promise<Usage[]> => {
return []
}
}

export const updateEndpointAuthenticated = async (
endpointId: string,
authenticated: boolean,
): Promise<void> => {
await axios.put(`${getAPIURL()}/endpoint/${endpointId}/authenticated`, {
authenticated,
})
}
48 changes: 46 additions & 2 deletions frontend/src/components/Endpoint/Overview.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import React from "react"
import React, { useState } from "react"
import { ApiEndpointDetailed, Usage } from "@common/types"
import { Box, Badge, Grid, GridItem, Stack } from "@chakra-ui/react"
import {
Box,
Badge,
Grid,
GridItem,
Stack,
HStack,
Checkbox,
} from "@chakra-ui/react"
import dynamic from "next/dynamic"
import { DataAttribute, DataHeading } from "components/utils/Card"
import EndpointUsageChart from "./UsageChart"
import { RISK_TO_COLOR } from "~/constants"
import EndpointPIIChart from "./PIIChart"
import { getDateTimeString } from "utils"
import { DataTag, Status } from "@common/enums"
import { updateEndpointAuthenticated } from "api/endpoints"

const SpecComponent = dynamic(() => import("./SpecComponent"), { ssr: false })

Expand All @@ -21,6 +30,20 @@ const EndpointOverview: React.FC<EndpointOverviewProps> = React.memo(
const piiFields = endpoint.dataFields.filter(
field => field.dataTag === DataTag.PII,
)
const [authenticated, setAuthenticated] = useState(
endpoint.isAuthenticatedUserSet,
)

const handleAuthenticatedCheck = (
checked: boolean,
authenticated: boolean,
) => {
if (!checked) {
authenticated = null
}
updateEndpointAuthenticated(endpoint.uuid, authenticated)
setAuthenticated(authenticated)
}

return (
<Stack
Expand Down Expand Up @@ -73,6 +96,27 @@ const EndpointOverview: React.FC<EndpointOverviewProps> = React.memo(
{getDateTimeString(endpoint.lastActive) || "N/A"}
</DataAttribute>
</GridItem>
<GridItem>
<DataHeading>Authenticated</DataHeading>
<HStack>
<Checkbox
isChecked={authenticated}
onChange={e =>
handleAuthenticatedCheck(e.target.checked, true)
}
>
Yes
</Checkbox>
<Checkbox
isChecked={authenticated !== null && !authenticated}
onChange={e =>
handleAuthenticatedCheck(e.target.checked, false)
}
>
No
</Checkbox>
</HStack>
</GridItem>
{usage.length > 0 && (
<GridItem w="100%" colSpan={2}>
<DataHeading>Usage</DataHeading>
Expand Down

0 comments on commit 2837a30

Please sign in to comment.