Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SM-1023: Remove Instant from Client in SDK #397

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/bitwarden/src/auth/renew.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{Duration, Instant};
use chrono::Utc;

#[cfg(feature = "internal")]
use crate::{auth::api::request::ApiTokenRequest, client::UserLoginMethod};
Expand All @@ -9,10 +9,10 @@ use crate::{
};

pub(crate) async fn renew_token(client: &mut Client) -> Result<()> {
const TOKEN_RENEW_MARGIN: Duration = Duration::from_secs(5 * 60);
const TOKEN_RENEW_MARGIN_SECONDS: i64 = 5 * 60;

if let (Some(expires), Some(login_method)) = (&client.token_expires_in, &client.login_method) {
if expires > &(Instant::now() + TOKEN_RENEW_MARGIN) {
if Utc::now().timestamp() < expires - TOKEN_RENEW_MARGIN_SECONDS {
return Ok(());
}

Expand Down
7 changes: 3 additions & 4 deletions crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::time::{Duration, Instant};

use chrono::Utc;
use reqwest::header::{self};
use uuid::Uuid;

Expand Down Expand Up @@ -69,7 +68,7 @@ pub(crate) enum ServiceAccountLoginMethod {
pub struct Client {
token: Option<String>,
pub(crate) refresh_token: Option<String>,
pub(crate) token_expires_in: Option<Instant>,
pub(crate) token_expires_in: Option<i64>,
pub(crate) login_method: Option<LoginMethod>,

/// Use Client::get_api_configurations() to access this.
Expand Down Expand Up @@ -190,7 +189,7 @@ impl Client {
) {
self.token = Some(token.clone());
self.refresh_token = refresh_token;
self.token_expires_in = Some(Instant::now() + Duration::from_secs(expires_in));
self.token_expires_in = Some(Utc::now().timestamp() + expires_in as i64);
self.login_method = Some(login_method);
self.__api_configurations.identity.oauth_access_token = Some(token.clone());
self.__api_configurations.api.oauth_access_token = Some(token);
Expand Down