1use std::time::{Duration, SystemTime};
12
13use serde::Deserialize;
14use thiserror::Error;
15
16use crate::Error;
17
18component! {
19 TokenProvider : TokenProviderInner {
20 tokens: Vec<Token> = vec![],
21 }
22}
23
24#[derive(Debug, Error)]
25pub enum TokenError {
26 #[error("no tokens available")]
27 Empty,
28}
29
30impl From<TokenError> for Error {
31 fn from(err: TokenError) -> Self {
32 Error::unavailable(err)
33 }
34}
35
36#[derive(Clone, Debug)]
37pub struct Token {
38 pub access_token: String,
39 pub expires_in: Duration,
40 pub token_type: String,
41 pub scopes: Vec<String>,
42 pub timestamp: SystemTime,
43}
44
45#[derive(Deserialize)]
46#[serde(rename_all = "camelCase")]
47struct TokenData {
48 access_token: String,
49 expires_in: u64,
50 token_type: String,
51 scope: Vec<String>,
52}
53
54impl TokenProvider {
55 fn find_token(&self, scopes: Vec<&str>) -> Option<usize> {
56 self.lock(|inner| {
57 (0..inner.tokens.len()).find(|&i| inner.tokens[i].in_scopes(scopes.clone()))
58 })
59 }
60
61 pub async fn get_token(&self, scopes: &str) -> Result<Token, Error> {
66 let client_id = self.session().client_id();
67 self.get_token_with_client_id(scopes, &client_id).await
68 }
69
70 pub async fn get_token_with_client_id(
71 &self,
72 scopes: &str,
73 client_id: &str,
74 ) -> Result<Token, Error> {
75 if client_id.is_empty() {
76 return Err(Error::invalid_argument("Client ID cannot be empty"));
77 }
78
79 if let Some(index) = self.find_token(scopes.split(',').collect()) {
80 let cached_token = self.lock(|inner| inner.tokens[index].clone());
81 if cached_token.is_expired() {
82 self.lock(|inner| inner.tokens.remove(index));
83 } else {
84 return Ok(cached_token);
85 }
86 }
87
88 trace!(
89 "Requested token in scopes {scopes:?} unavailable or expired, requesting new token."
90 );
91
92 let query_uri = format!(
93 "hm://keymaster/token/authenticated?scope={}&client_id={}&device_id={}",
94 scopes,
95 client_id,
96 self.session().device_id(),
97 );
98 let request = self.session().mercury().get(query_uri)?;
99 let response = request.await?;
100 let data = response.payload.first().ok_or(TokenError::Empty)?.to_vec();
101 let token = Token::from_json(String::from_utf8(data)?)?;
102 trace!("Got token: {token:#?}");
103 self.lock(|inner| inner.tokens.push(token.clone()));
104 Ok(token)
105 }
106}
107
108impl Token {
109 const EXPIRY_THRESHOLD: Duration = Duration::from_secs(10);
110
111 pub fn from_json(body: String) -> Result<Self, Error> {
112 let data: TokenData = serde_json::from_slice(body.as_ref())?;
113 Ok(Self {
114 access_token: data.access_token,
115 expires_in: Duration::from_secs(data.expires_in),
116 token_type: data.token_type,
117 scopes: data.scope,
118 timestamp: SystemTime::now(),
119 })
120 }
121
122 pub fn is_expired(&self) -> bool {
123 self.timestamp + self.expires_in.saturating_sub(Self::EXPIRY_THRESHOLD) < SystemTime::now()
124 }
125
126 pub fn in_scope(&self, scope: &str) -> bool {
127 for s in &self.scopes {
128 if *s == scope {
129 return true;
130 }
131 }
132 false
133 }
134
135 pub fn in_scopes(&self, scopes: Vec<&str>) -> bool {
136 for s in scopes {
137 if !self.in_scope(s) {
138 return false;
139 }
140 }
141 true
142 }
143}