Skip to content

Commit 35f321e

Browse files
authored
Merge pull request #13 from peddermaster2/cleanup
Update to rust edition 2018
2 parents 724a5be + 11c3820 commit 35f321e

17 files changed

+46
-93
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ repository = "http://github.com/mikkyang/rust-jwt"
99
readme = "README.md"
1010
keywords = ["JWT", "token", "web"]
1111
license = "MIT"
12+
edition = "2018"
1213

1314
[dependencies]
1415
base64 = "0.12"
1516
crypto-mac = "0.8"
1617
digest = "0.9"
1718
hmac = "0.8"
1819
sha2 = "0.9"
19-
serde = "1.0"
20-
serde_derive = "1.0"
20+
serde = { version = "1.0", features = ["derive"] }
2121
serde_json = "1.0"
2222

2323
[dependencies.openssl]

README.md

-20
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ Claims can be any `serde::Serialize` type, usually derived with
2626
`serde_derive`.
2727

2828
```rust
29-
extern crate hmac;
30-
extern crate jwt;
31-
extern crate sha2;
32-
3329
use hmac::{Hmac, NewMac};
3430
use jwt::SignWithKey;
3531
use sha2::Sha256;
@@ -50,10 +46,6 @@ Claims can be any `serde::Deserialize` type, usually derived with
5046
`serde_derive`.
5147

5248
```rust
53-
extern crate hmac;
54-
extern crate jwt;
55-
extern crate sha2;
56-
5749
use hmac::{Hmac, NewMac};
5850
use jwt::VerifyWithKey;
5951
use sha2::Sha256;
@@ -78,10 +70,6 @@ fields, but any type that implements `JoseHeader` can be used.
7870
Both header and claims have to implement `serde::Serialize`.
7971

8072
```rust
81-
extern crate hmac;
82-
extern crate jwt;
83-
extern crate sha2;
84-
8573
use hmac::{Hmac, NewMac};
8674
use jwt::{AlgorithmType, Header, SignWithKey, Token};
8775
use sha2::Sha384;
@@ -105,10 +93,6 @@ assert_eq!(token.as_str(), "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJzb21lb25lIn0.WM_WnPU
10593
Both header and claims have to implement `serde::Deserialize`.
10694

10795
```rust
108-
extern crate hmac;
109-
extern crate jwt;
110-
extern crate sha2;
111-
11296
use hmac::{Hmac, NewMac};
11397
use jwt::{AlgorithmType, Header, Token, VerifyWithKey};
11498
use sha2::Sha384;
@@ -137,10 +121,6 @@ For the trait `VerifyWithStore`, the key id from the deserialized header will be
137121
to use.
138122

139123
```rust
140-
extern crate hmac;
141-
extern crate jwt;
142-
extern crate sha2;
143-
144124
use hmac::{Hmac, NewMac};
145125
use jwt::{Header, SignWithStore, Token, VerifyWithStore};
146126
use sha2::Sha512;

examples/custom_claims.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
extern crate hmac;
2-
extern crate jwt;
3-
#[macro_use]
4-
extern crate serde_derive;
5-
extern crate sha2;
6-
71
use hmac::{Hmac, NewMac};
82
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
3+
use serde::{Deserialize, Serialize};
94
use sha2::Sha256;
10-
use std::default::Default;
115

126
#[derive(Default, Deserialize, Serialize)]
137
struct Custom {

examples/hs256.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
extern crate hmac;
2-
extern crate jwt;
3-
extern crate sha2;
4-
51
use hmac::{Hmac, NewMac};
62
use jwt::{RegisteredClaims, SignWithKey, VerifyWithKey};
73
use sha2::Sha256;
8-
use std::default::Default;
94

105
fn new_token(user_id: &str, password: &str) -> Result<String, &'static str> {
116
// Dummy auth

examples/hs256_legacy.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#![allow(deprecated)]
22

3-
extern crate jwt;
4-
extern crate sha2;
5-
63
use jwt::legacy::{Header, Registered, Token};
74
use sha2::Digest;
85
use sha2::Sha256;
9-
use std::default::Default;
106

117
fn new_token(user_id: &str, password: &str) -> Option<String> {
128
// Dummy auth

src/algorithm/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
//! module. The `none` algorithm is explicitly not supported.
55
//! ## Examples
66
//! ```
7-
//! extern crate hmac;
8-
//! extern crate sha2;
9-
//!
107
//! use hmac::{Hmac, NewMac};
118
//! use sha2::Sha256;
129
//!
1310
//! let hs256_key: Hmac<Sha256> = Hmac::new_varkey(b"some-secret").unwrap();
1411
//! ```
1512
13+
use serde::{Deserialize, Serialize};
14+
1615
use crate::error::Error;
1716

