Skip to content

Commit

Permalink
implement write to database
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Aug 12, 2024
1 parent 32be7d3 commit 24dbea2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE "users"
(
user_id BLOB PRIMARY KEY NOT NULL,
keyset TEXT NOT NULL
pubkeys TEXT NOT NULL
) STRICT;
3 changes: 2 additions & 1 deletion apps/identity_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod jwk;
mod uuid;
pub mod v1;

mod uuid;

use axum::routing::get;
use color_eyre::eyre::Context as _;
use tower_http::trace::TraceLayer;
Expand Down
45 changes: 43 additions & 2 deletions apps/identity_server/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use axum::{
};
use color_eyre::eyre::Context as _;
use jose_jwk::{Jwk, JwkSet};
use tracing::{debug, error};
use uuid::Uuid;

use crate::uuid::UuidProvider;
Expand Down Expand Up @@ -54,9 +55,46 @@ impl RouterConfig {
}))
}
}
async fn create(state: State<RouterState>, _pubkey: Json<Jwk>) -> Redirect {

#[derive(thiserror::Error, Debug)]
enum CreateErr {
#[error(transparent)]
Internal(#[from] color_eyre::Report),
}

impl IntoResponse for CreateErr {
fn into_response(self) -> axum::response::Response {
error!("{self:?}");
match self {
Self::Internal(err) => {
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response()
}
}
}
}

#[tracing::instrument(skip_all)]
async fn create(
state: State<RouterState>,
pubkey: Json<Jwk>,
) -> Result<Redirect, CreateErr> {
let uuid = state.uuid_provider.next_v4();
Redirect::to(&format!("/users/{}/did.json", uuid.as_hyphenated()))
let jwks = JwkSet {
keys: vec![pubkey.0],
};
let serialized_jwks = serde_json::to_string(&jwks).expect("infallible");

sqlx::query("INSERT INTO users (user_id, pubkeys) VALUES ($1, $2)")
.bind(uuid)
.bind(serialized_jwks)
.execute(&state.db_pool)
.await
.wrap_err("failed to insert identity into db")?;

Ok(Redirect::to(&format!(
"/users/{}/did.json",
uuid.as_hyphenated()
)))
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -69,6 +107,7 @@ enum ReadErr {

impl IntoResponse for ReadErr {
fn into_response(self) -> axum::response::Response {
error!("{self:?}");
match self {
ReadErr::NoSuchUser => {
(StatusCode::NOT_FOUND, self.to_string()).into_response()
Expand All @@ -80,6 +119,7 @@ impl IntoResponse for ReadErr {
}
}

#[tracing::instrument(skip_all)]
async fn read(
state: State<RouterState>,
Path(user_id): Path<Uuid>,
Expand All @@ -96,5 +136,6 @@ async fn read(
// TODO: Do we actually care about round-trip validating the JwkSet here?
let keyset: JwkSet = serde_json::from_str(&keyset_in_string)
.wrap_err("failed to deserialize JwkSet from database")?;

Ok(Json(keyset))
}

0 comments on commit 24dbea2

Please sign in to comment.