Refactor(medium): split auth login flow, small frontend buddy helper, bump cache
This commit is contained in:
parent
592eafe10a
commit
796e493376
3 changed files with 73 additions and 53 deletions
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Web OSCAR Chat</title>
|
||||
<link rel="stylesheet" href="/style.css?v=32">
|
||||
<link rel="stylesheet" href="/style.css?v=33">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
|
|
@ -98,6 +98,6 @@
|
|||
</main>
|
||||
</div>
|
||||
|
||||
<script type="module" src="/app.js?v=32"></script>
|
||||
<script type="module" src="/app.js?v=33"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue