feat: test gotrue when app start, use state for gotrue client, use go… (#24)

* feat: test gotrue when app start, use state for gotrue client, use gotrue UUID instead of BIGINT for user uid

* feat: backward compatibility with native authetication

* fix: native auth use uuid instead

* fix: sqlx preparation

* fix: sqlx metadata and unused variable

* fix: clippy

* fix: create workspace if not exist after sign in

* chore: update .sqlx with new query

* feat: query workspace

* revert: revert using i64 for user id

* fix: update sqlx queries

* fix: update .sqlx metadata

* feat: only use uuid from sqlx types, select user_profile view

* ci: add version check

* ci: add missing sqlx files

* chore: cargo fmt
This commit is contained in:
Zack 2023-09-11 17:16:41 +08:00 committed by GitHub
parent d0fdde7bf5
commit 853f089ca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 367 additions and 73 deletions

View file

@ -42,4 +42,4 @@ jobs:
run: cargo fmt --check
- name: Clippy
run: SQLX_OFFLINE=true cargo clippy -- -D warnings
run: SQLX_OFFLINE=true cargo clippy -- -D warnings

View file

@ -0,0 +1,58 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT * FROM public.af_workspace WHERE owner_uid = $1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "workspace_id",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "database_storage_id",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "owner_uid",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 4,
"name": "workspace_type",
"type_info": "Int4"
},
{
"ordinal": 5,
"name": "deleted_at",
"type_info": "Timestamptz"
},
{
"ordinal": 6,
"name": "workspace_name",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
false,
true,
true,
true,
false,
true,
true
]
},
"hash": "23697d21dc4aa7a71027ac5671d415f3ead15394a18414911475fb946a00dd01"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT uid, password\n FROM af_user \n WHERE email = $1\n ",
"query": "\n SELECT uid, password\n FROM af_user\n WHERE email = $1\n ",
"describe": {
"columns": [
{
@ -24,5 +24,5 @@
false
]
},
"hash": "dd713d68bddf1e4d13230dee18a9ef6f6490ad4e70e5f2e0f54a4dad8db7fbbf"
"hash": "6b5d36d77c80339e481aece5d09bf7345c8c81a5e387c43cdd2eebd01f9c9436"
}

View file

@ -0,0 +1,76 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT *\n FROM public.af_user_profile_view WHERE uuid = $1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "uid",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "uuid",
"type_info": "Uuid"
},
{
"ordinal": 2,
"name": "email",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "password",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "name",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "encryption_sign",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "deleted_at",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "updated_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "latest_workspace_id",
"type_info": "Uuid"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
true,
true,
true,
true,
true,
true,
true,
true,
true,
true
]
},
"hash": "a4610ec5cb6fdccf7ef07a0526d35b7659a7e953b47b1e7b87188fa4b7277b1f"
}

View file

@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO af_user (uuid)\n SELECT $1\n WHERE NOT EXISTS (\n SELECT 1 FROM public.af_user WHERE uuid = $1\n )\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": []
},
"hash": "e09ecf50811c2a1f424ce638e11a4de140168e90be85ef9cdc2223ae48d85239"
}

73
Cargo.lock generated
View file

