|
| 1 | +/* |
| 2 | + * Copyright 2026 LiteFarm.org |
| 3 | + * This file is part of LiteFarm. |
| 4 | + * |
| 5 | + * LiteFarm is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * LiteFarm is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details, see <https://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +import { NextFunction, Response } from 'express'; |
| 17 | +import jwt from 'jsonwebtoken'; |
| 18 | +import { tokenType } from '../../util/jwt.js'; |
| 19 | +import userModel from '../../models/userModel.js'; |
| 20 | +import farmModel from '../../models/farmModel.js'; |
| 21 | +import userFarmModel from '../../models/userFarmModel.js'; |
| 22 | +import { HttpError, LiteFarmRequest } from '../../types.js'; |
| 23 | + |
| 24 | +export interface OfflineEventLogReqBody { |
| 25 | + logs: { |
| 26 | + event_name?: string; |
| 27 | + event_at?: Date | number; |
| 28 | + status_code?: number; |
| 29 | + url?: string; |
| 30 | + }[]; |
| 31 | + went_online_at?: Date | number; |
| 32 | + farm_id?: string; |
| 33 | + app_version?: string; |
| 34 | + network?: string; |
| 35 | + session_id?: string; |
| 36 | +} |
| 37 | + |
| 38 | +export function checkAuthForOfflineLogs() { |
| 39 | + return async ( |
| 40 | + req: LiteFarmRequest<unknown, unknown, unknown, OfflineEventLogReqBody>, |
| 41 | + res: Response, |
| 42 | + next: NextFunction, |
| 43 | + ) => { |
| 44 | + const token = req.headers.authorization?.split(' ')[1]; |
| 45 | + |
| 46 | + try { |
| 47 | + if (token) { |
| 48 | + const decoded = jwt.verify(token, tokenType.access!, { ignoreExpiration: true }); |
| 49 | + |
| 50 | + if (typeof decoded === 'string' || typeof decoded.exp !== 'number' || !decoded.user_id) { |
| 51 | + throw new Error('invalid token'); |
| 52 | + } |
| 53 | + |
| 54 | + const now = Math.floor(Date.now() / 1000); // JWT exp is in seconds |
| 55 | + |
| 56 | + const tokenExpired = decoded.exp < now; |
| 57 | + |
| 58 | + res.locals.log_status = tokenExpired ? 'expired' : 'authenticated'; |
| 59 | + res.locals.user_id = decoded.user_id; |
| 60 | + } else { |
| 61 | + res.locals.log_status = 'anonymous'; |
| 62 | + } |
| 63 | + |
| 64 | + next(); |
| 65 | + } catch (err) { |
| 66 | + console.error(err); |
| 67 | + return res.status(401).json({ error: 'Unauthorized' }); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export function checkOfflineLogs() { |
| 73 | + return async ( |
| 74 | + req: LiteFarmRequest<unknown, unknown, unknown, OfflineEventLogReqBody>, |
| 75 | + res: Response, |
| 76 | + next: NextFunction, |
| 77 | + ) => { |
| 78 | + try { |
| 79 | + if (!Array.isArray(req.body.logs) || req.body.logs.length === 0) { |
| 80 | + throw new Error('logs must be a non-empty array'); |
| 81 | + } |
| 82 | + |
| 83 | + if (res.locals.user_id) { |
| 84 | + const user = await userModel.query().findOne({ user_id: res.locals.user_id }); |
| 85 | + |
| 86 | + if (!user) { |
| 87 | + throw new Error('User not found'); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + const { farm_id } = req.body; |
| 92 | + |
| 93 | + if (farm_id) { |
| 94 | + /* @ts-expect-error known issue with models */ |
| 95 | + const farm = await farmModel.query().findOne({ farm_id }); |
| 96 | + |
| 97 | + if (!farm) { |
| 98 | + throw new Error('Invalid farm_id'); |
| 99 | + } |
| 100 | + |
| 101 | + if (res.locals.user_id) { |
| 102 | + const userFarm = await userFarmModel |
| 103 | + .query() |
| 104 | + .findOne({ user_id: res.locals.user_id, farm_id }); |
| 105 | + |
| 106 | + if (!userFarm) { |
| 107 | + throw new Error('user_id and farm_id do not match'); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + res.locals.country_id = farm.country_id; |
| 112 | + } |
| 113 | + |
| 114 | + next(); |
| 115 | + } catch (error: unknown) { |
| 116 | + console.error(error); |
| 117 | + |
| 118 | + const err = error as HttpError; |
| 119 | + const status = err.status || err.code || 500; |
| 120 | + return res.status(status).json({ |
| 121 | + error: err.message || err, |
| 122 | + }); |
| 123 | + } |
| 124 | + }; |
| 125 | +} |
0 commit comments