lesspass-client 0.10.0

LessPass API server client library and CLI written in Rust
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//
// lesspass-client client.rs
// Copyright (C) 2021-2025 Óscar García Amor <ogarcia@connectical.com>
// Distributed under terms of the GNU GPLv3 license.
//

use log::{debug, trace};
use reqwest::{Method, Response, Url};
use serde::Serialize;

use super::error::Result;
use super::model::{
    Token,
    Auth,
    Refresh,
    User,
    UserPassword,
    UserChangePassword,
    NewPassword,
    Passwords
};

const USER_AGENT: &str = concat!(
    env!("CARGO_PKG_NAME"),
    "/",
    env!("CARGO_PKG_VERSION")
);

/// Client for connecting to LessPass server
#[derive(Clone, Debug)]
pub struct Client {
    pub url: String,
    pub client: reqwest::Client
}

/// Builder interface to Client
///
/// Usage:
/// ```
/// use lesspass_client::Client;
///
/// let url = "https://api.lesspass.com";
/// let lpc = Client::new(url);
/// ```
impl Client {

    /// Configure the client itself
    pub fn new(url: impl Into<String>) -> Client {
        Client {
            url: url.into(),
            client: reqwest::Client::builder()
                .connection_verbose(true)
                .user_agent(USER_AGENT)
                .build()
                .expect("Client::new()")
        }
    }

    /// Internal helper function to join host url with endpoint path
    fn build_url(&self, path: &str) -> Result<Url> {
        Ok(Url::parse(&self.url)?.join(path)?)
    }

    /// Internal function to perform authenticated empty requests
    async fn empty_request(&self, method: Method, url: &Url, token: &str) -> Result<Response> {
        let authorization = format!("Bearer {}", token);
        Ok(self.client.request(method, url.as_str())
            .header("Authorization", authorization)
            .send().await?
            .error_for_status()?)
    }

    /// Internal function to perform (un)authenticated requests with body
    async fn request<J: Serialize + ?Sized>(&self, method: Method, url: &Url, token: Option<&str>, json: &J) -> Result<Response> {
        match token {
            Some(token) => {
                let authorization = format!("Bearer {}", token);
                Ok(self.client.request(method, url.as_str())
                    .header("Authorization", authorization)
                    .json(&json).send().await?
                    .error_for_status()?)
            },
            None => Ok(self.client.request(method, url.as_str()).json(&json).send().await?.error_for_status()?)
        }
    }

    /// Internal function to perform authenticated get requests
    async fn get(&self, path: &str, token: &str) -> Result<Response> {
        let url = self.build_url(path)?;
        trace!("GET: {url}");
        self.empty_request(Method::GET, &url, token).await
    }

    /// Internal function to perform (un)authenticated post requests
    async fn post<J: Serialize + ?Sized>(&self, path: &str, token: Option<&str>, json: &J) -> Result<Response> {
        let url = self.build_url(path)?;
        trace!("POST: {url}");
        self.request(Method::POST, &url, token, json).await
    }

    /// Internal function to perform authenticated put requests
    async fn put<J: Serialize + ?Sized>(&self, path: &str, token: &str, json: &J) -> Result<Response> {
        let url = self.build_url(path)?;
        trace!("PUT: {url}");
        self.request(Method::PUT, &url, Some(token), json).await
    }

    /// Internal function to perform authenticated delete requests without body
    async fn empty_delete(&self, path: &str, token: &str) -> Result<Response> {
        let url = self.build_url(path)?;
        trace!("DELETE: {url}");
        self.empty_request(Method::DELETE, &url, token).await
    }

    /// Internal function to perform authenticated delete requests
    async fn delete<J: Serialize + ?Sized>(&self, path: &str, token: &str, json: &J) -> Result<Response> {
        let url = self.build_url(path)?;
        trace!("DELETE: {url}");
        self.request(Method::DELETE, &url, Some(token), json).await
    }

    /// Parse self configured url as Url
    pub fn parse_url(&self) -> Result<Url> {
        Ok(Url::parse(&self.url)?)
    }

    /// Create a new token (perform initial auth with email and password)
    pub async fn create_token(&self, email: &str, password: &str) -> Result<Token> {
        debug!("Requesting new token");
        let body = Auth { email: email.into(), password: password.into() };
        Ok(self.post("auth/jwt/create/", None, &body).await?.json::<Token>().await?)
    }

