Skip to content

Commit

Permalink
feat: moving verifyAndExtractToken to common package
Browse files Browse the repository at this point in the history
  • Loading branch information
aversini committed Jun 24, 2024
1 parent 3a70e5d commit d632bf7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
7 changes: 4 additions & 3 deletions packages/auth-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
29 changes: 29 additions & 0 deletions packages/auth-common/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as jose from "jose";

export const AUTH_TYPES = {
ID_TOKEN: "id_token",
};
Expand All @@ -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;
}
};
10 changes: 0 additions & 10 deletions packages/auth-provider/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-----`;
27 changes: 7 additions & 20 deletions packages/auth-provider/src/common/utilities.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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";

Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d632bf7

Please sign in to comment.