Skip to content

Commit

Permalink
fix: algorithm transformation & decode header fn
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 24, 2024
1 parent 4f7dd78 commit a9b3d4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
20 changes: 20 additions & 0 deletions packages/jsonwebtoken/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ impl From<Algorithm> for jsonwebtoken::Algorithm {
}
}

impl From<jsonwebtoken::Algorithm> for Algorithm {
#[inline]
fn from(value: jsonwebtoken::Algorithm) -> Self {
match value {
jsonwebtoken::Algorithm::ES256 => Algorithm::ES256,
jsonwebtoken::Algorithm::ES384 => Algorithm::ES384,
jsonwebtoken::Algorithm::EdDSA => Algorithm::EdDSA,
jsonwebtoken::Algorithm::HS256 => Algorithm::HS256,
jsonwebtoken::Algorithm::HS384 => Algorithm::HS384,
jsonwebtoken::Algorithm::HS512 => Algorithm::HS512,
jsonwebtoken::Algorithm::PS256 => Algorithm::PS256,
jsonwebtoken::Algorithm::PS384 => Algorithm::PS384,
jsonwebtoken::Algorithm::PS512 => Algorithm::PS512,
jsonwebtoken::Algorithm::RS256 => Algorithm::RS256,
jsonwebtoken::Algorithm::RS384 => Algorithm::RS384,
jsonwebtoken::Algorithm::RS512 => Algorithm::RS512,
}
}
}

impl Default for Algorithm {
fn default() -> Self {
Self::HS256
Expand Down
5 changes: 3 additions & 2 deletions packages/jsonwebtoken/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use napi_derive::napi;
use crate::header::Header;

#[napi]
pub fn decode_header(token: &str) -> Header {
pub fn decode_header(token: String) -> Header {
let result = jsonwebtoken::decode_header(&token);

let header = Header::from(result.unwrap());
let header = result.unwrap().into();
return header;
}
6 changes: 3 additions & 3 deletions packages/jsonwebtoken/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ impl From<&Header> for jsonwebtoken::Header {
}
}

impl From<&jsonwebtoken::Header> for Header {
impl From<jsonwebtoken::Header> for Header {
#[inline]
fn from(value: &jsonwebtoken::Header) -> Header {
fn from(value: jsonwebtoken::Header) -> Header {
Header {
algorithm: value.alg.clone(),
algorithm: Algorithm::from(value.alg.clone()).into(),
content_type: value.cty.clone(),
json_key_url: value.jku.clone(),
key_id: value.kid.clone(),
Expand Down

0 comments on commit a9b3d4f

Please sign in to comment.