Add actix-jwt-session working docs

This commit is contained in:
eraden 2023-08-17 20:50:27 +02:00
parent 6792a71820
commit 2852f89895
3 changed files with 48 additions and 31 deletions

View File

@ -4,9 +4,17 @@
//!
//! Examples:
//!
//! ```
//! ```no_run
//! use std::boxed::Box;
//! use std::sync::Arc;
//! use actix_jwt_session::*;
//! use actix_web::get;
//! use actix_web::web::Data;
//! use actix_web::{HttpResponse, App, HttpServer};
//! use ring::rand::SystemRandom;
//! use ring::signature::{Ed25519KeyPair, KeyPair};
//! use jsonwebtoken::*;
//! use serde::{Serialize, Deserialize};
//!
//! #[tokio::main]
//! async fn main() {
@ -23,7 +31,7 @@
//! };
//!
//! let keys = JwtSigningKeys::generate().unwrap();
//! let factory = RedisMiddlewareFactory::<Claims>::new(
//! let factory = RedisMiddlewareFactory::<AppClaims>::new(
//! Arc::new(keys.encoding_key),
//! Arc::new(keys.decoding_key),
//! Algorithm::EdDSA,
@ -39,19 +47,47 @@
//! HttpServer::new(move || {
//! let factory = factory.clone();
//! App::new()
//! .app_data(factory.storage())
//! .app_data(Data::new(factory.storage()))
//! .wrap(factory)
//! .app_data(Data::new(redis.clone()))
//! .service(sign_in)
//! .service(sign_out)
//! .service(session)
//! .service(root);
//! .service(storage_access)
//! .service(must_be_signed_in)
//! .service(may_be_signed_in)
//! })
//! .bind(("0.0.0.0", 8080))?
//! .bind(("0.0.0.0", 8080)).unwrap()
//! .run()
//! .await.unwrap();
//! }
//!
//! #[derive(Clone, PartialEq, Serialize, Deserialize)]
//! pub struct AppClaims {
//! id: uuid::Uuid,
//! subject: String,
//! }
//!
//! impl Claims for AppClaims {
//! fn jti(&self) -> uuid::Uuid { self.id }
//! fn subject(&self) -> &str { &self.subject }
//! }
//!
//! #[get("/access-storage")]
//! async fn storage_access(session_store: Data<SessionStorage<AppClaims>>) -> HttpResponse {
//! HttpResponse::Ok().body("")
//! }
//!
//! #[get("/authorized")]
//! async fn must_be_signed_in(session: Authenticated<AppClaims>) -> HttpResponse {
//! let jit = session.jti();
//! HttpResponse::Ok().body("")
//! }
//!
//! #[get("/maybe-authorized")]
//! async fn may_be_signed_in(session: MaybeAuthenticated<AppClaims>) -> HttpResponse {
//! if let Some(session) = session.into_option() {
//! }
//! HttpResponse::Ok().body("")
//! }
//!
//! pub struct JwtSigningKeys {
//! encoding_key: EncodingKey,
//! decoding_key: DecodingKey,

View File

@ -195,22 +195,3 @@ where
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Out {
id: uuid::Uuid,
}
impl Claims for Out {
fn jti(&self) -> uuid::Uuid {
self.id
}
}
#[tokio::test]
async fn extract() {}
}

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use actix_jwt_session::{Authenticated, RedisMiddlewareFactory, RedisStorage, TokenStorage, HeaderExtractor};
use actix_jwt_session::{Authenticated, RedisMiddlewareFactory, HeaderExtractor, SessionStorage};
use actix_web::http::StatusCode;
use actix_web::web::{Data, Json};
use actix_web::HttpResponse;
@ -51,7 +51,7 @@ async fn not_authenticated() {
);
let app = App::new()
.app_data(factory.storage())
.app_data(Data::new(factory.storage()))
.wrap(factory.clone())
.app_data(Data::new(redis.clone()))
.service(sign_in)
@ -96,7 +96,7 @@ async fn not_authenticated() {
#[post("/in")]
async fn sign_in(
store: Data<RedisStorage<Claims>>,
store: Data<SessionStorage<Claims>>,
claims: Json<Claims>,
) -> Result<HttpResponse, actix_web::Error> {
let claims = claims.into_inner();
@ -110,7 +110,7 @@ async fn sign_in(
}
#[post("/out")]
async fn sign_out(_store: Data<RedisStorage<Claims>>) -> HttpResponse {
async fn sign_out(_store: Data<SessionStorage<Claims>>) -> HttpResponse {
HttpResponse::Ok().body("")
}