-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
261 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "@recnet/recnet-api-model", | ||
"version": "0.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@recnet/recnet-date-fns", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"firebase": "^10.7.2", | ||
"tslib": "^2.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@recnet/recnet-jwt", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"@recnet/recnet-api-model": "0.0.0", | ||
"jsonwebtoken": "^9.0.2", | ||
"tslib": "^2.3.0", | ||
"zod": "^3.22.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key | ||
# Don't add passphrase | ||
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub | ||
# cat jwtRS256.key | ||
# cat jwtRS256.key.pub | ||
|
||
# put them in .env file | ||
echo "PRIVATE_KEY='$(cat jwtRS256.key)'" > ../.env.local | ||
echo "PUBLIC_KEY='$(cat jwtRS256.key.pub)'" >> ../.env.local | ||
|
||
echo "Generated jwtRS256.key and jwtRS256.key.pub and put them in .env.local file." | ||
# clean up | ||
rm jwtRS256.key | ||
rm jwtRS256.key.pub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,86 @@ | ||
import { recnetJwt } from "./recnet-jwt"; | ||
import { generateJwt, verifyJwt } from "./recnet-jwt"; | ||
import { expect, test, describe } from "vitest"; | ||
import { z } from "zod"; | ||
|
||
describe("recnetJwt", () => { | ||
it("should work", () => { | ||
expect(recnetJwt()).toEqual("recnet-jwt"); | ||
const envSchema = z.object({ | ||
PRIVATE_KEY: z.string().transform((s) => s.replace(/\\n/gm, "\n")), | ||
PUBLIC_KEY: z.string().transform((s) => s.replace(/\\n/gm, "\n")), | ||
}); | ||
|
||
test("Keys pair is provided", () => { | ||
const envRes = envSchema.safeParse(process.env); | ||
expect(envRes.success).toBe(true); | ||
if (envRes.success) { | ||
expect(envRes.data).toBeDefined(); | ||
expectTypeOf(envRes.data.PRIVATE_KEY).toBeString(); | ||
expectTypeOf(envRes.data.PUBLIC_KEY).toBeString(); | ||
} | ||
}); | ||
|
||
describe("Sign", () => { | ||
const env = envSchema.parse(process.env); | ||
|
||
test("should throw error if payload has wrong type", () => { | ||
const sub = "randomUserId"; | ||
const role = "INVALID_ROLE"; | ||
// @ts-expect-error: role is invalid here | ||
expect(() => generateJwt({ sub, role }, env.PRIVATE_KEY)).toThrowError( | ||
"Invalid payload" | ||
); | ||
}); | ||
|
||
test("should return jwt token", () => { | ||
const sub = "randomUserId"; | ||
const adminToken = generateJwt({ sub, role: "ADMIN" }, env.PRIVATE_KEY); | ||
const userToken = generateJwt({ sub, role: "USER" }, env.PRIVATE_KEY); | ||
expectTypeOf(adminToken).toBeString(); | ||
expectTypeOf(userToken).toBeString(); | ||
}); | ||
}); | ||
|
||
describe("Verify", () => { | ||
const env = envSchema.parse(process.env); | ||
|
||
test("Should able to decode if using correct PUBLIC KEY", () => { | ||
const token = generateJwt({ sub: "123", role: "ADMIN" }, env.PRIVATE_KEY); | ||
const payload = verifyJwt(token, env.PUBLIC_KEY); | ||
expect(payload).toBeDefined(); | ||
expect(payload.sub).toBe("123"); | ||
expect(payload.role).toBe("ADMIN"); | ||
}); | ||
|
||
test("Should throw error if using incorrect PUBLIC KEY", () => { | ||
const token = generateJwt({ sub: "123", role: "ADMIN" }, env.PRIVATE_KEY); | ||
expect(() => verifyJwt(token, env.PUBLIC_KEY + "INVALID")).toThrowError(); | ||
}); | ||
|
||
test("Should throw error if token is expired", () => { | ||
const dateNow = Date.now(); | ||
const token = generateJwt( | ||
{ | ||
sub: "123", | ||
role: "ADMIN", | ||
iat: Math.floor(dateNow / 1000) - 60 * 60 * 24 * 8, | ||
}, | ||
env.PRIVATE_KEY | ||
); | ||
expect(() => verifyJwt(token, env.PUBLIC_KEY)).toThrowError("jwt expired"); | ||
}); | ||
|
||
test("Should not throw error if not expired", () => { | ||
const dateNow = Date.now(); | ||
const token = generateJwt( | ||
{ | ||
sub: "123", | ||
role: "ADMIN", | ||
iat: Math.floor(dateNow / 1000) - 60 * 60 * 24 * 5, | ||
}, | ||
env.PRIVATE_KEY | ||
); | ||
console.log(token); | ||
const payload = verifyJwt(token, env.PUBLIC_KEY); | ||
expect(payload).toBeDefined(); | ||
expect(payload.role).toEqual("ADMIN"); | ||
expect(payload.sub).toEqual("123"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,68 @@ | ||
import { sign, verify, SignOptions, VerifyOptions } from "jsonwebtoken"; | ||
import { z } from "zod"; | ||
import { userRoleSchema } from "@recnet/recnet-api-model"; | ||
|
||
export function recnetJwt(): string { | ||
return "recnet-jwt"; | ||
} | ||
|
||
export const recnetJwtPayloadSchema = z | ||
.object({ | ||
role: userRoleSchema, | ||
sub: z.string(), | ||
}) | ||
.passthrough(); | ||
export type RecNetJwtPayload = z.infer<typeof recnetJwtPayloadSchema>; | ||
|
||
/** | ||
Generate jwt token and sign it by private key. | ||
Use RS256 algorithm. | ||
Throw error if payload is invalid. | ||
Note: timestamp's unit is second. | ||
*/ | ||
export function generateJwt( | ||
payload: RecNetJwtPayload, | ||
sk: string, | ||
signOptions?: SignOptions | ||
): string { | ||
const payloadRes = recnetJwtPayloadSchema.safeParse(payload); | ||
if (!payloadRes.success) { | ||
throw new Error("Invalid payload"); | ||
} | ||
const parsedPayload = payloadRes.data; | ||
const options = signOptions || {}; | ||
const token = sign(parsedPayload, sk, { | ||
algorithm: "RS256", | ||
expiresIn: "7 days", | ||
audience: "recnet-api", | ||
issuer: "recnet", | ||
...options, | ||
}); | ||
return token; | ||
} | ||
|
||
/** | ||
Verify jwt token and return payload if it's valid and not expired. | ||
Throw error if token is invalid. | ||
Note: timestamp's unit is second. | ||
*/ | ||
export function verifyJwt( | ||
token: string, | ||
pk: string, | ||
verifyOptions?: VerifyOptions | ||
): RecNetJwtPayload { | ||
const options = verifyOptions || {}; | ||
const payload = verify(token, pk, { | ||
algorithms: ["RS256"], | ||
audience: "recnet-api", | ||
issuer: "recnet", | ||
...options, | ||
}); | ||
const payloadRes = recnetJwtPayloadSchema.safeParse(payload); | ||
if (!payloadRes.success) { | ||
throw new Error("Invalid payload"); | ||
} | ||
return payloadRes.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.