-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sqlite and postgres database draft
- Loading branch information
Showing
26 changed files
with
631 additions
and
261 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
37 changes: 19 additions & 18 deletions
37
crates/common/src/model/auth/login.rs → crates/common/src/auth/login.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
/* Photos.network · A privacy first photo storage and sharing service for fediverse. | ||
* Copyright (C) 2020 Photos network developers | ||
* | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
use crate::sensitive::Sensitive; | ||
|
||
//! Login request | ||
//! | ||
//! Provides an abstraction over a vlue for sensitive data like passwords. | ||
//! It is not printing its value to logs or tracing | ||
//! | ||
//! | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::model::sensitive::Sensitive; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Default)] | ||
pub struct Login { | ||
pub username_or_email: Sensitive<String>, | ||
pub password: Sensitive<String>, | ||
pub totp_2fa_token: Option<String>, | ||
pub username_or_email: Sensitive<String>, | ||
pub password: Sensitive<String>, | ||
pub totp_2fa_token: Option<String>, | ||
} | ||
|
||
//! Login response | ||
//! | ||
//! * `jwt` - None if email verification is enabled. | ||
//! * `verify_email_sent` - Indicates if an email verification is needed. | ||
//! | ||
#[skip_serializing_none] | ||
// Login response | ||
// | ||
// * `jwt` - None if email verification is enabled. | ||
// * `verify_email_sent` - Indicates if an email verification is needed. | ||
// | ||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
pub struct LoginResponse { | ||
pub jwt: Option<Sensitive<String>>, | ||
pub registration_created: bool, | ||
pub verify_email_sent: bool, | ||
pub jwt: Option<Sensitive<String>>, | ||
pub registration_created: bool, | ||
pub verify_email_sent: bool, | ||
} |
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 login; | ||
pub mod user; |
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,12 @@ | ||
use async_trait::async_trait; | ||
use std::error::Error; | ||
|
||
use crate::auth::user::User; | ||
|
||
#[async_trait] | ||
pub trait Database { | ||
async fn setup(&mut self) -> Result<(), Box<dyn Error>>; | ||
async fn create_user(&self, user: &User) -> Result<(), Box<dyn Error>>; | ||
async fn update_email(&self, email: &str, user_id: &str) -> Result<(), Box<dyn Error>>; | ||
async fn get_users(&self) -> Result<Vec<User>, Box<dyn Error>>; | ||
} |
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 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
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,112 +1,3 @@ | ||
/* Photos.network · A privacy first photo storage and sharing service for fediverse. | ||
* Copyright (C) 2020 Photos network developers | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
//! This crate offers a database abstraction for [Photos.network](https://photos.network) core application. | ||
//! | ||
use async_trait::async_trait; | ||
use sqlx::PgPool; | ||
use sqlx::Row; | ||
use std::error::Error; | ||
use tracing::{error, info}; | ||
use uuid::Uuid; | ||
|
||
pub struct User { | ||
pub uuid: String, | ||
pub email: String, | ||
pub password: String, | ||
pub lastname: String, | ||
pub firstname: String, | ||
} | ||
|
||
#[async_trait] | ||
pub trait Database { | ||
async fn setup(&mut self) -> Result<(), Box<dyn Error>>; | ||
async fn create_user(&self, user: &User) -> Result<(), Box<dyn Error>>; | ||
async fn update_user(&self, user: &User) -> Result<(), Box<dyn Error>>; | ||
async fn get_users(&self) -> Result<Vec<User>, Box<dyn Error>>; | ||
} | ||
|
||
pub struct PostgresDatabase { | ||
pub pool: PgPool, | ||
} | ||
|
||
impl PostgresDatabase { | ||
pub async fn new(db_url: &str) -> Self { | ||
let pool = sqlx::postgres::PgPool::connect(db_url).await.unwrap(); | ||
|
||
PostgresDatabase { pool } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Database for PostgresDatabase { | ||
async fn setup(&mut self) -> Result<(), Box<dyn Error>> { | ||
// run migrations from `migrations` directory | ||
sqlx::migrate!("./migrations").run(&self.pool).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn create_user(&self, user: &User) -> Result<(), Box<dyn Error>> { | ||
let query = "INSERT INTO users (uuid, email, password, lastname, firstname) VALUES ($1, $2, $3, $4, $5)"; | ||
let id = Uuid::new_v4().hyphenated().to_string(); | ||
info!("create new user with id `{}`.", id); | ||
sqlx::query(query) | ||
.bind(id) | ||
.bind(&user.email) | ||
.bind(&user.password) | ||
.bind(&user.lastname) | ||
.bind(&user.firstname) | ||
.execute(&self.pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn update_user(&self, user: &User) -> Result<(), Box<dyn Error>> { | ||
let query = "UPDATE users SET email = %1 WHERE uuid = $2"; | ||
|
||
sqlx::query(query) | ||
.bind(&user.email) | ||
.bind(&user.uuid) | ||
.execute(&self.pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn get_users(&self) -> Result<Vec<User>, Box<dyn Error>> { | ||
let query = "SELECT uuid, email, password, lastname, firstname FROM users"; | ||
|
||
let res = sqlx::query(query); | ||
|
||
let rows = res.fetch_all(&self.pool).await?; | ||
|
||
let users = rows | ||
.iter() | ||
.map(|row| User { | ||
uuid: row.get("uuid"), | ||
email: row.get("email"), | ||
password: row.get("password"), | ||
lastname: row.get("lastname"), | ||
firstname: row.get("firstname"), | ||
}) | ||
.collect(); | ||
|
||
Ok(users) | ||
} | ||
} | ||
//pub mod postgres; | ||
pub mod postgres; | ||
pub mod sqlite; |
Oops, something went wrong.