Skip to content
Open
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

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE sessions ALTER COLUMN expires DROP DEFAULT;
ALTER TABLE sessions ALTER COLUMN refresh_expires DROP DEFAULT;
34 changes: 31 additions & 3 deletions apps/labrinth/src/auth/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ where
executor,
redis,
session_queue,
false,
)
.await?
else {
Expand Down Expand Up @@ -61,6 +62,7 @@ where
executor,
redis,
session_queue,
false,
)
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
Expand Down Expand Up @@ -95,12 +97,38 @@ where
Ok((scopes, User::from_full(db_user)))
}

pub async fn get_user_from_bearer_token<'a, E>(
req: &HttpRequest,
token: Option<&str>,
executor: E,
redis: &RedisPool,
session_queue: &AuthQueue,
allow_expired: bool,
) -> Result<(Scopes, User), AuthenticationError>
where
E: crate::database::Executor<'a, Database = sqlx::Postgres> + Copy,
{
let (scopes, db_user) = get_user_record_from_bearer_token(
req,
token,
executor,
redis,
session_queue,
allow_expired,
)
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;

Ok((scopes, User::from_full(db_user)))
}

pub async fn get_user_record_from_bearer_token<'a, 'b, E>(
req: &HttpRequest,
token: Option<&str>,
executor: E,
redis: &RedisPool,
session_queue: &AuthQueue,
allow_expired: bool,
) -> Result<Option<(Scopes, user_item::DBUser)>, AuthenticationError>
where
E: crate::database::Executor<'a, Database = sqlx::Postgres> + Copy,
Expand All @@ -120,7 +148,7 @@ where
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;

if pat.expires < Utc::now() {
if !allow_expired && pat.expires < Utc::now() {
return Err(AuthenticationError::InvalidCredentials);
}

Expand All @@ -139,7 +167,7 @@ where
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;

if session.expires < Utc::now() {
if !allow_expired && session.expires < Utc::now() {
return Err(AuthenticationError::InvalidCredentials);
}

Expand Down Expand Up @@ -169,7 +197,7 @@ where
.await?
.ok_or(AuthenticationError::InvalidCredentials)?;

if access_token.expires < Utc::now() {
if !allow_expired && access_token.expires < Utc::now() {
return Err(AuthenticationError::InvalidCredentials);
}

Expand Down
15 changes: 13 additions & 2 deletions apps/labrinth/src/database/models/session_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub struct SessionBuilder {

pub ip: String,
pub user_agent: String,

// When None, database default of 14 days will be used
pub expires: Option<DateTime<Utc>>,
// When None, database default of 60 days will be used
pub session_expires: Option<DateTime<Utc>>,
}

impl SessionBuilder {
Expand All @@ -38,11 +43,13 @@ impl SessionBuilder {
"
INSERT INTO sessions (
id, session, user_id, os, platform,
city, country, ip, user_agent
city, country, ip, user_agent,
expires, refresh_expires
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9
$6, $7, $8, $9,
$10, $11
)
",
id as DBSessionId,
Expand All @@ -54,6 +61,10 @@ impl SessionBuilder {
self.country,
self.ip,
self.user_agent,
self.expires
.unwrap_or_else(|| Utc::now() + chrono::Duration::days(14)),
self.session_expires
.unwrap_or_else(|| Utc::now() + chrono::Duration::days(60)),
)
.execute(&mut *transaction)
.await?;
Expand Down
1 change: 1 addition & 0 deletions apps/labrinth/src/routes/internal/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub async fn count_download(
&**pool,
&redis,
&session_queue,
false,
)
.await
.ok()
Expand Down
14 changes: 10 additions & 4 deletions apps/labrinth/src/routes/internal/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ pub async fn init(
&**client,
&redis,
&session_queue,
false,
)
.await
.ok()
Expand Down Expand Up @@ -1114,6 +1115,7 @@ pub async fn init(
&**client,
&redis,
&session_queue,
false,
)
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
Expand Down Expand Up @@ -1308,7 +1310,8 @@ pub async fn auth_callback(
};

let session =
issue_session(req, user_id, &mut transaction, &redis).await?;
issue_session(req, user_id, &mut transaction, &redis, None)
.await?;
transaction.commit().await?;

let redirect_url = format!(
Expand Down Expand Up @@ -1533,7 +1536,8 @@ pub async fn create_account_with_password(
.insert(&mut transaction)
.await?;

let session = issue_session(req, user_id, &mut transaction, &redis).await?;
let session =
issue_session(req, user_id, &mut transaction, &redis, None).await?;
let res = crate::models::sessions::Session::from(session, true, None);

let mailbox: Mailbox = new_account.email.parse().map_err(|_| {
Expand Down Expand Up @@ -1627,7 +1631,7 @@ pub async fn login_password(
} else {
let mut transaction = pool.begin().await?;
let session =
issue_session(req, user.id, &mut transaction, &redis).await?;
issue_session(req, user.id, &mut transaction, &redis, None).await?;
let res = crate::models::sessions::Session::from(session, true, None);
transaction.commit().await?;

Expand Down Expand Up @@ -1757,7 +1761,7 @@ pub async fn login_2fa(
DBFlow::remove(&login.flow, &redis).await?;

let session =
issue_session(req, user_id, &mut transaction, &redis).await?;
issue_session(req, user_id, &mut transaction, &redis, None).await?;
let res = crate::models::sessions::Session::from(session, true, None);
transaction.commit().await?;

Expand Down Expand Up @@ -1945,6 +1949,7 @@ pub async fn remove_2fa(
&**pool,
&redis,
&session_queue,
false,
)
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
Expand Down Expand Up @@ -2150,6 +2155,7 @@ pub async fn change_password(
&**pool,
&redis,
&session_queue,
false,
)
.await?
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
Expand Down
Loading