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

fix: make the CI pass #11

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 5 additions & 4 deletions crates/supabase-auth/src/jwt_expiry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::task::{Context, Poll};
use std::time::Duration;
use core::pin::Pin;
use core::task::{Context, Poll};
use core::time::Duration;

use futures::Future;
use futures_timer::Delay;
use pin_project::pin_project;

#[pin_project]
pub(crate) struct JwtExpiry {
pub struct JwtExpiry {
#[pin]
delay: Delay,
}
Expand All @@ -22,7 +23,7 @@ impl JwtExpiry {
impl Future for JwtExpiry {
type Output = ();

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().delay.poll(cx)
}
}
Expand Down
41 changes: 23 additions & 18 deletions crates/supabase-auth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extern crate alloc;

mod jwt_expiry;
use std::borrow::Cow;
use std::ops::{Div, Mul};
use std::pin::Pin;
use std::task::{Context, Poll};
use alloc::borrow::Cow;
use core::ops::{Div, Mul};
use core::pin::Pin;
use core::task::{Context, Poll};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use base64::prelude::*;
Expand All @@ -26,7 +28,8 @@ pub struct SupabaseAuth {

impl SupabaseAuth {
/// Creates a new [`SupabaseAuth`].
pub fn new(url: url::Url, api_key: String) -> Self {
#[must_use]
pub const fn new(url: url::Url, api_key: String) -> Self {
Self { url, api_key }
}

Expand Down Expand Up @@ -63,7 +66,8 @@ pub struct TokenBody<'a> {
}

impl<'a> TokenBody<'a> {
pub fn new(email: &'a str, password: &'a str) -> Self {
#[must_use]
pub const fn new(email: &'a str, password: &'a str) -> Self {
Self {
email: Cow::Borrowed(email),
password: redact::Secret::new(Cow::Borrowed(password)),
Expand All @@ -73,17 +77,17 @@ impl<'a> TokenBody<'a> {

#[pin_project]
pub struct RefreshStream<'a> {
password_url: url::Url,
refresh_url: url::Url,
pub password_url: url::Url,
pub refresh_url: url::Url,
pub api_key: String,
client: Client,
token_body: TokenBody<'a>,
pub client: Client,
pub token_body: TokenBody<'a>,
#[pin]
state: RefreshStreamState,
pub state: RefreshStreamState,
}

#[pin_project]
enum RefreshStreamState {
pub enum RefreshStreamState {
PasswordLogin,
WaitingForResponse(
#[pin] futures::future::BoxFuture<'static, Result<Response, reqwest::Error>>,
Expand Down Expand Up @@ -147,15 +151,15 @@ impl<'a> Stream for RefreshStream<'a> {
// Get the current time as Unix timestamp
let current_ts = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.unwrap_or_else(|_| Duration::from_secs(0)) // Fallback to 0
.as_secs();

// Calculate the duration until expiration
let valid_for = if expires_at_ts > current_ts {
// and divide by `2/3` just to be on the safe side
Duration::from_secs(expires_at_ts - current_ts)
.mul(3)
.div(2)
// Use saturating_sub to handle underflow
let remaining_secs = expires_at_ts.saturating_sub(current_ts);
// Divide by `2/3` just to be on the safe side
Duration::from_secs(remaining_secs).mul(2).div(3)
} else {
Duration::from_secs(1)
};
Expand All @@ -182,7 +186,8 @@ impl<'a> Stream for RefreshStream<'a> {
refresh_token,
access_expiry,
} => {
let _res = std::task::ready!(access_expiry.poll_unpin(cx));
std::task::ready!(access_expiry.poll_unpin(cx));

let request_future = this
.client
.post(this.refresh_url.clone())
Expand Down
1 change: 1 addition & 0 deletions crates/supabase-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ reqwest.workspace = true
tokio.workspace = true
tracing.workspace = true
thiserror.workspace = true
futures.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/supabase-client/src/client/authenticated.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate alloc;
use std::sync::Arc;

use tokio::sync::RwLock;
Expand Down
2 changes: 1 addition & 1 deletion crates/supabase-client/src/client/unauthenticated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use supabase_auth::futures::StreamExt;
use futures::StreamExt;
use supabase_auth::TokenBody;
use tokio::sync::RwLock;

Expand Down
6 changes: 3 additions & 3 deletions crates/supabase-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl SupabaseMockServer {
}

fn register_jwt_custom_grant_type(&mut self, jwt: &str, grant_type: &str) -> &mut Self {
let _m = self
let mock = self
.mockito_server
.mock("POST", "/auth/v1/token")
.match_query(Matcher::Regex(format!("grant_type={grant_type}")))
Expand All @@ -54,7 +54,7 @@ impl SupabaseMockServer {
json!({
"access_token": jwt,
"refresh_token": "some-refresh-token",
"expires_in": 3600,
"expires_in": 3_600_i32,
"token_type": "bearer",
"user": {
"id": "user-id",
Expand All @@ -64,7 +64,7 @@ impl SupabaseMockServer {
.to_string(),
)
.create();
self.api_mock.push(_m);
self.api_mock.push(mock);
self
}
}
Expand Down
Loading