Skip to content

Commit

Permalink
add /api/delete endpoint and DeleteResponse model
Browse files Browse the repository at this point in the history
  • Loading branch information
wleightond committed Jun 13, 2024
1 parent e395325 commit a579213
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions canarytokens/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,10 @@ class SettingsResponse(BaseModel):
message: Literal["success", "failure"]


class DeleteResponse(BaseModel):
message: Literal["success", "failure"]


class ManageTokenSettingsRequest(BaseModel):
token: str
auth: str
Expand Down
18 changes: 16 additions & 2 deletions frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
ClonedWebTokenResponse,
CSSClonedWebTokenRequest,
CSSClonedWebTokenResponse,
DeleteResponse,
DownloadCSSClonedWebRequest,
DownloadCSSClonedWebResponse,
CMDTokenRequest,
Expand Down Expand Up @@ -270,6 +271,7 @@ def capture_exception(error: BaseException, context: tuple[str, Any]):


async def _parse_for_x(request: Request, expected_type: Any) -> Any:
data: Any
if request.headers.get("Content-Type", "application/json") == "application/json":
if all([o in request.query_params.keys() for o in ["token", "auth"]]):
data = dict(request.query_params.items())
Expand Down Expand Up @@ -388,7 +390,9 @@ async def generate(request: Request) -> AnyTokenResponse: # noqa: C901 # gen i
)

try:
token_request_details = parse_obj_as(AnyTokenRequest, token_request_data)
token_request_details: AnyTokenRequest = parse_obj_as(
AnyTokenRequest, token_request_data
)
except ValidationError: # DESIGN: can we specialise on what went wrong?
return response_error(1, "Malformed request, invalid data supplied.")

Expand Down Expand Up @@ -827,7 +831,7 @@ async def api_manage_canarytoken(token: str, auth: str) -> ManageResponse:
tags=["Canarytokens History"],
response_model=HistoryResponse,
)
async def api_history(token: str, auth: str) -> JSONResponse:
async def api_history(token: str, auth: str) -> HistoryResponse:
canarydrop = get_canarydrop_and_authenticate(token=token, auth=auth)
response = {
"canarydrop": canarydrop,
Expand All @@ -837,6 +841,16 @@ async def api_history(token: str, auth: str) -> JSONResponse:
return HistoryResponse(**response)


@api.post("/delete", response_model=DeleteResponse)
async def api_delete(request: Request) -> DeleteResponse:
data = await request.json()
token = data.get("token", "")
auth = data.get("auth", "")
canarydrop = get_canarydrop_and_authenticate(token=token, auth=auth)
queries.delete_canarydrop(canarydrop)
return DeleteResponse(message="success")


@api.post(
"/settings",
tags=["Canarytokens Settings"],
Expand Down

0 comments on commit a579213

Please sign in to comment.