1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use anyhow::Result;
use isahc::prelude::*;
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::rsa::Rsa;
use openssl::sign::Signer;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

const BASE: &str = "https://api.bunq.com";

#[derive(Serialize)]
struct Installation<'a> {
    client_public_key: &'a str,
}

#[derive(Deserialize)]
struct Token {
    token: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct InstallationResponse {
    token: Token,
}

#[derive(Serialize)]
struct DeviceServer<'a> {
    description: &'a str,
    secret: &'a str,
    permitted_ips: &'a [&'a str],
}

#[derive(Serialize)]
struct SessionServer<'a> {
    secret: &'a str,
}

#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct SessionServerResponse {
    token: Token,
    user_person: UserPerson,
}

#[derive(Deserialize)]
struct UserPerson {
    id: i64,
}

fn sign<K: openssl::pkey::HasPrivate>(body: &str, key: &PKey<K>) -> Result<String> {
    let mut signer = Signer::new(MessageDigest::sha256(), key)?;

    let sig = signer.sign_oneshot_to_vec(body.as_bytes())?;

    Ok(base64::encode(&sig))
}

#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct RawResponse {
    response: Vec<serde_json::Value>,
    pagination: Option<Pagination>,
}

struct Response<T> {
    response: T,
    pagination: Option<Pagination>,
}

impl RawResponse {
    fn decode_retarded<T: DeserializeOwned>(self) -> Result<Response<T>> {
        let mut map = serde_json::Map::new();
        for e in self.response {
            if let serde_json::Value::Object(e) = e {
                let (k, v) = e
                    .into_iter()
                    .next()
                    .ok_or_else(|| anyhow::anyhow!("malformed response"))?;
                map.insert(k, v);
            }
        }
        Ok(Response {
            response: serde_json::from_value(map.into())?,
            pagination: self.pagination,
        })
    }
}

fn deserialize_retarded_response<T: DeserializeOwned>(r: &str) -> Result<Response<T>> {
    let r: RawResponse = serde_json::from_str(r)?;
    r.decode_retarded()
}

fn deserialize_normal_response<T: DeserializeOwned>(r: &str) -> Result<Response<T>> {
    let r: RawResponse = serde_json::from_str(r)?;
    Ok(Response {
        response: serde_json::from_value(r.response.into())?,
        pagination: r.pagination,
    })
}

#[derive(Serialize, Deserialize, Default)]
struct AppState {
    token: String,
    pem_private: String,
}
#[derive(Serialize, Deserialize, Default)]
pub struct BunqConfig {
    api_key: String,
    state: Option<AppState>,
}
pub struct BunqConfigReady {
    token: String,
    keypair: PKey<openssl::pkey::Private>,
    user_id: i64,
}
impl BunqConfig {
    pub fn load() -> Result<BunqConfig> {
        Ok(confy::load("bunq-rs")?)
    }
    pub fn save(&self) -> Result<()> {
        confy::store("bunq-rs", self)?;
        Ok(())
    }
    pub fn install(mut self) -> Result<BunqConfigReady> {
        let api_key = &self.api_key;

        let keypair = if let Some(state) = &self.state {
            PKey::private_key_from_pem(state.pem_private.as_bytes())?
        } else {
            let rsa = Rsa::generate(2048)?;
            let pem_private = rsa.private_key_to_pem()?;
            let pem_private = String::from_utf8(pem_private)?;

            let keypair = PKey::from_rsa(rsa)?;

            let pem_public = String::from_utf8(keypair.public_key_to_pem()?)?;

            let body = Installation {
                client_public_key: &pem_public,
            };
            let response = isahc::post(
                format!("{}/v1/installation", BASE),
                serde_json::to_string(&body)?,
            )?
            .text()?;
            let response: InstallationResponse = deserialize_retarded_response(&response)?.response;
            let token = response.token.token;

            let body = DeviceServer {
                description: "awesome",
                secret: &api_key,
                permitted_ips: &["31.21.118.143", "*"],
            };
            let body = serde_json::to_string(&body)?;
            let mut response = isahc::http::Request::post(format!("{}/v1/device-server", BASE))
                .header("X-Bunq-Client-Authentication", &token)
                .body(body)?
                .send()?;
            println!("{}", response.text()?);

            self.state = Some(AppState { pem_private, token });
            self.save()?;

            keypair
        };
        let token = self.state.unwrap().token;
        let body = SessionServer { secret: &api_key };
        let body = serde_json::to_string(&body)?;
        let sig = sign(&body, &keypair)?;
        let response = isahc::http::Request::post(format!("{}/v1/session-server", BASE))
            .header("X-Bunq-Client-Authentication", &token)
            .header("X-Bunq-Client-Signature", &sig)
            .body(body)?
            .send()?
            .text()?;
        let r: SessionServerResponse = deserialize_retarded_response(&response)?.response;
        Ok(BunqConfigReady {
            keypair,
            token: r.token.token,
            user_id: r.user_person.id,
        })
    }
}

impl BunqConfigReady {
    pub fn monetary_accounts(&self) -> Result<Vec<MonetaryAccountBank>> {
        let response = isahc::http::Request::get(format!(
            "{}/v1/user/{}/monetary-account",
            BASE, self.user_id
        ))
        .header("X-Bunq-Client-Authentication", &self.token)
        .body(())?
        .send()?
        .text()?;
        Ok(
            deserialize_normal_response::<Vec<MonetaryAccount>>(&response)?
                .response
                .into_iter()
                .map(|m| m.monetary_account_bank)
                .collect(),
        )
    }
    pub fn payments(&self, acc: &MonetaryAccountBank) -> Result<Vec<Payment>> {
        let next_page = |url: &str| -> Result<(_, _)> {
            let response = isahc::http::Request::get(url)
                .header("X-Bunq-Client-Authentication", &self.token)
                .body(())?
                .send()?
                .text()?;
            let Response {
                response,
                pagination,
            } = deserialize_normal_response::<Vec<PaymentPayment>>(&response)?;
            Ok((
                response.into_iter().map(|p| p.payment).collect(),
                pagination,
            ))
        };
        let mut url = format!(
            "/v1/user/{}/monetary-account/{}/payment",
            self.user_id, acc.id
        );
        let mut all = Vec::new();
        loop {
            let (mut payments, pag) = next_page(&format!("{}{}", BASE, url))?;
            all.append(&mut payments);
            if let Some(Pagination{older_url: Some(older_url),..}) = pag {
                url = older_url;
            } else {
                break;
            }
        }
        Ok(all)
    }
}

#[derive(Deserialize, Debug)]
pub struct LabelMonetaryAccount {
    pub iban: Option<String>,
    pub display_name: String,
    pub merchant_category_code: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct Amount {
    pub value: String,
    pub currency: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct PaymentPayment {
    payment: Payment,
}

#[derive(Deserialize, Debug)]
struct Pagination {
    future_url: Option<String>,
    newer_url: Option<String>,
    older_url: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct Payment {
    pub alias: LabelMonetaryAccount,
    pub counterparty_alias: LabelMonetaryAccount,
    pub amount: Amount,
    pub balance_after_mutation: Amount,
    pub created: String,
    pub updated: String,
    pub description: String,
    pub id: i64,
    pub monetary_account_id: i64,
    #[serde(rename = "type")]
    pub type_: String,
    pub sub_type: String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct MonetaryAccount {
    monetary_account_bank: MonetaryAccountBank,
}
#[derive(Deserialize, Debug)]
pub struct MonetaryAccountBank {
    pub id: i64,
    pub description: String,
}