    /// Refresh a token
    ///
    /// Need refresh token string
    pub async fn refresh_token(&self, token: &str) -> Result<Token> {
        debug!("Requesting refreshed token");
        let body = Refresh { refresh: token.into() };
        Ok(self.post("auth/jwt/refresh/", None, &body).await?.json::<Token>().await?)
    }

    /// Creates a new user
    pub async fn create_user(&self, email: &str, password: &str) -> Result<()> {
        debug!("Requesting new user");
        let body = Auth { email: email.into(), password: password.into() };
        self.post("auth/users/", None, &body).await?;
        Ok(())
    }

    /// Gets current user info
    ///
    /// Need access token string
    pub async fn get_user(&self, token: &str) -> Result<User> {
        debug!("Requesting user info");
        Ok(self.get("auth/users/me/", token).await?.json::<User>().await?)
    }

    /// Changes current user password
    ///
    /// Need access token string
    pub async fn change_user_password(&self, token: &str, current_password: &str, new_password: &str) -> Result<()> {
        debug!("Requesting a password change");
        let body = UserChangePassword { current_password: current_password.into(), new_password: new_password.into() };
        self.post("auth/users/set_password/", Some(token), &body).await?;
        Ok(())
    }

    /// Deletes current user
    ///
    /// Need access token string
    pub async fn delete_user(&self, token: &str, current_password: &str) -> Result<()> {
        debug!("Requesting user deletion");
        let body = UserPassword { current_password: current_password.into() };
        self.delete("auth/users/me/", token, &body).await?;
        Ok(())
    }

    /// Gets the password list
    ///
    /// Need access token string
    pub async fn get_passwords(&self, token: &str) -> Result<Passwords> {
        Ok(self.get("passwords/", token).await?.json::<Passwords>().await?)
    }

    /// Creates a new password
    ///
    /// Need access token string
    pub async fn post_password(&self, token: &str, password: &NewPassword) -> Result<()> {
        self.post("passwords/", Some(token), password).await?;
        Ok(())
    }

    /// Updates existing password
    ///
    /// Need access token string
    pub async fn put_password(&self, token: &str, id: &str, password: &NewPassword) -> Result<()> {
        self.put(&format!("passwords/{id}/"), token, password).await?;
        Ok(())
    }

