@@ -88,146 +88,6 @@ class AuthAuditService extends BaseService {
88
88
* @param {Object } params.extra - Additional information related to the event.
89
89
* @returns {Promise<void> } - A promise that resolves when the event is recorded.
90
90
*/
91
-
92
-
93
- ++ ++ + src / backend / src / services / abuse - prevention / AuthAuditService . js
94
- /*
95
- * Copyright (C) 2024 Puter Technologies Inc.
96
- *
97
- * This file is part of Puter.
98
- *
99
- * Puter is free software: you can redistribute it and/or modify
100
- * it under the terms of the GNU Affero General Public License as published
101
- * by the Free Software Foundation, either version 3 of the License, or
102
- * (at your option) any later version.
103
- *
104
- * This program is distributed in the hope that it will be useful,
105
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
106
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
107
- * GNU Affero General Public License for more details.
108
- *
109
- * You should have received a copy of the GNU Affero General Public License
110
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
111
- */
112
-
113
- const BaseService = require ( "../BaseService" ) ;
114
- const { DB_WRITE } = require ( "../database/consts" ) ;
115
-
116
- /**
117
- * This class handles authentication audit services.
118
- *
119
- * @extends BaseService
120
- */
121
- class AuthAuditService extends BaseService {
122
- /**
123
- * Modules used by this service.
124
- *
125
- * @static
126
- * @type {Object }
127
- */
128
- static MODULES = {
129
- uuidv4 : require ( 'uuid' ) . v4 ,
130
- } ;
131
-
132
- /**
133
- * Initializes the AuthAuditService.
134
- *
135
- * @async
136
- * @private
137
- * @returns {Promise<void> } - A promise that resolves when initialization is complete.
138
- */
139
- async _init ( ) {
140
- this . db = this . services . get ( 'database' ) . get ( DB_WRITE , 'auth:audit' ) ;
141
- }
142
-
143
- /**
144
- * Records an authentication audit event.
145
- *
146
- * This method logs an authentication audit event with the provided parameters.
147
- * It generates a unique identifier for the event, serializes the requester,
148
- * body, and extra information, and writes the event to the database.
149
- *
150
- * @param {Object } params - The parameters for the authentication audit event.
151
- * @param {Object } params.requester - The requester information.
152
- * @param {string } params.requester.ip - The IP address of the requester.
153
- * @param {string } params.requester.ua - The user-agent string of the requester.
154
- * @param {Function } params.requester.serialize - A function to serialize the requester information.
155
- * @param {string } params.action - The action performed during the authentication event.
156
- * @param {Object } params.body - The body of the request.
157
- * @param {Object } params.extra - Additional information related to the event.
158
- * @returns {Promise<void> } - A promise that resolves when the event is recorded.
159
- */
160
- async record ( parameters ) {
161
- try {
162
- await this . _record ( parameters ) ;
163
- } catch ( err ) {
164
- this . errors . report ( 'auth-audit-service.record' , {
165
- source : err ,
166
- trace : true ,
167
- alarm : true ,
168
- } ) ;
169
- }
170
- }
171
-
172
- /**
173
- * Internal method to record an authentication audit event.
174
- *
175
- * @private
176
- * @param {Object } params - The parameters for the authentication audit event.
177
- * @param {Object } params.requester - The requester information.
178
- * @param {string } params.requester.ip - The IP address of the requester.
179
- * @param {string } params.requester.ua - The user-agent string of the requester.
180
- * @param {Function } params.requester.serialize - A function to serialize the requester information.
181
- * @param {string } params.action - The action performed during the authentication event.
182
- * @param {Object } params.body - The body of the request.
183
- * @param {Object } params.extra - Additional information related to the event.
184
- * @returns {Promise<void> } - A promise that resolves when the event is recorded.
185
- */
186
- async _record ( { requester, action, body, extra } ) {
187
- const uid = 'aas-' + this . modules . uuidv4 ( ) ;
188
-
189
- const json_values = {
190
- requester : requester . serialize ( ) ,
191
- body : body ,
192
- extra : extra ?? { } ,
193
- } ;
194
-
195
- let has_parse_error = 0 ;
196
-
197
- for ( const k in json_values ) {
198
- let value = json_values [ k ] ;
199
- try {
200
- value = JSON . stringify ( value ) ;
201
- } catch ( err ) {
202
- has_parse_error = 1 ;
203
- value = { parse_error : err . message } ;
204
- }
205
- json_values [ k ] = value ;
206
- }
207
-
208
- await this . db . write (
209
- `INSERT INTO auth_audit (` +
210
- `uid, ip_address, ua_string, action, ` +
211
- `requester, body, extra, ` +
212
- `has_parse_error` +
213
- `) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )` ,
214
- [
215
- uid ,
216
- requester . ip ,
217
- requester . ua ,
218
- action ,
219
- JSON . stringify ( requester . serialize ( ) ) ,
220
- JSON . stringify ( body ) ,
221
- JSON . stringify ( extra ?? { } ) ,
222
- has_parse_error ,
223
- ]
224
- ) ;
225
- }
226
- }
227
-
228
- module . exports = {
229
- AuthAuditService,
230
- } ;
231
91
async _record ( { requester, action, body, extra } ) {
232
92
const uid = 'aas-' + this . modules . uuidv4 ( ) ;
233
93
0 commit comments