Skip to content

Commit 153a924

Browse files
committed
fix: minor adjustments & add test suite
1 parent d46dfb7 commit 153a924

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

packages/jsonwebtoken/__tests__/jsonwebtoken.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'node:path'
44
import test from 'ava'
55
import { decode as nodeJwtDecode } from 'jsonwebtoken'
66

7-
import { Algorithm, sign, signSync, verifySync, verify } from '../index.js'
7+
import { Algorithm, sign, signSync, verifySync, verify, decodeHeader } from '../index.js'
88

99
const getUtcTimestamp = () => Math.floor(new Date().getTime() / 1000)
1010
const oneDayInSeconds = 86400
@@ -30,6 +30,20 @@ test('signSync and sign (async) should produce the same result', async (t) => {
3030
t.truthy(nodeJwtDecode(resAsync))
3131
})
3232

33+
test('should decode header', async (t) => {
34+
const data = {
35+
id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
36+
}
37+
const claims = { data, exp: getUtcTimestamp() + oneDayInSeconds }
38+
const secretKey = 'secret'
39+
const headers = { algorithm: Algorithm.HS384 }
40+
41+
const token = await sign(claims, secretKey, headers)
42+
43+
const header = decodeHeader(token);
44+
t.is(header.algorithm, Algorithm.HS384);
45+
})
46+
3347
test('verify should return the decoded claims', async (t) => {
3448
const data = {
3549
id: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6',

packages/jsonwebtoken/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ export function verify(token: string, key: string | Buffer, validation?: Validat
160160

161161
export function verifySync(token: string, key: string | Buffer, validation?: Validation | undefined | null): Claims
162162

163+
export function decodeHeader(token: string) : Header;

packages/jsonwebtoken/src/decode.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use napi::bindgen_prelude::*;
21
use napi_derive::napi;
32

43
use crate::header::Header;

packages/jsonwebtoken/src/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ impl From<&Header> for jsonwebtoken::Header {
7373
}
7474
}
7575

76-
impl From<jsonwebtoken::Header> for Header {
76+
impl From<&jsonwebtoken::Header> for Header {
7777
#[inline]
7878
fn from(value: &jsonwebtoken::Header) -> Header {
7979
Header {
80-
algorithm: value.alg.into(),
80+
algorithm: value.alg.clone(),
8181
content_type: value.cty.clone(),
8282
json_key_url: value.jku.clone(),
8383
key_id: value.kid.clone(),

0 commit comments

Comments
 (0)