-
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.
feat: login endpoint and
Account
rename (#7)
<!-- Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. --> Introduces the `POST /api/v1/account/login` endpoint to generate access tokens for registered users and also migrates from `User` to `Account`.
- Loading branch information
1 parent
84021a8
commit bffda89
Showing
30 changed files
with
340 additions
and
93 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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
crates/core/src/user/model.rs → crates/core/src/account/model.rs
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,24 @@ | ||
use http::StatusCode; | ||
use thiserror::Error; | ||
|
||
use crate::error::HttpStatusCode; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AuthErrorCode { | ||
#[error("Provided credentials are not valid")] | ||
InvalidCredentials, | ||
} | ||
|
||
impl HttpStatusCode for AuthErrorCode { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
AuthErrorCode::InvalidCredentials => StatusCode::BAD_REQUEST, | ||
} | ||
} | ||
|
||
fn error_code(&self) -> &'static str { | ||
match self { | ||
AuthErrorCode::InvalidCredentials => "INVALID_CREDENTIALS", | ||
} | ||
} | ||
} |
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,2 @@ | ||
pub mod error; | ||
pub mod service; |
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,40 @@ | ||
use std::sync::Arc; | ||
|
||
use matrix::client::resources::login::Login; | ||
use matrix::Client as MatrixAdminClient; | ||
|
||
use crate::util::secret::Secret; | ||
use crate::Result; | ||
|
||
pub struct LoginCredentials { | ||
pub username: String, | ||
pub password: Secret, | ||
} | ||
|
||
pub struct LoginCredentialsResponse { | ||
pub access_token: Secret, | ||
} | ||
|
||
pub struct AuthService { | ||
admin: Arc<MatrixAdminClient>, | ||
} | ||
|
||
impl AuthService { | ||
pub fn new(admin: Arc<MatrixAdminClient>) -> Self { | ||
Self { admin } | ||
} | ||
|
||
pub async fn login(&self, credentials: LoginCredentials) -> Result<LoginCredentialsResponse> { | ||
let login_response = Login::login_credentials( | ||
&self.admin, | ||
credentials.username, | ||
credentials.password.inner(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(LoginCredentialsResponse { | ||
access_token: Secret::new(login_response.access_token), | ||
}) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1 @@ | ||
pub mod client; | ||
pub mod resources; | ||
|
||
pub use client::Client; |
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 @@ | ||
pub mod resources; |
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,48 @@ | ||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::instrument; | ||
|
||
use crate::http::Client; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct LoginCredentials { | ||
pub access_token: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct LoginCredentialsPayload { | ||
pub r#type: &'static str, | ||
pub user: String, | ||
pub password: String, | ||
} | ||
|
||
pub struct Login; | ||
|
||
impl Login { | ||
/// Retrieves an access token by logging in with Username and Password | ||
/// | ||
/// This is equivalent to executing: | ||
/// | ||
/// ```ignore | ||
/// curl -sS -d '{"type":"m.login.password", "user":"X", "password":"Y"}' http://server:port/_matrix/client/v3/login | ||
/// ``` | ||
#[instrument(skip(client, username, password))] | ||
pub async fn login_credentials( | ||
client: &Client, | ||
username: impl AsRef<str>, | ||
password: impl AsRef<str>, | ||
) -> Result<LoginCredentials> { | ||
let resp = client | ||
.post_json( | ||
"/_matrix/client/v3/login", | ||
&LoginCredentialsPayload { | ||
r#type: "m.login.password", | ||
user: username.as_ref().to_string(), | ||
password: password.as_ref().to_string(), | ||
}, | ||
) | ||
.await?; | ||
|
||
Ok(resp.json().await?) | ||
} | ||
} |
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 @@ | ||
pub mod login; |
File renamed without changes.
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
Oops, something went wrong.