@elysiajs/jwt 0.3.0 → 0.5.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ import { type Elysia } from 'elysia';
2
+ import { type JWTPayload, type JWSHeaderParameters } from 'jose';
3
+ import type { Static, TSchema } from '@sinclair/typebox';
4
+ type UnwrapSchema<Schema extends TSchema | undefined, Fallback = unknown> = Schema extends TSchema ? Static<NonNullable<Schema>> : Fallback;
5
+ export interface JWTPayloadSpec {
6
+ iss?: string;
7
+ sub?: string;
8
+ aud?: string | string[];
9
+ jti?: string;
10
+ nbf?: number;
11
+ exp?: number;
12
+ iat?: number;
13
+ }
14
+ export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWSHeaderParameters, Omit<JWTPayload, 'nbf' | 'exp'> {
15
+ name?: Name;
16
+ secret: string;
17
+ schema?: Schema;
18
+ nbf?: string | number;
19
+ exp?: string | number;
20
+ }
21
+ export declare const jwt: <Name extends string = "jwt", Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => (app: Elysia) => Elysia<{
22
+ store: {};
23
+ request: { [key in Name extends string ? Name : "jwt"]: {
24
+ readonly sign: (morePayload: UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec) => Promise<string>;
25
+ readonly verify: (jwt?: string) => Promise<false | (UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec)>;
26
+ }; };
27
+ schema: {};
28
+ meta: Record<typeof import("elysia").SCHEMA, {}> & Record<typeof import("elysia").DEFS, {}> & Record<typeof import("elysia").EXPOSED, {}>;
29
+ }>;
30
+ export default jwt;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jwt = void 0;
4
+ const elysia_1 = require("elysia");
5
+ const jose_1 = require("jose");
6
+ const typebox_1 = require("@sinclair/typebox");
7
+ const jwt = ({ name = 'jwt', secret, alg = 'HS256', crit, schema, nbf, exp, ...payload }) => (app) => {
8
+ if (!secret)
9
+ throw new Error("Secret can't be empty");
10
+ const key = new TextEncoder().encode(secret);
11
+ const validator = schema
12
+ ? (0, elysia_1.getSchemaValidator)(typebox_1.Type.Union([
13
+ schema,
14
+ typebox_1.Type.Object({
15
+ iss: typebox_1.Type.Optional(typebox_1.Type.String()),
16
+ sub: typebox_1.Type.Optional(typebox_1.Type.String()),
17
+ aud: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Array(typebox_1.Type.String())])),
18
+ jti: typebox_1.Type.Optional(typebox_1.Type.String()),
19
+ nbf: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Number()])),
20
+ exp: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Number()])),
21
+ iat: typebox_1.Type.Optional(typebox_1.Type.String())
22
+ })
23
+ ]), {})
24
+ : undefined;
25
+ return app.decorate(name, {
26
+ sign: (morePayload) => {
27
+ let jwt = new jose_1.SignJWT({
28
+ ...payload,
29
+ ...morePayload,
30
+ nbf: undefined,
31
+ exp: undefined
32
+ }).setProtectedHeader({
33
+ alg,
34
+ crit
35
+ });
36
+ if (nbf)
37
+ jwt = jwt.setNotBefore(nbf);
38
+ if (exp)
39
+ jwt = jwt.setExpirationTime(exp);
40
+ return jwt.sign(key);
41
+ },
42
+ verify: async (jwt) => {
43
+ if (!jwt)
44
+ return false;
45
+ try {
46
+ const data = (await (0, jose_1.jwtVerify)(jwt, key)).payload;
47
+ if (validator && !validator.Check(data))
48
+ throw new elysia_1.ValidationError('JWT', validator, data);
49
+ return data;
50
+ }
51
+ catch (_) {
52
+ return false;
53
+ }
54
+ }
55
+ });
56
+ };
57
+ exports.jwt = jwt;
58
+ exports.default = exports.jwt;
package/dist/index.d.ts CHANGED
@@ -19,10 +19,10 @@ export interface JWTOption<Name extends string | undefined = 'jwt', Schema exten
19
19
  exp?: string | number;
