Skip to content

Commit

Permalink
feat: add get connect token endpoint to data api (#162)
Browse files Browse the repository at this point in the history
* feat: adding get connect token endpoint to data api

* bump: flake and cargo

* refa: removing some tracing debugs
  • Loading branch information
nicolasauler authored Nov 29, 2024
1 parent 09c4c38 commit 6da3c8f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 63 deletions.
82 changes: 38 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 42 additions & 4 deletions src/data/openfinance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,53 @@ use crate::{auth::AuthSession, client::pluggy::account::ListAccountsResponse, Ap

pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/api/pluggyconnect/token", get(connect_token))
.route("/api/pluggyconnect/success", post(connect_success))
.route("/api/accounts", get(list_accounts))
.route("/api/transactions", get(list_transactions))
}

#[derive(Deserialize, Serialize)]
#[derive(Serialize)]
struct ConnectTokenResponse {
connect_token: String,
}

async fn connect_token(
auth_session: AuthSession,
State(shared_state): State<Arc<AppState>>,
) -> Result<Json<ConnectTokenResponse>, StatusCode> {
use crate::client::pluggy::auth::CreateConnectTokenOutcome;

let Some(user) = auth_session.user else {
panic!("user not logged in")
};

match crate::client::pluggy::auth::create_connect_token(
&shared_state.pluggy_api_key.lock().await,
"",
&user.email,
)
.await
{
Ok(CreateConnectTokenOutcome::Success(token)) => Ok(Json(ConnectTokenResponse {
connect_token: token.access_token,
})),
Ok(CreateConnectTokenOutcome::NotFound | CreateConnectTokenOutcome::Internal) => {
tracing::error!(user.id, "couldn't create connect token in pluggy");
Err(StatusCode::FAILED_DEPENDENCY)
}
Ok(CreateConnectTokenOutcome::Forbidden) => {
tracing::error!(user.id, "error authenticating with api key");
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
Err(e) => {
tracing::error!(?e, user.id, "error creating connect token");
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}

#[derive(Deserialize)]
struct ConnectRequest {
item_id: Uuid,
}
Expand All @@ -34,7 +75,6 @@ async fn connect_success(
let Some(user) = auth_session.user else {
panic!("user not logged in")
};
tracing::debug!("User logged in");

match crate::features::openfinance::add_item(
&shared_state.pool,
Expand Down Expand Up @@ -69,7 +109,6 @@ async fn list_accounts(
let Some(user) = auth_session.user else {
panic!("user not logged in")
};
tracing::debug!("User logged in");

match crate::client::pluggy::account::list_accounts(
&shared_state.pluggy_api_key.lock().await,
Expand Down Expand Up @@ -98,7 +137,6 @@ async fn list_transactions(
let Some(user) = auth_session.user else {
panic!("user not logged in")
};
tracing::debug!("User logged in");

match crate::client::pluggy::transactions::list_transactions(
&shared_state.pluggy_api_key.lock().await,
Expand Down
2 changes: 0 additions & 2 deletions src/hypermedia/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub async fn mfa_qr(
let Some(user) = auth_session.user else {
return Err((StatusCode::UNAUTHORIZED, [("HX-Redirect", "/auth/signin")]).into_response());
};
tracing::debug!("User logged in");

// TODO: create logic for changing MFA method
if user.otp_enabled {
Expand Down Expand Up @@ -127,7 +126,6 @@ pub async fn mfa_verify(
let Some(user) = auth_session.user else {
return (StatusCode::UNAUTHORIZED, [("HX-Redirect", "/auth/signin")]).into_response();
};
tracing::debug!("User logged in");

let secret = Secret::Encoded(user.otp_secret.unwrap());
let totp = TOTP::new(
Expand Down
1 change: 0 additions & 1 deletion src/hypermedia/service/pluggy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub async fn widget(
let Some(user) = auth_session.user else {
return (StatusCode::UNAUTHORIZED, [("HX-Redirect", "/auth/signin")]).into_response();
};
tracing::debug!("User logged in");

let Ok(webhook_url) = create_user_endpoint(&env.svix_api_key, user.id).await else {
return (
Expand Down

0 comments on commit 6da3c8f

Please sign in to comment.