Use serde with derive feature

This commit is contained in:
Vincent Prouillet 2019-11-03 15:46:08 +00:00
parent 210e96063d
commit 417e00780d
10 changed files with 19 additions and 40 deletions

View File

@ -12,8 +12,7 @@ edition = "2018"
[dependencies]
serde_json = "1.0"
serde_derive = "1.0"
serde = "1.0"
serde = {version = "1.0", features = ["derive"] }
ring = { version = "0.16.5", features = ["std"] }
base64 = "0.11"
# For validation

View File

@ -1,11 +1,6 @@
extern crate jsonwebtoken as jwt;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate chrono;
use chrono::prelude::*;
use jwt::{Header, Key, Validation};
use jsonwebtoken::{Header, Key, Validation};
use serde::{Deserialize, Serialize};
const SECRET: &str = "some-secret";
@ -44,9 +39,6 @@ mod jwt_numeric_date {
#[cfg(test)]
mod tests {
use super::*;
use jwt::{Header, Validation};
const EXPECTED_TOKEN: &str = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJDdXN0b20gRGF0ZVRpbWUgc2VyL2RlIiwiaWF0IjowLCJleHAiOjMyNTAzNjgwMDAwfQ.RTgha0S53MjPC2pMA4e2oMzaBxSY3DMjiYR2qFfV55A";
use super::super::{Claims, SECRET};
@ -59,13 +51,13 @@ mod jwt_numeric_date {
let claims = Claims { sub: sub.clone(), iat, exp };
let token = jwt::encode(&Header::default(), &claims, Key::Hmac(SECRET.as_ref()))
let token = encode(&Header::default(), &claims, Key::Hmac(SECRET.as_ref()))
.expect("Failed to encode claims");
assert_eq!(&token, EXPECTED_TOKEN);
let decoded =
jwt::decode::<Claims>(&token, Key::Hmac(SECRET.as_ref()), &Validation::default())
decode::<Claims>(&token, Key::Hmac(SECRET.as_ref()), &Validation::default())
.expect("Failed to decode token");
assert_eq!(decoded.claims, claims);
@ -77,7 +69,7 @@ mod jwt_numeric_date {
let overflow_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJDdXN0b20gRGF0ZVRpbWUgc2VyL2RlIiwiaWF0IjowLCJleHAiOjkyMjMzNzIwMzY4NTQ3NzYwMDB9.G2PKreA27U8_xOwuIeCYXacFYeR46f9FyENIZfCrvEc";
let decode_result =
jwt::decode::<Claims>(&overflow_token, SECRET.as_ref(), &Validation::default());
decode::<Claims>(&overflow_token, SECRET.as_ref(), &Validation::default());
assert!(decode_result.is_err());
}
@ -91,12 +83,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let claims = Claims { sub: sub.clone(), iat, exp };
let token = jwt::encode(&Header::default(), &claims, Key::Hmac(SECRET.as_ref()))?;
let token = jsonwebtoken::encode(&Header::default(), &claims, Key::Hmac(SECRET.as_ref()))?;
println!("serialized token: {}", &token);
let token_data =
jwt::decode::<Claims>(&token, Key::Hmac(SECRET.as_ref()), &Validation::default())?;
jsonwebtoken::decode::<Claims>(&token, Key::Hmac(SECRET.as_ref()), &Validation::default())?;
println!("token data:\n{:#?}", &token_data);
Ok(())

View File

@ -1,9 +1,7 @@
extern crate jsonwebtoken as jwt;
#[macro_use]
extern crate serde_derive;
use serde::{Deserialize, Serialize};
use jwt::errors::ErrorKind;
use jwt::{decode, encode, Algorithm, Header, Key, Validation};
use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{decode, encode, Algorithm, Header, Key, Validation};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {

View File

@ -1,9 +1,6 @@
extern crate jsonwebtoken as jwt;
#[macro_use]
extern crate serde_derive;
use jwt::errors::ErrorKind;
use jwt::{decode, encode, Header, Key, Validation};
use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{decode, encode, Header, Key, Validation};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {

View File

@ -1,4 +1,5 @@
use crate::errors::{new_error, Error, ErrorKind, Result};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
/// The algorithms supported for signing/verifying

View File

@ -1,4 +1,5 @@
use crate::algorithms::Algorithm;
use serde::{Deserialize, Serialize};
/// A basic JWT header, the alg defaults to HS256 and typ is automatically
/// set to `JWT`. All the other fields are optional.

View File

@ -3,15 +3,6 @@
//! Documentation: [stable](https://docs.rs/jsonwebtoken/)
#![deny(missing_docs)]
#[macro_use]
extern crate serde_derive;
extern crate base64;
extern crate chrono;
extern crate ring;
extern crate serde;
extern crate serde_json;
extern crate simple_asn1;
mod algorithms;
mod crypto;
/// All the errors

View File

@ -1,6 +1,6 @@
use chrono::Utc;
use jsonwebtoken::{decode, decode_pem, encode, sign, verify, Algorithm, Header, Key, Validation};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Claims {

View File

@ -3,7 +3,7 @@ use jsonwebtoken::{
dangerous_unsafe_decode, decode, decode_header, encode, sign, verify, Algorithm, Header, Key,
Validation,
};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Claims {

View File

@ -1,6 +1,6 @@
use chrono::Utc;
use jsonwebtoken::{decode, decode_pem, encode, sign, verify, Algorithm, Header, Key, Validation};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
const RSA_ALGORITHMS: &[Algorithm] = &[
Algorithm::RS256,