Skip to content

Commit

Permalink
Merge pull request #2 from OpenXbox/feat/token_validity
Browse files Browse the repository at this point in the history
tokens: Implement validity check, define token lifetimes as DateTime<Utc> (instead of string)
  • Loading branch information
tuxuser committed Dec 21, 2023
2 parents 78db95b + 677f114 commit 69232a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/src/bin/auth_minecraft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn main() -> Result<(), Error> {
)
.await?;

xsts_mc_services.check_validity()?;
let identity_token = xsts_mc_services.authorization_header_value();
println!("identityToken: {identity_token}");

Expand Down
1 change: 1 addition & 0 deletions examples/src/bin/xbl_signed_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let xsts_token = token_store
.authorization_token
.ok_or(Error::GeneralError("No XSTS token was acquired".into()))?;
xsts_token.check_validity()?;

// Send a http request
// Request will get signed and MS-CV header populated
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Definition of custom error type.
//!

use chrono::{DateTime, Utc};
use oauth2::{
basic::BasicErrorResponse, reqwest::AsyncHttpClientError, DeviceCodeErrorResponse,
RequestTokenError,
Expand Down Expand Up @@ -65,6 +66,9 @@ pub enum Error {
/// Failed processing HTTP request
#[error("Failed processing HTTP request")]
InvalidRequest(String),
/// Token expired
#[error("Token expired")]
TokenExpired(DateTime<Utc>),
/// Unknown error
#[error("unknown xal error")]
Unknown,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//! &mut authenticator,
//! CliCallbackHandler
//! ).await?;
//!
//!
//! // User will be prompted on commandline to proceed with authentication
//!
//! token_store.update_timestamp();
Expand Down
22 changes: 18 additions & 4 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ pub mod request {

/// HTTP Response models
pub mod response {
use chrono::{DateTime, TimeZone, Utc};
use oauth2::basic::BasicTokenResponse;
use url::Url;

use crate::Error;

use super::{Deserialize, HashMap, Serialize, SigningPolicy};

/// Alias type for Windows Live token response
Expand Down Expand Up @@ -296,9 +299,9 @@ pub mod response {
#[serde(rename_all = "PascalCase")]
pub struct XTokenResponse<T> {
/// Issue datetime of token
pub issue_instant: String,
pub issue_instant: DateTime<Utc>,
/// Expiry datetime of token
pub not_after: String,
pub not_after: DateTime<Utc>,
/// Token value
pub token: String,
/// XSTS display claims
Expand All @@ -322,14 +325,25 @@ pub mod response {
impl<T> From<&str> for XTokenResponse<T> {
fn from(s: &str) -> Self {
Self {
issue_instant: "2020-12-15T00:00:00.0000000Z".into(),
not_after: "2199-12-15T00:00:00.0000000Z".into(),
issue_instant: Utc.with_ymd_and_hms(2020, 12, 15, 0, 0, 0).unwrap(),
not_after: Utc.with_ymd_and_hms(2199, 12, 15, 0, 0, 0).unwrap(),
token: s.to_owned(),
display_claims: None,
}
}
}

impl<T> XTokenResponse<T> {
/// Check if token is valid
pub fn check_validity(&self) -> Result<(), Error> {
if self.not_after < chrono::offset::Utc::now() {
return Err(Error::TokenExpired(self.not_after));
}

Ok(())
}
}

/// Sisu authentication repsonse
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
Expand Down

0 comments on commit 69232a3

Please sign in to comment.