diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 16a08b8..a2bf9bc 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -51,6 +51,11 @@ not narrative. This does not promote any in-review binding above. See [`backend/docs/retry-safe-messages.md`](backend/docs/retry-safe-messages.md) for the guard, deployment requirements, crash limits, and executable checks. +The `UserEmails` table maps each account email to one user id so registration +can refuse a duplicate and login can find the account. It is private account +metadata, not part of the commons, the federation export, or the record. No +binding changes; see [`backend/README.md`](backend/README.md) for the rollout. + ### Browser garden planner The Next.js `/garden-planner` page is a member-local planning aid, not a ledger diff --git a/backend/README.md b/backend/README.md index ef3c8d5..899ac31 100644 --- a/backend/README.md +++ b/backend/README.md @@ -81,7 +81,7 @@ Domain routes (when not in minimal mode): ## Data Model (DynamoDB tables) -- `Users`, `Keys` +- `Users`, `UserEmails`, `Keys` - `Listings`, `Contracts`, `Transactions` - `Conversations`, `Messages` - `Notifications` @@ -92,6 +92,33 @@ Each model file exports a `*_TABLE_NAME` and an item builder, e.g.: - `models/user.js` → `USER_TABLE_NAME`, `createUserItem()` - `models/transaction.js` → `TRANSACTION_TABLE_NAME`, `createTransactionItem()` +`UserEmails` (`models/userEmail.js`) holds one `{ email, userId }` item per +account email, trimmed and lower-cased. `/api/auth/register` and `POST /users` +write the claim and the user in one transaction, so a second account with the +same email gets `409`; login reads the claim instead of scanning `Users`. +`USER_EMAIL_TABLE_NAME` defaults to `UserEmails`. Before deploying, create the +table and claim the emails of existing accounts: + +```sh +aws dynamodb create-table \ + --table-name UserEmails \ + --attribute-definitions AttributeName=email,AttributeType=S \ + --key-schema AttributeName=email,KeyType=HASH \ + --billing-mode PAY_PER_REQUEST +node scripts/claim-user-emails.js +``` + +Grant the backend `GetItem` and `PutItem` on `UserEmails`; the script also +scans `Users`. Run the script again after the deploy to claim accounts +registered in between. It only adds claims. An email shared by several existing +accounts gets a claim that names no account (`needsReview: true`), and the +script prints their ids: no new account can register that email, and none of +the listed accounts can log in until an operator decides which one owns it and +replaces the claim with `{ email, userId }`. Rows marked as imported from a +federation peer (`sourceNodeId`) are never claimed. Rows imported before peers +were marked carry no such field, so check where each listed id came from before +choosing an owner, and run the script only as part of this rollout. + ## Chat & Streaming Endpoints (used by the Chat UI): diff --git a/backend/__tests__/dynamodbClient.test.js b/backend/__tests__/dynamodbClient.test.js index d94eaff..262993e 100644 --- a/backend/__tests__/dynamodbClient.test.js +++ b/backend/__tests__/dynamodbClient.test.js @@ -11,9 +11,16 @@ test('DynamoDB adapter marshals records and preserves conditional error codes', res.setHeader('Content-Type', 'application/x-amz-json-1.0'); if (requests.length === 1) res.end('{}'); else if (requests.length === 2) res.end(JSON.stringify({ Item: { id: { S: 'item' }, count: { N: '2' } } })); - else { + else if (requests.length === 3) { res.statusCode = 400; res.end(JSON.stringify({ __type: 'ConditionalCheckFailedException', message: 'missing item' })); + } else { + res.statusCode = 400; + res.end(JSON.stringify({ + __type: 'TransactionCanceledException', + message: 'cancelled', + CancellationReasons: [{ Code: 'ConditionalCheckFailed' }, { Code: 'None' }], + })); } }); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); @@ -41,4 +48,13 @@ test('DynamoDB adapter marshals records and preserves conditional error codes', await assert.rejects(client.update({ TableName: 'Items', Key: { id: 'missing' } }).promise(), { code: 'ConditionalCheckFailedException', }); + await assert.rejects(client.transactWrite({ TransactItems: [ + { Put: { TableName: 'Claims', Item: { id: 'item' }, ConditionExpression: 'attribute_not_exists(id)' } }, + { Put: { TableName: 'Items', Item: { id: 'item' } } }, + ] }).promise(), (error) => { + assert.equal(error.code, 'TransactionCanceledException'); + assert.equal(error.CancellationReasons[0].Code, 'ConditionalCheckFailed'); + return true; + }); + assert.deepEqual(requests[3].TransactItems[0].Put.Item, { id: { S: 'item' } }); }); diff --git a/backend/lib/dynamodbClient.js b/backend/lib/dynamodbClient.js index fbf241c..fef5ac6 100644 --- a/backend/lib/dynamodbClient.js +++ b/backend/lib/dynamodbClient.js @@ -8,7 +8,7 @@ const client = DynamoDBDocument.from(new DynamoDBClient({ // Keep the request interface used by existing models while using the supported SDK. module.exports = Object.fromEntries( - ['get', 'put', 'update', 'delete', 'query', 'scan', 'batchWrite'].map((method) => [ + ['get', 'put', 'update', 'delete', 'query', 'scan', 'batchWrite', 'transactWrite'].map((method) => [ method, (params) => ({ promise: async () => { try { return await client[method](params); diff --git a/backend/models/__tests__/userEmail.test.js b/backend/models/__tests__/userEmail.test.js new file mode 100644 index 0000000..3250118 --- /dev/null +++ b/backend/models/__tests__/userEmail.test.js @@ -0,0 +1,84 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); + +const docClient = require('../../lib/dynamodbClient'); +const { memoryUserTables } = require('../../test-support/memoryUserTables'); +const { claimExistingEmails, createUserWithEmail, findUserByEmail } = require('../userEmail'); + +function withUsers(users) { + const db = memoryUserTables(); + for (const user of users) db.tables.Users.set(user.id, user); + Object.assign(docClient, db); + return db; +} + +test('refuses to replace an existing account and leaves its email claim alone', async () => { + const db = withUsers([]); + assert.equal(await createUserWithEmail({ id: 'u1', email: 'ann@example.com' }), true); + + await assert.rejects(createUserWithEmail({ id: 'u1', email: 'bob@example.com' }), { + code: 'TransactionCanceledException', + }); + assert.deepEqual([...db.tables.UserEmails.values()], [{ email: 'ann@example.com', userId: 'u1' }]); + assert.deepEqual(db.tables.Users.get('u1'), { id: 'u1', email: 'ann@example.com' }); +}); + +test('finds the claimed user with strongly consistent reads', async () => { + const db = withUsers([{ id: 'u1', email: 'ann@example.com' }]); + db.tables.UserEmails.set('ann@example.com', { email: 'ann@example.com', userId: 'u1' }); + const reads = []; + docClient.get = (params) => { reads.push(params); return db.get(params); }; + + assert.equal((await findUserByEmail(' ANN@example.com ')).id, 'u1'); + assert.deepEqual(reads.map(({ ConsistentRead }) => ConsistentRead), [true, true]); +}); + +test('claims normalised emails from every scan page and is safe to re-run', async () => { + const db = withUsers([ + { id: 'u1', email: ' Ann@Example.com' }, + { id: 'u2', email: 'bob@example.com' }, + { id: 'u3' }, + ]); + + assert.deepEqual(await claimExistingEmails(), { claimed: 2, alreadyClaimed: 0, needsReview: [] }); + assert.deepEqual(db.tables.UserEmails.get('ann@example.com'), { email: 'ann@example.com', userId: 'u1' }); + assert.deepEqual(db.tables.UserEmails.get('bob@example.com'), { email: 'bob@example.com', userId: 'u2' }); + assert.deepEqual(await claimExistingEmails(), { claimed: 0, alreadyClaimed: 2, needsReview: [] }); +}); + +test('holds an email shared by several accounts for review and reports their ids', async () => { + const db = withUsers([ + { id: 'u1', email: 'john@example.com' }, + { id: 'u2', email: 'JOHN@example.com ' }, + ]); + + assert.deepEqual(await claimExistingEmails(), { claimed: 0, alreadyClaimed: 0, needsReview: [['u1', 'u2']] }); + assert.deepEqual([...db.tables.UserEmails.values()], [{ email: 'john@example.com', needsReview: true }]); + // A new registration cannot take the held email, and login finds no owner. + assert.equal(await createUserWithEmail({ id: 'u3', email: 'John@example.com' }), false); + assert.equal(db.tables.Users.has('u3'), false); + assert.equal(await findUserByEmail('john@example.com'), undefined); + // A re-run reports the same accounts and leaves the hold in place. + assert.deepEqual(await claimExistingEmails(), { claimed: 0, alreadyClaimed: 0, needsReview: [['u1', 'u2']] }); + assert.deepEqual([...db.tables.UserEmails.values()], [{ email: 'john@example.com', needsReview: true }]); +}); + +test('never claims an email for a user row imported from a federation peer', async () => { + const db = withUsers([ + { id: 'u1', email: 'dana@example.com', sourceNodeId: 'peer-1' }, + { id: 'u2', email: 'erin@example.com' }, + { id: 'u3', email: 'erin@example.com', sourceNodeId: 'peer-1' }, + ]); + + assert.deepEqual(await claimExistingEmails(), { claimed: 1, alreadyClaimed: 0, needsReview: [] }); + assert.equal(db.tables.UserEmails.has('dana@example.com'), false); + assert.deepEqual(db.tables.UserEmails.get('erin@example.com'), { email: 'erin@example.com', userId: 'u2' }); +}); + +test('reports an email already claimed by a different account', async () => { + const db = withUsers([{ id: 'u2', email: 'carl@example.com' }]); + db.tables.UserEmails.set('carl@example.com', { email: 'carl@example.com', userId: 'u9' }); + + assert.deepEqual(await claimExistingEmails(), { claimed: 0, alreadyClaimed: 0, needsReview: [['u9', 'u2']] }); + assert.equal(db.tables.UserEmails.get('carl@example.com').userId, 'u9'); +}); diff --git a/backend/models/userEmail.js b/backend/models/userEmail.js new file mode 100644 index 0000000..6bfd8fa --- /dev/null +++ b/backend/models/userEmail.js @@ -0,0 +1,100 @@ +const docClient = require('../lib/dynamodbClient'); +const { USER_TABLE_NAME } = require('./user'); + +// One item per normalised email, { email, userId }, so a conditional write can +// refuse a second account and login can find the owner without scanning Users. +const USER_EMAIL_TABLE_NAME = process.env.USER_EMAIL_TABLE_NAME || 'UserEmails'; + +function normalizeEmail(email) { + return typeof email === 'string' ? email.trim().toLowerCase() : ''; +} + +// Writes the email claim and a new user together; returns false if the email is +// taken. An id that already exists cancels the transaction and throws. +async function createUserWithEmail(user) { + try { + await docClient.transactWrite({ + TransactItems: [ + { Put: { + TableName: USER_EMAIL_TABLE_NAME, + Item: { email: normalizeEmail(user.email), userId: user.id }, + ConditionExpression: 'attribute_not_exists(email)', + } }, + { Put: { TableName: USER_TABLE_NAME, Item: user, ConditionExpression: 'attribute_not_exists(id)' } }, + ], + }).promise(); + return true; + } catch (error) { + if (error.code === 'TransactionCanceledException' && + error.CancellationReasons?.[0]?.Code === 'ConditionalCheckFailed') return false; + throw error; + } +} + +async function findUserByEmail(email) { + const key = normalizeEmail(email); + if (!key) return undefined; + const { Item: claim } = await docClient.get({ + TableName: USER_EMAIL_TABLE_NAME, + Key: { email: key }, + ConsistentRead: true, + }).promise(); + // A claim held for review names no account, so nobody logs in with it. + if (!claim?.userId) return undefined; + const { Item: user } = await docClient.get({ + TableName: USER_TABLE_NAME, + Key: { id: claim.userId }, + ConsistentRead: true, + }).promise(); + return user; +} + +// Claims the emails of accounts created before UserEmails existed. Safe to re-run; +// Users rows are never changed, and rows imported from a federation peer +// (sourceNodeId) are never claimed. An email held by more than one account gets a +// claim naming no account: registration cannot take it, and none of them can log +// in until an operator replaces it with the owner's userId. +async function claimExistingEmails() { + const owners = new Map(); + let ExclusiveStartKey; + do { + const page = await docClient.scan({ TableName: USER_TABLE_NAME, ExclusiveStartKey }).promise(); + for (const user of page.Items || []) { + const key = normalizeEmail(user.email); + if (key && !user.sourceNodeId) owners.set(key, [...(owners.get(key) || []), user.id]); + } + ExclusiveStartKey = page.LastEvaluatedKey; + } while (ExclusiveStartKey); + + const report = { claimed: 0, alreadyClaimed: 0, needsReview: [] }; + for (const [email, ids] of owners) { + const shared = ids.length > 1; + try { + await docClient.put({ + TableName: USER_EMAIL_TABLE_NAME, + Item: shared ? { email, needsReview: true } : { email, userId: ids[0] }, + ConditionExpression: 'attribute_not_exists(email)', + }).promise(); + if (shared) report.needsReview.push(ids); + else report.claimed++; + } catch (error) { + if (error.code !== 'ConditionalCheckFailedException') throw error; + const { Item } = await docClient.get({ + TableName: USER_EMAIL_TABLE_NAME, + Key: { email }, + ConsistentRead: true, + }).promise(); + if (!shared && Item?.userId === ids[0]) report.alreadyClaimed++; + else report.needsReview.push([...new Set([Item?.userId, ...ids].filter(Boolean))]); + } + } + return report; +} + +module.exports = { + USER_EMAIL_TABLE_NAME, + normalizeEmail, + createUserWithEmail, + findUserByEmail, + claimExistingEmails, +}; diff --git a/backend/routes/__tests__/authRoutes.test.js b/backend/routes/__tests__/authRoutes.test.js index 55a778f..ddbe6f2 100644 --- a/backend/routes/__tests__/authRoutes.test.js +++ b/backend/routes/__tests__/authRoutes.test.js @@ -2,69 +2,140 @@ const request = require('supertest'); const express = require('express'); const bcrypt = require('bcryptjs'); -// Mock DynamoDB client methods -jest.mock('../../lib/dynamodbClient', () => { - return { - put: jest.fn().mockReturnValue({ - promise: jest.fn().mockResolvedValue({}) - }), - scan: jest.fn().mockReturnValue({ - promise: jest.fn().mockResolvedValue({ Items: [] }) - }) - }; -}); +// Replaced in beforeEach with in-memory Users and UserEmails tables +jest.mock('../../lib/dynamodbClient', () => ({})); const dynamoClient = require('../../lib/dynamodbClient'); +const { memoryUserTables } = require('../../test-support/memoryUserTables'); const authRoutes = require('../authRoutes'); +function cancelled(code) { + return Object.assign(new Error('Transaction cancelled'), { + code: 'TransactionCanceledException', + CancellationReasons: [{ Code: code }, { Code: 'None' }] + }); +} + describe('authRoutes', () => { let app; + let db; beforeEach(() => { process.env.JWT_SECRET = 'test-jwt-secret'; app = express(); app.use(express.json()); app.use('/', authRoutes); - jest.clearAllMocks(); + db = memoryUserTables(); + Object.assign(dynamoClient, db); }); + const register = (email, password = 'plainpass') => + request(app).post('/register').send({ username: 'john', email, phone: '123', password }); + describe('POST /register', () => { - it('responds with 201 and hashes password', async () => { - const res = await request(app) - .post('/register') - .send({ username: 'john', email: 'john@example.com', phone: '123', password: 'plainpass' }) - .expect(201); + it('responds with 201, hashes password and claims the normalised email', async () => { + const res = await register(' John@Example.COM ').expect(201); expect(res.body).toEqual({ message: 'User registered. Please verify your email/phone.' }); - expect(dynamoClient.put).toHaveBeenCalled(); + const [user] = db.tables.Users.values(); + expect(user.email).toBe('john@example.com'); + expect(user.password).not.toBe('plainpass'); + expect(await bcrypt.compare('plainpass', user.password)).toBe(true); + expect(db.tables.UserEmails.get('john@example.com')).toEqual({ email: 'john@example.com', userId: user.id }); + }); + + it('refuses a second account for the same email in any case or spacing', async () => { + await register('john@example.com').expect(201); + const res = await register(' JOHN@example.com ').expect(409); + + expect(res.body).toEqual({ error: 'Account could not be created' }); + expect(db.tables.Users.size).toBe(1); + }); + + it('lets only one of two concurrent registrations claim an email', async () => { + // Hold writes until both requests reach one, so any reads they make come + // first and only a conditional write can refuse the second account. + let arrived = 0; + let release; + const bothArrived = new Promise((resolve) => { release = resolve; }); + for (const method of ['put', 'transactWrite']) { + const write = db[method]; + dynamoClient[method] = (params) => ({ promise: async () => { + if (++arrived === 2) release(); + await bothArrived; + return write(params).promise(); + } }); + } + + const results = await Promise.all([register('ann@example.com'), register('Ann@Example.com')]); + + expect(results.map((res) => res.status).sort()).toEqual([201, 409]); + expect(db.tables.Users.size).toBe(1); + expect(db.tables.UserEmails.size).toBe(1); + }); + + it('returns 409 and writes no user when the conditional claim fails', async () => { + dynamoClient.transactWrite = () => ({ promise: () => Promise.reject(cancelled('ConditionalCheckFailed')) }); + + await register('john@example.com').expect(409); + expect(db.tables.Users.size).toBe(0); + }); + + it('reports other cancelled transactions as errors, not duplicates', async () => { + dynamoClient.transactWrite = () => ({ promise: () => Promise.reject(cancelled('TransactionConflict')) }); + + await register('john@example.com').expect(500); + }); - const params = dynamoClient.put.mock.calls[0][0]; - const item = params.Item || params; - expect(item.password).not.toBe('plainpass'); + it('requires an email', async () => { + await register(undefined).expect(400); + await register(' ').expect(400); + expect(db.tables.Users.size).toBe(0); }); }); describe('POST /login', () => { it('valid email/password returns token and user without password', async () => { - const hashed = await bcrypt.hash('plainpass', 10); - dynamoClient.scan.mockReturnValueOnce({ - promise: () => Promise.resolve({ Items: [{ email: 'john@example.com', password: hashed, username: 'john' }] }) - }); + await register('john@example.com').expect(201); + const [stored] = db.tables.Users.values(); const res = await request(app) .post('/login') - .send({ email: 'john@example.com', password: 'plainpass' }) + .send({ email: ' John@Example.com ', password: 'plainpass' }) .expect(200); expect(res.body.token).toBeDefined(); expect(res.body.user).toBeDefined(); + expect(res.body.user.id).toBe(stored.id); expect(res.body.user.password).toBeUndefined(); }); - it('invalid password/email returns 401', async () => { - const hashed = await bcrypt.hash('rightpass', 10); - dynamoClient.scan.mockReturnValueOnce({ - promise: () => Promise.resolve({ Items: [{ email: 'john@example.com', password: hashed }] }) - }); + it('finds the claimed account when it is not on the first scan page', async () => { + const password = await bcrypt.hash('plainpass', 10); + db.tables.Users.set('u1', { id: 'u1', email: 'other@example.com', password }); + db.tables.Users.set('u2', { id: 'u2', email: 'john@example.com', password }); + db.tables.UserEmails.set('john@example.com', { email: 'john@example.com', userId: 'u2' }); + + const res = await request(app) + .post('/login') + .send({ email: 'john@example.com', password: 'plainpass' }) + .expect(200); + + expect(res.body.user.id).toBe('u2'); + }); + + it('does not pick one of several accounts sharing an unclaimed email', async () => { + const password = await bcrypt.hash('plainpass', 10); + db.tables.Users.set('u1', { id: 'u1', email: 'john@example.com', password }); + db.tables.Users.set('u2', { id: 'u2', email: 'john@example.com', password }); + + await request(app) + .post('/login') + .send({ email: 'john@example.com', password: 'plainpass' }) + .expect(401); + }); + + it('invalid password returns 401', async () => { + await register('john@example.com', 'rightpass').expect(201); await request(app) .post('/login') @@ -73,16 +144,10 @@ describe('authRoutes', () => { }); it('returns 401 when no user matches the email', async () => { - dynamoClient.scan.mockReturnValueOnce({ - promise: () => Promise.resolve({ Items: [] }) - }); - await request(app) .post('/login') .send({ email: 'missing@example.com', password: 'plainpass' }) .expect(401); - - expect(dynamoClient.scan).toHaveBeenCalled(); }); }); }); diff --git a/backend/routes/__tests__/userRoutes.test.js b/backend/routes/__tests__/userRoutes.test.js index 8e0886f..393700a 100644 --- a/backend/routes/__tests__/userRoutes.test.js +++ b/backend/routes/__tests__/userRoutes.test.js @@ -4,7 +4,7 @@ const bcrypt = require('bcryptjs'); jest.mock('../../lib/dynamodbClient', () => ({ scan: jest.fn(), - put: jest.fn() + transactWrite: jest.fn() })); const docClient = require('../../lib/dynamodbClient'); @@ -17,7 +17,7 @@ describe('userRoutes', () => { app.use(express.json()); app.use('/users', userRoutes); docClient.scan.mockReset(); - docClient.put.mockReset(); + docClient.transactWrite.mockReset(); }); describe('GET /users', () => { @@ -54,7 +54,7 @@ describe('userRoutes', () => { describe('POST /users', () => { it('creates user with hashed password and defaults', async () => { - docClient.put.mockReturnValue({ promise: () => Promise.resolve({}) }); + docClient.transactWrite.mockReturnValue({ promise: () => Promise.resolve({}) }); const res = await request(app) .post('/users') @@ -79,12 +79,32 @@ describe('userRoutes', () => { }); expect(res.body.user.password).toBeUndefined(); - const params = docClient.put.mock.calls[0][0]; - const stored = params.Item; + const [claim, put] = docClient.transactWrite.mock.calls[0][0].TransactItems; + const stored = put.Put.Item; + expect(claim.Put.Item).toEqual({ email: 'a@b.com', userId: '1' }); + expect(claim.Put.ConditionExpression).toBe('attribute_not_exists(email)'); + expect(put.Put.ConditionExpression).toBe('attribute_not_exists(id)'); expect(stored.verified).toBe(false); expect(stored.reputationScore).toBe(0); expect(await bcrypt.compare('secret', stored.password)).toBe(true); }); + + it('refuses an email that is already registered, in any case or spacing', async () => { + docClient.transactWrite.mockReturnValue({ + promise: () => Promise.reject(Object.assign(new Error('Transaction cancelled'), { + code: 'TransactionCanceledException', + CancellationReasons: [{ Code: 'ConditionalCheckFailed' }, { Code: 'None' }] + })) + }); + + const res = await request(app) + .post('/users') + .send({ id: '2', username: 'alice2', email: ' A@B.com ', password: 'secret' }); + + expect(res.status).toBe(409); + const [claim] = docClient.transactWrite.mock.calls[0][0].TransactItems; + expect(claim.Put.Item).toEqual({ email: 'a@b.com', userId: '2' }); + }); }); }); diff --git a/backend/routes/authRoutes.js b/backend/routes/authRoutes.js index 9d4411c..b7783e2 100644 --- a/backend/routes/authRoutes.js +++ b/backend/routes/authRoutes.js @@ -1,8 +1,8 @@ const express = require("express"); const bcrypt = require("../utils/bcrypt"); const jwt = require("../utils/jwt"); -const { USER_TABLE_NAME, createUserItem } = require("../models/user"); -const docClient = require('../lib/dynamodbClient'); +const { createUserItem } = require("../models/user"); +const { normalizeEmail, createUserWithEmail, findUserByEmail } = require("../models/userEmail"); const { randomUUID } = require('crypto'); const router = express.Router(); try { @@ -18,10 +18,14 @@ router.get("/test", (req, res) => { // Register User router.post("/register", async (req, res) => { try { - const { username, email, phone, password } = req.body; + const { username, phone, password } = req.body; + const email = normalizeEmail(req.body.email); + if (!email) return res.status(400).json({ error: "Email is required." }); const hashedPassword = await bcrypt.hash(password, 10); const user = createUserItem({ id: randomUUID(), username, email, phone, password: hashedPassword }); - await docClient.put({ TableName: USER_TABLE_NAME, Item: user }).promise(); + if (!(await createUserWithEmail(user))) { + return res.status(409).json({ error: "Account could not be created" }); + } res.status(201).json({ message: "User registered. Please verify your email/phone." }); } catch (error) { res.status(500).json({ error: "Error registering user." }); @@ -32,12 +36,7 @@ router.post("/register", async (req, res) => { router.post("/login", async (req, res) => { try { const { email, password } = req.body; - const { Items = [] } = await docClient.scan({ - TableName: USER_TABLE_NAME, - FilterExpression: 'email = :email', - ExpressionAttributeValues: { ':email': email }, - }).promise(); - const user = Items[0]; + const user = await findUserByEmail(email); if (!user || !(await bcrypt.compare(password, user.password))) { return res.status(401).json({ error: "Invalid credentials" }); diff --git a/backend/routes/userRoutes.js b/backend/routes/userRoutes.js index 18e12fa..7148357 100644 --- a/backend/routes/userRoutes.js +++ b/backend/routes/userRoutes.js @@ -3,6 +3,7 @@ const bcrypt = require('../utils/bcrypt'); const router = express.Router(); const docClient = require('../lib/dynamodbClient'); const { USER_TABLE_NAME, createUserItem } = require('../models/user'); +const { normalizeEmail, createUserWithEmail } = require('../models/userEmail'); // GET /users - fetch all users, omit sensitive data router.get('/', async (req, res) => { @@ -38,14 +39,19 @@ router.get('/', async (req, res) => { router.post('/', async (req, res) => { try { const { password, ...rest } = req.body; + const email = normalizeEmail(rest.email); + if (!email) return res.status(400).json({ error: 'Email is required' }); const hashedPassword = await bcrypt.hash(password, 10); const item = createUserItem({ ...rest, + email, password: hashedPassword, verified: false, reputationScore: 0 }); - await docClient.put({ TableName: USER_TABLE_NAME, Item: item }).promise(); + if (!(await createUserWithEmail(item))) { + return res.status(409).json({ error: 'User already exists' }); + } const { password: _pwd, ...user } = item; res.status(201).json({ message: 'User created', user }); } catch (err) { diff --git a/backend/scripts/claim-user-emails.js b/backend/scripts/claim-user-emails.js new file mode 100644 index 0000000..110f26e --- /dev/null +++ b/backend/scripts/claim-user-emails.js @@ -0,0 +1,18 @@ +// Claims the email of every existing account in UserEmails. Run once after +// creating that table, before deploying the code that requires it, and again +// after the deploy. It only adds claims; Users rows are never changed. +try { + require('dotenv').config(); +} catch { /* Environment may be supplied by the process. */ } +const { claimExistingEmails } = require('../models/userEmail'); + +claimExistingEmails().then((report) => { + console.log(`Claimed ${report.claimed} emails; ${report.alreadyClaimed} were already claimed.`); + for (const ids of report.needsReview) { + console.log(`Needs review, accounts sharing one email: ${ids.join(', ')}`); + } + if (report.needsReview.length) process.exitCode = 1; +}).catch((error) => { + console.error(error); + process.exitCode = 1; +}); diff --git a/backend/test-support/memoryUserTables.js b/backend/test-support/memoryUserTables.js new file mode 100644 index 0000000..ba1f4ac --- /dev/null +++ b/backend/test-support/memoryUserTables.js @@ -0,0 +1,41 @@ +// In-memory Users and UserEmails tables with the DynamoDB wrapper's interface. +// Scans return one row per page, so a caller that ignores LastEvaluatedKey +// only ever sees the first row. Every ConditionExpression used against these +// tables is attribute_not_exists on the key, which is all this models. +function memoryUserTables() { + const tables = { Users: new Map(), UserEmails: new Map() }; + const keyOf = (TableName, item) => (TableName === 'UserEmails' ? item.email : item.id); + const conditionFails = ({ TableName, Item, ConditionExpression }) => + Boolean(ConditionExpression) && tables[TableName].has(keyOf(TableName, Item)); + return { + tables, + get: ({ TableName, Key }) => ({ promise: async () => ({ Item: tables[TableName].get(keyOf(TableName, Key)) }) }), + put: (params) => ({ promise: async () => { + if (conditionFails(params)) { + throw Object.assign(new Error('condition failed'), { code: 'ConditionalCheckFailedException' }); + } + tables[params.TableName].set(keyOf(params.TableName, params.Item), params.Item); + return {}; + } }), + transactWrite: ({ TransactItems }) => ({ promise: async () => { + const puts = TransactItems.map(({ Put }) => Put); + const reasons = puts.map((put) => ({ Code: conditionFails(put) ? 'ConditionalCheckFailed' : 'None' })); + if (reasons.some(({ Code }) => Code !== 'None')) { + throw Object.assign(new Error('transaction cancelled'), { + code: 'TransactionCanceledException', CancellationReasons: reasons, + }); + } + for (const { TableName, Item } of puts) tables[TableName].set(keyOf(TableName, Item), Item); + return {}; + } }), + scan: ({ TableName, ExpressionAttributeValues, ExclusiveStartKey }) => ({ promise: async () => { + const rows = [...tables[TableName].values()]; + const start = ExclusiveStartKey ? rows.findIndex((row) => row.id === ExclusiveStartKey.id) + 1 : 0; + const Items = rows.slice(start, start + 1) + .filter((row) => !ExpressionAttributeValues || row.email === ExpressionAttributeValues[':email']); + return start + 1 < rows.length ? { Items, LastEvaluatedKey: { id: rows[start].id } } : { Items }; + } }), + }; +} + +module.exports = { memoryUserTables };