use crate::{SecretToken, Token};
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AuthoriseResponse {
pub(crate) access_token: SecretToken,
pub(crate) expiry: u64,
}
impl From<AuthoriseResponse> for Token {
fn from(resp: AuthoriseResponse) -> Self {
Token {
access_token: resp.access_token,
token_type: "Bearer".to_string(),
expires_at: resp.expiry,
refresh_token: None,
region: None,
client_id: None,
device_instance_id: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn authorise_response_maps_expiry_as_absolute_epoch() {
let resp = AuthoriseResponse {
access_token: SecretToken::new("cts-token"),
expiry: 1_900_000_000, };
let token = Token::from(resp);
assert_eq!(
token.expires_at(),
1_900_000_000,
"expiry must be carried through as-is, never offset by `now`"
);
assert_eq!(token.token_type(), "Bearer");
assert_eq!(token.access_token().as_str(), "cts-token");
assert!(
token.refresh_token.is_none(),
"/api/authorise issues no refresh token"
);
}
}