Skip to content

Commit

Permalink
fix(jwt): package missing util file (#162)
Browse files Browse the repository at this point in the history
* change `mts` file to `js`
* add `decode` function which is not verifying the token
  • Loading branch information
eladcon authored Mar 27, 2024
1 parent c0281de commit 27bbed0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
7 changes: 7 additions & 0 deletions jwt/lib.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 10 additions & 1 deletion jwt/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub struct VerifyOptions {
options: VerifyJwtOptions?;
}

pub struct DecodeOptions {
complete: bool?;
}

pub struct SignOptions {
algorithm: str?;
keyid: str?;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions jwt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jwt/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions jwt/utils.extern.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export default interface extern {
_jwt: () => Promise<IJwt$Inflight>,
}
export interface DecodeOptions {
readonly complete?: (boolean) | undefined;
}
export interface IJwksClientOptions {
readonly jwksUri: string;
}
Expand Down Expand Up @@ -33,6 +36,7 @@ export interface VerifyJwtOptions {
readonly subject?: (string) | undefined;
}
export interface IJwt$Inflight {
readonly decode: (token: string, options?: (DecodeOptions) | undefined) => Promise<Readonly<any>>;
readonly jwksClient: (options: IJwksClientOptions) => Promise<IJwksClient$Inflight>;
readonly sign: (data: Readonly<any>, secret: string, options?: (Readonly<any>) | undefined) => Promise<string>;
readonly verify: (token: string, secret: (arg0: JwtHeader, arg1: (arg0: string, arg1: string) => Promise<void>) => Promise<void>, options?: (VerifyJwtOptions) | undefined) => Promise<Readonly<any>>;
Expand Down
1 change: 1 addition & 0 deletions jwt/utils.mts → jwt/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

0 comments on commit 27bbed0

Please sign in to comment.