( headers: RawRequestDefaultExpression['headers'] )
| 246 | } |
| 247 | |
| 248 | export async function validateManageRequest( |
| 249 | headers: RawRequestDefaultExpression['headers'] |
| 250 | ): Promise<IServiceClientWithProject> { |
| 251 | const clientId = headers['openpanel-client-id'] as string; |
| 252 | const clientSecret = (headers['openpanel-client-secret'] as string) || ''; |
| 253 | |
| 254 | if ( |
| 255 | !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test( |
| 256 | clientId |
| 257 | ) |
| 258 | ) { |
| 259 | throw new Error('Manage: Client ID must be a valid UUIDv4'); |
| 260 | } |
| 261 | |
| 262 | const client = await getClientByIdCached(clientId); |
| 263 | |
| 264 | if (!client) { |
| 265 | throw new Error('Manage: Invalid client id'); |
| 266 | } |
| 267 | |
| 268 | if (!client.secret) { |
| 269 | throw new Error('Manage: Client has no secret'); |
| 270 | } |
| 271 | |
| 272 | if (client.type !== ClientType.root) { |
| 273 | throw new Error( |
| 274 | 'Manage: Only root clients are allowed to manage resources' |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | if (!(await verifyPassword(clientSecret, client.secret))) { |
| 279 | throw new Error('Manage: Invalid client secret'); |
| 280 | } |
| 281 | |
| 282 | return client; |
| 283 | } |
no test coverage detected