diff --git a/public/app.js b/public/app.js index d9b393c..c09df1a 100644 --- a/public/app.js +++ b/public/app.js @@ -1,7 +1,7 @@ // public/app.js - front-end entry point. Wires state, UI, API, and event stream. -import { api } from './js/api.js?v=32'; -import { createStream } from './js/stream.js?v=32'; -import * as ui from './js/ui.js?v=32'; +import { api } from './js/api.js?v=33'; +import { createStream } from './js/stream.js?v=33'; +import * as ui from './js/ui.js?v=33'; window.addEventListener('error', (e) => { console.error('[ui] error', e.error || e.message); @@ -96,6 +96,11 @@ function normalizeName(s) { return String(s || '').toLowerCase().replace(/\s+/g, ''); } +function buddyName(b) { + if (typeof b === 'string') return b; + return String(b?.aimId || b?.displayId || b?.screenName || b?.name || ''); +} + // --- Event handlers (M0 partially confirmed) --- function aimId(obj) { @@ -253,11 +258,10 @@ stream.on('toc_error', (event) => { function mergeBuddyPreservePresence(incoming) { const prev = new Map(); for (const b of (state.buddies || [])) { - const name = typeof b === 'string' ? b : (b.aimId || b.displayId || b.screenName || b.name || ''); - prev.set(normalizeName(name), b); + prev.set(normalizeName(buddyName(b)), b); } return (incoming || []).map((b) => { - const name = typeof b === 'string' ? b : (b.aimId || b.displayId || b.screenName || b.name || ''); + const name = buddyName(b); const old = prev.get(normalizeName(name)); if (!old || typeof old === 'string') return b; const base = typeof b === 'string' ? { screenName: b } : { ...b }; diff --git a/public/index.html b/public/index.html index 22a2718..71f814a 100644 --- a/public/index.html +++ b/public/index.html @@ -4,7 +4,7 @@ Web OSCAR Chat - +
@@ -98,6 +98,6 @@
- + diff --git a/src/routes/auth.js b/src/routes/auth.js index 88e3267..82e812e 100644 --- a/src/routes/auth.js +++ b/src/routes/auth.js @@ -66,28 +66,77 @@ authRouter.get('/challenge', (req, res) => { }); }); -authRouter.post('/login', async (req, res) => { - const screenName = cleanScreenName(req.body?.screenName); - const password = String(req.body?.password || '').replace(/[\u0000-\u001f\u007f]/g, '').slice(0, 256); +function readCaptcha(req) { const captchaAnswer = Number(req.body?.captchaAnswer); const expectedCaptcha = Number(req.session?.captchaAnswer); req.session.captchaAnswer = null; + return Number.isFinite(expectedCaptcha) && Number.isFinite(captchaAnswer) && captchaAnswer === expectedCaptcha; +} - if (!Number.isFinite(expectedCaptcha) || !Number.isFinite(captchaAnswer) || captchaAnswer !== expectedCaptcha) { +function readPassword(req) { + return String(req.body?.password || '').replace(/[\u0000-\u001f\u007f]/g, '').slice(0, 256); +} + +function resolveLoginTarget(body) { + const host = cleanHost(body?.host) || process.env.WEBAPI_HOST || process.env.TOC_HOST || 'oscar.claytonia.net'; + const webapiPort = cleanPort(body?.webapiPort, cleanPort(process.env.WEBAPI_PORT, 8081)); + const tocPort = cleanPort(body?.tocPort, cleanPort(process.env.TOC_PORT, 9898)); + const webapiBaseHost = net.isIP(host) === 6 ? `[${host}]` : host; + return { host, webapiPort, tocPort, webapiBase: `http://${webapiBaseHost}:${webapiPort}` }; +} + +async function tryWebApiLogin({ screenName, password, webapiBase, sessionId }) { + const loginData = await oscar.clientLogin(screenName, password, { base: webapiBase }); + let token = loginData?.token || loginData?.a || loginData?.loginCookie; + if (token && typeof token === 'object') token = token.a || token.token || token.loginCookie; + if (typeof token !== 'string' || !token) throw new Error('no token in login response'); + const startData = await oscar.startSession(token, { base: webapiBase }); + const aimsid = startData?.aimsid; + if (!aimsid) throw new Error('startSession returned no aimsid'); + return { + sessionId, + screenName, + token, + aimsid, + webapiBase, + seqNum: typeof startData?.lastSeqNum === 'number' ? startData.lastSeqNum : -1, + sseClients: new Set(), + polling: false, + fetchBaseURL: startData?.fetchBaseURL, + eventBuffer: [], + }; +} + +async function tryTocLogin(rec, { screenName, host, tocPort, password, webapiBase, sessionId }) { + const next = rec || { + sessionId, + screenName, + token: null, + aimsid: null, + webapiBase, + seqNum: -1, + sseClients: new Set(), + polling: false, + eventBuffer: [], + }; + await startTOCSession(next, { host, port: tocPort, password }); + return next; +} + +authRouter.post('/login', async (req, res) => { + const screenName = cleanScreenName(req.body?.screenName); + const password = readPassword(req); + if (!readCaptcha(req)) { return res.status(400).json({ error: 'Bot check failed. Please solve the math problem.' }); } if (!screenName || !password) { return res.status(400).json({ error: 'screenName and password are required' }); } - const host = cleanHost(req.body?.host) || process.env.WEBAPI_HOST || process.env.TOC_HOST || 'oscar.claytonia.net'; - const webapiPort = cleanPort(req.body?.webapiPort, cleanPort(process.env.WEBAPI_PORT, 8081)); - const tocPort = cleanPort(req.body?.tocPort, cleanPort(process.env.TOC_PORT, 9898)); + const { host, webapiPort: _webapiPort, tocPort, webapiBase } = resolveLoginTarget(req.body); if (await isDisallowedHost(host)) { return res.status(400).json({ error: 'Server host is not allowed. Local/private addresses are blocked.' }); } - const webapiBaseHost = net.isIP(host) === 6 ? `[${host}]` : host; - const webapiBase = `http://${webapiBaseHost}:${webapiPort}`; // If this browser session already has an OSCAR session, clean it up first. const existing = sessions.getSession(req.session.id); @@ -105,52 +154,19 @@ authRouter.post('/login', async (req, res) => { let tocErr = null; let rec = null; - // Try Web API login (buddy list/presence/profile). try { - const loginData = await oscar.clientLogin(screenName, password, { base: webapiBase }); - let token = loginData?.token || loginData?.a || loginData?.loginCookie; - if (token && typeof token === 'object') token = token.a || token.token || token.loginCookie; - if (typeof token !== 'string' || !token) throw new Error('no token in login response'); - const startData = await oscar.startSession(token, { base: webapiBase }); - const aimsid = startData?.aimsid; - if (!aimsid) throw new Error('startSession returned no aimsid'); - rec = { - sessionId: req.session.id, - screenName, - token, - aimsid, - webapiBase, - seqNum: typeof startData?.lastSeqNum === 'number' ? startData.lastSeqNum : -1, - sseClients: new Set(), - polling: false, - fetchBaseURL: startData?.fetchBaseURL, - eventBuffer: [], - }; + rec = await tryWebApiLogin({ screenName, password, webapiBase, sessionId: req.session.id }); } catch (err) { webErr = err; console.warn('[login] webapi failed:', err.message); } - // Try TOC login for IM/chat/invites (works where Web API chat is unavailable). try { - if (!rec) { - rec = { - sessionId: req.session.id, - screenName, - token: null, - aimsid: null, - webapiBase, - seqNum: -1, - sseClients: new Set(), - polling: false, - eventBuffer: [], - }; - } - await startTOCSession(rec, { host, port: tocPort, password }); + rec = await tryTocLogin(rec, { screenName, host, tocPort, password, webapiBase, sessionId: req.session.id }); } catch (err) { tocErr = err; console.warn('[login] toc failed:', err.message); - if (!rec?.aimsid) rec = null; // no usable session + if (!rec?.aimsid) rec = null; } if (!rec) {