Add actix-jwt-session working docs
This commit is contained in:
parent
6792a71820
commit
2852f89895
@ -4,9 +4,17 @@
|
|||||||
//!
|
//!
|
||||||
//! Examples:
|
//! Examples:
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```no_run
|
||||||
|
//! use std::boxed::Box;
|
||||||
|
//! use std::sync::Arc;
|
||||||
//! use actix_jwt_session::*;
|
//! use actix_jwt_session::*;
|
||||||
//! use actix_web::get;
|
//! 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]
|
//! #[tokio::main]
|
||||||
//! async fn main() {
|
//! async fn main() {
|
||||||
@ -23,7 +31,7 @@
|
|||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! let keys = JwtSigningKeys::generate().unwrap();
|
//! let keys = JwtSigningKeys::generate().unwrap();
|
||||||
//! let factory = RedisMiddlewareFactory::<Claims>::new(
|
//! let factory = RedisMiddlewareFactory::<AppClaims>::new(
|
||||||
//! Arc::new(keys.encoding_key),
|
//! Arc::new(keys.encoding_key),
|
||||||
//! Arc::new(keys.decoding_key),
|
//! Arc::new(keys.decoding_key),
|
||||||
//! Algorithm::EdDSA,
|
//! Algorithm::EdDSA,
|
||||||
@ -39,19 +47,47 @@
|
|||||||
//! HttpServer::new(move || {
|
//! HttpServer::new(move || {
|
||||||
//! let factory = factory.clone();
|
//! let factory = factory.clone();
|
||||||
//! App::new()
|
//! App::new()
|
||||||
//! .app_data(factory.storage())
|
//! .app_data(Data::new(factory.storage()))
|
||||||
//! .wrap(factory)
|
//! .wrap(factory)
|
||||||
//! .app_data(Data::new(redis.clone()))
|
//! .app_data(Data::new(redis.clone()))
|
||||||
//! .service(sign_in)
|
//! .service(storage_access)
|
||||||
//! .service(sign_out)
|
//! .service(must_be_signed_in)
|
||||||
//! .service(session)
|
//! .service(may_be_signed_in)
|
||||||
//! .service(root);
|
|
||||||
//! })
|
//! })
|
||||||
//! .bind(("0.0.0.0", 8080))?
|
//! .bind(("0.0.0.0", 8080)).unwrap()
|
||||||
//! .run()
|
//! .run()
|
||||||
//! .await.unwrap();
|
//! .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 {
|
//! pub struct JwtSigningKeys {
|
||||||
//! encoding_key: EncodingKey,
|
//! encoding_key: EncodingKey,
|
||||||
//! decoding_key: DecodingKey,
|
//! decoding_key: DecodingKey,
|
||||||
|
@ -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() {}
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::sync::Arc;
|
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::http::StatusCode;
|
||||||
use actix_web::web::{Data, Json};
|
use actix_web::web::{Data, Json};
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
@ -51,7 +51,7 @@ async fn not_authenticated() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let app = App::new()
|
let app = App::new()
|
||||||
.app_data(factory.storage())
|
.app_data(Data::new(factory.storage()))
|
||||||
.wrap(factory.clone())
|
.wrap(factory.clone())
|
||||||
.app_data(Data::new(redis.clone()))
|
.app_data(Data::new(redis.clone()))
|
||||||
.service(sign_in)
|
.service(sign_in)
|
||||||
@ -96,7 +96,7 @@ async fn not_authenticated() {
|
|||||||
|
|
||||||
#[post("/in")]
|
#[post("/in")]
|
||||||
async fn sign_in(
|
async fn sign_in(
|
||||||
store: Data<RedisStorage<Claims>>,
|
store: Data<SessionStorage<Claims>>,
|
||||||
claims: Json<Claims>,
|
claims: Json<Claims>,
|
||||||
) -> Result<HttpResponse, actix_web::Error> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
let claims = claims.into_inner();
|
let claims = claims.into_inner();
|
||||||
@ -110,7 +110,7 @@ async fn sign_in(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/out")]
|
#[post("/out")]
|
||||||
async fn sign_out(_store: Data<RedisStorage<Claims>>) -> HttpResponse {
|
async fn sign_out(_store: Data<SessionStorage<Claims>>) -> HttpResponse {
|
||||||
HttpResponse::Ok().body("")
|
HttpResponse::Ok().body("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user