    /// Deletes existing password
    ///
    /// Need access token string
    pub async fn delete_password(&self, token: &str, id: &str) -> Result<()> {
        self.empty_delete(&format!("passwords/{id}/"), token).await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use chrono::{NaiveDate, Utc};
    use mockito::{Matcher, Server};

    const JH: (&str, &str) = ("content-type", "application/json");

    #[test]
    fn parse_url() {
        let url = Client::new("http://localhost").parse_url().unwrap();
        assert_eq!("http", url.scheme());
        assert_eq!(Some("localhost"), url.host_str());
        assert_eq!(Some(80), url.port_or_known_default());
        let url = Client::new("https://localhost").parse_url().unwrap();
        assert_eq!("https", url.scheme());
        assert_eq!(Some(443), url.port_or_known_default());
        let url = Client::new("https://example.com:6666").parse_url().unwrap();
        assert_eq!(Some("example.com"), url.host_str());
        assert_eq!(Some(6666), url.port_or_known_default());
        // Bad configured client
        let url_error = Client::new("bad").parse_url().unwrap_err();
        assert_eq!("URL parse error, relative URL without a base", url_error.to_string());
    }

    #[tokio::test]
    async fn create_token() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"{"email": "user@example.com", "password": "password"}"#;
        let response_body = r#"{"access": "access-token", "refresh": "refresh-token"}"#;
        let _m = server.mock("POST", "/auth/jwt/create/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .match_body(Matcher::JsonString(request_body.to_string()))
            .with_body(response_body)
            .create_async()
            .await;
        // Ok response
        let token = client.create_token("user@example.com", "password").await.unwrap();
        assert_eq!("access-token", &token.access);
        assert_eq!("refresh-token", &token.refresh);
        // Bad response caused by auth error
        let error_token = client.create_token("bad", "bad").await.unwrap_err();
        assert_eq!(Some(501), error_token.status());
        let _m = server.mock("POST", "/auth/jwt/create/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .with_body("unexpected")
            .create_async()
            .await;
        // Bad response caused by unexpected json body
        let error_body = client.create_token("bad", "bad").await.unwrap_err();
        assert_eq!("reqwest error, error decoding response body", error_body.to_string());
    }

    #[tokio::test]
    async fn refresh_token() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"{"refresh": "refresh-token"}"#;
        let response_body = r#"{"access": "new-access-token", "refresh": "new-refresh-token"}"#;
        let _m = server.mock("POST", "/auth/jwt/refresh/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .match_body(Matcher::JsonString(request_body.to_string()))
            .with_body(response_body)
            .create_async()
            .await;
        // Ok response
        let token = client.refresh_token("refresh-token").await.unwrap();
        assert_eq!("new-access-token", &token.access);
        assert_eq!("new-refresh-token", &token.refresh);
        // Bad response caused by auth error
        let error_token = client.refresh_token("bad-token").await.unwrap_err();
        assert_eq!(Some(501), error_token.status());
        let _m = server.mock("POST", "/auth/jwt/refresh/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .with_body("unexpected")
            .create_async()
            .await;
        // Bad response caused by unexpected json body
        let error_body = client.refresh_token("bad-token").await.unwrap_err();
        assert_eq!("reqwest error, error decoding response body", error_body.to_string());
    }

    #[tokio::test]
    async fn create_user() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"{"email": "newuser@example.com", "password": "newpassword"}"#;
        let _m = server.mock("POST", "/auth/users/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .match_body(Matcher::JsonString(request_body.to_string()))
            .create_async()
            .await;
        // Ok response
        let user = client.create_user("newuser@example.com", "newpassword").await.unwrap();
        assert_eq!((), user);
    }

    #[tokio::test]
    async fn get_user() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let response_body = r#"{"id":1, "email": "newuser@example.com"}"#;
        let _m = server.mock("GET", "/auth/users/me/")
            .with_status(200)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .with_body(response_body)
            .create_async()
            .await;
        // Ok response
        let user = client.get_user("access-token").await.unwrap();
        assert_eq!("1", user.id);
        assert_eq!("newuser@example.com", user.email);
        // Bad response caused by token error
        let error_in_token = client.get_user("bad-token").await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
    }

    #[tokio::test]
    async fn change_user_password() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"{"current_password": "current", "new_password": "new"}"#;
        let _m = server.mock("POST", "/auth/users/set_password/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .match_body(Matcher::JsonString(request_body.to_string()))
            .create_async()
            .await;
        // Ok response
        let change_password = client.change_user_password("access-token", "current", "new").await.unwrap();
        assert_eq!((), change_password);
        // Bad response caused by token error
        let error_in_token = client.change_user_password("bad-token", "current", "new").await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
    }

    #[tokio::test]
    async fn delete_user() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"{"current_password": "current"}"#;
        let _m = server.mock("DELETE", "/auth/users/me/")
            .with_status(200)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .match_body(Matcher::JsonString(request_body.to_string()))
            .create_async()
            .await;
        // Ok response
        let delete_user = client.delete_user("access-token", "current").await.unwrap();
        assert_eq!((), delete_user);
        // Bad response caused by token error
        let error_in_token = client.delete_user("bad-token", "current").await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
    }

    #[tokio::test]
    async fn get_passwords() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let response_body = r#"
        {
          "count": 3,
          "next": null,
          "previous": null,
          "results": [
            {
              "id": "e1a7e83c-9014-4585-95f5-4595160afe99",
              "login": "user@example.com",
              "site": "alice.example.com",
              "lowercase": true,
              "uppercase": true,
              "symbols": true,
              "digits": true,
              "counter": 10,
              "length": 16,
              "version": 2,
              "created": "2021-12-06T11:39:47.874027Z",
              "modified": "2021-12-06T11:39:47.874143Z"
            },
            {
              "id": "5f01f483-2b63-4faa-9c0c-b2dae03440f1",
              "login": "user@example.com",
              "site": "bob.example.com",
              "lowercase": false,
              "uppercase": true,
              "symbols": true,
              "numbers": false,
              "counter": 1,
              "length": 35,
              "version": 2,
              "created": "2021-11-21T11:34:18.361454Z",
              "modified": "2021-12-07T04:12:05.131415Z"
            },
            {
              "id": "10",
              "login": "user@example.com",
              "site": "charlie.example.com",
              "lowercase": false,
              "uppercase": true,
              "symbols": true,
              "digits": false,
              "numbers": true,
              "counter": 1,
              "length": 8,
              "version": 2,
              "created": "2023-05-10T12:05:36",
              "modified": "2023-06-02T17:33:54"
            }
          ]
        }
        "#;
        let _m = server.mock("GET", "/passwords/")
            .with_status(200)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .with_body(response_body)
            .create_async()
            .await;
        // Ok response
        let passwords = client.get_passwords("access-token").await.unwrap();
        assert_eq!(3, passwords.count);
        assert_eq!("e1a7e83c-9014-4585-95f5-4595160afe99", &passwords.results[0].id);
        assert_eq!("10", &passwords.results[2].id);
        assert_eq!(true, passwords.results[0].lowercase);
        assert_eq!("bob.example.com", &passwords.results[1].site);
        assert_eq!(false, passwords.results[1].digits);
        assert_eq!(NaiveDate::from_ymd_opt(2021, 11, 21).unwrap().and_hms_micro_opt(11, 34, 18, 361454).unwrap().and_local_timezone(Utc).unwrap(), passwords.results[1].created);
        assert_eq!(NaiveDate::from_ymd_opt(2021, 12, 7).unwrap().and_hms_micro_opt(4, 12, 5, 131415).unwrap().and_local_timezone(Utc).unwrap(), passwords.results[1].modified);
        assert_eq!(NaiveDate::from_ymd_opt(2023, 06, 2).unwrap().and_hms_micro_opt(17, 33, 54, 0).unwrap().and_local_timezone(Utc).unwrap(), passwords.results[2].modified);
        // Bad response caused by token error
        let error_in_token = client.get_passwords("bad-token").await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
    }

