Formatting

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-02-29 22:07:37 +01:00
parent 6255dc1c2f
commit 3de7ca94a2
4 changed files with 290 additions and 278 deletions

View File

@ -1,12 +1,12 @@
use anyhow::anyhow; use anyhow::anyhow;
use isahc::{ReadResponseExt, RequestExt};
use isahc::http::StatusCode; use isahc::http::StatusCode;
use isahc::{ReadResponseExt, RequestExt};
use rsa::pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, LineEnding}; use rsa::pkcs1::{DecodeRsaPrivateKey, EncodeRsaPrivateKey, LineEnding};
use rsa::pkcs8::EncodePublicKey; use rsa::pkcs8::EncodePublicKey;
use rsa::RsaPrivateKey; use rsa::RsaPrivateKey;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{BASE_URL, BunqClient, deserialize_retarded_response, sign}; use crate::{deserialize_retarded_response, sign, BunqClient, BASE_URL};
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
struct AppState { struct AppState {
@ -22,11 +22,17 @@ pub struct BunqConfig {
impl BunqConfig { impl BunqConfig {
pub fn load() -> anyhow::Result<BunqConfig> { pub fn load() -> anyhow::Result<BunqConfig> {
println!("Loading config file from {}", confy::get_configuration_file_path("bunq-rs", "bunq-rs")?.to_string_lossy()); println!(
"Loading config file from {}",
confy::get_configuration_file_path("bunq-rs", "bunq-rs")?.to_string_lossy()
);
Ok(confy::load("bunq-rs", "bunq-rs")?) Ok(confy::load("bunq-rs", "bunq-rs")?)
} }
pub fn save(&self) -> anyhow::Result<()> { pub fn save(&self) -> anyhow::Result<()> {
println!("Storing config file in {}", confy::get_configuration_file_path("bunq-rs", None)?.to_string_lossy()); println!(
"Storing config file in {}",
confy::get_configuration_file_path("bunq-rs", None)?.to_string_lossy()
);
confy::store("bunq-rs", "bunq-rs", self)?; confy::store("bunq-rs", "bunq-rs", self)?;
Ok(()) Ok(())
} }
@ -41,7 +47,9 @@ impl BunqConfig {
let bits = 2048; let bits = 2048;
let keypair = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key"); let keypair = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let pem_public = keypair.to_public_key().to_public_key_pem(LineEnding::CRLF)?; let pem_public = keypair
.to_public_key()
.to_public_key_pem(LineEnding::CRLF)?;
let body = Installation { let body = Installation {
client_public_key: &pem_public, client_public_key: &pem_public,
@ -50,7 +58,7 @@ impl BunqConfig {
format!("{}/v1/installation", BASE_URL), format!("{}/v1/installation", BASE_URL),
serde_json::to_string(&body)?, serde_json::to_string(&body)?,
)? )?
.text()?; .text()?;
let response: InstallationResponse = deserialize_retarded_response(&response)?.response; let response: InstallationResponse = deserialize_retarded_response(&response)?.response;
let token = response.token.token; let token = response.token.token;
@ -72,7 +80,10 @@ impl BunqConfig {
return Err(anyhow!(response_text)); return Err(anyhow!(response_text));
} }
self.state = Some(AppState { pem_private: keypair.to_pkcs1_pem(LineEnding::CRLF)?.to_string(), token }); self.state = Some(AppState {
pem_private: keypair.to_pkcs1_pem(LineEnding::CRLF)?.to_string(),
token,
});
self.save()?; self.save()?;
keypair keypair

View File

@ -1,8 +1,8 @@
use anyhow::Result; use anyhow::Result;
use rsa::pkcs1v15::SigningKey; use rsa::pkcs1v15::SigningKey;
use rsa::RsaPrivateKey;
use rsa::sha2::Sha256; use rsa::sha2::Sha256;
use rsa::signature::RandomizedSigner; use rsa::signature::RandomizedSigner;
use rsa::RsaPrivateKey;
use serde::{de::DeserializeOwned, Deserialize}; use serde::{de::DeserializeOwned, Deserialize};
pub use config::BunqConfig; pub use config::BunqConfig;
@ -94,7 +94,7 @@ fn deserialize_normal_response<T: DeserializeOwned>(r: &str) -> Result<Response<
} }
fn sign(body: &str, key: &RsaPrivateKey) -> Result<String> { fn sign(body: &str, key: &RsaPrivateKey) -> Result<String> {
use base64::prelude::{BASE64_STANDARD, Engine}; use base64::prelude::{Engine, BASE64_STANDARD};
let signing_key = SigningKey::<Sha256>::new(key.clone()); let signing_key = SigningKey::<Sha256>::new(key.clone());
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();

View File

@ -1,17 +1,17 @@
use isahc::{ReadResponseExt, RequestExt}; use isahc::{ReadResponseExt, RequestExt};
use serde::Deserialize; use serde::Deserialize;
use crate::{BASE_URL, BunqClient}; use crate::{BunqClient, BASE_URL};
pub(super) fn get(client: &BunqClient) -> anyhow::Result<Vec<MonetaryAccount>> { pub(super) fn get(client: &BunqClient) -> anyhow::Result<Vec<MonetaryAccount>> {
let response = isahc::http::Request::get(format!( let response = isahc::http::Request::get(format!(
"{}/v1/user/{}/monetary-account", "{}/v1/user/{}/monetary-account",
BASE_URL, client.user_id BASE_URL, client.user_id
)) ))
.header("X-Bunq-Client-Authentication", &client.token) .header("X-Bunq-Client-Authentication", &client.token)
.body(())? .body(())?
.send()? .send()?
.text()?; .text()?;
let accounts = crate::deserialize_normal_response::<Vec<MonetaryAccount>>(&response)?; let accounts = crate::deserialize_normal_response::<Vec<MonetaryAccount>>(&response)?;

View File

@ -2,13 +2,14 @@ use anyhow::Result;
use isahc::{ReadResponseExt, RequestExt}; use isahc::{ReadResponseExt, RequestExt};
use serde::Deserialize; use serde::Deserialize;
use crate::{BASE_URL, BunqClient, MonetaryAccount, Pagination, Response}; use crate::{BunqClient, MonetaryAccount, Pagination, Response, BASE_URL};
pub(super) fn get_from_to(client: &BunqClient, pub(super) fn get_from_to(
acc: &MonetaryAccount, client: &BunqClient,
from: Option<i64>, acc: &MonetaryAccount,
to: Option<i64>) -> Result<Vec<Payment>> from: Option<i64>,
{ to: Option<i64>,
) -> Result<Vec<Payment>> {
let next_page = |url: &str| -> Result<(_, _)> { let next_page = |url: &str| -> Result<(_, _)> {
let response = isahc::http::Request::get(url) let response = isahc::http::Request::get(url)
.header("X-Bunq-Client-Authentication", &client.token) .header("X-Bunq-Client-Authentication", &client.token)
@ -46,9 +47,9 @@ pub(super) fn get_from_to(client: &BunqClient,
all.append(&mut payments); all.append(&mut payments);
dbg!(&pag); dbg!(&pag);
if let Some(Pagination { if let Some(Pagination {
older_url: Some(older_url), older_url: Some(older_url),
.. ..
}) = pag }) = pag
{ {
if let (Some(latest), Some(from)) = (all.last(), from) { if let (Some(latest), Some(from)) = (all.last(), from) {
if latest.id <= from { if latest.id <= from {