diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 46dff0a..aaa4c4c 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,6 +30,8 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v7 + with: + submodules: recursive # Multi-arch (arm64) is only built and published from main; other branches and PRs # only need an amd64 build to validate they compile, which needs no emulation. diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6908350..78421f7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,6 +31,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + submodules: recursive - name: Setup Node.js uses: actions/setup-node@v4 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 428601d..d00718e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -31,6 +31,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + submodules: recursive - name: Set up Go uses: actions/setup-go@v5 @@ -53,6 +55,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + submodules: recursive - name: Set up Node.js uses: actions/setup-node@v4 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..56f7536 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "SharedCode"] + path = SharedCode + url = https://repos.gfranz.ch/Gabriel20xx/SharedCode.git diff --git a/SharedCode b/SharedCode new file mode 160000 index 0000000..c95865d --- /dev/null +++ b/SharedCode @@ -0,0 +1 @@ +Subproject commit c95865dbe2be79fce08035ecd6d0b324eea9116c diff --git a/backend/auth.go b/backend/auth.go index f32c45f..84a1eb4 100644 --- a/backend/auth.go +++ b/backend/auth.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "golang.org/x/crypto/bcrypt" + sharedauth "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/auth" ) const ( @@ -238,7 +238,7 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) { return } - hash, err := bcrypt.GenerateFromPassword([]byte(request.Password), bcrypt.DefaultCost) + hash, err := sharedauth.HashPassword(request.Password) if err != nil { writeJSONError(w, http.StatusInternalServerError, "Internal server error") return @@ -283,7 +283,7 @@ func (a *AuthService) handleLogin(w http.ResponseWriter, r *http.Request) { writeJSONError(w, http.StatusInternalServerError, "Internal server error") return } - if user == nil || bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(request.Password)) != nil { + if user == nil || !sharedauth.VerifyPassword(user.PasswordHash, request.Password) { writeJSONError(w, http.StatusUnauthorized, errInvalidCredentials.Error()) return } @@ -318,7 +318,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req writeJSONError(w, http.StatusBadRequest, "Invalid JSON body") return } - if request.CurrentPassword != nil && *request.CurrentPassword != "" && bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(*request.CurrentPassword)) != nil { + if request.CurrentPassword != nil && *request.CurrentPassword != "" && !sharedauth.VerifyPassword(user.PasswordHash, *request.CurrentPassword) { writeJSONError(w, http.StatusBadRequest, errCurrentPasswordBad.Error()) return } @@ -347,7 +347,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req writeJSONError(w, http.StatusBadRequest, err.Error()) return } - hash, err := bcrypt.GenerateFromPassword([]byte(*request.NewPassword), bcrypt.DefaultCost) + hash, err := sharedauth.HashPassword(*request.NewPassword) if err != nil { writeJSONError(w, http.StatusInternalServerError, "Internal server error") return diff --git a/backend/db.go b/backend/db.go index 14fbbd0..a3a8f52 100644 --- a/backend/db.go +++ b/backend/db.go @@ -7,8 +7,9 @@ import ( "time" "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgxpool" + + sharedpostgres "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/postgres" ) // DB owns the PostgreSQL pool. The schema deliberately uses the same physical @@ -19,13 +20,9 @@ type DB struct { } func connectDB(ctx context.Context, connectionString string) (*DB, error) { - pool, err := pgxpool.New(ctx, connectionString) + pool, err := sharedpostgres.Open(ctx, sharedpostgres.DefaultConfig(connectionString)) if err != nil { - return nil, fmt.Errorf("create pool: %w", err) - } - if err := pool.Ping(ctx); err != nil { - pool.Close() - return nil, fmt.Errorf("ping database: %w", err) + return nil, err } return &DB{pool: pool}, nil } @@ -35,19 +32,7 @@ func (d *DB) close() { } func (d *DB) withTx(ctx context.Context, fn func(pgx.Tx) error) error { - tx, err := d.pool.Begin(ctx) - if err != nil { - return fmt.Errorf("begin transaction: %w", err) - } - defer tx.Rollback(ctx) // harmless after a successful commit - - if err := fn(tx); err != nil { - return err - } - if err := tx.Commit(ctx); err != nil { - return fmt.Errorf("commit transaction: %w", err) - } - return nil + return sharedpostgres.WithTx(ctx, d.pool, fn) } // initSchema is idempotent so an existing CollabTable PostgreSQL database can @@ -211,8 +196,7 @@ func (d *DB) createUser(ctx context.Context, username, email, passwordHash strin RETURNING id, username, email `, username, email, passwordHash).Scan(&user.ID, &user.Username, &user.Email) if err != nil { - var pgErr *pgconn.PgError - if errors.As(err, &pgErr) && pgErr.Code == "23505" { + if sharedpostgres.IsUniqueViolation(err) { return nil, ErrSingletonUserExists } return nil, fmt.Errorf("create user: %w", err) diff --git a/backend/go.mod b/backend/go.mod index 817d68a..c6cd0e7 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -5,13 +5,17 @@ go 1.26.5 require ( github.com/jackc/pgx/v5 v5.10.0 github.com/joho/godotenv v1.5.1 - golang.org/x/crypto v0.54.0 ) +require golang.org/x/crypto v0.54.0 // indirect + require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect golang.org/x/sync v0.22.0 // indirect golang.org/x/text v0.40.0 // indirect + repos.gfranz.ch/Gabriel20xx/SharedCode/backend v0.0.0 ) + +replace repos.gfranz.ch/Gabriel20xx/SharedCode/backend => ../SharedCode/backend diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 10b42ed..01c9cf4 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -13,7 +13,8 @@ "bits-ui": "latest", "class-variance-authority": "latest", "clsx": "latest", - "tailwind-merge": "latest" + "tailwind-merge": "latest", + "@gabriel20xx/shared-code": "git+https://repos.gfranz.ch/Gabriel20xx/SharedCode.git#c95865d" }, "devDependencies": { "@eslint/js": "latest", @@ -3419,6 +3420,156 @@ "resolved": "https://registry.npmjs.org/zimmerframe/-/zimmerframe-1.1.4.tgz", "integrity": "sha512-B58NGBEoc8Y9MWWCQGl/gq9xBCe4IiKM0a2x7GZdQKOW5Exr8S1W24J6OgM1njK8xCRGvAJIL/MxXHf6SkmQKQ==", "license": "MIT" + }, + "node_modules/@gabriel20xx/shared-code": { + "version": "0.1.0", + "resolved": "git+https://repos.gfranz.ch/Gabriel20xx/SharedCode.git#c95865dbe2be79fce08035ecd6d0b324eea9116c", + "license": "MIT", + "workspaces": [ + "packages/*" + ], + "dependencies": { + "pg": "^8.17.2" + } + }, + "node_modules/pg": { + "version": "8.22.0", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.22.0.tgz", + "integrity": "sha512-8wih1vVIBMxoUM2oB4soJsD9tDnDpLv4OXBJ+EJzFsvycD+lfyIreC2gGHq78f8jbLLt+bvlPTFdFZfJkOuzAA==", + "license": "MIT", + "dependencies": { + "pg-connection-string": "^2.14.0", + "pg-pool": "^3.14.0", + "pg-protocol": "^1.15.0", + "pg-types": "2.2.0", + "pgpass": "1.0.5" + }, + "engines": { + "node": ">= 16.0.0" + }, + "optionalDependencies": { + "pg-cloudflare": "^1.4.0" + }, + "peerDependencies": { + "pg-native": ">=3.0.1" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-connection-string": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.14.0.tgz", + "integrity": "sha512-XwWDGcLRGCXAR8F/AM5bG7Q+A3Wm2s6QeEjlOKZLlH3UYcguiqCWKyWXVag5TLTIjR7oOJUY8kcADaZgWPyLeg==", + "license": "MIT" + }, + "node_modules/pg-pool": { + "version": "3.14.0", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz", + "integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==", + "license": "MIT", + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.15.0.tgz", + "integrity": "sha512-cq9sECI5s0+uPUXjbz8ioyPJni6RzsRib0US67i5IoTZKw8fNeYlVE7u8F4dG7vEJJtc5wdD1K189lCCUwqWTQ==", + "license": "MIT" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "license": "MIT", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "license": "MIT", + "dependencies": { + "split2": "^4.1.0" + } + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "license": "ISC", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz", + "integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "license": "MIT", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "license": "ISC", + "engines": { + "node": ">= 10.x" + } + }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "license": "MIT", + "engines": { + "node": ">=0.4" + } } } } diff --git a/frontend/package.json b/frontend/package.json index 1c8631c..fdbc744 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -30,6 +30,7 @@ "vite": "latest" }, "dependencies": { + "@gabriel20xx/shared-code": "git+https://repos.gfranz.ch/Gabriel20xx/SharedCode.git#c95865d", "@lucide/svelte": "latest", "@tanstack/svelte-query": "latest", "bits-ui": "latest", diff --git a/frontend/src/lib/components/Header.svelte b/frontend/src/lib/components/Header.svelte deleted file mode 100644 index ffbad5a..0000000 --- a/frontend/src/lib/components/Header.svelte +++ /dev/null @@ -1,126 +0,0 @@ - - - - -
- - - CollabTable - - - - - - -
- - - - - - {#snippet child({ props })} - - {/snippet} - - - (credentialsOpen = true)}> - - Profile - - - void auth.logout()}> - - Sign out - - - -
-
- - diff --git a/frontend/src/lib/features/auth/AuthPage.svelte b/frontend/src/lib/features/auth/AuthPage.svelte index e52c82d..1287944 100644 --- a/frontend/src/lib/features/auth/AuthPage.svelte +++ b/frontend/src/lib/features/auth/AuthPage.svelte @@ -1,119 +1,18 @@ -
-
-
- -

CollabTable

-

- {setupRequired ? 'Create the admin account for this dashboard.' : 'Sign in to view this dashboard.'} -

-
- -
- {#if setupRequired} - - - - - - {:else} - - - - {/if} -
- - -
- {#if setupRequired} -
- - -
- {/if} - {#if error} -

{error}

- {/if} - -
-
-
+ diff --git a/frontend/src/lib/features/auth/CredentialsDialog.svelte b/frontend/src/lib/features/auth/CredentialsDialog.svelte deleted file mode 100644 index 9d7669e..0000000 --- a/frontend/src/lib/features/auth/CredentialsDialog.svelte +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - Account - -
-

Changing your credentials will sign you out everywhere.

-
- - -
-
- - -
-
- - -
-
- - -
- {#if error} -

{error}

- {/if} - - - - -
-
-
diff --git a/frontend/src/lib/features/layout/AppShell.svelte b/frontend/src/lib/features/layout/AppShell.svelte index dab5782..bb24a29 100644 --- a/frontend/src/lib/features/layout/AppShell.svelte +++ b/frontend/src/lib/features/layout/AppShell.svelte @@ -1,12 +1,27 @@ @@ -19,11 +34,27 @@ {:else if !auth.status || auth.status.setupRequired || !auth.status.authenticated} {:else} -
-
-
+ auth.logout()} + onProfile={() => (credentialsOpen = true)} + onSettings={() => (settingsOpen = true)} + > + {#snippet children()} {@render children?.()} -
-
+ {/snippet} + {/if}
+ + + diff --git a/frontend/src/lib/features/layout/SettingsDialog.svelte b/frontend/src/lib/features/layout/SettingsDialog.svelte index 486a873..50595b5 100644 --- a/frontend/src/lib/features/layout/SettingsDialog.svelte +++ b/frontend/src/lib/features/layout/SettingsDialog.svelte @@ -1,7 +1,7 @@ - - - - Settings - +
Theme
@@ -69,8 +65,7 @@
- - - - -
+
+ +
+ diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index 0356ed6..f12c06e 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -1,5 +1,6 @@