chore: fix potentail override the new token with old one (#289)
* chore: fix potentail override the new token with old one * chore: log request id
This commit is contained in:
parent
a9ab3bbb22
commit
12eebd673e
|
|
@ -153,12 +153,16 @@ impl Client {
|
|||
|
||||
#[instrument(level = "debug", skip_all, err)]
|
||||
pub fn restore_token(&self, token: &str) -> Result<(), AppResponseError> {
|
||||
if token.is_empty() {
|
||||
return Err(AppError::OAuthError("Empty token".to_string()).into());
|
||||
match serde_json::from_str::<GotrueTokenResponse>(token) {
|
||||
Ok(token) => {
|
||||
self.token.write().set(token);
|
||||
Ok(())
|
||||
},
|
||||
Err(err) => {
|
||||
error!("fail to deserialize token:{}, error:{}", token, err);
|
||||
Err(err.into())
|
||||
},
|
||||
}
|
||||
let token = serde_json::from_str::<GotrueTokenResponse>(token)?;
|
||||
self.token.write().set(token);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Retrieves the string representation of the [GotrueTokenResponse]. The returned value can be
|
||||
|
|
@ -1230,7 +1234,7 @@ fn url_missing_param(param: &str) -> AppResponseError {
|
|||
|
||||
pub(crate) fn log_request_id(resp: &reqwest::Response) {
|
||||
if let Some(request_id) = resp.headers().get("x-request-id") {
|
||||
event!(tracing::Level::DEBUG, "request_id: {:?}", request_id);
|
||||
event!(tracing::Level::INFO, "request_id: {:?}", request_id);
|
||||
} else {
|
||||
event!(tracing::Level::DEBUG, "request_id: not found");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use anyhow::Error;
|
|||
use gotrue_entity::dto::GotrueTokenResponse;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use tokio::sync::broadcast::{channel, Receiver, Sender};
|
||||
use tracing::event;
|
||||
use tracing::{event, warn};
|
||||
|
||||
pub type TokenStateReceiver = Receiver<TokenState>;
|
||||
|
||||
|
|
@ -46,8 +46,12 @@ impl ClientToken {
|
|||
///
|
||||
/// - `token`: The new `AccessTokenResponse` to be set.
|
||||
pub(crate) fn set(&mut self, new_token: GotrueTokenResponse) {
|
||||
let is_new = match &self.token {
|
||||
None => true,
|
||||
match &self.token {
|
||||
None => {
|
||||
self.token = Some(new_token);
|
||||
tracing::trace!("Set new access token: {:?}", self.token);
|
||||
let _ = self.sender.send(TokenState::Refresh);
|
||||
},
|
||||
Some(old_token) => {
|
||||
event!(
|
||||
tracing::Level::INFO,
|
||||
|
|
@ -55,15 +59,19 @@ impl ClientToken {
|
|||
old_token,
|
||||
new_token
|
||||
);
|
||||
old_token.access_token != new_token.access_token
|
||||
|
||||
if old_token.expires_at > new_token.expires_at {
|
||||
warn!(
|
||||
"new token expires_at:{} is less than old token expires_at:{}",
|
||||
new_token.expires_at, old_token.expires_at
|
||||
);
|
||||
} else {
|
||||
self.token = Some(new_token);
|
||||
tracing::trace!("Set new access token: {:?}", self.token);
|
||||
let _ = self.sender.send(TokenState::Refresh);
|
||||
}
|
||||
},
|
||||
};
|
||||
self.token = Some(new_token);
|
||||
|
||||
if is_new {
|
||||
tracing::trace!("Set new access token: {:?}", self.token);
|
||||
let _ = self.sender.send(TokenState::Refresh);
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsets the current access token and notifies receivers of the invalidation.
|
||||
|
|
|
|||
Loading…
Reference in New Issue