-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
645 additions
and
34 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
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
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
Empty file.
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 @@ | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
||
CREATE TABLE IF NOT EXISTS | ||
"users" ( | ||
id UUID DEFAULT uuid_generate_v4() NOT NULL PRIMARY KEY, | ||
name VARCHAR(64), | ||
email VARCHAR(255) NOT NULL UNIQUE, | ||
phone INTEGER UNIQUE, | ||
account_type INTEGER DEFAULT 2, | ||
password VARCHAR(255), | ||
address varchar(255), | ||
avatar varchar(64), | ||
bio varchar(511), | ||
access_token text | ||
); | ||
|
||
CREATE SCHEMA IF NOT EXISTS "tower_sessions"; | ||
|
||
CREATE TABLE IF NOT EXISTS | ||
"tower_sessions"."sessions" ( | ||
id text PRIMARY KEY NOT NULL, | ||
data bytea NOT NULL, | ||
expiry_date timestamptz NOT NULL | ||
); |
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,79 @@ | ||
use axum::{Extension, Router}; | ||
use axum_login::{AuthManagerLayer, AuthManagerLayerBuilder}; | ||
use sqlx::{Pool, Postgres}; | ||
use tower_sessions::{Expiry, PostgresStore, SessionManagerLayer}; | ||
use tower_sessions::cookie::time::Duration; | ||
use app_core::database::SqlxManager; | ||
use common::config::Settings; | ||
use crate::provider::AuthProviders; | ||
use crate::routes; | ||
use crate::routes::auth::backend::Backend; | ||
|
||
pub struct WebApp { | ||
settings: Settings, | ||
database: SqlxManager | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct AppState { | ||
pub pool: Pool<Postgres>, | ||
pub settings: Settings, | ||
} | ||
|
||
impl WebApp { | ||
pub async fn new() -> Self { | ||
let settings = Settings::new("application.toml"); | ||
let database = SqlxManager::new(&settings.webserver.database) | ||
.await; | ||
|
||
sqlx::migrate!().run(&database.pool).await.expect("Failed to register tables"); | ||
|
||
Self { | ||
settings, | ||
database | ||
} | ||
} | ||
|
||
fn router(&self) -> Router { | ||
let state = AppState { | ||
pool: self.database.pool.clone(), | ||
settings: self.settings.clone(), | ||
}; | ||
|
||
Router::new() | ||
.nest("/api", | ||
routes::auth::router() | ||
) | ||
.with_state(state) | ||
.layer(self.auth_layer()) | ||
.layer(Extension(AuthProviders::new(&self.settings))) | ||
} | ||
|
||
fn session(&self) -> SessionManagerLayer<PostgresStore> { | ||
let session_store = PostgresStore::new(self.database.pool.clone()) | ||
.with_table_name("sessions") | ||
.unwrap(); | ||
|
||
SessionManagerLayer::new(session_store) | ||
.with_secure(false) | ||
.with_name("session_id") | ||
.with_expiry(Expiry::OnInactivity(Duration::days(10))) | ||
} | ||
|
||
fn auth_layer(&self) -> AuthManagerLayer<Backend, PostgresStore> { | ||
let backend = Backend::new( | ||
self.database.pool.clone() | ||
); | ||
|
||
AuthManagerLayerBuilder::new(backend, self.session()).build() | ||
} | ||
|
||
pub async fn serve(&self) { | ||
let listener = tokio::net::TcpListener::bind(self.settings.webserver.url()) | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, self.router()) | ||
.await | ||
.expect("Failed to start axum server"); | ||
} | ||
} |
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,41 @@ | ||
use oauth2::CsrfToken; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub enum Credentials { | ||
Password(LoginCreds), | ||
OAuth(OAuthCreds), | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct OAuthCreds { | ||
pub user: UserInfo, | ||
pub token: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct LoginCreds { | ||
pub email: String, | ||
pub password: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct SignCreds { | ||
pub email: String, | ||
pub name: String, | ||
pub phone: String, | ||
pub password: String, | ||
pub next: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct AuthzResp { | ||
pub code: String, | ||
pub state: CsrfToken, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct UserInfo { | ||
pub email: String, | ||
pub name: String | ||
} |
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 auth; |
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,20 +1,16 @@ | ||
use axum::Router; | ||
use core::database::SqlxManager; | ||
use common::config::Settings; | ||
use crate::app::WebApp; | ||
|
||
mod routes; | ||
mod models; | ||
mod app; | ||
pub mod json; | ||
mod provider; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let settings = Settings::new("application.toml"); | ||
let database = SqlxManager::new(&settings.webserver.database) | ||
.await; | ||
|
||
let router = Router::new() | ||
.with_state(database.pool); | ||
env_logger::init(); | ||
|
||
let listener = tokio::net::TcpListener::bind(settings.webserver.url()) | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, router) | ||
WebApp::new().await | ||
.serve() | ||
.await | ||
.expect("Failed to start axum server"); | ||
} |
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 users; |
Oops, something went wrong.