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

feat: endpoints for retrieving and updating profiles #58

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e9d79e7
WIP
avdb13 Feb 24, 2024
daa14d1
feat: Ruma API macro to clear up HTTP boilerplate
avdb13 Feb 24, 2024
dda107f
feat: reuse `User` type across all requests
avdb13 Feb 24, 2024
8a993bb
feat: admin migration complete, client wip
avdb13 Feb 25, 2024
e0da214
fix: client room API
avdb13 Feb 27, 2024
a431172
docs
avdb13 Feb 27, 2024
cad450f
fix: client session API
avdb13 Feb 27, 2024
cdc6c14
Matrix API finished
avdb13 Feb 27, 2024
8979119
feat: handler addition, one to rule them all
avdb13 Feb 28, 2024
eb6ccd0
wip
avdb13 Feb 29, 2024
a4077e4
fix: `OutgoingRequest`/`IncomingResponse` macro expanded
avdb13 Mar 1, 2024
a56c202
matrix crate compiles
avdb13 Mar 2, 2024
33e63ca
chore: formatting Cargo.toml
avdb13 Mar 8, 2024
0234008
fundamental changes
avdb13 Mar 9, 2024
dfed96a
suspicious failed registration test
avdb13 Mar 14, 2024
3c8341d
chore: requirement to automate the dummy auth flow
avdb13 Mar 18, 2024
ca8393a
feat: skip auth flows that only contain a dummy stage
avdb13 Mar 23, 2024
432b9b7
chore: enable end-to-end testing again
avdb13 Mar 23, 2024
4b4cd43
feat: registration/login tests
avdb13 Mar 24, 2024
7cad026
draft: sync handle (credits to `ruma/ruma-client-api`)
avdb13 Mar 24, 2024
ba38495
wip: account management API
avdb13 Mar 26, 2024
c2cb34c
chore: account management tests
avdb13 Mar 26, 2024
38bbb29
formatting ...
avdb13 Mar 26, 2024
c5dc99d
homeserver.yaml
avdb13 Mar 26, 2024
fe4cf62
chore: testing username availability
avdb13 Mar 27, 2024
6e4f886
chore: finish second part of tests
avdb13 Mar 27, 2024
f6f7c96
wip: email verification
avdb13 Mar 28, 2024
5ec2088
let us worry about email verification later
avdb13 Mar 28, 2024
42bdb26
email verification working, just need to complete the test
avdb13 Mar 28, 2024
9649cbb
wip: spaces
avdb13 Mar 28, 2024
17390a2
feat: implemented space creation
avdb13 Mar 29, 2024
6237893
feate: implemented space child creation
avdb13 Mar 29, 2024
be141c1
feat: space creation
avdb13 Mar 29, 2024
a0f0a7f
correction of shell operator usage in Justfile
avdb13 Apr 10, 2024
0f81242
mirrored `ruma::api::client::error`
avdb13 Apr 10, 2024
32b7de2
tests pass
avdb13 Apr 12, 2024
535055b
feat: retrieving profiles and updating profiles
avdb13 Apr 16, 2024
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
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ jobs:
env:
COMMUNE_SYNAPSE_ADMIN_TOKEN: ${{ env.COMMUNE_SYNAPSE_ADMIN_TOKEN }}
run: |
# just e2e -- --show-output
echo "warning: end-to-end tests are temporarily disabled"
just e2e -- --show-output

