Skip to content

Commit

Permalink
fix: expiring incorrect key (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdtfh authored Nov 22, 2023
1 parent 39a6139 commit a605d54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ H -- Response provided --> C
- `GET /request/:id`: Called by World App. Used to fetch the proof verification request. One time use.
- `PUT /response/:id`: Called by World App. Used to send the proof back to the application.
- `GET /response/:id`: Called by IDKit. Continuous pulling to fetch the status of the request and the response if available. Response can only be retrieved once.

## Local Development

An easy way to run is using a Dockerized Redis:

```
docker run -d -p 6379:6379 redis
```
31 changes: 21 additions & 10 deletions src/routes/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,38 @@ async fn insert_response(
Extension(mut redis): Extension<ConnectionManager>,
TemporaryForceDecodeJson(request): TemporaryForceDecodeJson<RequestPayload>,
) -> Result<StatusCode, StatusCode> {
//ANCHOR - Store the response
//ANCHOR - Check the request is valid
if !redis
.set_nx::<_, _, bool>(
format!("{RES_PREFIX}{request_id}"),
serde_json::to_vec(&request).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
)
.exists::<_, bool>(format!("{REQ_STATUS_PREFIX}{request_id}"))
.await
.map_err(handle_redis_error)?
{
return Err(StatusCode::BAD_REQUEST);
}

//ANCHOR - Check the response has not been set already
if redis
.exists::<_, bool>(format!("{RES_PREFIX}{request_id}"))
.await
.map_err(handle_redis_error)?
{
return Ok(StatusCode::CONFLICT);
return Err(StatusCode::CONFLICT);
}

//ANCHOR - Store the response
redis
.expire::<_, ()>(&request_id.to_string(), EXPIRE_AFTER_SECONDS)
.set_ex::<_, _, ()>(
format!("{RES_PREFIX}{request_id}"),
serde_json::to_vec(&request).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
EXPIRE_AFTER_SECONDS,
)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
.map_err(handle_redis_error)?;

//ANCHOR - Delete status
//NOTE - We can delete the status now as the presence of a response implies the request is complete
//NOTE - We can delete the status at this point as the presence of a response implies the request is complete
redis
.del::<_, Option<Vec<u8>>>(format!("{REQ_STATUS_PREFIX}{request_id}"))
.del(format!("{REQ_STATUS_PREFIX}{request_id}"))
.await
.map_err(handle_redis_error)?;

Expand Down

0 comments on commit a605d54

Please sign in to comment.