Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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

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

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

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

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

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

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

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

1 change: 1 addition & 0 deletions migrations/20240906090729_polling_token.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE pollingtoken;
7 changes: 7 additions & 0 deletions migrations/20240906090729_polling_token.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE pollingtoken (
id bigserial PRIMARY KEY,
token TEXT NOT NULL,
device_id bigint NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
FOREIGN KEY(device_id) REFERENCES "device"(id) ON DELETE CASCADE
);
2 changes: 1 addition & 1 deletion proto
Submodule proto updated 1 files
+12 −0 core/proxy.proto
1 change: 1 addition & 0 deletions src/db/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod oauth2authorizedapp;
pub mod oauth2client;
#[cfg(feature = "openid")]
pub mod oauth2token;
pub mod polling_token;
pub mod session;
pub mod settings;
pub mod user;
Expand Down
38 changes: 38 additions & 0 deletions src/db/models/polling_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use chrono::{NaiveDateTime, Utc};
use model_derive::Model;
use sqlx::{query_as, Error as SqlxError};

use crate::random::gen_alphanumeric;

use super::DbPool;

// Token used for polling requests.
#[derive(Clone, Debug, Model)]
pub struct PollingToken {
pub id: Option<i64>,
pub token: String,
pub device_id: i64,
pub created_at: NaiveDateTime,
}

impl PollingToken {
pub fn new(device_id: i64) -> Self {
Self {
id: None,
device_id,
token: gen_alphanumeric(32),
created_at: Utc::now().naive_utc(),
}
}

pub async fn find(pool: &DbPool, token: &str) -> Result<Option<Self>, SqlxError> {
query_as!(
Self,
"SELECT id, token, device_id, created_at
FROM pollingtoken WHERE token = $1",
token
)
.fetch_optional(pool)
.await
}
}
21 changes: 21 additions & 0 deletions src/db/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,27 @@ impl User {
}
Ok(())
}

pub async fn find_by_device_id<'e, E>(
executor: E,
device_id: i64,
) -> Result<Option<Self>, SqlxError>
where
E: PgExecutor<'e>,
{
query_as!(
Self,
"SELECT u.id \"id?\", u.username, u.password_hash, u.last_name, u.first_name, u.email, \
u.phone, u.mfa_enabled, u.totp_enabled, u.email_mfa_enabled, \
u.totp_secret, u.email_mfa_secret, u.mfa_method \"mfa_method: _\", u.recovery_codes, u.is_active, u.openid_login \
FROM \"user\" as u \
JOIN \"device\" as d ON u.id = d.user_id \
WHERE d.id = $1",
device_id
)
.fetch_optional(executor)
.await
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/enterprise/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod polling;
Loading