fix(homeserver): return session from /:pubky/sesison

This commit is contained in:
nazeh
2024-07-20 18:13:23 +03:00
parent 4f87c7d444
commit 6aa64aeb92
5 changed files with 67 additions and 7 deletions

41
Cargo.lock generated
View File

@@ -286,6 +286,23 @@ dependencies = [
"version_check",
]
[[package]]
name = "cookie_store"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4934e6b7e8419148b6ef56950d277af8561060b56afd59e2aadf98b59fce6baa"
dependencies = [
"cookie",
"idna",
"indexmap",
"log",
"serde",
"serde_derive",
"serde_json",
"time",
"url",
]
[[package]]
name = "cpufeatures"
version = "0.2.12"
@@ -482,6 +499,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "fiat-crypto"
version = "0.2.9"
@@ -652,6 +675,12 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "headers"
version = "0.4.0"
@@ -824,6 +853,16 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.11"
@@ -1949,6 +1988,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72139d247e5f97a3eff96229a7ae85ead5328a39efe76f8bf5a06313d505b6ea"
dependencies = [
"base64 0.22.1",
"cookie",
"cookie_store",
"flate2",
"log",
"once_cell",

View File

@@ -15,10 +15,13 @@ pub type SessionsTable = Database<Bytes, Session>;
pub const SESSIONS_TABLE: &str = "sessions";
// TODO: add IP address?
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Session {
pub created_at: u64,
pub name: String,
/// User specified name, defaults to the user-agent.
pub name: Option<String>,
pub user_agent: String,
}
impl<'a> BytesEncode<'a> for Session {

View File

@@ -7,6 +7,8 @@ use axum::{
};
use axum_extra::{headers::UserAgent, TypedHeader};
use bytes::Bytes;
use heed::BytesEncode;
use postcard::to_allocvec;
use tower_cookies::{Cookie, Cookies};
use pubky_common::{
@@ -57,7 +59,8 @@ pub async fn signup(
// TODO: handle not having a user agent?
let session = &Session {
created_at: Timestamp::now().into_inner(),
name: user_agent.to_string(),
user_agent: user_agent.to_string(),
name: None,
};
sessions.put(&mut wtxn, &session_secret, session)?;
@@ -92,7 +95,11 @@ pub async fn session(
&base32::decode(base32::Alphabet::Crockford, cookie.value()).unwrap_or_default(),
)? {
rtxn.commit()?;
return Ok(());
// TODO: avoid decoding then encoding sesison
let x: Vec<u8> = to_allocvec::<Session>(&session)
.map_err(|_| Error::with_status(StatusCode::INTERNAL_SERVER_ERROR))?;
return Ok(x);
};
rtxn.commit()?;

View File

@@ -7,7 +7,7 @@ edition = "2021"
pubky-common = { version = "0.1.0", path = "../pubky-common" }
pkarr = "2.1.0"
ureq = "2.10.0"
ureq = { version = "2.10.0", features = ["cookies"] }
thiserror = "1.0.62"
url = "2.5.2"
flume = { version = "0.11.0", features = ["select", "eventual-fairness"], default-features = false }

View File

@@ -65,14 +65,23 @@ impl PubkyClient {
/// Check the current sesison for a given Pubky in its homeserver.
pub fn session(&self, pubky: &PublicKey) -> Result<()> {
// TODO: use https://crates.io/crates/user-agent-parser to parse the session
// and get more informations from the user-agent.
let (homeserver, mut url) = self.resolve_pubky_homeserver(pubky)?;
url.set_path(&format!("/{}/sesison", pubky));
url.set_path(&format!("/{}/session", pubky));
let response = self
let mut bytes = vec![];
let reader = self
.request(HttpMethod::Get, &url)
.call()
.map_err(Box::new)?;
.map_err(Box::new)?
.into_reader()
.read_to_end(&mut bytes);
// TODO: return the decoded session
Ok(())
}