Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Commit 7d667c2

Browse files
committed
feat: Expose auth refresh for Client
Allows the client to refresh its authentication token when necessary.
1 parent ef0d22a commit 7d667c2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "proton-api-rs"
33
authors = ["Leander Beernaert <lbb-dev@pm.me>"]
4-
version = "0.2.0"
4+
version = "0.3.0"
55
edition = "2021"
66
license = "AGPL-3.0-only"
77
description = "Unofficial implemention of proton REST API in rust"

src/client/api.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::client::types::{LatestEventResponse, UserAuth};
1+
use crate::client::types::{AuthRefresh, AuthRefreshResponse, LatestEventResponse, UserAuth};
22
use crate::client::{HttpClient, RequestBuilder, X_PM_UID_HEADER};
33
use crate::domain::{Event, EventId, SecretString, User, UserUid};
44
use crate::RequestError;
@@ -23,6 +23,27 @@ impl Client {
2323
&self.user.refresh_token
2424
}
2525

26+
/// Refresh the authentication token.
27+
pub async fn refresh_auth(&mut self) -> Result<(), RequestError> {
28+
let response = self
29+
.http_client
30+
.post("/auth/v4/refresh")
31+
.header(X_PM_UID_HEADER, self.user.uid.expose_secret().as_str())
32+
.with_body(&AuthRefresh {
33+
uid: self.user.uid.expose_secret().as_str(),
34+
refresh_token: self.user.refresh_token.expose_secret(),
35+
grant_type: "refresh_token",
36+
response_type: "token",
37+
redirect_uri: "https://protonmail.ch/",
38+
})
39+
.execute()
40+
.await?;
41+
42+
let auth = response.json::<AuthRefreshResponse>().await?;
43+
self.user = UserAuth::from_auth_refresh_response(&auth);
44+
Ok(())
45+
}
46+
2647
/// Logout the current user. Consumes the type in the process. If the request fails, the
2748
/// current instance is returned in the Error.
2849
pub async fn logout(self) -> Result<(), (Self, RequestError)> {

0 commit comments

Comments
 (0)