chore: add login verification scripts and update deps

This commit is contained in:
Tiago Yamamoto 2025-12-24 14:22:56 -03:00
parent f7127235cc
commit 328496feaa
4 changed files with 109 additions and 2 deletions

View file

@ -0,0 +1,60 @@
package tests
import (
"database/sql"
"fmt"
"testing"
_ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt"
)
func TestVerifyLogin(t *testing.T) {
// 1. Config
dbURL := "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require"
pepper := "gohorse-pepper" // Using the simple pepper user agreed to
// Alternative: try the old one if needed, but let's stick to the latest instruction.
// If the user hasn't updated the seeder to 'gohorse-pepper' yet, this might fail unless I check what they actually used.
// The user claimed "variables are equal".
password := "Admin@2025!"
// 2. Connect DB
db, err := sql.Open("postgres", dbURL)
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
t.Fatalf("Failed to ping: %v", err)
}
// 3. Fetch User
var hash string
err = db.QueryRow("SELECT password_hash FROM users WHERE identifier = 'superadmin'").Scan(&hash)
if err != nil {
t.Fatalf("Failed to find user: %v", err)
}
fmt.Printf("found hash in DB: %s\n", hash)
// 4. Verify
passWithPepper := password + pepper
err = bcrypt.CompareHashAndPassword([]byte(hash), []byte(passWithPepper))
if err != nil {
t.Errorf("FAILED to verify with pepper '%s': %v", pepper, err)
// Try with the "old" random string pepper just in case
oldPepper := "some-random-string-for-password-hashing"
passWithOld := password + oldPepper
err2 := bcrypt.CompareHashAndPassword([]byte(hash), []byte(passWithOld))
if err2 == nil {
t.Logf("SUCCESS with OLD pepper: '%s'", oldPepper)
} else {
t.Logf("Failed with old pepper as well.")
}
} else {
t.Logf("SUCCESS! Password verifies with pepper '%s'", pepper)
}
}

View file

@ -20,4 +20,3 @@ BACKEND_API_URL=http://localhost:8521/api/v1
# MUST match backend PASSWORD_PEPPER for login to work
PASSWORD_PEPPER=some-random-string-for-password-hashing
PASSWORD_PEPPER=some-random-string-for-password-hashing

View file

@ -10,7 +10,8 @@
"migrate": "node src/migrate.js",
"seed:users": "node src/seeders/users.js",
"seed:companies": "node src/seeders/companies.js",
"seed:jobs": "node src/seeders/jobs.js"
"seed:jobs": "node src/seeders/jobs.js",
"verify": "node src/verify-login.js"
},
"keywords": [
"seeder",

View file

@ -0,0 +1,47 @@
import { pool } from './db.js';
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
dotenv.config();
const PASSWORD_PEPPER = process.env.PASSWORD_PEPPER || '';
async function verify() {
console.log('🔍 Verifying SuperAdmin credentials...');
console.log(`🌶️ Using Pepper: "${PASSWORD_PEPPER}"`);
try {
const res = await pool.query("SELECT password_hash FROM users WHERE identifier = 'superadmin'");
if (res.rows.length === 0) {
console.error('❌ SuperAdmin user not found in DB!');
return;
}
const hash = res.rows[0].password_hash;
const password = 'Admin@2025!';
const passwordWithPepper = password + PASSWORD_PEPPER;
const match = await bcrypt.compare(passwordWithPepper, hash);
if (match) {
console.log('✅ SUCCESS: Database hash matches (Password + Pepper)');
console.log('👉 If login fails on the server, the server likely has the WRONG pepper or NO pepper.');
} else {
console.error('❌ FAILURE: Database hash does NOT match local logic.');
// Try without pepper
const matchNoPepper = await bcrypt.compare(password, hash);
if (matchNoPepper) {
console.warn('⚠️ WARNING: Hash matches password WITHOUT pepper. The seeder ignored the pepper.');
} else {
console.error('❌ Hash matches neither.');
}
}
} catch (err) {
console.error('Error:', err);
} finally {
await pool.end();
}
}
verify();