    #[tokio::test]
    async fn post_password() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"
        {
          "login": "newuser@example.com",
          "site": "new.example.com",
          "uppercase": true,
          "lowercase": true,
          "digits": false,
          "symbols": true,
          "length": 18,
          "counter": 5,
          "version": 2
        }
        "#;
        let password = NewPassword {
            site: "new.example.com".to_string(),
            login: "newuser@example.com".to_string(),
            lowercase: true,
            uppercase: true,
            symbols: true,
            digits: false,
            length: 18,
            counter: 5,
            version: 2
        };
        let _m = server.mock("POST", "/passwords/")
            .with_status(201)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .match_body(Matcher::JsonString(request_body.to_string()))
            .create_async()
            .await;
        // Ok Response
        let post_password = client.post_password("access-token", &password).await.unwrap();
        assert_eq!((), post_password);
        // Bad response caused by token error
        let error_in_token = client.post_password("bad-token", &password).await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
    }

    #[tokio::test]
    async fn put_password() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let request_body = r#"
        {
          "login": "updateuser@example.com",
          "site": "update.example.com",
          "uppercase": true,
          "lowercase": true,
          "digits": false,
          "symbols": false,
          "length": 22,
          "counter": 1,
          "version": 2
        }
        "#;
        let password = NewPassword {
            site: "update.example.com".to_string(),
            login: "updateuser@example.com".to_string(),
            lowercase: true,
            uppercase: true,
            symbols: false,
            digits: false,
            length: 22,
            counter: 1,
            version: 2
        };
        let _m = server.mock("PUT", "/passwords/ce2835da-9047-43eb-a107-bad4f01d22a0/")
            .with_status(200)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .match_body(Matcher::JsonString(request_body.to_string()))
            .create_async()
            .await;
        // Ok Response
        let put_password = client.put_password("access-token", "ce2835da-9047-43eb-a107-bad4f01d22a0", &password).await.unwrap();
        assert_eq!((), put_password);
        // Bad response caused by token error
        let error_in_token = client.put_password("bad-token", "ce2835da-9047-43eb-a107-bad4f01d22a0", &password).await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
        // Bad response caused by id error
        let error_in_id = client.put_password("access-token", "bad-id", &password).await.unwrap_err();
        assert_eq!(Some(501), error_in_id.status());
    }

    #[tokio::test]
    async fn delete_password() {
        let mut server = Server::new_async().await;
        let client = Client::new(&server.url());
        let _m = server.mock("DELETE", "/passwords/1c461df9-11eb-4bf1-976b-1c49d5598b8f/")
            .with_status(204)
            .with_header(JH.0, JH.1)
            .match_header("authorization", "Bearer access-token")
            .create_async()
            .await;
        // Ok Response
        let delete_password = client.delete_password("access-token", "1c461df9-11eb-4bf1-976b-1c49d5598b8f").await.unwrap();
        assert_eq!((), delete_password);
        // Bad response caused by token error
        let error_in_token = client.delete_password("bad-token", "1c461df9-11eb-4bf1-976b-1c49d5598b8f").await.unwrap_err();
        assert_eq!(Some(501), error_in_token.status());
        // Bad response caused by id error
        let error_in_id = client.delete_password("access-token", "bad-id").await.unwrap_err();
        assert_eq!(Some(501), error_in_id.status());
    }
}