@ooneex/jwt 0.0.13 → 0.0.15

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.js CHANGED
@@ -1,80 +1,4 @@
1
1
  // @bun
2
- // src/Jwt.ts
3
- import * as jose from "jose";
2
+ import*as r from"jose";import{Exception as f}from"@ooneex/exception";import{HttpStatus as T}from"@ooneex/http-status";class o extends f{constructor(e,s={}){super(e,{status:T.Code.InternalServerError,data:s});this.name="JwtException"}}class l{secret;constructor(e){if(e=e||Bun.env.JWT_SECRET||"",!e)throw new o("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 w=e?.payload??{},{iss:a,sub:n,aud:i,exp:c,iat:d,jti:p,nbf:u,...y}=w,t=new r.SignJWT(y).setProtectedHeader({...{alg:"HS256"},...e?.header??{}});if(a)t.setIssuer(a);if(n)t.setSubject(n);if(i)t.setAudience(i);if(c)t.setExpirationTime(c??"1h");if(d)t.setIssuedAt(d);if(u)t.setNotBefore(u);if(p)t.setJti(p);return await t.sign(this.secret)}getSecret(){return this.secret}async isValid(e){try{return await r.jwtVerify(e,this.secret),!0}catch(s){return!1}}getHeader(e){return r.decodeProtectedHeader(e)}getPayload(e){return r.decodeJwt(e)}}export{o as JwtException,l as Jwt};
4
3
 
5
- // src/JwtException.ts
6
- import { Exception } from "@ooneex/exception";
7
- import { HttpStatus } from "@ooneex/http-status";
8
-
9
- class JwtException extends Exception {
10
- constructor(message, data = {}) {
11
- super(message, {
12
- status: HttpStatus.Code.InternalServerError,
13
- data
14
- });
15
- this.name = "JwtException";
16
- }
17
- }
18
-
19
- // src/Jwt.ts
20
- class Jwt {
21
- secret;
22
- constructor(secret) {
23
- secret = secret || Bun.env.JWT_SECRET || "";
24
- if (!secret) {
25
- throw new JwtException("JWT secret is required. Please provide a secret either through the constructor options or set the JWT_SECRET environment variable.");
26
- }
27
- this.secret = new TextEncoder().encode(secret);
28
- }
29
- async create(config) {
30
- const alg = "HS256";
31
- const payload = config?.payload ?? {};
32
- const { iss, sub, aud, exp, iat, jti, nbf, ...rest } = payload;
33
- const token = new jose.SignJWT(rest).setProtectedHeader({ ...{ alg }, ...config?.header ?? {} });
34
- if (iss) {
35
- token.setIssuer(iss);
36
- }
37
- if (sub) {
38
- token.setSubject(sub);
39
- }
40
- if (aud) {
41
- token.setAudience(aud);
42
- }
43
- if (exp) {
44
- token.setExpirationTime(exp ?? "1h");
45
- }
46
- if (iat) {
47
- token.setIssuedAt(iat);
48
- }
49
- if (nbf) {
50
- token.setNotBefore(nbf);
51
- }
52
- if (jti) {
53
- token.setJti(jti);
54
- }
55
- return await token.sign(this.secret);
56
- }
57
- getSecret() {
58
- return this.secret;
59
- }
60
- async isValid(token) {
61
- try {
62
- await jose.jwtVerify(token, this.secret);
63
- return true;
64
- } catch (_error) {
65
- return false;
66
- }
67
- }
68
- getHeader(token) {
69
- return jose.decodeProtectedHeader(token);
70
- }
71
- getPayload(token) {
72
- return jose.decodeJwt(token);
73
- }
74
- }
75
- export {
76
- JwtException,
77
- Jwt
78
- };
79
-
80
- //# debugId=6BCEB267163A258F64756E2164756E21
4
+ //# debugId=4D91E005A55822E964756E2164756E21
package/dist/index.js.map CHANGED
@@ -5,7 +5,7 @@
5
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 || \"\";\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 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": ";;AACA;;;ACDA;AACA;AAAA;AAEO,MAAM,qBAAqB,UAAU;AAAA,EAC1C,WAAW,CAAC,SAAiB,OAAgC,CAAC,GAAG;AAAA,IAC/D,MAAM,SAAS;AAAA,MACb,QAAQ,WAAW,KAAK;AAAA,MACxB;AAAA,IACF,CAAC;AAAA,IAED,KAAK,OAAO;AAAA;AAEhB;;;ADPO,MAAM,IAAoB;AAAA,EACvB;AAAA,EAER,WA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AAO,EAAQ,CACf,MAAO,IAIJ,SAAkC,CAAC,EAAkB,CAC1D,OAAY,wBAAsB,CAAK,EAGlC,UAAuE,CAAC,EAAkC,CAC/G,OAAY,YAAU,CAAK,EAE/B",
9
+ "debugId": "4D91E005A55822E964756E2164756E21",
10
10
  "names": []
11
11
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ooneex/jwt",
3
3
  "description": "JWT token generation and validation using the JOSE library for secure authentication",
4
- "version": "0.0.13",
4
+ "version": "0.0.15",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -28,8 +28,8 @@
28
28
  "test": "bun test tests"
29
29
  },
30
30
  "dependencies": {
31
- "@ooneex/exception": "0.0.11",
32
- "@ooneex/http-status": "0.0.11",
31
+ "@ooneex/exception": "0.0.13",
32
+ "@ooneex/http-status": "0.0.13",
33
33
  "jose": "^6.1.2"
34
34
  },
35
35
  "devDependencies": {},