@ooneex/jwt 0.0.17 → 1.0.0
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/README.md +11 -25
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,49 +1,35 @@
|
|
|
1
1
|
# @ooneex/jwt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JWT authentication toolkit using the JOSE library -- generate, sign, verify, and decode JSON Web Tokens with support for multiple algorithms.
|
|
4
4
|
|
|
5
5
|

|
|
6
|
-

|
|
7
|
-

|
|
8
6
|

|
|
9
7
|

|
|
10
8
|
|
|
11
9
|
## Features
|
|
12
10
|
|
|
13
|
-
✅ **JOSE Powered** - Built on the robust JOSE library for secure JWT operations
|
|
11
|
+
✅ **JOSE Powered** - Built on the robust JOSE library for secure JWT operations with HS256 signing
|
|
14
12
|
|
|
15
|
-
✅ **Token Generation** - Create signed JWT tokens with custom payloads and headers
|
|
13
|
+
✅ **Token Generation** - Create signed JWT tokens with custom payloads and protected headers
|
|
16
14
|
|
|
17
|
-
✅ **Token Validation** - Verify token signatures and expiration
|
|
15
|
+
✅ **Token Validation** - Verify token signatures and expiration via jwtVerify
|
|
18
16
|
|
|
19
|
-
✅ **Payload Extraction** - Decode and retrieve payload data from tokens
|
|
17
|
+
✅ **Payload Extraction** - Decode and retrieve payload and header data from tokens without verification
|
|
20
18
|
|
|
21
|
-
✅ **Flexible Expiration** - Support for various expiration formats (seconds, minutes, hours, days)
|
|
19
|
+
✅ **Flexible Expiration** - Support for various expiration formats (seconds, minutes, hours, days, weeks, years)
|
|
22
20
|
|
|
23
21
|
✅ **Type-Safe** - Full TypeScript support with generic payload types
|
|
24
22
|
|
|
25
|
-
✅ **
|
|
23
|
+
✅ **Standard Claims** - Built-in support for iss, sub, aud, exp, iat, nbf, and jti claims
|
|
26
24
|
|
|
27
|
-
|
|
25
|
+
✅ **Dependency Injection** - Injectable via @ooneex/container for seamless DI integration
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
bun add @ooneex/jwt
|
|
32
|
-
```
|
|
27
|
+
✅ **Environment Config** - Automatic secret loading from JWT_SECRET environment variable
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
pnpm add @ooneex/jwt
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Yarn
|
|
40
|
-
```bash
|
|
41
|
-
yarn add @ooneex/jwt
|
|
42
|
-
```
|
|
29
|
+
## Installation
|
|
43
30
|
|
|
44
|
-
### npm
|
|
45
31
|
```bash
|
|
46
|
-
|
|
32
|
+
bun add @ooneex/jwt
|
|
47
33
|
```
|
|
48
34
|
|
|
49
35
|
## Usage
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import*as
|
|
2
|
+
var y=function(e,t,a,n){var i=arguments.length,r=i<3?t:n===null?n=Object.getOwnPropertyDescriptor(t,a):n,c;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")r=Reflect.decorate(e,t,a,n);else for(var d=e.length-1;d>=0;d--)if(c=e[d])r=(i<3?c(r):i>3?c(t,a,r):c(t,a))||r;return i>3&&r&&Object.defineProperty(t,a,r),r};var T=(e,t)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(e,t)};import{injectable as g}from"@ooneex/container";import*as s from"jose";import{Exception as w}from"@ooneex/exception";import{HttpStatus as J}from"@ooneex/http-status";class p extends w{constructor(e,t={}){super(e,{status:J.Code.InternalServerError,data:t});this.name="JwtException"}}class u{secret;constructor(e){if(e=e||Bun.env.JWT_SECRET,!e)throw new p("JWT secret is required. Please provide a secret either through the constructor options or set the JWT_SECRET environment variable.");this.secret=new TextEncoder().encode(e)}async create(e){let a=e?.payload??{},{iss:n,sub:i,aud:r,exp:c,iat:d,jti:l,nbf:f,...m}=a,o=new s.SignJWT(m).setProtectedHeader({...{alg:"HS256"},...e?.header??{}});if(n)o.setIssuer(n);if(i)o.setSubject(i);if(r)o.setAudience(r);if(c)o.setExpirationTime(c??"1h");if(d)o.setIssuedAt(d);if(f)o.setNotBefore(f);if(l)o.setJti(l);return await o.sign(this.secret)}getSecret(){return this.secret}async isValid(e){try{return await s.jwtVerify(e,this.secret),!0}catch(t){return!1}}getHeader(e){return s.decodeProtectedHeader(e)}getPayload(e){return s.decodeJwt(e)}}u=y([g(),T("design:paramtypes",[String])],u);export{p as JwtException,u as Jwt};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=21941014F4FC9FC464756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["src/Jwt.ts", "src/JwtException.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { JWTHeaderParameters } from \"jose\";\nimport * as jose from \"jose\";\nimport { JwtException } from \"./JwtException\";\nimport type { IJwt, JwtDefaultPayloadType, JwtPayloadType } from \"./types\";\n\nexport class Jwt implements IJwt {\n private secret: Uint8Array<ArrayBuffer>;\n\n constructor(secret?: string) {\n secret = secret || Bun.env.JWT_SECRET
|
|
5
|
+
"import { injectable } from \"@ooneex/container\";\nimport type { JWTHeaderParameters } from \"jose\";\nimport * as jose from \"jose\";\nimport { JwtException } from \"./JwtException\";\nimport type { IJwt, JwtDefaultPayloadType, JwtPayloadType } from \"./types\";\n\n@injectable()\nexport class Jwt implements IJwt {\n private secret: Uint8Array<ArrayBuffer>;\n\n constructor(secret?: string) {\n secret = secret || Bun.env.JWT_SECRET;\n\n if (!secret) {\n throw new JwtException(\n \"JWT secret is required. Please provide a secret either through the constructor options or set the JWT_SECRET environment variable.\",\n );\n }\n\n this.secret = new TextEncoder().encode(secret);\n }\n\n public async create<T extends Record<string, unknown> = Record<string, unknown>>(config?: {\n payload?: JwtDefaultPayloadType & JwtPayloadType<T>;\n header?: JWTHeaderParameters;\n }): Promise<string> {\n const alg = \"HS256\";\n const payload: JwtDefaultPayloadType = config?.payload ?? {};\n\n const { iss, sub, aud, exp, iat, jti, nbf, ...rest } = payload;\n\n const token = new jose.SignJWT(rest).setProtectedHeader({ ...{ alg }, ...(config?.header ?? {}) });\n\n if (iss) {\n token.setIssuer(iss);\n }\n\n if (sub) {\n token.setSubject(sub);\n }\n\n if (aud) {\n token.setAudience(aud);\n }\n\n if (exp) {\n token.setExpirationTime(exp ?? \"1h\");\n }\n\n if (iat) {\n token.setIssuedAt(iat);\n }\n\n if (nbf) {\n token.setNotBefore(nbf);\n }\n\n if (jti) {\n token.setJti(jti);\n }\n\n return await token.sign(this.secret);\n }\n\n public getSecret(): Uint8Array<ArrayBuffer> {\n return this.secret;\n }\n\n public async isValid(token: string): Promise<boolean> {\n try {\n await jose.jwtVerify(token, this.secret);\n\n return true;\n } catch (_error) {\n return false;\n }\n }\n\n public getHeader<T = JWTHeaderParameters>(token: string): T {\n return jose.decodeProtectedHeader(token) as T;\n }\n\n public getPayload<T extends Record<string, unknown> = Record<string, unknown>>(token: string): JwtPayloadType<T> {\n return jose.decodeJwt(token) as JwtPayloadType<T>;\n }\n}\n",
|
|
6
6
|
"import { Exception } from \"@ooneex/exception\";\nimport { HttpStatus } from \"@ooneex/http-status\";\n\nexport class JwtException extends Exception {\n constructor(message: string, data: Record<string, unknown> = {}) {\n super(message, {\n status: HttpStatus.Code.InternalServerError,\n data,\n });\n\n this.name = \"JwtException\";\n }\n}\n"
|
|
7
7
|
],
|
|
8
|
-
"mappings": ";
|
|
9
|
-
"debugId": "
|
|
8
|
+
"mappings": ";ybAAA,qBAAS,0BAET,uBCFA,oBAAS,0BACT,qBAAS,4BAEF,MAAM,UAAqB,CAAU,CAC1C,WAAW,CAAC,EAAiB,EAAgC,CAAC,EAAG,CAC/D,MAAM,EAAS,CACb,OAAQ,EAAW,KAAK,oBACxB,MACF,CAAC,EAED,KAAK,KAAO,eAEhB,CDLO,MAAM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"JWT authentication toolkit using the JOSE library — generate, sign, verify, and decode JSON Web Tokens with support for multiple algorithms",
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,8 +28,9 @@
|
|
|
28
28
|
"test": "bun test tests"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/exception": "0.0.
|
|
32
|
-
"@ooneex/
|
|
31
|
+
"@ooneex/exception": "0.0.18",
|
|
32
|
+
"@ooneex/container": "0.0.19",
|
|
33
|
+
"@ooneex/http-status": "0.0.18",
|
|
33
34
|
"jose": "^6.1.2"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {},
|