Skip to content

Commit

Permalink
Lowercase entity slugs and email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
marcua committed Aug 13, 2023
1 parent 8201c2a commit cb8f056
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 1 addition & 3 deletions migrations/postgres/20230720_email_authentication.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
CREATE EXTENSION IF NOT EXISTS citext;

CREATE TABLE authentication_method (
id SERIAL NOT NULL,
entity_id INT NOT NULL,
method_type SMALLINT NOT NULL,
status SMALLINT NOT NULL,
email_address CITEXT NOT NULL,
email_address VARCHAR(256) NOT NULL,

PRIMARY KEY(id),
FOREIGN KEY(entity_id) REFERENCES entity(id),
Expand Down
2 changes: 1 addition & 1 deletion migrations/sqlite/20230720_email_authentication.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE authentication_method (
entity_id INT NOT NULL,
method_type SMALLINT NOT NULL,
status SMALLINT NOT NULL,
email_address TEXT COLLATE NOCASE NOT NULL,
email_address VARCHAR(256) NOT NULL,

FOREIGN KEY(entity_id) REFERENCES entity(id),
UNIQUE(method_type, email_address)
Expand Down
8 changes: 4 additions & 4 deletions src/http/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::http::structs::{
EntityDatabasePath,
};
use crate::http::tokens::{decrypt_auth_token, encrypt_auth_token};
use crate::http::utils::get_header;
use crate::http::utils::{get_header, get_lowercased_header};
use actix_web::{post, web, HttpRequest, HttpResponse};

#[post("/v1/confirm")]
Expand Down Expand Up @@ -99,7 +99,7 @@ async fn log_in(
ayb_db: web::Data<Box<dyn AybDb>>,
ayb_config: web::Data<AybConfig>,
) -> Result<HttpResponse, AybError> {
let entity = get_header(&req, "entity")?;
let entity = get_lowercased_header(&req, "entity")?;
let desired_entity = ayb_db.get_entity(&entity).await;

if let Ok(instantiated_entity) = desired_entity {
Expand Down Expand Up @@ -154,8 +154,8 @@ async fn register(
ayb_db: web::Data<Box<dyn AybDb>>,
ayb_config: web::Data<AybConfig>,
) -> Result<HttpResponse, AybError> {
let entity = get_header(&req, "entity")?;
let email_address = get_header(&req, "email-address")?;
let entity = get_lowercased_header(&req, "entity")?;
let email_address = get_lowercased_header(&req, "email-address")?;
let entity_type = get_header(&req, "entity-type")?;
let desired_entity = ayb_db.get_entity(&entity).await;
// Ensure that there are no authentication methods aside from
Expand Down
4 changes: 4 additions & 0 deletions src/http/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ pub fn get_header(req: &HttpRequest, header_name: &str) -> Result<String, AybErr
}),
}
}

pub fn get_lowercased_header(req: &HttpRequest, header_name: &str) -> Result<String, AybError> {
return Ok(get_header(req, header_name)?.to_lowercase());
}

0 comments on commit cb8f056

Please sign in to comment.