From d632bf7b7af1ddca3cb6ad7ae8750b83ca194f0f Mon Sep 17 00:00:00 2001 From: Arno V Date: Mon, 24 Jun 2024 17:17:48 -0400 Subject: [PATCH] feat: moving verifyAndExtractToken to common package --- packages/auth-common/package.json | 7 +++-- packages/auth-common/src/components/index.ts | 29 +++++++++++++++++++ .../auth-provider/src/common/constants.ts | 10 ------- .../auth-provider/src/common/utilities.ts | 27 +++++------------ .../components/AuthProvider/AuthProvider.tsx | 7 ++--- pnpm-lock.yaml | 6 +++- 6 files changed, 47 insertions(+), 39 deletions(-) diff --git a/packages/auth-common/package.json b/packages/auth-common/package.json index b84688d..ecda318 100644 --- a/packages/auth-common/package.json +++ b/packages/auth-common/package.json @@ -14,9 +14,7 @@ "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", - "files": [ - "dist" - ], + "files": ["dist"], "scripts": { "build:check": "tsc", "build:js": "vite build", @@ -28,5 +26,8 @@ "dev": "npm-run-all clean --parallel dev:js dev:types", "lint": "biome lint src", "start": "static-server dist --port 5173" + }, + "peerDependencies": { + "jose": "^5.4.1" } } diff --git a/packages/auth-common/src/components/index.ts b/packages/auth-common/src/components/index.ts index dc14c38..16d7528 100644 --- a/packages/auth-common/src/components/index.ts +++ b/packages/auth-common/src/components/index.ts @@ -1,3 +1,5 @@ +import * as jose from "jose"; + export const AUTH_TYPES = { ID_TOKEN: "id_token", }; @@ -11,3 +13,30 @@ export const JWT = { USER_ID_KEY: "_id", ISSUER: "gizmette.com", }; + +export const JWT_PUBLIC_KEY = `-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7 +w5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5 +i1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle +aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+ +l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE +sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81 +awIDAQAB +-----END PUBLIC KEY-----`; + +export const verifyAndExtractToken = async ( + token: string, + audience: string, +) => { + try { + const alg = JWT.ALG; + const spki = JWT_PUBLIC_KEY; + const publicKey = await jose.importSPKI(spki, alg); + return await jose.jwtVerify(token, publicKey, { + issuer: JWT.ISSUER, + audience, + }); + } catch (_error) { + return undefined; + } +}; diff --git a/packages/auth-provider/src/common/constants.ts b/packages/auth-provider/src/common/constants.ts index dfccc95..1ebd405 100644 --- a/packages/auth-provider/src/common/constants.ts +++ b/packages/auth-provider/src/common/constants.ts @@ -11,13 +11,3 @@ export const API_ENDPOINT = { }; export const LOCAL_STORAGE_PREFIX = "@@auth@@"; - -export const JWT_PUBLIC_KEY = `-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7 -w5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5 -i1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle -aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+ -l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE -sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81 -awIDAQAB ------END PUBLIC KEY-----`; diff --git a/packages/auth-provider/src/common/utilities.ts b/packages/auth-provider/src/common/utilities.ts index ab2c035..237d00e 100644 --- a/packages/auth-provider/src/common/utilities.ts +++ b/packages/auth-provider/src/common/utilities.ts @@ -1,8 +1,12 @@ -import { AUTH_TYPES, HEADERS, JWT } from "@versini/auth-common"; -import * as jose from "jose"; +import { + AUTH_TYPES, + HEADERS, + JWT, + verifyAndExtractToken, +} from "@versini/auth-common"; import { v4 as uuidv4 } from "uuid"; -import { API_ENDPOINT, JWT_PUBLIC_KEY } from "./constants"; +import { API_ENDPOINT } from "./constants"; import type { ServiceCallProps } from "./types"; export const isProd = process.env.NODE_ENV === "production"; @@ -45,23 +49,6 @@ export const serviceCall = async ({ params = {} }: ServiceCallProps) => { } }; -export const verifyAndExtractToken = async ( - token: string, - audience: string, -) => { - try { - const alg = JWT.ALG; - const spki = JWT_PUBLIC_KEY; - const publicKey = await jose.importSPKI(spki, alg); - return await jose.jwtVerify(token, publicKey, { - issuer: JWT.ISSUER, - audience, - }); - } catch (_error) { - return undefined; - } -}; - export const authenticateUser = async ({ username, password, diff --git a/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx b/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx index 95bb45b..9a6553e 100644 --- a/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx +++ b/packages/auth-provider/src/components/AuthProvider/AuthProvider.tsx @@ -1,4 +1,4 @@ -import { JWT } from "@versini/auth-common"; +import { JWT, verifyAndExtractToken } from "@versini/auth-common"; import { useLocalStorage } from "@versini/ui-hooks"; import { useEffect, useState } from "react"; @@ -8,10 +8,7 @@ import { LOGOUT_SESSION, } from "../../common/constants"; import type { AuthProviderProps, AuthState } from "../../common/types"; -import { - authenticateUser, - verifyAndExtractToken, -} from "../../common/utilities"; +import { authenticateUser } from "../../common/utilities"; import { usePrevious } from "../hooks/usePrevious"; import { AuthContext } from "./AuthContext"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c896ad..9d98837 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -39,7 +39,11 @@ importers: specifier: 18.3.1 version: 18.3.1(react@18.3.1) - packages/auth-common: {} + packages/auth-common: + dependencies: + jose: + specifier: ^5.4.1 + version: 5.4.1 packages/auth-provider: dependencies: