1use crate::{
2 protobufs::steammessages_auth_steamclient::CAuthentication_AccessToken_GenerateForApp_Request,
3 steamapi::{AuthenticationClient, EResult},
4 token::{Jwt, Tokens},
5 transport::Transport,
6};
7
8pub struct TokenRefresher<T>
9where
10 T: Transport,
11{
12 client: AuthenticationClient<T>,
13}
14
15impl<T> TokenRefresher<T>
16where
17 T: Transport,
18{
19 pub fn new(client: AuthenticationClient<T>) -> Self {
20 Self { client }
21 }
22
23 pub fn refresh(&mut self, steam_id: u64, tokens: &Tokens) -> Result<Jwt, anyhow::Error> {
24 let mut req = CAuthentication_AccessToken_GenerateForApp_Request::new();
25 req.set_steamid(steam_id);
26 req.set_refresh_token(tokens.refresh_token().expose_secret().to_owned());
27
28 let resp = self
29 .client
30 .generate_access_token(req, tokens.access_token())?;
31
32 if resp.result != EResult::OK {
33 return Err(anyhow::anyhow!(
34 "Failed to refresh access token: {:?}",
35 resp.result
36 ));
37 }
38
39 let mut resp = resp.into_response_data();
40
41 Ok(resp.take_access_token().into())
42 }
43}