1817
#[cfg(feature = "openssl")]

src/algorithm/openssl.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
//! can only be used for verification.
44
//! ## Examples
55
//! ```
6-
//! extern crate jwt;
7-
//! extern crate openssl;
8-
//!
96
//! use jwt::PKeyWithDigest;
107
//! use openssl::hash::MessageDigest;
118
//! use openssl::pkey::PKey;
@@ -14,12 +11,12 @@
1411
//! digest: MessageDigest::sha256(),
1512
//! key: PKey::public_key_from_pem(pem).unwrap(),
1613
//! };
17-
//!
1814
//! ```
1915
2016
use crate::algorithm::{AlgorithmType, SigningAlgorithm, VerifyingAlgorithm};
2117
use crate::error::Error;
2218
use crate::SEPARATOR;
19+
2320
use openssl::bn::BigNum;
2421
use openssl::ecdsa::EcdsaSig;
2522
use openssl::hash::MessageDigest;
@@ -112,11 +109,12 @@ fn jose_to_der(jose: &[u8]) -> Result<Vec<u8>, Error> {
112109
#[cfg(test)]
113110
mod tests {
114111
use crate::algorithm::openssl::PKeyWithDigest;
115-
use crate::algorithm::AlgorithmType::{self, *};
112+
use crate::algorithm::AlgorithmType::*;
116113
use crate::algorithm::{SigningAlgorithm, VerifyingAlgorithm};
117114
use crate::error::Error;
118115
use crate::header::PrecomputedAlgorithmOnlyHeader as AlgOnly;
119116
use crate::ToBase64;
117+
120118
use openssl::hash::MessageDigest;
121119
use openssl::pkey::PKey;
122120

src/algorithm/rust_crypto.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
//! According to that organization, only hmac is safely implemented at the
33
//! moment.
44
5-
use base64;
65
use crypto_mac::Mac;
76
use digest::generic_array::ArrayLength;
87
use digest::{BlockInput, FixedOutput, Reset, Update};
98
use hmac::Hmac;
10-
use sha2;
119

1210
use crate::algorithm::{AlgorithmType, SigningAlgorithm, VerifyingAlgorithm};
1311
use crate::error::Error;

src/claims.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Convenience structs for commonly defined fields in claims.
22
3-
use serde_json;
43
use std::collections::BTreeMap;
54

5+
use serde::{Deserialize, Serialize};
6+
67
/// Generic [JWT claims](https://tools.ietf.org/html/rfc7519#page-8) with
78
/// defined fields for registered and private claims.
89
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]

src/error.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use self::Error::*;
2-
use crate::algorithm::AlgorithmType;
1+
use std::fmt;
2+
use std::string::FromUtf8Error;
3+
34
use base64::DecodeError;
45
use crypto_mac::{InvalidKeyLength, MacError};
56
use serde_json::Error as JsonError;
6-
use std::fmt;
7-
use std::string::FromUtf8Error;
7+
8+
use self::Error::*;
9+
use crate::algorithm::AlgorithmType;
810

911
#[derive(Debug)]
1012
pub enum Error {
@@ -52,7 +54,7 @@ impl fmt::Display for Error {
5254
impl std::error::Error for Error {}
5355

5456
macro_rules! error_wrap {
55-
($f: ty, $e: expr) => {
57+
($f:ty, $e:expr) => {
5658
impl From<$f> for Error {
5759
fn from(f: $f) -> Error {
5860
$e(f)

src/header.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Convenience structs for commonly defined fields in headers.
22
3+
use std::borrow::Cow;
4+
5+
use serde::{Deserialize, Serialize};
6+
37
use crate::algorithm::AlgorithmType;
48
use crate::error::Error;
59
use crate::ToBase64;
6-
use std::borrow::Cow;
710

811
/// A trait for any header than can conform to the
912
/// [JWT specification](https://tools.ietf.org/html/rfc7519#page-11).

src/legacy/claims.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use serde_json::Value as Json;
21
use std::collections::BTreeMap;
32

3+
use serde::{Deserialize, Serialize};
4+
use serde_json::Value as Json;
5+
46
#[deprecated(note = "Please use jwt::Claims instead")]
57
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
68
pub struct Claims {

src/legacy/header.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::default::Default;
22

3+
use serde::{Deserialize, Serialize};
4+
35
#[deprecated(note = "Please use jwt::Header instead")]
46
#[derive(Debug, PartialEq, Serialize, Deserialize)]
57
pub struct Header {

src/legacy/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Legacy support.
22
3+
use digest::generic_array::ArrayLength;
4+
use digest::*;
5+
use hmac::{Hmac, NewMac};
6+
37
use crate::algorithm::{self, SigningAlgorithm, VerifyingAlgorithm};
48
use crate::error::Error;
59
use crate::token::verified::split_components;
610
use crate::{FromBase64, ToBase64, SEPARATOR};
7-
use digest::generic_array::ArrayLength;
8-
use digest::*;
9-
use hmac::{Hmac, NewMac};
1011

1112
pub use crate::legacy::claims::Claims;
1213
pub use crate::legacy::claims::Registered;

src/lib.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
//! Claims can be any `serde::Serialize` type, usually derived with
99
//! `serde_derive`.
1010
//! ```rust
11-
//! extern crate hmac;
12-
//! extern crate sha2;
13-
//!
1411
//! use hmac::{Hmac, NewMac};
1512
//! use jwt::SignWithKey;
1613
//! use sha2::Sha256;
@@ -31,9 +28,6 @@
3128
//! Claims can be any `serde::Deserialize` type, usually derived with
3229
//! `serde_derive`.
3330
//! ```rust
34-
//! extern crate hmac;
35-
//! extern crate sha2;
36-
//!
3731
//! use hmac::{Hmac, NewMac};
3832
//! use jwt::VerifyWithKey;
3933
//! use sha2::Sha256;
@@ -56,9 +50,6 @@
5650
//! #### Signing
5751
//! Both header and claims have to implement `serde::Serialize`.
5852
//! ```rust
59-
//! extern crate hmac;
60-
//! extern crate sha2;
61-
//!
6253
//! use hmac::{Hmac, NewMac};
6354
//! use jwt::{AlgorithmType, Header, SignWithKey, Token};
6455
//! use sha2::Sha384;
@@ -82,9 +73,6 @@
8273
//! #### Verification
8374
//! Both header and claims have to implement `serde::Deserialize`.
8475
//! ```rust
85-
//! extern crate hmac;
86-
//! extern crate sha2;
87-
//!
8876
//! use hmac::{Hmac, NewMac};
8977
//! use jwt::{AlgorithmType, Header, Token, VerifyWithKey};
9078
//! use sha2::Sha384;
@@ -104,27 +92,15 @@
10492
//! # try_main().unwrap()
10593
//! ```
10694
107-
extern crate base64;
108-
extern crate crypto_mac;
109-
extern crate digest;
110-
#[cfg(doctest)]
111-
#[macro_use]
112-
extern crate doc_comment;
113-
extern crate hmac;
114-
#[cfg(feature = "openssl")]
115-
extern crate openssl;
116-
extern crate serde;
117-
#[macro_use]
118-
extern crate serde_derive;
119-
extern crate serde_json;
120-
extern crate sha2;
121-
12295
#[cfg(doctest)]
12396
doctest!("../README.md");
12497

125-
use serde::{Deserialize, Serialize};
12698
use std::borrow::Cow;
12799

100+
#[cfg(doctest)]
101+
use doc_comment::doctest;
102+
use serde::{Deserialize, Serialize};
103+
128104
#[cfg(feature = "openssl")]
129105
pub use crate::algorithm::openssl::PKeyWithDigest;
130106
pub use crate::algorithm::store::Store;

src/token/signed.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@ impl<H, C> Into<String> for Token<H, C, Signed> {
139139

140140
#[cfg(test)]
141141
mod tests {
142+
use std::collections::BTreeMap;
143+
144+
use hmac::{Hmac, NewMac};
145+
use serde::Serialize;
146+
use sha2::{Sha256, Sha512};
147+
142148
use crate::algorithm::AlgorithmType;
143149
use crate::error::Error;
144150
use crate::header::Header;
145151
use crate::token::signed::{SignWithKey, SignWithStore};
146152
use crate::Token;
147-
use hmac::{Hmac, NewMac};
148-
use sha2::{Sha256, Sha512};
149-
use std::collections::BTreeMap;
150153

151154
#[derive(Serialize)]
152155
struct Claims<'a> {

src/token/verified.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ pub(crate) fn split_components(token: &str) -> Result<[&str; 3], Error> {
144144

145145
#[cfg(test)]
146146
mod tests {
147+
use std::collections::BTreeMap;
148+
149+
use hmac::{Hmac, NewMac};
150+
use serde::Deserialize;
151+
use sha2::{Sha256, Sha512};
152+
147153
use crate::algorithm::VerifyingAlgorithm;
148154
use crate::error::Error;
149155
use crate::token::verified::{VerifyWithKey, VerifyWithStore};
150-
use hmac::{Hmac, NewMac};
151-
use sha2::{Sha256, Sha512};
152-
use std::collections::BTreeMap;
153156

154157
#[derive(Deserialize)]
155158
struct Claims {

0 commit comments

Comments
 (0)