From 647f33e5ed2888dcc57b1168bb70f29410727120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dhiego=20Cassiano=20Foga=C3=A7a=20Barbosa?= Date: Tue, 20 Feb 2024 00:22:01 -0300 Subject: [PATCH] Update dependencies --- package-lock.json | 90 +++++++++++----------- src/app/handler/AuthHandler.ts | 14 ++-- src/app/middleware/OauthScopeMiddleware.ts | 9 ++- src/app/providers/GamesServiceProvider.ts | 2 +- src/app/providers/OpenIDServiceProvider.ts | 2 +- src/app/services/PrismaUserService.ts | 7 +- src/config.ts | 1 - src/core/dao/UserDAO.ts | 51 ------------ src/core/entity/User.ts | 17 ---- src/providers.ts | 2 +- src/server.ts | 4 +- tsconfig.json | 7 ++ 12 files changed, 73 insertions(+), 133 deletions(-) delete mode 100644 src/core/dao/UserDAO.ts delete mode 100644 src/core/entity/User.ts diff --git a/package-lock.json b/package-lock.json index 78d8007..d6073a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,9 +50,9 @@ } }, "node_modules/@jridgewell/resolve-uri": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz", - "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", "dev": true, "engines": { "node": ">=6.0.0" @@ -130,9 +130,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "18.19.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.3.tgz", - "integrity": "sha512-k5fggr14DwAytoA/t8rPrIz++lXK7/DqckthCmoZOKNsEbJkId4Z//BqgApXBUGrGddrigYa1oqheo/7YmW4rg==", + "version": "18.19.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.17.tgz", + "integrity": "sha512-SzyGKgwPzuWp2SHhlpXKzCX0pIOfcI4V2eF37nNBJOhwlegQ83omtVQ1XxZpDE06V/d6AQvfQdPfnw0tRC//Ng==", "dev": true, "dependencies": { "undici-types": "~5.26.4" @@ -154,9 +154,9 @@ "dev": true }, "node_modules/acorn": { - "version": "8.11.2", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.2.tgz", - "integrity": "sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==", + "version": "8.11.3", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", + "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", "dev": true, "bin": { "acorn": "bin/acorn" @@ -166,9 +166,9 @@ } }, "node_modules/acorn-walk": { - "version": "8.3.1", - "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.1.tgz", - "integrity": "sha512-TgUZgYvqZprrl7YldZNoa9OciCAyZR+Ejm9eXzKCmjsF5IKp/wgQ7Z/ZpjpGTIUPwrHQIcYeI8qDh4PsEwxMbw==", + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz", + "integrity": "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==", "dev": true, "engines": { "node": ">=0.4.0" @@ -305,16 +305,10 @@ } }, "node_modules/chokidar": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", - "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", "dev": true, - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], "dependencies": { "anymatch": "~3.1.2", "braces": "~3.0.2", @@ -327,6 +321,9 @@ "engines": { "node": ">= 8.10.0" }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, "optionalDependencies": { "fsevents": "~2.3.2" } @@ -380,14 +377,14 @@ } }, "node_modules/dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "version": "16.4.4", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.4.tgz", + "integrity": "sha512-XvPXc8XAQThSjAbY6cQ/9PcBXmFoWuw1sQ3b8HqUCR6ziGXjkTi//kB9SWa2UwqlgdAIuRqAa/9hVljzPehbYg==", "engines": { "node": ">=12" }, "funding": { - "url": "https://github.com/motdotla/dotenv?sponsor=1" + "url": "https://dotenvx.com" } }, "node_modules/fill-range": { @@ -461,9 +458,9 @@ } }, "node_modules/hasown": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", - "integrity": "sha512-vUptKVTpIJhcczKBbgnS+RtcuYMB8+oNzPK2/Hp3hanz8JmpATdmmgLgSaadVREkDm+e2giHwY3ZRkyjSIDDFA==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", + "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", "dev": true, "dependencies": { "function-bind": "^1.1.2" @@ -585,15 +582,18 @@ }, "node_modules/midori": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/modscleo4/midori.git#51da7495b4381a87ce260c9c06e593491446d762", + "resolved": "git+ssh://git@github.com/modscleo4/midori.git#1c8adac10e19cd5da8a4e27587040f63730eb22e", "license": "Apache-2.0", "dependencies": { "mime-types": "^2.1.35" + }, + "engines": { + "node": ">=16" } }, "node_modules/midori-swaggerui": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/modscleo4/midori-swaggerui.git#c4f7895bef8e8c12e583e329d61cc5ff61142afb", + "resolved": "git+ssh://git@github.com/modscleo4/midori-swaggerui.git#3416f54dfa65c38894a3bbe7c285c8fc7c27d0a5", "license": "Apache-2.0", "dependencies": { "midori": "github:modscleo4/midori" @@ -646,9 +646,9 @@ "dev": true }, "node_modules/node-gyp-build": { - "version": "4.7.1", - "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.7.1.tgz", - "integrity": "sha512-wTSrZ+8lsRRa3I3H8Xr65dLWSgCvY2l4AOnaeKdPA9TB/WYMPaTcrzf3rXvFoVvjKNVnu0CcWSx54qq9GKRUYg==", + "version": "4.8.0", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.0.tgz", + "integrity": "sha512-u6fs2AEUljNho3EYTJNBfImO5QTo/J/1Etd+NVdCj7qWKUSN/bSLkZwhDv7I+w/MSC6qJ4cknepkAYykDdK8og==", "optional": true, "bin": { "node-gyp-build": "bin.js", @@ -930,9 +930,9 @@ } }, "node_modules/ts-patch": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/ts-patch/-/ts-patch-3.1.1.tgz", - "integrity": "sha512-ReGYz9jQYC80PFafBx25TC0UI9cSgmUBtpT+WIy8IrhpLVzEHf430k03XQYOMldQMyZDBbzn5fBPELgtIl65cA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/ts-patch/-/ts-patch-3.1.2.tgz", + "integrity": "sha512-n58F5AqjUMdp9RAKq+E1YBkmONltPVbt1nN+wrmZXoYZek6QcvaTuqvKMhYhr5BxtC53kD/exxIPA1cP1RQxsA==", "dev": true, "dependencies": { "chalk": "^4.1.2", @@ -948,9 +948,9 @@ } }, "node_modules/ts-patch/node_modules/semver": { - "version": "7.5.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", - "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", + "version": "7.6.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", + "integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -990,9 +990,9 @@ } }, "node_modules/typescript-transform-paths": { - "version": "3.4.6", - "resolved": "https://registry.npmjs.org/typescript-transform-paths/-/typescript-transform-paths-3.4.6.tgz", - "integrity": "sha512-qdgpCk9oRHkIBhznxaHAapCFapJt5e4FbFik7Y4qdqtp6VyC3smAIPoDEIkjZ2eiF7x5+QxUPYNwJAtw0thsTw==", + "version": "3.4.7", + "resolved": "https://registry.npmjs.org/typescript-transform-paths/-/typescript-transform-paths-3.4.7.tgz", + "integrity": "sha512-1Us1kdkdfKd2unbkBAOV2HHRmbRBYpSuk9nJ7cLD2hP4QmfToiM/VpxNlhJc1eezVwVqSKSBjNSzZsK/fWR/9A==", "dev": true, "dependencies": { "minimatch": "^3.0.4" @@ -1045,9 +1045,9 @@ } }, "node_modules/ws": { - "version": "8.15.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.15.0.tgz", - "integrity": "sha512-H/Z3H55mrcrgjFwI+5jKavgXvwQLtfPCUEp6pi35VhoB0pfcHnSoyuTzkBEZpzq49g1193CUEwIvmsjcotenYw==", + "version": "8.16.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", "engines": { "node": ">=10.0.0" }, diff --git a/src/app/handler/AuthHandler.ts b/src/app/handler/AuthHandler.ts index 16d7505..e893359 100644 --- a/src/app/handler/AuthHandler.ts +++ b/src/app/handler/AuthHandler.ts @@ -22,7 +22,7 @@ import { AuthServiceProvider, JWTServiceProvider } from "midori/providers"; import { Payload } from "midori/util/jwt.js"; import { generateUUID } from "midori/util/uuid.js"; -import UserDAO from "@core/dao/UserDAO.js"; +import { prisma } from "@core/lib/Prisma.js"; import { OpenIDServiceProvider } from "@app/providers/OpenIDServiceProvider.js"; import OpenIDService from "@app/services/OpenIDService.js"; @@ -95,11 +95,13 @@ export class Callback extends Handler { const OauthUser = await identity_response.json(); - const user = await UserDAO.create({ - id: generateUUID(), - username: OauthUser.preferred_username, - password: null, - email: OauthUser.email, + const user = await prisma.user.create({ + data: { + id: generateUUID(), + username: OauthUser.preferred_username, + password: null, + email: OauthUser.email, + } }); const issuedAt = Date.now(); diff --git a/src/app/middleware/OauthScopeMiddleware.ts b/src/app/middleware/OauthScopeMiddleware.ts index cbcd55a..04efae5 100644 --- a/src/app/middleware/OauthScopeMiddleware.ts +++ b/src/app/middleware/OauthScopeMiddleware.ts @@ -16,13 +16,16 @@ import { HTTPError } from "midori/errors"; import { EStatusCode, Middleware, Request, Response } from "midori/http"; +import { AuthBearerMiddleware } from "midori/middlewares"; +import { Payload } from "midori/util/jwt.js"; import { Constructor } from "midori/util/types.js"; export default function OauthScopeMiddlewareFactory(options: { scopes: string[]; }): Constructor { return class extends Middleware { - async process(req: Request, next: (req: Request) => Promise): Promise { - if (req.container.get('::jwt')) { - const userScopes = (req.container.get('::jwt').scope ?? '').split(' '); + override async process(req: Request, next: (req: Request) => Promise): Promise { + const jwt = req.container.get(AuthBearerMiddleware.TokenKey) as (Payload & { username: string; scope: string; }) | undefined; + if (jwt) { + const userScopes = (jwt.scope ?? '').split(' '); for (const scope of options.scopes) { if (!userScopes.includes(scope)) { throw new HTTPError(`Insufficient permissions: ${scope}`, EStatusCode.FORBIDDEN); diff --git a/src/app/providers/GamesServiceProvider.ts b/src/app/providers/GamesServiceProvider.ts index 264ba83..7d1da0f 100644 --- a/src/app/providers/GamesServiceProvider.ts +++ b/src/app/providers/GamesServiceProvider.ts @@ -19,7 +19,7 @@ import { ServiceProvider, Application } from 'midori/app'; import GamesService from '@app/services/GamesService.js'; export default class GamesServiceProvider extends ServiceProvider { - static service: string = 'ChessGames'; + static override service: symbol = Symbol('ChessGames'); register(app: Application): GamesService { return new GamesService(); diff --git a/src/app/providers/OpenIDServiceProvider.ts b/src/app/providers/OpenIDServiceProvider.ts index 0969725..b7732ea 100644 --- a/src/app/providers/OpenIDServiceProvider.ts +++ b/src/app/providers/OpenIDServiceProvider.ts @@ -20,7 +20,7 @@ import { Constructor } from "midori/util/types.js"; import OpenIDService from "@app/services/OpenIDService.js"; export abstract class OpenIDServiceProvider extends ServiceProvider { - static service: string = 'OpenID'; + static override service: symbol = Symbol('OpenID'); } export default function (openIDService: OpenIDService): Constructor & { [K in keyof typeof OpenIDServiceProvider]: typeof OpenIDServiceProvider[K] } { diff --git a/src/app/services/PrismaUserService.ts b/src/app/services/PrismaUserService.ts index 48a0d11..17069c5 100644 --- a/src/app/services/PrismaUserService.ts +++ b/src/app/services/PrismaUserService.ts @@ -14,11 +14,10 @@ * limitations under the License. */ +import { prisma } from "@core/lib/Prisma.js"; import { User, UserService } from "midori/auth"; import { Hash } from "midori/hash"; -import UserDAO from "@core/dao/UserDAO.js"; - export default class PrismaUserService extends UserService { #hash: Hash; @@ -29,11 +28,11 @@ export default class PrismaUserService extends UserService { } async getUserById(id: string): Promise { - return await UserDAO.get({ select: { id: true, username: true }, where: { id } }); + return await prisma.user.findFirst({ select: { id: true, username: true }, where: { id } }); } async getUserByCredentials(username: string, password: string): Promise { - const user = await UserDAO.get({ select: { id: true, username: true, password: true }, where: { username } }); + const user = await prisma.user.findFirst({ select: { id: true, username: true, password: true }, where: { username } }); if (!user || !user.password) { return null; diff --git a/src/config.ts b/src/config.ts index 798a79f..0ef6545 100644 --- a/src/config.ts +++ b/src/config.ts @@ -59,7 +59,6 @@ export default function config(server: Server): void { enc: process.env.JWE_ENCRYPTION || 'A256GCM', secret: process.env.JWE_SECRET, privateKeyFile: process.env.JWE_PRIVATE_KEY, - ephemeralPrivateKeyFile: process.env.JWE_EPHEMERAL_KEY, }, })); diff --git a/src/core/dao/UserDAO.ts b/src/core/dao/UserDAO.ts deleted file mode 100644 index f294a20..0000000 --- a/src/core/dao/UserDAO.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2022 Dhiego Cassiano Fogaça Barbosa - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { PrismaDTO, prisma } from "@core/lib/Prisma.js"; -import { User } from "@core/entity/User.js"; - -export default class UserDAO { - static async all(args?: PrismaDTO.UserFindManyArgs): Promise { - return await prisma.user.findMany(args); - } - - static async create(data: PrismaDTO.UserCreateInput): Promise { - return await prisma.user.create({ - data - }); - } - - static async get(args: PrismaDTO.UserFindFirstArgs): Promise { - return await prisma.user.findFirst(args); - } - - static async save(id: string, data: PrismaDTO.UserUpdateInput): Promise { - return await prisma.user.update({ - where: { - id - }, - data - }); - } - - static async delete(id: string): Promise { - return await prisma.user.delete({ - where: { - id - } - }); - } -} diff --git a/src/core/entity/User.ts b/src/core/entity/User.ts deleted file mode 100644 index cde52b6..0000000 --- a/src/core/entity/User.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2022 Dhiego Cassiano Fogaça Barbosa - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export { User } from "@prisma/client"; diff --git a/src/providers.ts b/src/providers.ts index 0871bd7..72fb6fd 100644 --- a/src/providers.ts +++ b/src/providers.ts @@ -43,7 +43,7 @@ import OpenIDService from "@app/services/OpenIDService.js"; export default function providers(server: Server): void { server.install(RouterServiceProviderFactory(router)); - server.install(LoggerServiceProviderFactory(new ConsoleLogger({ colorsEnabled: true, minLevel: LogLevel.DEBUG }))); + server.install(LoggerServiceProviderFactory(new ConsoleLogger({ formattingEnabled: true, minLevel: LogLevel.DEBUG }))); // Add providers here // Recover the provider with app.services.get(ServiceProvider) in your handlers and middleware constructors diff --git a/src/server.ts b/src/server.ts index b4770a9..2827039 100644 --- a/src/server.ts +++ b/src/server.ts @@ -38,14 +38,12 @@ config(server); providers(server); pipeline(server); cron(server); -ws(server); const port = parseInt(process.env.PORT || '3000'); await new Promise((resolve, reject) => { server.listen(port).on('listening', async () => { - console.log(`Server is running on port ${port}`); - + console.log(`Server is running on port ${port} in ${server.production ? 'production' : 'development'} mode`); await prisma.$connect(); resolve(); }).on('close', async () => { diff --git a/tsconfig.json b/tsconfig.json index 4774984..5045719 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,13 @@ "experimentalDecorators": true, "esModuleInterop": true, "baseUrl": ".", + "skipLibCheck": true, + "noImplicitReturns": true, + "noImplicitAny": true, + "noImplicitOverride": true, + "noImplicitThis": true, + "strictBindCallApply": true, + "strictNullChecks": true, "paths": { "@app/*": [ "./src/app/*"