From 63725941d6c6c0dc0db3635ea35f32b2f861db1a Mon Sep 17 00:00:00 2001 From: Romain Lanz Date: Tue, 11 Nov 2025 22:42:01 +0100 Subject: [PATCH] refactor(encryption): migrate to @boringnode/encryption --- benchmarks/adonisjs.js | 9 ++++-- factories/request.ts | 8 ++--- factories/response.ts | 8 ++--- factories/router.ts | 8 ++--- factories/server_factory.ts | 8 ++--- factories/url_builder_factory.ts | 8 ++--- package.json | 1 + src/cookies/client.ts | 6 ++-- src/cookies/drivers/encrypted.ts | 10 +++++-- src/cookies/drivers/signed.ts | 15 ++++++---- src/cookies/parser.ts | 4 +-- src/cookies/serializer.ts | 4 +-- src/helpers.ts | 6 ++-- src/request.ts | 8 ++--- src/response.ts | 4 +-- src/router/main.ts | 6 ++-- src/router/signed_url_builder.ts | 4 +-- src/server/main.ts | 6 ++-- tests/cookies/client.spec.ts | 4 +-- tests/cookies/drivers/encrypted.spec.ts | 4 +-- tests/cookies/drivers/signed.spec.ts | 4 +-- tests/cookies/parser.spec.ts | 4 +-- tests/cookies/serializer.spec.ts | 4 +-- tests/redirect.spec.ts | 4 +-- tests/request.spec.ts | 4 +-- tests/response.spec.ts | 4 +-- tests/router/legacy_url_builder.spec.ts | 40 ++++++++++++------------- tests/router/router.spec.ts | 26 ++++++++-------- tests/router/url_builder.spec.ts | 14 ++++----- 29 files changed, 125 insertions(+), 110 deletions(-) diff --git a/benchmarks/adonisjs.js b/benchmarks/adonisjs.js index ffe134d..06833d9 100644 --- a/benchmarks/adonisjs.js +++ b/benchmarks/adonisjs.js @@ -10,7 +10,7 @@ import { createServer } from 'node:http' import { Logger } from '@adonisjs/logger' import { Emitter } from '@adonisjs/events' -import { Encryption } from '@adonisjs/encryption' +import { EncryptionManager } from '@boringnode/encryption' import { Application } from '@adonisjs/application' import { defineConfig, Server } from '../build/index.js' @@ -21,7 +21,12 @@ const app = new Application(new URL('./', import.meta.url), { }) await app.init() -const encryption = new Encryption({ secret: 'averylongrandom32charslongsecret' }) +const encryption = new EncryptionManager({ + default: 'nova', + list: { + nova: { keys: ['averylongrandom32charslongsecret'] }, + }, +}) const server = new Server( app, diff --git a/factories/request.ts b/factories/request.ts index 7458252..a75779f 100644 --- a/factories/request.ts +++ b/factories/request.ts @@ -10,9 +10,9 @@ import { Socket } from 'node:net' import proxyAddr from 'proxy-addr' import { randomUUID } from 'node:crypto' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { IncomingMessage, ServerResponse } from 'node:http' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { Request } from '../src/request.ts' import { QsParserFactory } from './qs_parser_factory.ts' @@ -23,7 +23,7 @@ type FactoryParameters = { method: string req: IncomingMessage res: ServerResponse - encryption: Encryption + encryption: EncryptionManager config: Partial } @@ -82,7 +82,7 @@ export class RequestFactory { * signed URLs */ #createEncryption() { - return this.#parameters.encryption || new EncryptionFactory().create() + return this.#parameters.encryption || new EncryptionManagerFactory().create() } /** diff --git a/factories/response.ts b/factories/response.ts index 6179119..909e80c 100644 --- a/factories/response.ts +++ b/factories/response.ts @@ -8,9 +8,9 @@ */ import { Socket } from 'node:net' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { IncomingMessage, ServerResponse } from 'node:http' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from './router.ts' import { Response } from '../src/response.ts' @@ -21,7 +21,7 @@ import { type ResponseConfig } from '../src/types/response.ts' type FactoryParameters = { req: IncomingMessage res: ServerResponse - encryption: Encryption + encryption: EncryptionManager config: Partial router: Router } @@ -81,7 +81,7 @@ export class ResponseFactory { * signed URLs */ #createEncryption() { - return this.#parameters.encryption || new EncryptionFactory().create() + return this.#parameters.encryption || new EncryptionManagerFactory().create() } /** diff --git a/factories/router.ts b/factories/router.ts index 84aebc5..465822e 100644 --- a/factories/router.ts +++ b/factories/router.ts @@ -7,17 +7,17 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import type { Application } from '@adonisjs/application' import { AppFactory } from '@adonisjs/application/factories' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { Router } from '../src/router/main.ts' import { QsParserFactory } from './qs_parser_factory.ts' type FactoryParameters = { app: Application - encryption: Encryption + encryption: EncryptionManager } /** @@ -42,7 +42,7 @@ export class RouterFactory { * signed URLs */ #createEncryption() { - return this.#parameters.encryption || new EncryptionFactory().create() + return this.#parameters.encryption || new EncryptionManagerFactory().create() } /** diff --git a/factories/server_factory.ts b/factories/server_factory.ts index 5274ea7..603b73a 100644 --- a/factories/server_factory.ts +++ b/factories/server_factory.ts @@ -9,10 +9,10 @@ import { Logger } from '@adonisjs/logger' import { Emitter } from '@adonisjs/events' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import type { Application } from '@adonisjs/application' import { AppFactory } from '@adonisjs/application/factories' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { Server } from '../src/server/main.ts' import { defineConfig } from '../src/define_config.ts' @@ -21,7 +21,7 @@ import type { ServerConfig } from '../src/types/server.ts' type FactoryParameters = { app: Application logger: Logger - encryption: Encryption + encryption: EncryptionManager emitter: Emitter config: Partial } @@ -69,7 +69,7 @@ export class ServerFactory { * signed URLs */ #createEncryption() { - return this.#parameters.encryption || new EncryptionFactory().create() + return this.#parameters.encryption || new EncryptionManagerFactory().create() } /** diff --git a/factories/url_builder_factory.ts b/factories/url_builder_factory.ts index 2ace2e6..118523a 100644 --- a/factories/url_builder_factory.ts +++ b/factories/url_builder_factory.ts @@ -7,8 +7,8 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import type { EncryptionManager } from '@boringnode/encryption' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from './router.ts' import type { Router } from '../src/router/main.ts' @@ -18,7 +18,7 @@ import { createSignedUrlBuilder } from '../src/router/signed_url_builder.ts' type FactoryParameters = { router: Router - encryption: Encryption + encryption: EncryptionManager } /** @@ -42,7 +42,7 @@ export class URLBuilderFactory { * signed URLs */ #createEncryption() { - return this.#parameters.encryption || new EncryptionFactory().create() + return this.#parameters.encryption || new EncryptionManagerFactory().create() } /** diff --git a/package.json b/package.json index 9f5fc18..6218271 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@adonisjs/logger": "^7.1.0-next.0", "@adonisjs/prettier-config": "^1.4.5", "@adonisjs/tsconfig": "^2.0.0-next.0", + "@boringnode/encryption": "^0.1.0", "@fastify/middie": "^9.0.3", "@japa/assert": "^4.1.1", "@japa/expect-type": "^2.0.3", diff --git a/src/cookies/client.ts b/src/cookies/client.ts index ff10b25..8b93133 100644 --- a/src/cookies/client.ts +++ b/src/cookies/client.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import * as plainCookiesDriver from './drivers/plain.ts' import * as signedCookiesDriver from './drivers/signed.ts' @@ -34,14 +34,14 @@ export class CookieClient { /** * Private encryption instance used for signing and encrypting cookies */ - #encryption: Encryption + #encryption: EncryptionManager /** * Create a new instance of CookieClient * * @param encryption - The encryption instance for cookie operations */ - constructor(encryption: Encryption) { + constructor(encryption: EncryptionManager) { this.#encryption = encryption } diff --git a/src/cookies/drivers/encrypted.ts b/src/cookies/drivers/encrypted.ts index 60d00e5..b27ec2a 100644 --- a/src/cookies/drivers/encrypted.ts +++ b/src/cookies/drivers/encrypted.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' /** * Encrypt a value to be set as cookie @@ -17,7 +17,7 @@ import type { Encryption } from '@adonisjs/encryption' * @param encryption - The encryption instance * @returns The encrypted cookie string or null if value is null/undefined */ -export function pack(key: string, value: any, encryption: Encryption): null | string { +export function pack(key: string, value: any, encryption: EncryptionManager): null | string { if (value === undefined || value === null) { return null } @@ -45,7 +45,11 @@ export function canUnpack(encryptedValue: string) { * @param encryption - The encryption instance * @returns The decrypted value or null if decryption fails */ -export function unpack(key: string, encryptedValue: string, encryption: Encryption): null | any { +export function unpack( + key: string, + encryptedValue: string, + encryption: EncryptionManager +): null | any { const value = encryptedValue.slice(2) if (!value) { return null diff --git a/src/cookies/drivers/signed.ts b/src/cookies/drivers/signed.ts index 34f26d2..97669d3 100644 --- a/src/cookies/drivers/signed.ts +++ b/src/cookies/drivers/signed.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' /** * Signs a value to be shared as a cookie. The signed output has a @@ -18,11 +18,12 @@ import type { Encryption } from '@adonisjs/encryption' * @param encryption - The encryption instance * @returns The signed cookie string or null if value is null/undefined */ -export function pack(key: string, value: any, encryption: Encryption): null | string { +export function pack(key: string, value: any, encryption: EncryptionManager): null | string { if (value === undefined || value === null) { return null } - return `s:${encryption.verifier.sign(value, undefined, key)}` + console.log(encryption) + return `s:${encryption.getMessageVerifier().sign(value, undefined, key)}` } /** @@ -45,11 +46,15 @@ export function canUnpack(signedValue: string) { * @param encryption - The encryption instance * @returns The original value or null if verification fails */ -export function unpack(key: string, signedValue: string, encryption: Encryption): null | any { +export function unpack( + key: string, + signedValue: string, + encryption: EncryptionManager +): null | any { const value = signedValue.slice(2) if (!value) { return null } - return encryption.verifier.unsign(value, key) + return encryption.getMessageVerifier().unsign(value, key) } diff --git a/src/cookies/parser.ts b/src/cookies/parser.ts index 8650703..60c4371 100644 --- a/src/cookies/parser.ts +++ b/src/cookies/parser.ts @@ -8,7 +8,7 @@ */ import cookie from 'cookie' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { CookieClient } from './client.ts' @@ -54,7 +54,7 @@ export class CookieParser { * @param cookieHeader - The raw cookie header string from the request * @param encryption - The encryption instance for cookie operations */ - constructor(cookieHeader: string, encryption: Encryption) { + constructor(cookieHeader: string, encryption: EncryptionManager) { this.#client = new CookieClient(encryption) this.#cookies = this.#parse(cookieHeader) } diff --git a/src/cookies/serializer.ts b/src/cookies/serializer.ts index d8b1d39..73093be 100644 --- a/src/cookies/serializer.ts +++ b/src/cookies/serializer.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { CookieClient } from './client.ts' import { serializeCookie } from '../helpers.ts' @@ -29,7 +29,7 @@ export class CookieSerializer { * * @param encryption - The encryption instance for cookie operations */ - constructor(encryption: Encryption) { + constructor(encryption: EncryptionManager) { this.#client = new CookieClient(encryption) } diff --git a/src/helpers.ts b/src/helpers.ts index 1e9c94f..a0b0515 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -11,7 +11,7 @@ import cookie from 'cookie' // @ts-expect-error import matchit from '@poppinss/matchit' import string from '@poppinss/utils/string' -import { type Encryption } from '@adonisjs/encryption' +import { type EncryptionManager } from '@boringnode/encryption' import { parseBindingReference } from '@adonisjs/fold' import { type Qs } from './qs.ts' @@ -91,7 +91,7 @@ export function createSignedURL( identifier: string, tokens: MatchItRouteToken[], searchParamsStringifier: (qs: Record) => string, - encryption: Encryption, + encryption: EncryptionManager, params?: any[] | { [param: string]: any }, options?: SignedURLOptions ): string { @@ -103,7 +103,7 @@ export function createSignedURL( * on their 2 different domains, but we ignore that case for now and can consider * it later (when someone asks for it) */ - const signature = encryption.verifier.sign( + const signature = encryption.getMessageVerifier().sign( createURL(identifier, tokens, searchParamsStringifier, params, { ...options, prefixUrl: undefined, diff --git a/src/request.ts b/src/request.ts index 3e89801..1f621c2 100644 --- a/src/request.ts +++ b/src/request.ts @@ -16,7 +16,7 @@ import proxyaddr from 'proxy-addr' import { safeEqual } from '@poppinss/utils' import Macroable from '@poppinss/macroable' import lodash from '@poppinss/utils/lodash' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { type ServerResponse, type IncomingMessage, type IncomingHttpHeaders } from 'node:http' import type { Qs } from './qs.ts' @@ -44,7 +44,7 @@ export class Request extends Macroable { * Encryption module to verify signed URLs and unsign/decrypt * cookies */ - #encryption: Encryption + #encryption: EncryptionManager /** * Request config @@ -118,7 +118,7 @@ export class Request extends Macroable { public request: IncomingMessage, /** Native Node.js server response instance */ public response: ServerResponse, - encryption: Encryption, + encryption: EncryptionManager, config: RequestConfig, qsParser: Qs ) { @@ -1043,7 +1043,7 @@ export class Request extends Macroable { /* * Return false when signature fails */ - const signedUrl = this.#encryption.verifier.unsign(signature, purpose) + const signedUrl = this.#encryption.getMessageVerifier().unsign(signature, purpose) if (!signedUrl) { return false } diff --git a/src/response.ts b/src/response.ts index 949f027..82f1b53 100644 --- a/src/response.ts +++ b/src/response.ts @@ -19,7 +19,7 @@ import Macroable from '@poppinss/macroable' import { createReadStream } from 'node:fs' import contentDisposition from 'content-disposition' import { safeStringify } from '@poppinss/utils/json' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { RuntimeException } from '@poppinss/utils/exception' import { type ServerResponse, type IncomingMessage, type OutgoingHttpHeaders } from 'node:http' @@ -167,7 +167,7 @@ export class Response extends Macroable { constructor( public request: IncomingMessage, public response: ServerResponse, - encryption: Encryption, + encryption: EncryptionManager, config: ResponseConfig, router: Router, qs: Qs diff --git a/src/router/main.ts b/src/router/main.ts index 893949d..3befcb7 100644 --- a/src/router/main.ts +++ b/src/router/main.ts @@ -9,7 +9,7 @@ import is from '@sindresorhus/is' import { moduleImporter } from '@adonisjs/fold' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import type { Application } from '@adonisjs/application' import { RuntimeException } from '@poppinss/utils/exception' import type { Constructor, LazyImport } from '@poppinss/utils/types' @@ -79,7 +79,7 @@ export class Router { /** * Encryption for making signed URLs */ - #encryption: Encryption + #encryption: EncryptionManager /** * Global matchers to test route params against regular expressions. @@ -151,7 +151,7 @@ export class Router { * @param encryption - Encryption service for signed URLs * @param qsParser - Query string parser for URL generation */ - constructor(app: Application, encryption: Encryption, qsParser: Qs) { + constructor(app: Application, encryption: EncryptionManager, qsParser: Qs) { this.#app = app this.#encryption = encryption this.qs = qsParser diff --git a/src/router/signed_url_builder.ts b/src/router/signed_url_builder.ts index 4992326..e6701bd 100644 --- a/src/router/signed_url_builder.ts +++ b/src/router/signed_url_builder.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import { type Router } from './main.ts' import { createSignedURL } from '../helpers.ts' @@ -22,7 +22,7 @@ import { type UrlFor, type LookupList, type SignedURLOptions } from '../types/ur */ export function createSignedUrlBuilder( router: Router, - encryption: Encryption, + encryption: EncryptionManager, searchParamsStringifier: (qs: Record) => string ): UrlFor { let domainsList: string[] diff --git a/src/server/main.ts b/src/server/main.ts index cc967b2..0b26e86 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -11,7 +11,7 @@ import onFinished from 'on-finished' import Middleware from '@poppinss/middleware' import type { Logger } from '@adonisjs/logger' import type { LazyImport } from '@poppinss/utils/types' -import type { Encryption } from '@adonisjs/encryption' +import type { EncryptionManager } from '@boringnode/encryption' import type { Server as HttpsServer } from 'node:https' import type { Application } from '@adonisjs/application' import type { EmitterLike } from '@adonisjs/events/types' @@ -100,7 +100,7 @@ export class Server { /** * Encryption service for secure cookie handling and data encryption */ - #encryption: Encryption + #encryption: EncryptionManager /** * Server configuration settings including timeouts, middleware options, etc. @@ -173,7 +173,7 @@ export class Server { */ constructor( app: Application, - encryption: Encryption, + encryption: EncryptionManager, emitter: EmitterLike, logger: Logger, config: ServerConfig diff --git a/tests/cookies/client.spec.ts b/tests/cookies/client.spec.ts index a866b9c..487f2a7 100644 --- a/tests/cookies/client.spec.ts +++ b/tests/cookies/client.spec.ts @@ -8,11 +8,11 @@ */ import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { CookieClient } from '../../src/cookies/client.ts' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() test.group('Cookie Client', () => { test('sign cookie', async ({ assert }) => { diff --git a/tests/cookies/drivers/encrypted.spec.ts b/tests/cookies/drivers/encrypted.spec.ts index 525627c..8cd9680 100644 --- a/tests/cookies/drivers/encrypted.spec.ts +++ b/tests/cookies/drivers/encrypted.spec.ts @@ -17,10 +17,10 @@ */ import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { pack, unpack, canUnpack } from '../../../src/cookies/drivers/encrypted.ts' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() test.group('Cookie | driver | encrypted', () => { test('encrypt value', ({ assert }) => { diff --git a/tests/cookies/drivers/signed.spec.ts b/tests/cookies/drivers/signed.spec.ts index 2a8e615..0ffda7a 100644 --- a/tests/cookies/drivers/signed.spec.ts +++ b/tests/cookies/drivers/signed.spec.ts @@ -17,10 +17,10 @@ */ import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { pack, unpack, canUnpack } from '../../../src/cookies/drivers/signed.ts' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() test.group('Cookie | driver | signed', () => { test('sign value', ({ assert }) => { diff --git a/tests/cookies/parser.spec.ts b/tests/cookies/parser.spec.ts index 9d9685a..9998521 100644 --- a/tests/cookies/parser.spec.ts +++ b/tests/cookies/parser.spec.ts @@ -8,12 +8,12 @@ */ import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { CookieParser } from '../../src/cookies/parser.ts' import { CookieSerializer } from '../../src/cookies/serializer.ts' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() const serializer = new CookieSerializer(encryption) test.group('Cookie | parse', () => { diff --git a/tests/cookies/serializer.spec.ts b/tests/cookies/serializer.spec.ts index 0e85bea..9d663d1 100644 --- a/tests/cookies/serializer.spec.ts +++ b/tests/cookies/serializer.spec.ts @@ -10,12 +10,12 @@ import { test } from '@japa/runner' import { setTimeout } from 'node:timers/promises' import { MessageBuilder } from '@poppinss/utils' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { CookieSerializer } from '../../src/cookies/serializer.ts' import base64 from '@poppinss/utils/base64' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() test.group('Cookie | serialize', () => { test('serialize and sign cookie', ({ assert }) => { diff --git a/tests/redirect.spec.ts b/tests/redirect.spec.ts index b6c5bee..21383e7 100644 --- a/tests/redirect.spec.ts +++ b/tests/redirect.spec.ts @@ -10,7 +10,7 @@ import supertest from 'supertest' import { test } from '@japa/runner' import { AppFactory } from '@adonisjs/application/factories' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from '../factories/router.ts' import { httpServer } from '../factories/http_server.ts' @@ -19,7 +19,7 @@ import { ResponseFactory } from '../factories/response.ts' const BASE_URL = new URL('./app/', import.meta.url) const app = new AppFactory().create(BASE_URL, () => {}) -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ app, encryption }).create() test.group('Redirect', () => { diff --git a/tests/request.spec.ts b/tests/request.spec.ts index eb27ea5..ef3993e 100644 --- a/tests/request.spec.ts +++ b/tests/request.spec.ts @@ -13,7 +13,7 @@ import proxyAddr from 'proxy-addr' import { test } from '@japa/runner' import Middleware from '@poppinss/middleware' import { createServer as httpsServer } from 'node:https' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from '../factories/router.ts' import { RequestFactory } from '../factories/request.ts' @@ -21,7 +21,7 @@ import { httpServer } from '../factories/http_server.ts' import { CookieSerializer } from '../src/cookies/serializer.ts' import { HttpContextFactory } from '../factories/http_context.ts' -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() const serializer = new CookieSerializer(encryption) test.group('Request', () => { diff --git a/tests/response.spec.ts b/tests/response.spec.ts index a0f659e..64d5ea4 100644 --- a/tests/response.spec.ts +++ b/tests/response.spec.ts @@ -18,7 +18,7 @@ import { Readable } from 'node:stream' import { fileURLToPath } from 'node:url' import { AppFactory } from '@adonisjs/application/factories' import { createWriteStream, createReadStream } from 'node:fs' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { type Response } from '../src/response.ts' import { RouterFactory } from '../factories/router.ts' @@ -29,7 +29,7 @@ import { ResponseFactory } from '../factories/response.ts' const BASE_URL = new URL('./app/', import.meta.url) const BASE_PATH = fileURLToPath(BASE_URL) -const encryption = new EncryptionFactory().create() +const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new RouterFactory().merge({ app, encryption }).create() diff --git a/tests/router/legacy_url_builder.spec.ts b/tests/router/legacy_url_builder.spec.ts index 90ba7b4..4fa619a 100644 --- a/tests/router/legacy_url_builder.spec.ts +++ b/tests/router/legacy_url_builder.spec.ts @@ -9,7 +9,7 @@ import { test } from '@japa/runner' import { AppFactory } from '@adonisjs/application/factories' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { Router } from '../../src/router/main.ts' import { RequestFactory } from '../../factories/request.ts' @@ -20,7 +20,7 @@ const BASE_URL = new URL('./app/', import.meta.url) test.group('Legacy URL builder', () => { test('create url for a route', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.route('/users/:id', ['GET'], () => {}) @@ -30,7 +30,7 @@ test.group('Legacy URL builder', () => { test('create url for a route by its name', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.route('/users/:id', ['GET'], () => {}).as('users.show') @@ -40,7 +40,7 @@ test.group('Legacy URL builder', () => { test('create url for a route by its name for the home path', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.route('/', ['GET'], () => {}).as('home') @@ -50,7 +50,7 @@ test.group('Legacy URL builder', () => { test('create url for a route by the handler name', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.route('/users/:id', ['GET'], '#controllers/posts') @@ -60,7 +60,7 @@ test.group('Legacy URL builder', () => { test('create url for a route by the handler name for the home path', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.route('/', ['GET'], '#controllers/home') @@ -70,7 +70,7 @@ test.group('Legacy URL builder', () => { test('raise error when unable to lookup route', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) assert.throws(() => router.builder().make('/users/:id'), 'Cannot lookup route "/users/:id"') @@ -78,7 +78,7 @@ test.group('Legacy URL builder', () => { test('create url without performing route lookup', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) assert.equal(router.builder().params([1]).disableRouteLookup().make('/users/:id'), '/users/1') @@ -86,7 +86,7 @@ test.group('Legacy URL builder', () => { test('define params as an object', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) assert.equal( @@ -96,7 +96,7 @@ test.group('Legacy URL builder', () => { }) test('do not overwrite existing params when undefined params are shared', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -107,7 +107,7 @@ test.group('Legacy URL builder', () => { }) test('raise error when one or params are missing', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -118,7 +118,7 @@ test.group('Legacy URL builder', () => { }) test('allow missing params when param is optional', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -126,7 +126,7 @@ test.group('Legacy URL builder', () => { }) test('make route with wildcard params', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -137,7 +137,7 @@ test.group('Legacy URL builder', () => { }) test('define wildcard param as an object', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -154,7 +154,7 @@ test.group('Legacy URL builder', () => { }) test('raise error when wildcard params are missing', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -165,7 +165,7 @@ test.group('Legacy URL builder', () => { }) test('prefix url', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -176,7 +176,7 @@ test.group('Legacy URL builder', () => { }) test('define query string with arrays', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -194,7 +194,7 @@ test.group('Legacy URL builder', () => { }) test('create and verify signed URLs', async ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -211,7 +211,7 @@ test.group('Legacy URL builder', () => { }) test('create and verify signed URLs with query string', async ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const app = new AppFactory().create(BASE_URL, () => {}) const router = new Router(app, encryption, new QsParserFactory().create()) @@ -236,7 +236,7 @@ test.group('Legacy URL builder', () => { test('build route with params and extension', ({ assert }) => { const app = new AppFactory().create(BASE_URL, () => {}) - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new Router(app, encryption, new QsParserFactory().create()) router.get('/users/:slug.html', () => {}).as('users.show') diff --git a/tests/router/router.spec.ts b/tests/router/router.spec.ts index bd0c29f..04630d1 100644 --- a/tests/router/router.spec.ts +++ b/tests/router/router.spec.ts @@ -9,7 +9,7 @@ import { parse } from 'qs' import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from '../../factories/router.ts' @@ -877,7 +877,7 @@ test.group('Router | Make url', () => { test.group('Make signed url', () => { test('make signed url to a given route', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', async function handler() {}) @@ -885,11 +885,11 @@ test.group('Make signed url', () => { const url = router.makeSignedUrl('/posts/:id', { id: 1 })! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/posts/1') }) test("make signed url to a given route by it's name", ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', async function handler() {}).as('showPost') @@ -897,11 +897,11 @@ test.group('Make signed url', () => { const url = router.makeSignedUrl('showPost', { id: 1 })! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/posts/1') }) test("make signed url to a given route by it's controller method", ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', '#controllers/posts.index').as('showPost') @@ -909,11 +909,11 @@ test.group('Make signed url', () => { const url = router.makeSignedUrl('#controllers/posts.index', { id: 1 })! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/posts/1') }) test('make url for a specific domain', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', '#controllers/posts.index').as('showPost') @@ -932,11 +932,11 @@ test.group('Make signed url', () => { } )! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/articles/1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/articles/1') }) test('make signed url with expiry', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', 'PostsController.index') @@ -945,11 +945,11 @@ test.group('Make signed url', () => { const url = router.makeSignedUrl('PostsController.index', { id: 1, expiresIn: '1m' })! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/posts/1') }) test('make signed url with custom query string', ({ assert }) => { - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const router = new RouterFactory().merge({ encryption }).create() router.get('posts/:id', 'PostsController.index') @@ -960,7 +960,7 @@ test.group('Make signed url', () => { })! const qs = parse(url.split('?')[1]) - assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1?page=1') + assert.equal(encryption.getMessageVerifier().unsign(qs.signature as string), '/posts/1?page=1') assert.equal(Number(qs.page), 1) }) diff --git a/tests/router/url_builder.spec.ts b/tests/router/url_builder.spec.ts index 9ec4641..be9a5f8 100644 --- a/tests/router/url_builder.spec.ts +++ b/tests/router/url_builder.spec.ts @@ -8,7 +8,7 @@ */ import { test } from '@japa/runner' -import { EncryptionFactory } from '@adonisjs/encryption/factories' +import { EncryptionManagerFactory } from '@boringnode/encryption/factories' import { RouterFactory } from '../../factories/router.ts' import { RequestFactory } from '../../factories/request.ts' @@ -162,7 +162,7 @@ test.group('URLBuilder', () => { test('create and verify signed routes', async ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory<{ ALL: { 'users.show': { @@ -188,7 +188,7 @@ test.group('URLBuilder', () => { test('create and verify signed routes for specific methods', ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory<{ ALL: { @@ -257,7 +257,7 @@ test.group('URLBuilder', () => { test('create and verify signed URLs with query string', async ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory<{ ALL: { 'users.show': { @@ -387,7 +387,7 @@ test.group('URLBuilder', () => { test('create and verify signed URLs', async ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory().merge({ router, encryption }).create() router.route('/users', ['GET'], () => {}).as('users.index') @@ -405,7 +405,7 @@ test.group('URLBuilder', () => { test('create and verify signed URLs with query string', async ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory().merge({ router, encryption }).create() router.route('/users', ['GET'], () => {}).as('users.index') @@ -478,7 +478,7 @@ test.group('URLBuilder', () => { test('create signed url for a route from specific domain', ({ assert }) => { const router = new RouterFactory().create() - const encryption = new EncryptionFactory().create() + const encryption = new EncryptionManagerFactory().create() const { signedUrlFor } = new URLBuilderFactory<{ ALL: { 'posts.show': {