From 9fc2acbc355099a0a98cca1ac6bee4f47c014542 Mon Sep 17 00:00:00 2001 From: Zack <33050391+speed2exe@users.noreply.github.com> Date: Thu, 14 Sep 2023 20:41:04 +0800 Subject: [PATCH] feat: Docker/gotrue (#43) * feat: use gotrue from source instead of docker hub image * test: fix test due to gotrue upgrade * fix: update prod docker-compose * chore: cargo fmt --all --- docker-compose-dev.yml | 6 ++++-- docker-compose.yml | 8 +++++--- docker/gotrue.Dockerfile | 6 ++++++ docker/{Dockerfile_postgres => postgres.Dockerfile} | 0 libs/client-api/src/http.rs | 2 +- libs/gotrue-entity/src/lib.rs | 6 +++--- libs/shared-entity/src/error.rs | 12 +++++++----- tests/client/sign_in.rs | 4 +++- tests/client/update.rs | 11 ++++++++++- 9 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 docker/gotrue.Dockerfile rename docker/{Dockerfile_postgres => postgres.Dockerfile} (100%) diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index dcf4698e..91085e9c 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -3,7 +3,7 @@ services: postgres: build: context: . - dockerfile: docker/Dockerfile_postgres + dockerfile: docker/postgres.Dockerfile environment: - POSTGRES_USER=${POSTGRES_USER:-postgres} - POSTGRES_DB=${POSTGRES_DB:-postgres} @@ -20,7 +20,9 @@ services: - 6380:6379 gotrue: - image: supabase/gotrue + build: + context: . + dockerfile: docker/gotrue.Dockerfile restart: on-failure depends_on: - postgres diff --git a/docker-compose.yml b/docker-compose.yml index 5ab9c032..642fcff2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: postgres: build: context: . - dockerfile: docker/Dockerfile_postgres + dockerfile: docker/postgres.Dockerfile environment: - POSTGRES_USER=${POSTGRES_USER:-postgres} - POSTGRES_DB=${POSTGRES_DB:-postgres} @@ -12,7 +12,7 @@ services: ports: - 5433:5432 volumes: - - ./migrations/before/:/docker-entrypoint-initdb.d + - ./migrations/before:/docker-entrypoint-initdb.d redis: image: redis @@ -20,7 +20,9 @@ services: - 6380:6379 gotrue: - image: supabase/gotrue + build: + context: . + dockerfile: docker/gotrue.Dockerfile restart: on-failure depends_on: - postgres diff --git a/docker/gotrue.Dockerfile b/docker/gotrue.Dockerfile new file mode 100644 index 00000000..56760b22 --- /dev/null +++ b/docker/gotrue.Dockerfile @@ -0,0 +1,6 @@ +FROM golang +WORKDIR /go/src/supabase +RUN git clone https://github.com/supabase/gotrue.git +WORKDIR /go/src/supabase/gotrue +RUN git checkout v2.95.2 && go install +CMD ["gotrue"] diff --git a/docker/Dockerfile_postgres b/docker/postgres.Dockerfile similarity index 100% rename from docker/Dockerfile_postgres rename to docker/postgres.Dockerfile diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 2a2c1d76..99856066 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -80,7 +80,7 @@ impl Client { access_token, token_type: token_type.ok_or(url_missing_param("token_type"))?, expires_in: expires_in.ok_or(url_missing_param("expires_in"))?, - expires_at, + expires_at: expires_at.ok_or(url_missing_param("expires_at"))?, refresh_token: refresh_token.ok_or(url_missing_param("refresh_token"))?, user, provider_access_token, diff --git a/libs/gotrue-entity/src/lib.rs b/libs/gotrue-entity/src/lib.rs index 94257587..fd0f93ff 100644 --- a/libs/gotrue-entity/src/lib.rs +++ b/libs/gotrue-entity/src/lib.rs @@ -70,7 +70,7 @@ pub struct AccessTokenResponse { pub access_token: String, pub token_type: String, pub expires_in: i64, - pub expires_at: Option, // older versions of GoTrue do not return this + pub expires_at: i64, pub refresh_token: String, pub user: User, pub provider_access_token: Option, @@ -106,8 +106,8 @@ pub struct GoTrueSettings { pub mailer_autoconfirm: bool, pub phone_autoconfirm: bool, pub sms_provider: String, - pub mfa_enabled: Option, // older versions of GoTrue do not return this - pub saml_enabled: Option, // older versions of GoTrue do not return this + pub mfa_enabled: bool, + pub saml_enabled: bool, } #[derive(Serialize, Deserialize, Debug)] diff --git a/libs/shared-entity/src/error.rs b/libs/shared-entity/src/error.rs index ffd6562e..5225a217 100644 --- a/libs/shared-entity/src/error.rs +++ b/libs/shared-entity/src/error.rs @@ -53,14 +53,16 @@ impl From for AppError { #[cfg(feature = "cloud")] impl From for AppError { fn from(err: gotrue_entity::GoTrueError) -> Self { - match err.code { - 401 => AppError::new(ErrorCode::OAuthError, err.msg), + match (err.code, err.msg.as_str()) { + (401, _) => AppError::new(ErrorCode::OAuthError, err.msg), + (422, "New password should be different from the old password.") => { + AppError::new(ErrorCode::InvalidPassword, err.msg) + }, _ => AppError::new( ErrorCode::Unhandled, format!( - "gotrue error: {}, id: {}", - err.code, - err.error_id.unwrap_or("".to_string()) + "gotrue error: {}, message: {}, id: {:?}", + err.code, err.msg, err.error_id, ), ), } diff --git a/tests/client/sign_in.rs b/tests/client/sign_in.rs index ac4797a6..79ff1fa6 100644 --- a/tests/client/sign_in.rs +++ b/tests/client/sign_in.rs @@ -73,7 +73,9 @@ async fn sign_in_with_url() { Ok(()) => panic!("should not be ok"), Err(e) => { assert_eq!(e.code, ErrorCode::OAuthError); - assert!(e.message.starts_with("Invalid token: token is expired by")); + assert!(e + .message + .starts_with("invalid JWT: unable to parse or verify signature, token is expired by")); }, } } diff --git a/tests/client/update.rs b/tests/client/update.rs index 2cd67fa2..f1cc9950 100644 --- a/tests/client/update.rs +++ b/tests/client/update.rs @@ -1,3 +1,5 @@ +use shared_entity::error_code::ErrorCode; + use crate::client::utils::{ generate_unique_email, REGISTERED_EMAIL, REGISTERED_PASSWORD, REGISTERED_USER_MUTEX, }; @@ -20,9 +22,16 @@ async fn update_password_same_password() { c.sign_in_password(®ISTERED_EMAIL, ®ISTERED_PASSWORD) .await .unwrap(); - c.update(®ISTERED_EMAIL, ®ISTERED_PASSWORD) + let err = c + .update(®ISTERED_EMAIL, ®ISTERED_PASSWORD) .await + .err() .unwrap(); + assert_eq!(err.code, ErrorCode::InvalidPassword); + assert_eq!( + err.message, + "New password should be different from the old password." + ); } #[tokio::test]