mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-23 00:59:53 +08:00
feat: improve address credential connections
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { WORKER_URL, createTestAddress, deleteAddress } from '../../fixtures/test-helpers';
|
||||
import { WORKER_URL, createTestAddress, deleteAddress, hashPassword } from '../../fixtures/test-helpers';
|
||||
|
||||
test.describe('Address Password Login', () => {
|
||||
test('set password then login with it', async ({ request }) => {
|
||||
const { jwt, address } = await createTestAddress(request, 'pwd-login');
|
||||
const passwordHash = hashPassword('test-password-123');
|
||||
|
||||
try {
|
||||
// Set a password on the address
|
||||
const changePwdRes = await request.post(`${WORKER_URL}/api/address_change_password`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data: { new_password: 'test-password-123' },
|
||||
data: { new_password: passwordHash },
|
||||
});
|
||||
expect(changePwdRes.ok()).toBe(true);
|
||||
const changePwdBody = await changePwdRes.json();
|
||||
@@ -17,7 +18,7 @@ test.describe('Address Password Login', () => {
|
||||
|
||||
// Login with the correct password
|
||||
const loginRes = await request.post(`${WORKER_URL}/api/address_login`, {
|
||||
data: { email: address, password: 'test-password-123' },
|
||||
data: { email: address, password: passwordHash },
|
||||
});
|
||||
expect(loginRes.ok()).toBe(true);
|
||||
const loginBody = await loginRes.json();
|
||||
@@ -36,12 +37,13 @@ test.describe('Address Password Login', () => {
|
||||
|
||||
test('login with wrong password returns 401', async ({ request }) => {
|
||||
const { jwt, address } = await createTestAddress(request, 'pwd-wrong');
|
||||
const passwordHash = hashPassword('correct-password');
|
||||
|
||||
try {
|
||||
// Set a password
|
||||
const changePwdRes = await request.post(`${WORKER_URL}/api/address_change_password`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data: { new_password: 'correct-password' },
|
||||
data: { new_password: passwordHash },
|
||||
});
|
||||
expect(changePwdRes.ok()).toBe(true);
|
||||
const changePwdBody = await changePwdRes.json();
|
||||
@@ -49,11 +51,117 @@ test.describe('Address Password Login', () => {
|
||||
|
||||
// Login with wrong password
|
||||
const loginRes = await request.post(`${WORKER_URL}/api/address_login`, {
|
||||
data: { email: address, password: 'wrong-password' },
|
||||
data: { email: address, password: hashPassword('wrong-password') },
|
||||
});
|
||||
expect(loginRes.status()).toBe(401);
|
||||
} finally {
|
||||
await deleteAddress(request, jwt);
|
||||
}
|
||||
});
|
||||
|
||||
test('admin reset stores frontend-hashed address password', async ({ request }) => {
|
||||
const { jwt, address, address_id } = await createTestAddress(request, 'pwd-admin-reset');
|
||||
const plainPassword = `admin-reset-${Date.now()}`;
|
||||
const passwordHash = hashPassword(plainPassword);
|
||||
|
||||
try {
|
||||
const resetRes = await request.post(`${WORKER_URL}/admin/address/${address_id}/reset_password`, {
|
||||
data: { password: passwordHash },
|
||||
});
|
||||
expect(resetRes.ok()).toBe(true);
|
||||
await expect(resetRes.json()).resolves.toMatchObject({ success: true });
|
||||
|
||||
const plaintextLoginRes = await request.post(`${WORKER_URL}/api/address_login`, {
|
||||
data: { email: address, password: plainPassword },
|
||||
});
|
||||
expect(plaintextLoginRes.status()).toBe(401);
|
||||
|
||||
const loginRes = await request.post(`${WORKER_URL}/api/address_login`, {
|
||||
data: { email: address, password: passwordHash },
|
||||
});
|
||||
expect(loginRes.ok()).toBe(true);
|
||||
const loginBody = await loginRes.json();
|
||||
expect(loginBody.jwt).toBeTruthy();
|
||||
expect(loginBody.address).toBe(address);
|
||||
} finally {
|
||||
await deleteAddress(request, jwt);
|
||||
}
|
||||
});
|
||||
|
||||
test('admin address list does not expose stored password hash', async ({ request }) => {
|
||||
const { jwt, address } = await createTestAddress(request, 'pwd-list-hidden');
|
||||
const passwordHash = hashPassword('list-hidden-password');
|
||||
|
||||
try {
|
||||
const changePwdRes = await request.post(`${WORKER_URL}/api/address_change_password`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data: { new_password: passwordHash },
|
||||
});
|
||||
expect(changePwdRes.ok()).toBe(true);
|
||||
|
||||
const listRes = await request.get(
|
||||
`${WORKER_URL}/admin/address?limit=10&offset=0&query=${encodeURIComponent(address)}`
|
||||
);
|
||||
expect(listRes.ok()).toBe(true);
|
||||
const listBody = await listRes.json();
|
||||
const listedAddress = listBody.results.find((row: { name: string }) => row.name === address);
|
||||
expect(listedAddress).toBeTruthy();
|
||||
expect(listedAddress).not.toHaveProperty('password');
|
||||
} finally {
|
||||
await deleteAddress(request, jwt);
|
||||
}
|
||||
});
|
||||
|
||||
test('user bind address list does not expose stored password hash', async ({ request }) => {
|
||||
const userEmail = `pwd-bind-hidden-${Date.now()}@test.example.com`;
|
||||
const userPasswordHash = hashPassword('bind-hidden-user-password');
|
||||
const { jwt, address } = await createTestAddress(request, 'pwd-bind-hidden');
|
||||
const addressPasswordHash = hashPassword('bind-hidden-address-password');
|
||||
|
||||
try {
|
||||
const enableRes = await request.post(`${WORKER_URL}/admin/user_settings`, {
|
||||
data: {
|
||||
enable: true,
|
||||
enableMailVerify: false,
|
||||
},
|
||||
});
|
||||
expect(enableRes.ok()).toBe(true);
|
||||
|
||||
const registerRes = await request.post(`${WORKER_URL}/user_api/register`, {
|
||||
data: { email: userEmail, password: userPasswordHash },
|
||||
});
|
||||
expect(registerRes.ok()).toBe(true);
|
||||
|
||||
const loginRes = await request.post(`${WORKER_URL}/user_api/login`, {
|
||||
data: { email: userEmail, password: userPasswordHash },
|
||||
});
|
||||
expect(loginRes.ok()).toBe(true);
|
||||
const { jwt: userJwt } = await loginRes.json();
|
||||
|
||||
const changePwdRes = await request.post(`${WORKER_URL}/api/address_change_password`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data: { new_password: addressPasswordHash },
|
||||
});
|
||||
expect(changePwdRes.ok()).toBe(true);
|
||||
|
||||
const bindRes = await request.post(`${WORKER_URL}/user_api/bind_address`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
'x-user-token': userJwt,
|
||||
},
|
||||
});
|
||||
expect(bindRes.ok()).toBe(true);
|
||||
|
||||
const listRes = await request.get(`${WORKER_URL}/user_api/bind_address`, {
|
||||
headers: { 'x-user-token': userJwt },
|
||||
});
|
||||
expect(listRes.ok()).toBe(true);
|
||||
const listBody = await listRes.json();
|
||||
const listedAddress = listBody.results.find((row: { name: string }) => row.name === address);
|
||||
expect(listedAddress).toBeTruthy();
|
||||
expect(listedAddress).not.toHaveProperty('password');
|
||||
} finally {
|
||||
await deleteAddress(request, jwt);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { WORKER_URL, createTestAddress, deleteAddress } from '../../fixtures/test-helpers';
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
/**
|
||||
* SHA-256 hash matching frontend hashPassword utility.
|
||||
*/
|
||||
function hashPassword(password: string): string {
|
||||
return crypto.createHash('sha256').update(password).digest('hex');
|
||||
}
|
||||
import { WORKER_URL, createTestAddress, deleteAddress, hashPassword } from '../../fixtures/test-helpers';
|
||||
|
||||
test.describe('Turnstile Login Endpoints (ENABLE_GLOBAL_TURNSTILE_CHECK disabled)', () => {
|
||||
|
||||
@@ -110,14 +102,14 @@ test.describe('Turnstile Login Endpoints (ENABLE_GLOBAL_TURNSTILE_CHECK disabled
|
||||
// Set a password
|
||||
await request.post(`${WORKER_URL}/api/address_change_password`, {
|
||||
headers: { Authorization: `Bearer ${jwt}` },
|
||||
data: { new_password: 'addr-pass-123' },
|
||||
data: { new_password: hashPassword('addr-pass-123') },
|
||||
});
|
||||
|
||||
// Login with cf_token field present but empty
|
||||
const loginRes = await request.post(`${WORKER_URL}/api/address_login`, {
|
||||
data: {
|
||||
email: address,
|
||||
password: 'addr-pass-123',
|
||||
password: hashPassword('addr-pass-123'),
|
||||
cf_token: ''
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user