-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alpha release (Account management API 1/2) (#51)
First three routes are tested, all the other ones just need to have their tests implemented except email stuff which I'm leaving for another day. | Account | route | commune-server | commune-rs | | :--- | :--- | :---: | :---: | | Create Account | `POST /account` | ✅ | ✅ | | Login | `POST /login` | ✅ | ✅ | | Logout | `GET /logout` | ✅ | ✅ | | Validate Session | `GET /account/session` | ✅ | ❌ | | Validate Token | `POST /account/token` | ✅ | ❌ | | Send Password Recovery Code| `POST /account/password` | ✅ | ❌ | | Verify Password | `POST /account/password/verify` | ✅ | ❌ | | Reset Password | `POST /account/password/reset` | ✅ | ❌ | | Update Password | `POST /account/password/update` | ✅ | ❌ | | Send Verifications Code | `POST /account/verify/code` | ✅ | ❌ | | Verify Code | `POST /account/verify` | ✅ | ❌ | | Verify Email | `POST /account/verify/email` | ✅ | ❌ | | Update Display Name | `POST /account/display_name` | ✅ | ❌ | | Update Avatar | `POST /account/avatar` | ✅ | ❌ | | Check Username Availability | `GET /username` | ✅ | ❌ | | Validate Email | `GET /email` | ✅ | ❌ |
- Loading branch information
Showing
172 changed files
with
3,382 additions
and
5,233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
password = "" | ||
tls = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
use email_address::EmailAddress; | ||
use matrix::admin::registration_tokens::new::*; | ||
use rand::{distributions::Uniform, prelude::Distribution}; | ||
|
||
use crate::{commune, error::Result}; | ||
|
||
pub async fn service(address: EmailAddress) -> Result<()> { | ||
let uni = Uniform::new('0', '9'); | ||
let token: String = uni.sample_iter(rand::thread_rng()).take(6).collect(); | ||
|
||
let req = Request::new( | ||
token.clone(), | ||
1, | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
// panics below should never happen | ||
.expect("system time overflow") | ||
.as_millis() | ||
.try_into() | ||
.expect("system time overflow"), | ||
); | ||
|
||
commune() | ||
.send_matrix_request(req, Some(&commune().config.matrix.admin_token.inner())) | ||
.await?; | ||
|
||
commune().send_email_verification(address, token).await?; | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use matrix::client::{login::*, uiaa::UserIdentifier}; | ||
|
||
use crate::{commune, error::Result, util::secret::Secret}; | ||
|
||
pub async fn service(username: impl Into<String>, password: &Secret) -> Result<Response> { | ||
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(Into::into) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use matrix::client::logout::root::*; | ||
|
||
use crate::{commune, error::Result}; | ||
|
||
pub async fn service(access_token: impl AsRef<str>) -> Result<Response> { | ||
let req = Request::new(); | ||
|
||
commune() | ||
.send_matrix_request(req, Some(access_token.as_ref())) | ||
.await | ||
.map_err(Into::into) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::Result, util::secret::Secret}; | ||
|
||
pub async fn service( | ||
access_token: impl AsRef<str>, | ||
username: impl Into<String>, | ||
old_password: Secret, | ||
new_password: Secret, | ||
) -> Result<Response> { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use http::StatusCode; | ||
use matrix::{ | ||
client::{ | ||
register::root::*, | ||
uiaa::{Auth, AuthData, AuthType, Dummy, UiaaResponse}, | ||
}, | ||
ruma_client::Error::FromHttpResponse, | ||
ruma_common::api::error::{FromHttpResponseError, MatrixError, MatrixErrorBody}, | ||
}; | ||
|
||
use crate::{commune, error::Result, util::secret::Secret}; | ||
|
||
pub async fn service(username: impl Into<String>, password: Secret) -> Result<Response> { | ||
let req = Request::new( | ||
username.into(), | ||
password.inner(), | ||
Some("commune".to_owned()), | ||
None, | ||
None, | ||
); | ||
|
||
let mut retry_req = req.clone(); | ||
|
||
match commune().send_matrix_request(req, None).await { | ||
Ok(resp) => Ok(resp), | ||
Err(e) => match e { | ||
FromHttpResponse(FromHttpResponseError::Server(MatrixError { | ||
status_code: StatusCode::UNAUTHORIZED, | ||
body: MatrixErrorBody::Json(ref body), | ||
})) => { | ||
let UiaaResponse { flows, session, .. } = | ||
serde_json::from_value::<UiaaResponse>(body.clone()).unwrap(); | ||
|
||
match flows.as_slice() { | ||
[value] => match value.stages.as_slice() { | ||
[AuthType::Dummy] => { | ||
retry_req.auth = Some(Auth::new(AuthData::Dummy(Dummy {}), session)); | ||
|
||
commune() | ||
.send_matrix_request(retry_req, None) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
_ => Err(e.into()), | ||
}, | ||
_ => Err(e.into()), | ||
} | ||
} | ||
|
||
_ => Err(e.into()), | ||
}, | ||
} | ||
} |
Oops, something went wrong.