diff options
author | Galen Guyer <galen@galenguyer.com> | 2022-04-15 22:19:13 -0400 |
---|---|---|
committer | Galen Guyer <galen@galenguyer.com> | 2022-04-15 22:20:08 -0400 |
commit | 3d2681c140e6b2be39ad29a0a76f393d12eb30e1 (patch) | |
tree | 1601eb2b9f740f3081153d023ce2cafb181bd7ba | |
parent | c9d9c9d70a661fa5d00a375645ec80ecf02a62c6 (diff) |
add uuid id for users
-rw-r--r-- | migrations/2022-04-09-create-schema.sql | 9 | ||||
-rw-r--r-- | src/db/models.rs | 1 | ||||
-rw-r--r-- | src/db/strings.rs | 4 | ||||
-rw-r--r-- | src/extractors.rs | 7 | ||||
-rw-r--r-- | src/routes/v1/users.rs | 2 |
5 files changed, 16 insertions, 7 deletions
diff --git a/migrations/2022-04-09-create-schema.sql b/migrations/2022-04-09-create-schema.sql index 663e44f..238020f 100644 --- a/migrations/2022-04-09-create-schema.sql +++ b/migrations/2022-04-09-create-schema.sql @@ -1,5 +1,8 @@ +CREATE extension IF NOT EXISTS "uuid-ossp"; + CREATE TABLE users ( - email varchar(255) NOT NULL UNIQUE PRIMARY KEY, + id varchar(64) PRIMARY KEY DEFAULT uuid_generate_v4(), + email varchar(255) NOT NULL UNIQUE, password varchar(255) NOT NULL, display_name varchar(255), created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), @@ -13,8 +16,8 @@ CREATE TABLE zones ( id varchar(255) NOT NULL UNIQUE PRIMARY KEY, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), modified_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'), - owner_email varchar(255) NOT NULL, - CONSTRAINT owner_email_fk FOREIGN KEY (owner_email) REFERENCES users (email) ON DELETE CASCADE + owner_uuid varchar(64) NOT NULL, + constraint owner_uuid_fk foreign key (owner_uuid) references users (id) ); CREATE OR REPLACE FUNCTION update_modified_column() diff --git a/src/db/models.rs b/src/db/models.rs index 10d5b57..6e2e50a 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -4,6 +4,7 @@ use sqlx::FromRow; #[derive(Serialize, Deserialize, FromRow, Debug)] pub struct User { + pub id: String, pub email: String, #[serde(skip_serializing)] pub password: String, diff --git a/src/db/strings.rs b/src/db/strings.rs index 131f965..45b8575 100644 --- a/src/db/strings.rs +++ b/src/db/strings.rs @@ -2,12 +2,12 @@ use lazy_static::lazy_static; lazy_static! { pub(crate) static ref GET_USER: &'static str = r" - SELECT email,password,display_name,created_at,modified_at,admin,enabled,totp_secret + SELECT id,email,password,display_name,created_at,modified_at,admin,enabled,totp_secret FROM users WHERE email = $1 "; pub(crate) static ref GET_ALL_USERS: &'static str = r" - SELECT email,password,display_name,created_at,modified_at,admin,enabled,totp_secret + SELECT id,email,password,display_name,created_at,modified_at,admin,enabled,totp_secret FROM users "; pub(crate) static ref CREATE_USER: &'static str = r" diff --git a/src/extractors.rs b/src/extractors.rs index 7954cb2..907ba44 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -59,7 +59,12 @@ where let token = header.replace("Bearer ", ""); let claims: BTreeMap<String, String> = match token.verify_with_key(&key) { Ok(claims) => claims, - Err(_) => return Err((StatusCode::UNAUTHORIZED, axum::Json(json!({ "error": "Invalid token" })))), + Err(_) => { + return Err(( + StatusCode::UNAUTHORIZED, + axum::Json(json!({ "error": "Invalid token" })), + )) + } }; let token = Token { diff --git a/src/routes/v1/users.rs b/src/routes/v1/users.rs index 5fb1595..0046b61 100644 --- a/src/routes/v1/users.rs +++ b/src/routes/v1/users.rs @@ -125,7 +125,7 @@ pub async fn login( // https://www.iana.org/assignments/jwt/jwt.xhtml claims.insert("iss", "fdns"); - claims.insert("sub", &user.email); + claims.insert("sub", &user.id); claims.insert("iat", &iat); claims.insert("exp", &exp); claims.insert("dn", &dn); |