Skip to content

Commit

Permalink
Switch from chrono::Duration to std::time::Duration.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvanstraten committed Jul 27, 2023
1 parent 6082ea8 commit 56e6a44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["derive", "./."]

[package]
name = "actix-jwt-auth-middleware"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Michael van Straten"]
repository = "https://github.com/michaelvanstraten/actix-jwt-auth-middleware"
Expand Down
25 changes: 14 additions & 11 deletions src/token_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::AuthError;
use crate::AuthResult;

use std::marker::PhantomData;
use std::time::Duration;

use actix_web::cookie::Cookie;
use actix_web::http::header::HeaderValue;
use chrono::Duration;
use chrono::Duration as ChronoDuration;
use derive_builder::Builder;
use jwt_compact::Algorithm;
use jwt_compact::AlgorithmExt;
Expand All @@ -21,11 +22,11 @@ use serde::Serialize;
For example, the [`crate::Authority`] uses it to automatically refresh the `access`/`refresh` token.
# Example
```rust
# use std::time::Duration;
# use actix_jwt_auth_middleware::{TokenSigner, AuthError};
# use serde::Serialize;
# use exonum_crypto::KeyPair;
# use jwt_compact::{alg::Ed25519, TimeOptions};
# use chrono::Duration;
#[derive(Serialize, Clone)]
struct User {
id: u32
Expand All @@ -37,9 +38,9 @@ use serde::Serialize;
.signing_key(key_pair.secret_key().clone())
.access_token_name("my_access_token")
// makes every refresh token generated be valid for 2 hours
.refresh_token_lifetime(Duration::minutes(120))
.refresh_token_lifetime(Duration::from_secs(120 * 60))
// generated tokens can still be used up to 10 seconds after they expired
.time_options(TimeOptions::from_leeway(Duration::seconds(10)))
.time_options(TimeOptions::from_leeway(chrono::Duration::seconds(10)))
.algorithm(Ed25519)
.build().unwrap();
Expand Down Expand Up @@ -69,9 +70,9 @@ where
/**
The lifetime duration of the access token.
Defaults to `Duration::seconds(60)`
Defaults to `Duration::from_secs(60)`
*/
#[builder(default = "Duration::seconds(60)")]
#[builder(default = "Duration::from_secs(60)")]
access_token_lifetime: Duration,
/**
The name of the future refresh tokens.
Expand All @@ -86,9 +87,9 @@ where
/**
The lifetime duration of the refresh token.
Defaults to `Duration::minutes(30)`
Defaults to `Duration::from_secs(30 * 60)`
*/
#[builder(default = "Duration::minutes(30)")]
#[builder(default = "Duration::from_secs(30 * 60)")]
refresh_token_lifetime: Duration,
/**
JWT Header used in the creation of access and refresh tokens.
Expand Down Expand Up @@ -117,7 +118,7 @@ where
Defaults to `TimeOptions::from_leeway(Duration::seconds(0))`
*/
#[builder(default = "TimeOptions::from_leeway(Duration::seconds(0))")]
#[builder(default = "TimeOptions::from_leeway(ChronoDuration::seconds(0))")]
pub(crate) time_options: TimeOptions,
#[doc(hidden)]
#[builder(setter(skip), default = "PhantomData")]
Expand Down Expand Up @@ -263,8 +264,10 @@ where
claims: &Claims,
token_lifetime: Duration,
) -> AuthResult<String> {
let token_claims =
TokenClaims::new(claims).set_duration_and_issuance(&self.time_options, token_lifetime);
let token_claims = TokenClaims::new(claims).set_duration_and_issuance(
&self.time_options,
ChronoDuration::from_std(token_lifetime).unwrap(),
);

self.algorithm
.token(self.header.clone(), &token_claims, &self.signing_key)
Expand Down

0 comments on commit 56e6a44

Please sign in to comment.