@ooneex/jwt 0.0.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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +11 -0
- package/dist/ooneex-jwt-0.0.1.tgz +0 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ooneex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @ooneex/jwt
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { JWTHeaderParameters as JWTHeaderParameters2 } from "jose";
|
|
2
|
+
import { JWTHeaderParameters } from "jose";
|
|
3
|
+
type JwtClassType = new (...args: any[]) => IJwt;
|
|
4
|
+
type JwtExpiresInType = `${number}s` | `${number}m` | `${number}h` | `${number}d` | `${number}w` | `${number}y`;
|
|
5
|
+
type JwtDefaultPayloadType = {
|
|
6
|
+
iss?: string;
|
|
7
|
+
sub?: string;
|
|
8
|
+
aud?: string | string[];
|
|
9
|
+
jti?: string;
|
|
10
|
+
nbf?: number | string | Date;
|
|
11
|
+
exp?: number | JwtExpiresInType | Date;
|
|
12
|
+
iat?: number | string | Date;
|
|
13
|
+
};
|
|
14
|
+
type JwtPayloadType<T> = JwtDefaultPayloadType & T;
|
|
15
|
+
interface IJwt {
|
|
16
|
+
create: <T extends Record<string, unknown> = Record<string, unknown>>(config?: {
|
|
17
|
+
payload?: JwtDefaultPayloadType & JwtPayloadType<T>;
|
|
18
|
+
header?: JWTHeaderParameters;
|
|
19
|
+
}) => Promise<string>;
|
|
20
|
+
getPayload: <T extends Record<string, unknown> = Record<string, unknown>>(token: string) => JwtPayloadType<T>;
|
|
21
|
+
getHeader: (token: string) => JWTHeaderParameters;
|
|
22
|
+
isValid: (token: string) => Promise<boolean> | boolean;
|
|
23
|
+
getSecret: () => Uint8Array<ArrayBuffer>;
|
|
24
|
+
}
|
|
25
|
+
declare class Jwt implements IJwt {
|
|
26
|
+
private secret;
|
|
27
|
+
constructor(secret?: string);
|
|
28
|
+
create<T extends Record<string, unknown> = Record<string, unknown>>(config?: {
|
|
29
|
+
payload?: JwtDefaultPayloadType & JwtPayloadType<T>;
|
|
30
|
+
header?: JWTHeaderParameters2;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
getSecret(): Uint8Array<ArrayBuffer>;
|
|
33
|
+
isValid(token: string): Promise<boolean>;
|
|
34
|
+
getHeader<T = JWTHeaderParameters2>(token: string): T;
|
|
35
|
+
getPayload<T extends Record<string, unknown> = Record<string, unknown>>(token: string): JwtPayloadType<T>;
|
|
36
|
+
}
|
|
37
|
+
import { Exception } from "@ooneex/exception";
|
|
38
|
+
declare class JwtException extends Exception {
|
|
39
|
+
constructor(message: string, data?: Record<string, unknown>);
|
|
40
|
+
}
|
|
41
|
+
export { JwtPayloadType, JwtExpiresInType, JwtException, JwtDefaultPayloadType, JwtClassType, Jwt, IJwt };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// @bun
|
|
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};
|
|
3
|
+
|
|
4
|
+
//# debugId=4D91E005A55822E964756E2164756E21
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["src/Jwt.ts", "src/JwtException.ts"],
|
|
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 || \"\";\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
|
+
"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> = {})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C,UAAuE,CAAC,EAAkC,CAC/G,OAAY,YAAU,CAAK,EAE/B",
|
|
9
|
+
"debugId": "4D91E005A55822E964756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ooneex/jwt",
|
|
3
|
+
"description": "",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"README.md",
|
|
10
|
+
"package.json"
|
|
11
|
+
],
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "bunup",
|
|
26
|
+
"lint": "tsgo --noEmit && bunx biome lint",
|
|
27
|
+
"publish:prod": "bun publish --tolerate-republish --access public",
|
|
28
|
+
"publish:pack": "bun pm pack --destination ./dist",
|
|
29
|
+
"publish:dry": "bun publish --dry-run"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@ooneex/exception": "0.0.1",
|
|
33
|
+
"@ooneex/http-status": "0.0.1",
|
|
34
|
+
"jose": "^6.1.2"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {},
|
|
37
|
+
"peerDependencies": {}
|
|
38
|
+
}
|