From d9c8809107ed3aa3ea763989e732b71b53d8032a Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Fri, 7 Jun 2024 16:46:50 -0400 Subject: [PATCH 1/7] feat: add vanilla adapter --- jsr.json | 3 ++- package.json | 8 +++++++- src/adapters/vanilla.ts | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/adapters/vanilla.ts diff --git a/jsr.json b/jsr.json index 57e7e9cd..2aef3ed9 100644 --- a/jsr.json +++ b/jsr.json @@ -1,8 +1,9 @@ { "name": "@jmondi/oauth2-server", - "version": "3.3.1", + "version": "3.4.0-next.0", "exports": { ".": "./src/index.ts", + "./vanilla": "./src/adapters/vanilla.ts", "./express": "./src/adapters/express.ts", "./fastify": "./src/adapters/fastify.ts" } diff --git a/package.json b/package.json index fbb46ecc..7ad642e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jmondi/oauth2-server", - "version": "3.3.1", + "version": "3.4.0-next.0", "type": "module", "author": "Jason Raimondi ", "funding": "https://github.com/sponsors/jasonraimondi", @@ -22,6 +22,7 @@ }, "exports": { ".": "./src/index.ts", + "./vanilla": "./src/adapters/vanilla.ts", "./express": "./src/adapters/express.ts", "./fastify": "./src/adapters/fastify.ts" }, @@ -35,6 +36,11 @@ "require": "./dist/index.cjs", "types": "./dist/index.d.ts" }, + "./vanilla": { + "import": "./dist/vanilla.js", + "require": "./dist/vanilla.cjs", + "types": "./dist/vanilla.d.ts" + }, "./express": { "import": "./dist/express.js", "require": "./dist/express.cjs", diff --git a/src/adapters/vanilla.ts b/src/adapters/vanilla.ts new file mode 100644 index 00000000..c1f03ec9 --- /dev/null +++ b/src/adapters/vanilla.ts @@ -0,0 +1,37 @@ +import { OAuthRequest } from "../requests/request.js"; +import { OAuthResponse } from "../responses/response.js"; + +export function responseFromVanilla(res: Response): OAuthResponse { + const headers: Record = {}; + Object.entries(res.headers).forEach(([key, value]) => { + headers[key] = value; + }); + + return new OAuthResponse({ + headers: headers, + }); +} + +export function requestFromVanilla(req: Request): OAuthRequest { + const url = new URL(req.url); + const query: Record = {}; + url.searchParams.forEach((value, key) => { + query[key] = value; + }); + + let body: Record = {}; + if (req.body != null && (req.method === "POST" || req.method === "PUT" || req.method === "PATCH")) { + body = JSON.parse(req.body.toString()); + } + + const headers: Record = {}; + Object.entries(req.headers).forEach(([key, value]) => { + headers[key] = value; + }); + + return new OAuthRequest({ + query: query, + body: body, + headers: headers, + }); +} From 10393418b7f4271b00e77e27c86ef3383194791c Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Fri, 7 Jun 2024 16:56:03 -0400 Subject: [PATCH 2/7] chore: fix vanilla build for export --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7ad642e0..c50a480f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jmondi/oauth2-server", - "version": "3.4.0-next.0", + "version": "3.4.0-next.1", "type": "module", "author": "Jason Raimondi ", "funding": "https://github.com/sponsors/jasonraimondi", @@ -98,6 +98,7 @@ "tsup": { "entry": { "index": "./src/index.ts", + "vanilla": "./src/adapters/vanilla.ts", "express": "./src/adapters/express.ts", "fastify": "./src/adapters/fastify.ts" }, From d9fdd3e6325d99bd2bccca389871584adfdaf260 Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Fri, 7 Jun 2024 16:58:19 -0400 Subject: [PATCH 3/7] chore: fix docs --- docs/getting_started/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/getting_started/index.md b/docs/getting_started/index.md index 0fb4751b..1569a194 100644 --- a/docs/getting_started/index.md +++ b/docs/getting_started/index.md @@ -71,7 +71,7 @@ import { requestFromExpress } from "@jmondi/oauth2-server/express"; app.get("/authorize", async (req: Express.Request, res: Express.Response) => { try { // Validate the HTTP request and return an AuthorizationRequest. - const authRequest = await authorizationServer.validateAuthorizationRequest(request, requestFromExpress(req)); + const authRequest = await authorizationServer.validateAuthorizationRequest(request); // You will probably redirect the user to a login endpoint. if (!req.user) { From 5cde125caa3100dc581624742c01a1583e014cd2 Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Fri, 7 Jun 2024 17:55:34 -0400 Subject: [PATCH 4/7] fix: remove method check for requestFromVanilla --- package.json | 2 +- src/adapters/vanilla.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c50a480f..14d20791 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jmondi/oauth2-server", - "version": "3.4.0-next.1", + "version": "3.4.0-next.2", "type": "module", "author": "Jason Raimondi ", "funding": "https://github.com/sponsors/jasonraimondi", diff --git a/src/adapters/vanilla.ts b/src/adapters/vanilla.ts index c1f03ec9..254e77c2 100644 --- a/src/adapters/vanilla.ts +++ b/src/adapters/vanilla.ts @@ -20,7 +20,7 @@ export function requestFromVanilla(req: Request): OAuthRequest { }); let body: Record = {}; - if (req.body != null && (req.method === "POST" || req.method === "PUT" || req.method === "PATCH")) { + if (req.body != null) { body = JSON.parse(req.body.toString()); } From 9563ec60e5c84f545e936c63e20cdcf33682db7d Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Wed, 12 Jun 2024 21:53:45 -0400 Subject: [PATCH 5/7] feat: add responseToVanilla adapter --- src/adapters/vanilla.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/adapters/vanilla.ts b/src/adapters/vanilla.ts index 254e77c2..fad369dc 100644 --- a/src/adapters/vanilla.ts +++ b/src/adapters/vanilla.ts @@ -1,5 +1,6 @@ import { OAuthRequest } from "../requests/request.js"; import { OAuthResponse } from "../responses/response.js"; +import { ErrorType, OAuthException } from "../exceptions/oauth.exception.js"; export function responseFromVanilla(res: Response): OAuthResponse { const headers: Record = {}; @@ -35,3 +36,22 @@ export function requestFromVanilla(req: Request): OAuthRequest { headers: headers, }); } + +export function responseToVanilla(oauthResponse: OAuthResponse): Response { + if (oauthResponse.status === 302) { + if (!oauthResponse.headers.location) { + throw new OAuthException(`missing redirect location`, ErrorType.InvalidRequest); + } + return new Response(null, { + status: 302, + headers: { + Location: oauthResponse.headers.location, + }, + }); + } + + return new Response(JSON.stringify(oauthResponse.body), { + status: oauthResponse.status, + headers: oauthResponse.headers, + }); +} From 83f3d0f4bbd630475b74c9265f77ab9d44a0cf72 Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Wed, 12 Jun 2024 21:54:38 -0400 Subject: [PATCH 6/7] feat: more explicit imports --- package.json | 2 +- src/code_verifiers/S256.verifier.ts | 4 ++-- src/utils/token.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 14d20791..36bbd01e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jmondi/oauth2-server", - "version": "3.4.0-next.2", + "version": "3.4.0-next.3", "type": "module", "author": "Jason Raimondi ", "funding": "https://github.com/sponsors/jasonraimondi", diff --git a/src/code_verifiers/S256.verifier.ts b/src/code_verifiers/S256.verifier.ts index 0ac05ca1..896e009d 100644 --- a/src/code_verifiers/S256.verifier.ts +++ b/src/code_verifiers/S256.verifier.ts @@ -1,4 +1,4 @@ -import crypto from "node:crypto"; +import { createHash } from "node:crypto"; import { base64urlencode } from "../utils/base64.js"; import { ICodeChallenge } from "./verifier.js"; @@ -7,7 +7,7 @@ export class S256Verifier implements ICodeChallenge { public readonly method = "S256"; verifyCodeChallenge(codeVerifier: string, codeChallenge: string): boolean { - const codeHash = crypto.createHash("sha256").update(codeVerifier).digest(); + const codeHash = createHash("sha256").update(codeVerifier).digest(); return codeChallenge === base64urlencode(codeHash); } } diff --git a/src/utils/token.ts b/src/utils/token.ts index bd1df1f6..70413ac2 100644 --- a/src/utils/token.ts +++ b/src/utils/token.ts @@ -1,5 +1,5 @@ -import crypto from "node:crypto"; +import { randomBytes } from "node:crypto"; export function generateRandomToken(len = 80): string { - return crypto.randomBytes(len / 2).toString("hex"); + return randomBytes(len / 2).toString("hex"); } From 0fd7b7c60adda0373d3f7a000cd1d96faca900a5 Mon Sep 17 00:00:00 2001 From: Jason Raimondi Date: Tue, 2 Jul 2024 10:20:41 -0400 Subject: [PATCH 7/7] chore: sync jsr.json --- jsr.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsr.json b/jsr.json index 2aef3ed9..ef519248 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@jmondi/oauth2-server", - "version": "3.4.0-next.0", + "version": "3.4.0-next.3", "exports": { ".": "./src/index.ts", "./vanilla": "./src/adapters/vanilla.ts",