20
20
  }
21
21
  export declare const jwt: <Name extends string = "jwt", Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => (app: Elysia) => Elysia<{
22
- store: Record<string, unknown>;
22
+ store: {};
23
23
  request: { [key in Name extends string ? Name : "jwt"]: {
24
- sign: (morePayload: UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec) => Promise<string>;
25
- verify: (jwt?: string) => Promise<false | (UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec)>;
24
+ readonly sign: (morePayload: UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec) => Promise<string>;
25
+ readonly verify: (jwt?: string) => Promise<false | (UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec)>;
26
26
  }; };
27
27
  schema: {};
28
28
  meta: Record<typeof import("elysia").SCHEMA, {}> & Record<typeof import("elysia").DEFS, {}> & Record<typeof import("elysia").EXPOSED, {}>;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createValidationError, getSchemaValidator } from 'elysia';
1
+ import { ValidationError, getSchemaValidator } from 'elysia';
2
2
  import { SignJWT, jwtVerify } from 'jose';
3
3
  import { Type as t } from '@sinclair/typebox';
4
4
  export const jwt = ({ name = 'jwt', secret, alg = 'HS256', crit, schema, nbf, exp, ...payload }) => (app) => {
@@ -42,7 +42,7 @@ export const jwt = ({ name = 'jwt', secret, alg = 'HS256', crit, schema, nbf, ex
42
42
  try {
43
43
  const data = (await jwtVerify(jwt, key)).payload;
44
44
  if (validator && !validator.Check(data))
45
- throw createValidationError('JWT', validator, data);
45
+ throw new ValidationError('JWT', validator, data);
46
46
  return data;
47
47
  }
48
48
  catch (_) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elysiajs/jwt",
3
3
  "description": "Plugin for Elysia for using JWT Authentication",
4
- "version": "0.3.0",
4
+ "version": "0.5.0-rc.1",
5
5
  "author": {
6
6
  "name": "saltyAom",
7
7
  "url": "https://github.com/SaltyAom",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "main": "./dist/index.js",
15
15
  "exports": {
16
- "require": "./dist/index.js",
16
+ "require": "./dist/cjs/index.js",
17
17
  "import": "./dist/index.js",
18
18
  "node": "./dist/index.js",
19
19
  "default": "./dist/index.js"
@@ -31,22 +31,23 @@
31
31
  "scripts": {
32
32
  "dev": "bun run --hot example/index.ts",
33
33
  "test": "bun wiptest",
34
- "build": "rimraf dist && tsc --project tsconfig.esm.json",
34
+ "build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
35
35
  "release": "npm run build && npm run test && npm publish --access public"
36
36
  },
37
37
  "dependencies": {
38
- "jose": "^4.12.0"
38
+ "jose": "^4.14.4"
39
39
  },
40
40
  "devDependencies": {
41
- "@elysiajs/cookie": "^0.1.1",
42
- "@sinclair/typebox": "^0.25.24",
43
- "@types/node": "^18.11.7",
44
- "bun-types": "^0.5.7",
45
- "eslint": "^8.26.0",
46
- "elysia": "^0.3.0-rc.1",
47
- "typescript": "^4.9.4"
41
+ "@elysiajs/cookie": "^0.3.0",
42
+ "@sinclair/typebox": "^0.28.10",
43
+ "@types/node": "^20.1.4",
44
+ "bun-types": "^0.5.8",
45
+ "elysia": "0.5.0-beta.3",
46
+ "eslint": "^8.40.0",
47
+ "rimraf": "4.3",
48
+ "typescript": "^5.0.4"
48
49
  },
49
50
  "peerDependencies": {
50
- "elysia": ">= 0.2.0"
51
+ "elysia": ">= 0.5.0-rc-3"
51
52
  }
52
53
  }