diff --git a/jwt/lib.test.w b/jwt/lib.test.w index 6c6a7d69..c80c77ec 100644 --- a/jwt/lib.test.w +++ b/jwt/lib.test.w @@ -36,3 +36,10 @@ test "sign with expiresIn" { expect.equal(e, "jwt expired"); } } + +test "sign and decode" { + let id = util.nanoid(); + let token = jwt.sign({ foo: id }, "shhhhh"); + let decoded1 = jwt.decode(token, complete: true); + expect.equal(decoded1.get("payload").get("foo").asStr(), id); +} diff --git a/jwt/lib.w b/jwt/lib.w index 0600f757..c6e75cbf 100644 --- a/jwt/lib.w +++ b/jwt/lib.w @@ -16,6 +16,10 @@ pub struct VerifyOptions { options: VerifyJwtOptions?; } +pub struct DecodeOptions { + complete: bool?; +} + pub struct SignOptions { algorithm: str?; keyid: str?; @@ -56,10 +60,11 @@ interface IJwt { inflight jwksClient(options: IJwksClientOptions): IJwksClient; inflight sign(data: Json, secret: str, options: Json?): str; inflight verify(token: str, secret: inflight (JwtHeader, inflight (str, str): void): void, options: VerifyJwtOptions?): Json; + inflight decode(token: str, options: DecodeOptions?): Json; } class JwtUtil { - extern "./utils.mts" pub static inflight _jwt(): IJwt; + extern "./utils.js" pub static inflight _jwt(): IJwt; } pub class Util { @@ -100,4 +105,8 @@ pub class Util { throw "Either secret or jwksUri must be provided"; } } + + pub inflight static decode(token: str, options: DecodeOptions?): Json { + return JwtUtil._jwt().decode(token, options); + } } diff --git a/jwt/package-lock.json b/jwt/package-lock.json index 10a4526f..90b14a1f 100644 --- a/jwt/package-lock.json +++ b/jwt/package-lock.json @@ -1,12 +1,12 @@ { "name": "@winglibs/jwt", - "version": "0.0.1", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/jwt", - "version": "0.0.1", + "version": "0.0.3", "license": "MIT", "peerDependencies": { "jsonwebtoken": "^9.0.2", diff --git a/jwt/package.json b/jwt/package.json index ee9d2e2b..2d1e43a9 100644 --- a/jwt/package.json +++ b/jwt/package.json @@ -1,7 +1,7 @@ { "name": "@winglibs/jwt", "description": "Wing library for JWT authentication", - "version": "0.0.2", + "version": "0.0.3", "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", diff --git a/jwt/utils.extern.d.ts b/jwt/utils.extern.d.ts index 809812eb..3f85e0a6 100644 --- a/jwt/utils.extern.d.ts +++ b/jwt/utils.extern.d.ts @@ -1,6 +1,9 @@ export default interface extern { _jwt: () => Promise, } +export interface DecodeOptions { + readonly complete?: (boolean) | undefined; +} export interface IJwksClientOptions { readonly jwksUri: string; } @@ -33,6 +36,7 @@ export interface VerifyJwtOptions { readonly subject?: (string) | undefined; } export interface IJwt$Inflight { + readonly decode: (token: string, options?: (DecodeOptions) | undefined) => Promise>; readonly jwksClient: (options: IJwksClientOptions) => Promise; readonly sign: (data: Readonly, secret: string, options?: (Readonly) | undefined) => Promise; readonly verify: (token: string, secret: (arg0: JwtHeader, arg1: (arg0: string, arg1: string) => Promise) => Promise, options?: (VerifyJwtOptions) | undefined) => Promise>; diff --git a/jwt/utils.mts b/jwt/utils.js similarity index 91% rename from jwt/utils.mts rename to jwt/utils.js index f5cabe4c..438be477 100644 --- a/jwt/utils.mts +++ b/jwt/utils.js @@ -5,6 +5,7 @@ import jwksClient from "jwks-rsa"; export const _jwt = () => ({ sign: promisify(jwt.sign), verify: promisify(jwt.verify), + decode: jwt.decode, jwksClient: jwksClient, });