Backend services: - targo-hub: extract deepGetValue to helpers.js, DRY disconnect reasons lookup map, compact CAPABILITIES, consolidate vision.js prompts/schemas, extract dispatch scoring weights, trim section dividers across 9 files - modem-bridge: extract getSession() helper (6 occurrences), resetIdleTimer(), consolidate DM query factory, fix duplicate username fill bug, trim headers (server.js -36%, tplink-session.js -47%, docker-compose.yml -57%) Frontend: - useWifiDiagnostic: extract THRESHOLDS const, split processDiagnostic into 6 focused helpers (processOnlineStatus, processWanIPs, processRadios, processMeshNodes, processClients, checkRadioIssues) - EquipmentDetail: merge duplicate ROLE_LABELS, remove verbose comments Documentation (17 → 13 files, -1,400 lines): - New consolidated README.md (architecture, services, dependencies, auth) - Merge ECOSYSTEM-OVERVIEW into ARCHITECTURE.md - Merge MIGRATION-PLAN + ARCHITECTURE-COMPARE + FIELD-GAP + CHANGELOG → MIGRATION.md - Merge COMPETITIVE-ANALYSIS into PLATFORM-STRATEGY.md - Update ROADMAP.md with current phase status - Delete CONTEXT.md (absorbed into README) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
130 lines
5.0 KiB
JavaScript
130 lines
5.0 KiB
JavaScript
'use strict'
|
|
const crypto = require('crypto')
|
|
const cfg = require('./config')
|
|
const { log, json, parseBody } = require('./helpers')
|
|
|
|
function base64url (buf) {
|
|
return (typeof buf === 'string' ? Buffer.from(buf) : buf)
|
|
.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
|
}
|
|
|
|
function base64urlDecode (str) {
|
|
str = str.replace(/-/g, '+').replace(/_/g, '/')
|
|
while (str.length % 4) str += '='
|
|
return Buffer.from(str, 'base64')
|
|
}
|
|
|
|
const JWT_HEADER = base64url(JSON.stringify({ alg: 'HS256', typ: 'JWT' }))
|
|
|
|
function signJwt (payload) {
|
|
const body = base64url(JSON.stringify(payload))
|
|
const data = JWT_HEADER + '.' + body
|
|
const sig = base64url(crypto.createHmac('sha256', cfg.JWT_SECRET).update(data).digest())
|
|
return data + '.' + sig
|
|
}
|
|
|
|
function verifyJwt (token) {
|
|
const parts = token.split('.')
|
|
if (parts.length !== 3) return null
|
|
const data = parts[0] + '.' + parts[1]
|
|
const sig = base64url(crypto.createHmac('sha256', cfg.JWT_SECRET).update(data).digest())
|
|
if (sig !== parts[2]) return null
|
|
try {
|
|
const payload = JSON.parse(base64urlDecode(parts[1]).toString())
|
|
if (payload.exp && Date.now() / 1000 > payload.exp) return null
|
|
return payload
|
|
} catch { return null }
|
|
}
|
|
|
|
function generateToken (techId, jobId, ttlHours = 72) {
|
|
const payload = {
|
|
sub: techId,
|
|
job: jobId,
|
|
iat: Math.floor(Date.now() / 1000),
|
|
exp: Math.floor(Date.now() / 1000) + ttlHours * 3600,
|
|
}
|
|
return signJwt(payload)
|
|
}
|
|
|
|
function generateLink (techId, jobId, ttlHours = 72) {
|
|
const token = generateToken(techId, jobId, ttlHours)
|
|
return `${cfg.FIELD_APP_URL}/t/${token}`
|
|
}
|
|
|
|
// Tech-level token: access to all their jobs (used in the schedule SMS)
|
|
function generateTechToken (techId, ttlHours = 72) {
|
|
const payload = {
|
|
sub: techId,
|
|
scope: 'all',
|
|
iat: Math.floor(Date.now() / 1000),
|
|
exp: Math.floor(Date.now() / 1000) + ttlHours * 3600,
|
|
}
|
|
return signJwt(payload)
|
|
}
|
|
|
|
function generateTechLink (techId, ttlHours = 72) {
|
|
const token = generateTechToken(techId, ttlHours)
|
|
return `${cfg.FIELD_APP_URL}/t/${token}`
|
|
}
|
|
|
|
async function handle (req, res, method, path) {
|
|
if (path === '/magic-link/generate' && method === 'POST') {
|
|
const body = await parseBody(req)
|
|
const { tech_id, job_id, ttl_hours } = body
|
|
if (!tech_id) return json(res, 400, { error: 'tech_id required' })
|
|
if (job_id) {
|
|
const link = generateLink(tech_id, job_id, ttl_hours || 72)
|
|
log(`Magic link generated: tech=${tech_id} job=${job_id}`)
|
|
return json(res, 200, { ok: true, link, token: generateToken(tech_id, job_id, ttl_hours || 72) })
|
|
} else {
|
|
const link = generateTechLink(tech_id, ttl_hours || 72)
|
|
log(`Magic link generated (all jobs): tech=${tech_id}`)
|
|
return json(res, 200, { ok: true, link, token: generateTechToken(tech_id, ttl_hours || 72) })
|
|
}
|
|
}
|
|
|
|
if (path === '/magic-link/verify' && method === 'GET') {
|
|
const url = new URL(req.url, `http://localhost:${cfg.PORT}`)
|
|
const token = url.searchParams.get('token')
|
|
if (!token) return json(res, 400, { error: 'token required' })
|
|
const payload = verifyJwt(token)
|
|
if (!payload) return json(res, 401, { error: 'invalid_or_expired', message: 'Ce lien a expiré. Un nouveau lien vous sera envoyé par SMS.' })
|
|
log(`Magic link verified: tech=${payload.sub} job=${payload.job || 'all'}`)
|
|
return json(res, 200, { ok: true, tech_id: payload.sub, job_id: payload.job || null, scope: payload.scope || 'job', exp: payload.exp })
|
|
}
|
|
|
|
if (path === '/magic-link/refresh' && method === 'POST') {
|
|
const body = await parseBody(req)
|
|
const { tech_id } = body
|
|
if (!tech_id) return json(res, 400, { error: 'tech_id required' })
|
|
const { erpFetch } = require('./helpers')
|
|
const techRes = await erpFetch(`/api/resource/Dispatch%20Technician/${encodeURIComponent(tech_id)}`)
|
|
if (techRes.status !== 200) return json(res, 404, { error: 'Tech not found' })
|
|
const tech = techRes.data.data
|
|
if (!tech.phone) return json(res, 400, { error: 'No phone number for this technician' })
|
|
const link = generateTechLink(tech_id, 72)
|
|
const msg = `Voici votre nouveau lien pour accéder à vos tâches:\n${link}`
|
|
const { sendSmsInternal } = require('./twilio')
|
|
const sid = await sendSmsInternal(tech.phone, msg)
|
|
if (!sid) return json(res, 502, { error: 'SMS send failed' })
|
|
log(`Magic link refreshed + SMS sent: tech=${tech_id} phone=${tech.phone}`)
|
|
return json(res, 200, { ok: true, sent_to: tech.phone })
|
|
}
|
|
|
|
return json(res, 404, { error: 'Magic link endpoint not found' })
|
|
}
|
|
|
|
function generateCustomerToken (customerId, customerName, email, ttlHours = 24) {
|
|
const payload = {
|
|
sub: customerId,
|
|
scope: 'customer',
|
|
name: customerName || customerId,
|
|
email: email || '',
|
|
iat: Math.floor(Date.now() / 1000),
|
|
exp: Math.floor(Date.now() / 1000) + ttlHours * 3600,
|
|
}
|
|
return signJwt(payload)
|
|
}
|
|
|
|
module.exports = { handle, generateToken, generateLink, generateTechToken, generateTechLink, generateCustomerToken, verifyJwt, signJwt }
|