@elysiajs/jwt 0.1.0-rc.4 → 0.1.0-rc.6

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.
package/dist/index.d.ts CHANGED
@@ -11,44 +11,10 @@ export interface JWTPayloadSpec {
11
11
  iat?: number;
12
12
  }
13
13
  export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWSHeaderParameters, Omit<JWTPayload, 'nbf' | 'exp'> {
14
- /**
15
- * Name to decorate method as
16
- *
17
- * ---
18
- * @example
19
- * For example, `jwt` will decorate Context with `Context.jwt`
20
- *
21
- * ```typescript
22
- * app
23
- * .decorate({
24
- * name: 'myJWTNamespace',
25
- * secret: process.env.JWT_SECRETS
26
- * })
27
- * .get('/sign/:name', ({ myJWTNamespace, params }) => {
28
- * return myJWTNamespace.sign(params)
29
- * })
30
- * ```
31
- */
32
14
  name?: Name;
33
- /**
34
- * JWT Secret
35
- */
36
15
  secret: string;
37
- /**
38
- * Type strict validation for JWT payload
39
- */
40
16
  schema?: Schema;
41
- /**
42
- * JWT Not Before
43
- *
44
- * @see [RFC7519#section-4.1.5](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5)
45
- */
46
17
  nbf?: string | number;
47
- /**
48
- * JWT Expiration Time
49
- *
50
- * @see [RFC7519#section-4.1.4](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4)
51
- */
52
18
  exp?: string | number;
53
19
  }
54
20
  export declare const jwt: <Name extends string | undefined, Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => (app: Elysia) => Elysia<{
package/dist/index.js CHANGED
@@ -1,12 +1,7 @@
1
1
  import { createValidationError, getSchemaValidator } from 'elysia';
2
2
  import { SignJWT, jwtVerify } from 'jose';
3
3
  import { Type as t } from '@sinclair/typebox';
4
- export const jwt = ({ name = 'jwt', secret,
5
- // Start JWT Header
6
- alg = 'HS256', crit, schema,
7
- // End JWT Header
8
- // Start JWT Payload
9
- nbf, exp, ...payload }) => (app) => {
4
+ export const jwt = ({ name = 'jwt', secret, alg = 'HS256', crit, schema, nbf, exp, ...payload }) => (app) => {
10
5
  if (!secret)
11
6
  throw new Error("Secret can't be empty");
12
7
  const key = new TextEncoder().encode(secret);
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.1.0-rc.4",
4
+ "version": "0.1.0-rc.6",
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/cjs/index.js",
16
+ "require": "./dist/index.js",
17
17
  "import": "./dist/index.js",
18
18
  "node": "./dist/index.js",
19
19
  "default": "./dist/index.js"
@@ -31,9 +31,7 @@
31
31
  "scripts": {
32
32
  "dev": "bun run --hot example/index.ts",
33
33
  "test": "bun wiptest",
34
- "build": "rimraf dist && npm run build:cjs && npm run build:esm",
35
- "build:cjs": "tsc --project tsconfig.cjs.json",
36
- "build:esm": "tsc --project tsconfig.esm.json",
34
+ "build": "rimraf dist && tsc --project tsconfig.esm.json",
37
35
  "release": "npm run build && npm run test && npm publish --access public"
38
36
  },
39
37
  "dependencies": {
@@ -1,62 +0,0 @@
1
- import { type Elysia, type UnwrapSchema } from 'elysia';
2
- import { type JWTPayload, type JWSHeaderParameters } from 'jose';
3
- import type { TSchema } from '@sinclair/typebox';
4
- export interface JWTPayloadSpec {
5
- iss?: string;
6
- sub?: string;
7
- aud?: string | string[];
8
- jti?: string;
9
- nbf?: number;
10
- exp?: number;
11
- iat?: number;
12
- }
13
- export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWSHeaderParameters, Omit<JWTPayload, 'nbf' | 'exp'> {
14
- /**
15
- * Name to decorate method as
16
- *
17
- * ---
18
- * @example
19
- * For example, `jwt` will decorate Context with `Context.jwt`
20
- *
21
- * ```typescript
22
- * app
23
- * .decorate({
24
- * name: 'myJWTNamespace',
25
- * secret: process.env.JWT_SECRETS
26
- * })
27
- * .get('/sign/:name', ({ myJWTNamespace, params }) => {
28
- * return myJWTNamespace.sign(params)
29
- * })
30
- * ```
31
- */
32
- name?: Name;
33
- /**
34
- * JWT Secret
35
- */
36
- secret: string;
37
- /**
38
- * Type strict validation for JWT payload
39
- */
40
- schema?: Schema;
41
- /**
42
- * JWT Not Before
43
- *
44
- * @see [RFC7519#section-4.1.5](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5)
45
- */
46
- nbf?: string | number;
47
- /**
48
- * JWT Expiration Time
49
- *
50
- * @see [RFC7519#section-4.1.4](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4)
51
- */
52
- exp?: string | number;
53
- }
54
- export declare const jwt: <Name extends string | undefined, Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => (app: Elysia) => Elysia<{
55
- store: {};
56
- request: { [key in Name extends string ? Name : "jwt"]: {
57
- sign: (morePayload: UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec) => Promise<string>;
58
- verify: (jwt?: string) => Promise<false | (UnwrapSchema<Schema, Record<string, string>> & JWTPayloadSpec)>;
59
- }; };
60
- schema: {};
61
- }>;
62
- export default jwt;
package/dist/cjs/index.js DELETED
@@ -1,63 +0,0 @@
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,
8
- // Start JWT Header
9
- alg = 'HS256', crit, schema,
10
- // End JWT Header
11
- // Start JWT Payload
12
- nbf, exp, ...payload }) => (app) => {
13
- if (!secret)
14
- throw new Error("Secret can't be empty");
15
- const key = new TextEncoder().encode(secret);
16
- const validator = schema
17
- ? (0, elysia_1.getSchemaValidator)(typebox_1.Type.Union([
18
- schema,
19
- typebox_1.Type.Object({
20
- iss: typebox_1.Type.Optional(typebox_1.Type.String()),
21
- sub: typebox_1.Type.Optional(typebox_1.Type.String()),
22
- aud: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Array(typebox_1.Type.String())])),
23
- jti: typebox_1.Type.Optional(typebox_1.Type.String()),
24
- nbf: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Number()])),
25
- exp: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.String(), typebox_1.Type.Number()])),
26
- iat: typebox_1.Type.Optional(typebox_1.Type.String())
27
- })
28
- ]))
29
- : undefined;
30
- return app.decorate(name, {
31
- sign: (morePayload) => {
32
- let jwt = new jose_1.SignJWT({
33
- ...payload,
34
- ...morePayload,
35
- nbf: undefined,
36
- exp: undefined
37
- }).setProtectedHeader({
38
- alg,
39
- crit
40
- });
41
- if (nbf)
42
- jwt = jwt.setNotBefore(nbf);
43
- if (exp)
44
- jwt = jwt.setExpirationTime(exp);
45
- return jwt.sign(key);
46
- },
47
- verify: async (jwt) => {
48
- if (!jwt)
49
- return false;
50
- try {
51
- const data = (await (0, jose_1.jwtVerify)(jwt, key)).payload;
52
- if (validator && !validator.Check(data))
53
- throw (0, elysia_1.createValidationError)('JWT', validator, data);
54
- return data;
55
- }
56
- catch (_) {
57
- return false;
58
- }
59
- }
60
- });
61
- };
62
- exports.jwt = jwt;
63
- exports.default = exports.jwt;