Skip to content

Commit

Permalink
feat(crypto-helper): jwt: implement local storage support.
Browse files Browse the repository at this point in the history
refactor(crypto-helper & jwt): move serde module to root. small
refactoring.
  • Loading branch information
TheBestTvarynka committed Mar 1, 2024
1 parent f52be56 commit 3972b84
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/crypto_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod info;
mod input;
mod macros;
mod output;
pub mod serde;

pub use algorithm::Algorithm;
use info::Info;
Expand Down Expand Up @@ -118,7 +117,7 @@ pub fn crypto_helper() -> Html {
let local_storage = use_local_storage::<String>(CRYPTO_HELPER_LOCAL_STORAGE_KEY.to_owned());
use_effect_with_deps(
move |algorithm| {
let algorithm: &Algorithm = &*algorithm;
let algorithm: &Algorithm = algorithm;
local_storage.set(
serde_json::to_string(algorithm).expect("algorithm serialization into json string should never fail"),
);
Expand Down
2 changes: 1 addition & 1 deletion src/crypto_helper/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rsa::pkcs1::{DecodeRsaPrivateKey, DecodeRsaPublicKey};
use rsa::{RsaPrivateKey, RsaPublicKey};
use serde::{Deserialize, Serialize};

use super::serde::*;
use crate::serde::*;

pub const MD5: &str = "MD5";
pub const SHA1: &str = "SHA1";
Expand Down
26 changes: 25 additions & 1 deletion src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::str::FromStr;

use web_sys::{HtmlInputElement, KeyboardEvent};
use yew::{function_component, html, use_effect_with_deps, use_state, Callback, Html, TargetCast};
use yew_hooks::use_location;
use yew_hooks::{use_local_storage, use_location};
use yew_notifications::{use_notification, Notification, NotificationType};

use crate::common::Checkbox;
Expand All @@ -20,6 +20,7 @@ use crate::jwt::jwt_utils::JwtUtils;
use crate::jwt::jwte::Jwte;
use crate::url_query_params;

const JWT_LOCAL_STORAGE_KEY: &str = "JWT_DATA";
const TEST_JWT: &str = "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY3MDAwNDI1NCwiaWF0IjoxNjcwMDA0MjU0fQ.ZGsN42vr-bM4uxXowtlNl7xRerkdKu6i29VS8DFQ4Tw";

#[function_component(Jwt)]
Expand Down Expand Up @@ -86,11 +87,23 @@ pub fn jwt() -> Html {
let jwt_setter = raw_jwt.setter();
let jwte_setter = jwte.setter();
let notifications = use_notification::<Notification>();
let local_storage = use_local_storage::<String>(JWT_LOCAL_STORAGE_KEY.to_owned());
use_effect_with_deps(
move |_: &[(); 0]| {
let query = &location.search;

if query.len() < 2 {
// URL query params is empty. We try to local JWT from local storage.
if let Some(raw_jwt) = (*local_storage).as_ref() {
match serde_json::from_str(raw_jwt.as_str()) {
Ok(jwt) => {
jwte_setter.set(Some(Jwte::Jwt(jwt)));
}
Err(err) => {
error!("Can not load JWT from local storage: {:?}", err);
}
}
}
return;
}

Expand Down Expand Up @@ -125,6 +138,17 @@ pub fn jwt() -> Html {
[],
);

let local_storage = use_local_storage::<String>(JWT_LOCAL_STORAGE_KEY.to_owned());
use_effect_with_deps(
move |jwte| {
let jwte: &Option<Jwte> = jwte;
if let Some(Jwte::Jwt(jwt)) = jwte {
local_storage.set(serde_json::to_string(jwt).expect("JWT serialization should not fail"));
}
},
jwte.clone(),
);

let jwte_setter = jwte.setter();
let set_jwt = Callback::from(move |jwt| {
jwte_setter.set(Some(Jwte::Jwt(jwt)));
Expand Down
5 changes: 4 additions & 1 deletion src/jwt/jwt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::signature::JwtSignatureAlgorithm;
use crate::serde::{deserialize_bytes, serialize_bytes};

pub mod editor;
pub mod viewer;

#[derive(Debug, PartialEq, Eq, Default, Clone)]
#[derive(Debug, PartialEq, Eq, Default, Clone, Serialize, Deserialize)]
pub struct Jwt {
pub raw_header: String,
pub parsed_header: String,
Expand All @@ -15,6 +17,7 @@ pub struct Jwt {

pub raw_signature: String,
pub parsed_signature: String,
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
pub signature: Vec<u8>,
pub signature_algorithm: JwtSignatureAlgorithm,

Expand Down
2 changes: 1 addition & 1 deletion src/jwt/jwt_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn validate_signature(jwt: &Jwt, spawn_notification: Callback<Notification>) ->
Some(jwt.signature == calculated_signature)
}

fn generate_jwt(jwt: &Jwt, spawn_notification: Callback<Notification>) -> Option<Vec<u8>> {
pub fn generate_jwt(jwt: &Jwt, spawn_notification: Callback<Notification>) -> Option<Vec<u8>> {
let signature = calculate_signature(jwt, spawn_notification)?;

let header = base64::encode_config(jwt.parsed_header.as_bytes(), base64::URL_SAFE_NO_PAD);
Expand Down
8 changes: 7 additions & 1 deletion src/jwt/signature.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use std::fmt::{self, Display};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::serde::{deserialize_bytes, serialize_bytes};

const JWT_SIGNATURE_ALGORITHMS: [&str; 10] = [
"HS256", "HS512", "none", "RS256", "HS384", "RS384", "RS512", "ES256", "ES384", "ES512",
];

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum JwtSignatureAlgorithm {
None,

/// HMAC using SHA-256
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
Hs256(Vec<u8>),

/// HMAC using SHA-384
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
Hs384(Vec<u8>),

/// HMAC using SHA-512
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
Hs512(Vec<u8>),

/// RSASSA-PKCS1-v1_5 using SHA-256
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod footer;
mod header;
mod jwt;
mod not_found;
pub mod serde;
mod url_query_params;
mod utils;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/url_query_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::crypto_helper::serde::{deserialize_bytes, serialize_bytes};
use crate::crypto_helper::Algorithm;
use crate::serde::{deserialize_bytes, serialize_bytes};

const APP_HOST: &str = env!("APP_HOST");

Expand Down

0 comments on commit 3972b84

Please sign in to comment.