- name: Print Logs
if: always()
Expand Down
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ Cargo.lock
# Development
/docker/*
!/docker/.gitkeep
!/docker/postgre
.env
access_token.txt
commune.toml
dump.sql

# System Specific
.DS_Store
50 changes: 36 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,66 @@ repository = "https://github.com/commune-os/commune-rs"
rust-version = "1.75.0"

[workspace]
default-members = ["crates/server"]
members = ["crates/core", "crates/matrix", "crates/server", "crates/test"]
members = ["crates/core", "crates/matrix", "crates/router", "crates/test"]
default-members = ["crates/router"]
resolver = "2"

[workspace.dependencies]
anyhow = "1.0.75"
as_variant = "1.2.0"
axum-extra = { version = "0.9.3", features = ["typed-header", "cookie"] }
async-trait = "0.1.74"
axum = { version = "0.7.4", features = ["tokio"] }
chrono = { version = "0.4.34", features = ["serde"] }
dotenv = "0.15.0"
# async-stream = "0.3.5"
base64 = "0.22.0"
bytes = "1.5.0"
email_address = { version = "0.2.4", features = ["serde", "serde_support"] }
figment = { version = "0.10.14", features = ["toml", "env"] }
hex = "0.4.3"
tokio-rustls = "0.25.0"
# futures = "0.3.30"
hmac = "0.12.1"
sha1 = "0.10.6"
anyhow = "1.0.75"
axum = { version = "0.7.4", features = ["tokio", "macros"] }
http = "0.2.11"
mime = "0.3.17"
mail-send = "0.4.7"
maud = "0.26.0"
headers = "0.4.0"
# openssl = { version = "0.10.63", features = ["vendored"] }
# openssl-sys = { version = "0.9.99", features = ["vendored"] }
reqwest = { version = "0.11.22", default-features = false, features = [
"blocking",
"json",
"multipart",
"rustls",
] }
serde = "1.0.192"
serde_json = "1.0.108"
serde_path_to_error = "0.1.14"
serde_qs = "0.12.0"
sha1 = "0.10.6"
serde_json = "1.0.114"
time = "0.3.34"
tokio = "1.34.0"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.4.1"
uuid = { version = "1.6.1", features = ["v4"] }
handlebars = "5.0.0"
lettre = "0.11"
rand = "0.8.5"
ring = "0.17.8"
thiserror = "1.0.50"
validator = { version = "0.16", features = ["derive"] }

router = { workspace = true, path = "crates/router" }
matrix = { workspace = true, path = "crates/matrix" }
commune = { workspace = true, path = "crates/core" }

ruma-events = { version = "0.27.11", default_features = false, features = [
"html",
"markdown",
] }
ruma-common = { version = "0.12.0", default_features = false, features = [
"api",
"rand",
] }
ruma-macros = { version = "0.12.0", default_features = false }
ruma-client = { version = "0.12.0", default_features = false }
ruma-identifiers-validation = { version = "0.9.3", default_features = false }

[workspace.lints.rust]
unreachable_pub = "warn"
unsafe_code = "forbid"
Expand Down
13 changes: 6 additions & 7 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ target_release := "x86_64-unknown-linux-musl"
default:
just --list

# Creates the `.env` file if it doesn't exist
# Creates the `commune.toml` file if it doesn't exist
# This indicates the first invocation of `just` so we also
# create the docker folders while we're at it
dotenv:
export DOCKER_USER="$(id -u):$(id -g)" && \
cp -n .env.example .env || true && \
mkdir -p docker/synapse || true
export DOCKER_USER="$(id -u):$(id -g)"; \
cp -n commune-example.toml commune.toml && true; \
mkdir -p docker/synapse && true

# Dump database to a file
backup_db:
Expand All @@ -35,7 +35,6 @@ gen_synapse_conf: dotenv
docker run -i --rm \
-u "$(id -u):$(id -g)" \
-v ./docker/synapse:/data \
--env-file .env \
matrixdotorg/synapse:v1.96.1 generate

# Generates a de-facto admin user
Expand All @@ -49,10 +48,10 @@ gen_synapse_admin: dotenv

# Retrieves admin access token uses de-facto admin user and Development Database Credentials
get_access_token:
sed -i "s/COMMUNE_SYNAPSE_ADMIN_TOKEN='.*'/COMMUNE_SYNAPSE_ADMIN_TOKEN='$( \
sed -i "s/admin_token:\s'.*'/admin_token:\s'$( \
curl -sS -d '{"type":"m.login.password", "user":"admin", "password":"admin"}' \
http://localhost:8008/_matrix/client/v3/login | jq --raw-output '.access_token' \
)'/" .env
)'/" commune-example.toml

# Runs backend dependency services
backend *args='': dotenv
Expand Down
23 changes: 23 additions & 0 deletions commune-example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
registration_verification = false
public_loopback = false
port = 6421
tls = true

# Either one works but not both
blocked_domains = []
# allowed_domains = ['gmail.com', 'outlook.com']

# `X-Forwarded-For` header
# xff = false

[matrix]
server_name = "matrix.localhost"
host = "http://0.0.0.0:8008"
admin_token = "syt_YWRtaW4_bqFYSIwUqELzKrQeXkMk_1PsWpx"
shared_registration_secret = "m@;wYOUOh0f:CH5XA65sJB1^q01~DmIriOysRImot,OR_vzN&B"

[mail]
host = "smtp://0.0.0.0:1025"
username = "user"
password = "pass"
tls = false
23 changes: 23 additions & 0 deletions commune.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
registration_verification = false
public_loopback = false
port = 6421
tls = true

# Either one works but not both
blocked_domains = []
# allowed_domains = ['gmail.com', 'outlook.com']

# `X-Forwarded-For` header
# xff = false

[matrix]
server_name = "matrix.localhost"
host = "http://0.0.0.0:8008"
admin_token = "syt_YWRtaW4_FllbTksPWcQaDRUVVcYR_3LJQZ2"
shared_registration_secret = "m@;wYOUOh0f:CH5XA65sJB1^q01~DmIriOysRImot,OR_vzN&B"

[mail]
host = "smtp://0.0.0.0:1025"
username = "user"
password = "pass"
tls = false
15 changes: 11 additions & 4 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ path = "src/lib.rs"

[dependencies]
# Workspace Dependencies
handlebars = { workspace = true }
lettre = { workspace = true }
reqwest = { workspace = true }
anyhow = { workspace = true }
axum = { workspace = true }
rand = { workspace = true }
email_address = { workspace = true }
thiserror = { workspace = true }
validator = { workspace = true, features = ["derive"] }
http = { workspace = true }
mail-send = { workspace = true }
maud = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true, features = ["serde"] }
figment = { workspace = true }
url = { workspace = true, features = ["serde"] }
tokio = { workspace = true, features = ["full"] }
headers = { workspace = true }
tokio-rustls = { workspace = true }
time = { workspace = true }

# Local Dependencies
matrix = { path = "../matrix" }
matrix = { path = "../matrix", features = ["client"] }
8 changes: 8 additions & 0 deletions crates/core/src/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod email;
pub mod login;
pub mod logout;
pub mod password;
pub mod register;
pub mod token;
pub mod username;
pub mod whoami;
26 changes: 26 additions & 0 deletions crates/core/src/account/email.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::time::UNIX_EPOCH;

use email_address::EmailAddress;
use matrix::admin::registration_tokens::new::*;
use rand::{distributions::Uniform, prelude::Distribution};

use crate::{commune, error::Error};

pub async fn service(address: EmailAddress) -> Result<String, Error> {
let uni = Uniform::new('0', '9');
let token: String = uni.sample_iter(rand::thread_rng()).take(6).collect();

// TODO: decide on whether to use timezones or UTC
let offset = UNIX_EPOCH.elapsed().unwrap().as_millis() + 60 * 60 * 60;
let req = Request::new(token.clone(), 1, offset.try_into().unwrap());

commune()
.send_matrix_request(req, Some(&commune().config.matrix.admin_token.inner()))
.await?;

commune()
.send_email_verification(address, token.clone())
.await?;

Ok(token)
}
38 changes: 0 additions & 38 deletions crates/core/src/account/error.rs

This file was deleted.

21 changes: 21 additions & 0 deletions crates/core/src/account/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use matrix::client::{login::*, uiaa::UserIdentifier};

use crate::{commune, error::Error, util::secret::Secret};

pub async fn service(username: impl Into<String>, password: &Secret) -> Result<Response, Error> {
let req = Request::new(
LoginType::Password {
password: password.inner(),
},
Some(UserIdentifier::User {
user: username.into(),
}),
"commune".to_owned(),
Some(true),
);

commune()
.send_matrix_request(req, None)
.await
.map_err(|e| e.into())
}
12 changes: 12 additions & 0 deletions crates/core/src/account/logout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use matrix::client::logout::root::*;

use crate::{commune, error::Error};

pub async fn service(access_token: impl AsRef<str>) -> Result<Response, Error> {
let req = Request::new();

commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await
.map_err(Into::into)
}
3 changes: 0 additions & 3 deletions crates/core/src/account/mod.rs

This file was deleted.

15 changes: 0 additions & 15 deletions crates/core/src/account/model.rs

This file was deleted.

20 changes: 20 additions & 0 deletions crates/core/src/account/password.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use matrix::{client::account::password::*, ruma_common::UserId};

use crate::{commune, error::Error, util::secret::Secret};

pub async fn service(
access_token: impl AsRef<str>,
username: impl Into<String>,
old_password: Secret,
new_password: Secret,
) -> Result<Response, Error> {
let server_name = &crate::commune().config.matrix.server_name;
let user_id = UserId::parse_with_server_name(username.into(), server_name)?;

let req = Request::new(new_password.inner()).with_password(user_id, old_password.inner());

commune()
.send_matrix_request(req, Some(access_token.as_ref()))
.await
.map_err(Into::into)
}
Loading