@ -71,7 +71,7 @@ dependencies = [
"actix-tls",
"actix-utils",
"ahash",
"base64 0.21.3",
"base64 0.21.4",
"bitflags 2.4.0",
"brotli",
"bytes",
@ -122,7 +122,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -295,18 +295,18 @@ dependencies = [
"actix-router",
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
name = "actix_derive"
version = "0.6.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d44b8fee1ced9671ba043476deddef739dd0959bf77030b26b738cc591737a7"
checksum = "7c7db3d5a9718568e4cf4a537cfd7070e6e6ff7481510d0237fb529ac850f6d3"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.32",
]
[[package]]
@ -476,7 +476,6 @@ dependencies = [
"tracing-log",
"tracing-subscriber",
"unicode-segmentation",
"uuid",
"validator",
]
@ -555,7 +554,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -608,9 +607,9 @@ checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5"
[[package]]
name = "base64"
version = "0.21.3"
version = "0.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "414dcefbc63d77c526a76b3afcf6fbb9b5e2791c19c3aa2297733208750c6e53"
checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2"
[[package]]
name = "base64ct"
@ -645,7 +644,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -1182,7 +1181,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -1394,7 +1393,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -1827,7 +1826,7 @@ version = "8.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6971da4d9c3aa03c3d8f3ff0f4155b534aad021292003895a469716b2a230378"
dependencies = [
"base64 0.21.3",
"base64 0.21.4",
"pem",
"ring",
"serde",
@ -1949,9 +1948,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "linux-raw-sys"
version = "0.4.5"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128"
[[package]]
name = "local-channel"
@ -2216,7 +2215,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -2336,7 +2335,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -2403,7 +2402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d"
dependencies = [
"proc-macro2",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -2645,7 +2644,7 @@ version = "0.11.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e9ad3fe7488d7e34558a2033d45a0c90b72d97b4f80705666fea71472e2e6a1"
dependencies = [
"base64 0.21.3",
"base64 0.21.4",
"bytes",
"cookie",
"cookie_store",
@ -2762,9 +2761,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.38.11"
version = "0.38.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453"
checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662"
dependencies = [
"bitflags 2.4.0",
"errno",
@ -2791,7 +2790,7 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2"
dependencies = [
"base64 0.21.3",
"base64 0.21.4",
]
[[package]]
@ -2902,14 +2901,14 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
name = "serde_json"
version = "1.0.105"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360"
checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2"
dependencies = [
"itoa",
"ryu",
@ -2924,7 +2923,7 @@ checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -2981,6 +2980,7 @@ dependencies = [
"serde",
"serde_json",
"serde_repr",
"sqlx",
"thiserror",
]
@ -3215,7 +3215,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ca69bf415b93b60b80dc8fda3cb4ef52b2336614d8da2de5456cc942a110482"
dependencies = [
"atoi",
"base64 0.21.3",
"base64 0.21.4",
"bitflags 2.4.0",
"byteorder",
"bytes",
@ -3259,7 +3259,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0db2df1b8731c3651e204629dd55e52adbae0462fa1bdcbed56a2302c18181e"
dependencies = [
"atoi",
"base64 0.21.3",
"base64 0.21.4",
"bitflags 2.4.0",
"byteorder",
"chrono",
@ -3363,9 +3363,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.31"
version = "2.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398"
checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2"
dependencies = [
"proc-macro2",
"quote",
@ -3414,7 +3414,7 @@ checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -3509,7 +3509,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -3632,7 +3632,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
]
[[package]]
@ -3815,7 +3815,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d"
dependencies = [
"getrandom 0.2.10",
"serde",
]
[[package]]
@ -3893,7 +3892,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
"wasm-bindgen-shared",
]
@ -3927,7 +3926,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
"syn 2.0.32",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

View file

@ -34,7 +34,6 @@ config = { version = "0.13.3", default-features = false, features = ["yaml"] }
once_cell = "1.13.0"
chrono = { version = "0.4.23", features = ["serde", "clock"], default-features = false }
derive_more = { version = "0.99" }
uuid = { version = "1", features = ["v4", "serde"] }
argon2 = { version = "0.5", features = ["std"] }
secrecy = { version = "0.8", features = ["serde"] }
rand = { version = "0.8", features = ["std_rng"] }

View file

@ -32,4 +32,7 @@ fi
# Kill any existing instances
pkill -f appflowy_cloud || true
# Run the migrations
cargo sqlx database create && cargo sqlx migrate run && cargo sqlx prepare
cargo run

View file

@ -21,6 +21,12 @@ impl Client {
}
}
pub async fn health(&self) -> Result<(), Error> {
let url: String = format!("{}/health", self.base_url);
let resp = self.client.get(url).send().await?;
check_response(resp).await
}
pub async fn settings(&self) -> Result<GoTrueSettings, Error> {
let url: String = format!("{}/settings", self.base_url);
let resp = self.client.get(url).send().await?;

View file

@ -13,4 +13,5 @@ serde_repr = "0.1.16"
gotrue = { path = "../gotrue" }
actix-web = { version = "4.4.0", default-features = false, features = ["http2"] }
thiserror = "1.0.47"
reqwest = "0.11.18"
sqlx = { version = "0.7", default-features = false, features = ["postgres"] }
reqwest = "0.11.18"

View file

@ -6,6 +6,7 @@ use actix_web::{http::StatusCode, HttpResponse};
use gotrue::models::{GoTrueError, OAuthError};
use serde::{Deserialize, Serialize};
use serde_json::Error;
use sqlx::types::uuid;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AppError {
@ -75,6 +76,18 @@ impl From<ErrorCode> for AppError {
}
}
impl From<uuid::Error> for AppError {
fn from(err: uuid::Error) -> Self {
AppError::new(ErrorCode::Unhandled, format!("uuid error: {}", err))
}
}
impl From<sqlx::Error> for AppError {
fn from(err: sqlx::Error) -> Self {
AppError::new(ErrorCode::Unhandled, format!("sqlx error: {}", err))
}
}
impl From<reqwest::Error> for AppError {
fn from(value: reqwest::Error) -> Self {
AppError::new(ErrorCode::Unhandled, value.to_string())

View file

@ -1,4 +1,8 @@
use serde::{Deserialize, Serialize};
use sqlx::types::{
chrono::{DateTime, Utc},
uuid,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateCollabParams {
@ -19,3 +23,38 @@ impl CreateCollabParams {
}
}
}
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct AfWorkspace {
pub workspace_id: uuid::Uuid,
pub database_storage_id: Option<sqlx::types::uuid::Uuid>,
pub owner_uid: Option<i64>,
pub created_at: Option<DateTime<Utc>>,
pub workspace_type: i32,
pub deleted_at: Option<DateTime<Utc>>,
pub workspace_name: Option<String>,
}
#[derive(Debug, sqlx::FromRow)]
pub struct AfUserProfileView {
pub uid: Option<i64>, // Made this field nullable based on the error
pub uuid: Option<uuid::Uuid>, // Made this field nullable based on the error
pub email: Option<String>, // Made this field nullable based on the error
pub password: Option<String>, // Made this field nullable based on the error
pub name: Option<String>, // Made this field nullable based on the error
pub encryption_sign: Option<String>, // Made this field nullable based on the error
pub deleted_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
pub created_at: Option<DateTime<Utc>>,
pub latest_workspace_id: Option<uuid::Uuid>,
}
pub struct AfWorkspaces(pub Vec<AfWorkspace>);
impl AfWorkspaces {
pub fn get_latest(&self, profile: AfUserProfileView) -> Option<AfWorkspace> {
match profile.latest_workspace_id {
Some(ws_id) => self.0.iter().find(|ws| ws.workspace_id == ws_id).cloned(),
None => None,
}
}
}

View file

@ -1,3 +1,4 @@
pub mod collab;
pub mod entities;
pub mod error;
pub mod workspace;

View file

@ -0,0 +1,56 @@
use sqlx::{
types::{uuid, Uuid},
PgPool,
};
use crate::entities::{AfUserProfileView, AfWorkspace};
pub async fn create_user_if_not_exists(
pool: &PgPool,
gotrue_uuid: uuid::Uuid,
) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
INSERT INTO af_user (uuid)
SELECT $1
WHERE NOT EXISTS (
SELECT 1 FROM public.af_user WHERE uuid = $1
)
"#,
gotrue_uuid
)
.execute(pool)
.await?;
Ok(())
}
pub async fn select_all_workspaces_owned(
pool: &PgPool,
owner_uid: i64,
) -> Result<Vec<AfWorkspace>, sqlx::Error> {
sqlx::query_as!(
AfWorkspace,
r#"
SELECT * FROM public.af_workspace WHERE owner_uid = $1
"#,
owner_uid
)
.fetch_all(pool)
.await
}
pub async fn select_user_profile_view_by_uuid(
pool: &PgPool,
gotrue_uuid: Uuid,
) -> Result<Option<AfUserProfileView>, sqlx::Error> {
sqlx::query_as!(
AfUserProfileView,
r#"
SELECT *
FROM public.af_user_profile_view WHERE uuid = $1
"#,
gotrue_uuid
)
.fetch_optional(pool)
.await
}

View file

@ -4,10 +4,11 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- user table
CREATE TABLE IF NOT EXISTS af_user (
uid BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL DEFAULT '' UNIQUE,
password TEXT NOT NULL,
uuid UUID NOT NULL, -- related to gotrue
email TEXT NOT NULL DEFAULT '' UNIQUE, -- not needed when authenticated with gotrue
password TEXT NOT NULL DEFAULT '', -- not needed when authenticated with gotrue
name TEXT NOT NULL DEFAULT '',
encryption_sign TEXT,
encryption_sign TEXT DEFAULT NULL, -- used to encrypt the user's data
deleted_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
@ -26,4 +27,4 @@ RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_prevent_reset_encryption_sign BEFORE
UPDATE ON af_user FOR EACH ROW EXECUTE FUNCTION prevent_reset_encryption_sign_func();
UPDATE ON af_user FOR EACH ROW EXECUTE FUNCTION prevent_reset_encryption_sign_func();

View file

@ -49,4 +49,4 @@ END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER manage_af_workspace_member_role_trigger
AFTER
INSERT ON af_workspace FOR EACH ROW EXECUTE FUNCTION manage_af_workspace_member_role_func();
INSERT ON af_workspace FOR EACH ROW EXECUTE FUNCTION manage_af_workspace_member_role_func();

View file

@ -159,4 +159,4 @@ WHERE workspace_id = p_workspace_id
IF NOT FOUND THEN RAISE EXCEPTION 'Unsupported operation: User is not the owner of the workspace.';
END IF;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;

View file

@ -35,18 +35,17 @@ pub fn user_scope() -> Scope {
async fn update_handler(
auth: Authorization,
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
) -> Result<Json<AppResponse<User>>> {
state: Data<State>,
) -> Result<JsonAppResponse<User>> {
let req = req.into_inner();
let user = biz::user::update(&gotrue_client, &auth.token, &req.email, &req.password).await?;
let user =
biz::user::update(&state.gotrue_client, &auth.token, &req.email, &req.password).await?;
Ok(AppResponse::Ok().with_data(user).into())
}
async fn sign_out_handler(
auth: Authorization,
gotrue_client: Data<gotrue::api::Client>,
) -> Result<JsonAppResponse<()>> {
gotrue_client
async fn sign_out_handler(auth: Authorization, state: Data<State>) -> Result<JsonAppResponse<()>> {
state
.gotrue_client
.logout(&auth.token)
.await
.map_err(InternalServerError::new)?;
@ -55,18 +54,24 @@ async fn sign_out_handler(
async fn sign_in_password_handler(
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
state: Data<State>,
) -> Result<JsonAppResponse<AccessTokenResponse>> {
let req = req.into_inner();
let token = biz::user::sign_in(&gotrue_client, req.email, req.password).await?;
let token = biz::user::sign_in(
&state.pg_pool,
&state.gotrue_client,
req.email,
req.password,
)
.await?;
Ok(AppResponse::Ok().with_data(token).into())
}
async fn sign_up_handler(
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
state: Data<State>,
) -> Result<JsonAppResponse<()>> {
biz::user::sign_up(&gotrue_client, &req.email, &req.password).await?;
biz::user::sign_up(&state.gotrue_client, &req.email, &req.password).await?;
Ok(AppResponse::Ok().into())
}

View file

@ -1,6 +1,6 @@
use crate::api::{user_scope, ws_scope};
use crate::component::auth::HEADER_TOKEN;
use crate::config::config::{Config, DatabaseSetting, TlsConfig};
use crate::config::config::{Config, DatabaseSetting, GoTrueSetting, TlsConfig};
use crate::middleware::cors::default_cors;
use crate::self_signed::create_self_signed_certificate;
use crate::state::{State, Storage};
@ -100,10 +100,6 @@ where
.service(ws_scope())
.app_data(Data::new(collab_server.clone()))
.app_data(Data::new(state.clone()))
.app_data(Data::new(storage.clone()))
.app_data(Data::new(gotrue::api::Client::new(
reqwest::Client::new(),
&config.gotrue.base_url)))
});
server = match pair {
@ -126,12 +122,14 @@ fn get_certificate_and_server_key(config: &Config) -> Option<(Secret<String>, Se
pub async fn init_state(config: &Config) -> State {
let pg_pool = get_connection_pool(&config.database).await;
let gotrue_client = get_gotrue_client(&config.gotrue).await;
State {
pg_pool,
config: Arc::new(config.clone()),
user: Arc::new(Default::default()),
id_gen: Arc::new(RwLock::new(Snowflake::new(1))),
gotrue_client,
}
}
@ -143,6 +141,15 @@ async fn get_connection_pool(setting: &DatabaseSetting) -> PgPool {
.expect("Failed to connect to Postgres")
}
async fn get_gotrue_client(setting: &GoTrueSetting) -> gotrue::api::Client {
let gotrue_client = gotrue::api::Client::new(reqwest::Client::new(), &setting.base_url);
gotrue_client
.health()
.await
.expect("Failed to connect to GoTrue");
gotrue_client
}
fn make_ssl_acceptor_builder(certificate: Secret<String>) -> SslAcceptorBuilder {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
let x509_cert = X509::from_pem(certificate.expose_secret().as_bytes()).unwrap();

View file

@ -1,3 +1,5 @@
use std::str::FromStr;
use anyhow::Result;
use gotrue::{
api::Client,
@ -9,25 +11,25 @@ use shared_entity::{error::AppError, server_error};
use validator::validate_email;
use crate::domain::validate_password;
use sqlx::{types::uuid, PgPool};
pub async fn sign_up(gotrue_client: &Client, email: &str, password: &str) -> Result<(), AppError> {
validate_email_password(email, password)?;
let user = gotrue_client.sign_up(email, password).await??;
tracing::info!("user: {:?}", user);
// TODO: set up workspace for new user
tracing::info!("user sign up: {:?}", user);
Ok(())
}
pub async fn sign_in(
pg_pool: &PgPool,
gotrue_client: &Client,
email: String,
password: String,
) -> Result<AccessTokenResponse, AppError> {
let grant = Grant::Password(PasswordGrant { email, password });
let token = gotrue_client.token(&grant).await??;
storage::workspace::create_user_if_not_exists(pg_pool, uuid::Uuid::from_str(&token.user.id)?)
.await?;
Ok(token)
}

View file

@ -63,7 +63,7 @@ async fn get_stored_credentials(
let row = sqlx::query!(
r#"
SELECT uid, password
FROM af_user
FROM af_user
WHERE email = $1
"#,
email,

View file

@ -332,10 +332,12 @@ pub fn logged_user_from_request(
pub fn uid_from_request(
request: &HttpRequest,
server_key: &Secret<String>,
) -> Result<Secret<i64>, AuthError> {
) -> Result<Secret<String>, AuthError> {
match request.headers().get(HEADER_TOKEN) {
Some(header) => match header.to_str() {
Ok(val) => Token::decode_token(server_key, val).map(|claim| Secret::new(claim.uid)),
Ok(val) => {
Token::decode_token(server_key, val).map(|claim| Secret::new(claim.uid.to_string()))
},
Err(_) => Err(AuthError::Unauthorized),
},
None => Err(AuthError::Unauthorized),

View file

@ -15,6 +15,7 @@ pub struct State {
pub config: Arc<Config>,
pub user: Arc<RwLock<UserCache>>,
pub id_gen: Arc<RwLock<Snowflake>>,
pub gotrue_client: gotrue::api::Client,
}
impl State {

View file

@ -57,4 +57,6 @@ async fn sign_in_success() {
.unwrap();
let token = c.token().unwrap();
assert!(token.user.confirmed_at.is_some());
// TODO: check that workspace is created for user
}

8
tests/gotrue/health.rs Normal file
View file

@ -0,0 +1,8 @@
use gotrue::api::Client;
#[tokio::test]
async fn gotrue_health() {
let http_client = reqwest::Client::new();
let gotrue_client = Client::new(http_client, "http://localhost:9998");
gotrue_client.health().await.unwrap();
}

View file

@ -1 +1,2 @@
mod health;
mod settings;