Fix session

This commit is contained in:
eraden 2023-08-17 08:07:11 +02:00
parent f20dbfc14b
commit a50cff494b
2 changed files with 70 additions and 2 deletions

View File

@ -1,3 +1,66 @@
//! General purpose JWT session validator for actix_web
//!
//! It's designed to extract session using middleware and validate path simply by using extractors.
//!
//! Examples:
//!
//! ```
//! use actix_jwt_session::*;
//! use actix_web::get;
//!
//! #[tokio::main]
//! async fn main() {
//! let redis = {
//! use redis_async_pool::{RedisConnectionManager, RedisPool};
//! RedisPool::new(
//! RedisConnectionManager::new(
//! redis::Client::open("redis://localhost:6379").expect("Fail to connect to redis"),
//! true,
//! None,
//! ),
//! 5,
//! )
//! };
//!
//! let keys = JwtSigningKeys::generate().unwrap();
//! let factory = RedisMiddlewareFactory::<Claims>::new(
//! Arc::new(keys.encoding_key),
//! Arc::new(keys.decoding_key),
//! Algorithm::EdDSA,
//! redis.clone(),
//! vec![Box::new(HeaderExtractor::new())]
//! );
//!
//! HttpServer::new(move || {
//! let app = App::new()
//! .app_data(factory.storage())
//! .wrap(factory.clone())
//! .app_data(Data::new(redis.clone()))
//! .service(sign_in)
//! .service(sign_out)
//! .service(session)
//! .service(root);
//! }
//!
//! pub struct JwtSigningKeys {
//! encoding_key: EncodingKey,
//! decoding_key: DecodingKey,
//! }
//!
//! impl JwtSigningKeys {
//! fn generate() -> Result<Self, Box<dyn std::error::Error>> {
//! let doc = Ed25519KeyPair::generate_pkcs8(&SystemRandom::new())?;
//! let keypair = Ed25519KeyPair::from_pkcs8(doc.as_ref())?;
//! let encoding_key = EncodingKey::from_ed_der(doc.as_ref());
//! let decoding_key = DecodingKey::from_ed_der(keypair.public_key().as_ref());
//! Ok(JwtSigningKeys {
//! encoding_key,
//! decoding_key,
//! })
//! }
//! }
//! ```
use actix_web::{dev::ServiceRequest, HttpResponse};
use actix_web::{FromRequest, HttpMessage};
use async_trait::async_trait;

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use actix_jwt_session::{Authenticated, RedisMiddlewareFactory, RedisStorage, TokenStorage};
use actix_jwt_session::{Authenticated, RedisMiddlewareFactory, RedisStorage, TokenStorage, HeaderExtractor};
use actix_web::http::StatusCode;
use actix_web::web::{Data, Json};
use actix_web::HttpResponse;
@ -15,12 +15,16 @@ use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Claims {
id: Uuid,
subject: String,
}
impl actix_jwt_session::Claims for Claims {
fn jti(&self) -> Uuid {
self.id
}
fn subject(&self) -> &str {
&self.subject
}
}
#[tokio::test(flavor = "multi_thread")]
@ -43,6 +47,7 @@ async fn not_authenticated() {
Arc::new(keys.decoding_key),
Algorithm::EdDSA,
redis.clone(),
vec![Box::new(HeaderExtractor::new())]
);
let app = App::new()
@ -75,7 +80,7 @@ async fn not_authenticated() {
.await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
let origina_claims = Claims { id: Uuid::new_v4() };
let origina_claims = Claims { id: Uuid::new_v4(), subject: "foo".to_string() };
let res = test::call_service(
&app,
test::TestRequest::default()