Minor refactoring

This commit is contained in:
Niels Andriesse 2021-03-18 13:21:10 +11:00
parent 672aa9a199
commit af03aba183
4 changed files with 6 additions and 24 deletions

View File

@ -24,28 +24,7 @@ apt install certbot // Only if you don't have certbot installed already
certbot certonly
```
Follow the instructions on-screen and then **copy** (don't move) the generated certificate and private key to the session-open-group folder (you'll need to rename them to tls_private_key.pem and tls_certificate respectively as well).
**Just want to run locally?**
Open src/main.rs and replace the following lines:
```
warp::serve(routes)
.tls()
.cert_path("tls_certificate.pem")
.key_path("tls_private_key.pem")
.run(([0, 0, 0, 0], 443))
.await;
```
by:
```
warp::serve(routes)
.run(([127, 0, 0, 1], 8080))
.await;
```
Follow the instructions on-screen and then copy the generated certificate and private key to the session-open-group folder (you'll need to rename them to tls_private_key.pem and tls_certificate respectively as well).
### Step 3: Build the project

View File

@ -201,6 +201,7 @@ pub async fn delete_message(row_id: i64, auth_token: Option<String>, pool: &stor
// Check authorization level
let (has_authorization_level, requesting_public_key) = has_authorization_level(auth_token, AuthorizationLevel::Basic, pool).await?;
if !has_authorization_level { return Err(warp::reject::custom(Error::Unauthorized)); }
// Check that the requesting user is either the sender of the message or a moderator
let sender_option: Option<String> = {
let conn = pool.get().map_err(|_| Error::DatabaseFailedInternally)?;
let raw_query = format!("SELECT public_key FROM {} WHERE rowid = (?1)", storage::MESSAGES_TABLE);
@ -283,7 +284,7 @@ pub async fn get_moderators(pool: &storage::DatabaseConnectionPool) -> Result<Re
return Ok(warp::reply::json(&public_keys).into_response());
}
/// Bans the given `public_key`, if the requesting user is a moderator.
/// Bans the given `public_key` if the requesting user is a moderator.
pub async fn ban(public_key: &str, auth_token: Option<String>, pool: &storage::DatabaseConnectionPool) -> Result<Response, Rejection> {
// Validate the public key
if !is_valid_public_key(&public_key) {
@ -313,7 +314,7 @@ pub async fn ban(public_key: &str, auth_token: Option<String>, pool: &storage::D
return Ok(StatusCode::OK.into_response());
}
/// Unbans the given `public_key`, if the requesting user is a moderator.
/// Unbans the given `public_key` if the requesting user is a moderator.
pub async fn unban(public_key: &str, auth_token: Option<String>, pool: &storage::DatabaseConnectionPool) -> Result<Response, Rejection> {
// Validate the public key
if !is_valid_public_key(&public_key) {

View File

@ -154,6 +154,7 @@ async fn handle_delete_request(rpc_call: RpcCall, uri: http::Uri, auth_token: Op
}
// Utilities
fn get_auth_token(rpc_call: &RpcCall) -> Option<String> {
if rpc_call.headers.is_empty() { return None; }
let headers: HashMap<String, String> = match serde_json::from_str(&rpc_call.headers) {

View File

@ -54,6 +54,7 @@ pub fn create_tables_if_needed(conn: &DatabaseConnection) {
)", PENDING_TOKENS_TABLE);
conn.execute(&pending_tokens_table_cmd, params![]).expect("Couldn't create pending tokens table.");
// Tokens
// The token is stored as hex here (rather than as bytes) because it's more convenient for lookup
let tokens_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
public_key STRING PRIMARY KEY,