diff --git a/actix-web-grants/Cargo.toml b/actix-web-grants/Cargo.toml index d770033..5bbd9d8 100644 --- a/actix-web-grants/Cargo.toml +++ b/actix-web-grants/Cargo.toml @@ -24,6 +24,9 @@ actix-web = { version = "4.3", default-features = false, features = ["macros"] } protect-endpoints-proc-macro = { workspace = true, features = ["actix-web"], optional = true } [dev-dependencies] +actix-web-httpauth = "0.8.0" actix-rt = "2" serde = { version = "1.0", features = ["derive"] } parse-display = "0.8.2" +jsonwebtoken = "9.1.0" +chrono = "0.4.19" diff --git a/actix-web-grants/README.md b/actix-web-grants/README.md index 0abd36a..cfa0e1d 100644 --- a/actix-web-grants/README.md +++ b/actix-web-grants/README.md @@ -36,7 +36,7 @@ App::new() .wrap(GrantsMiddleware::with_extractor(extract)) ``` -> Steps 1 and 2 can be replaced by custom middleware or integration with another libraries. Take a look at an [jwt-httpauth example](../examples/actix-web/jwt-httpauth/src/main.rs) +> Steps 1 and 2 can be replaced by custom middleware or integration with another libraries. Take a look at an [jwt-httpauth example](examples/jwt-httpauth/main.rs) 3. Protect your endpoints in any convenient way from the examples below: @@ -60,7 +60,7 @@ Here is an example using the `ty` and `expr` attributes. But these are independe `expr` allows you to include some checks in the macro based on function params, it can be combined with authorities by using `all`/`any`. `ty` allows you to use a custom type for th authorities (then the middleware needs to be configured). -Take a look at an [enum-role example](../examples/actix-web/enum-role/src/main.rs) +Take a look at an [enum-role example](examples/enum-role/main.rs) ```rust,ignore use enums::Role::{self, ADMIN}; @@ -142,6 +142,6 @@ You can find more [`examples`] in the git repository folder and [`documentation` * For `actix-web-grants: 2.*` supported version of `actix-web` is `3.*` * For `actix-web-grants: 3.*` & `4.*` supported version of `actix-web` is `4.*` -[`actix-web-httpauth`]: https://github.com/DDtKey/protect-endpoints/blob/main/examples/actix-web/jwt-httpauth -[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/actix-web +[`actix-web-httpauth`]: https://github.com/DDtKey/protect-endpoints/blob/main/actix-web-grants/examples/jwt-httpauth +[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/actix-web-gratns/examples [`documentation`]: https://docs.rs/actix-web-grants diff --git a/examples/actix-web/base_example.rs b/actix-web-grants/examples/base_example.rs similarity index 90% rename from examples/actix-web/base_example.rs rename to actix-web-grants/examples/base_example.rs index 8792196..c16a87e 100644 --- a/examples/actix-web/base_example.rs +++ b/actix-web-grants/examples/base_example.rs @@ -1,7 +1,8 @@ use actix_web::dev::ServiceRequest; use actix_web::{get, middleware, web, App, Error, HttpResponse, HttpServer}; +use std::collections::HashSet; -use actix_web_grants::authorites::{AuthDetails, AuthoritesCheck}; +use actix_web_grants::authorities::{AuthDetails, AuthoritiesCheck}; use actix_web_grants::{protect, AuthorityGuard, GrantsMiddleware}; const ROLE_ADMIN: &str = "ROLE_ADMIN"; @@ -61,10 +62,10 @@ async fn main() -> std::io::Result<()> { } // You can use both &ServiceRequest and &mut ServiceRequest -async fn extract(_req: &mut ServiceRequest) -> Result, Error> { +async fn extract(_req: &mut ServiceRequest) -> Result, Error> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Ok(vec![ROLE_ADMIN.to_string()]) + Ok(HashSet::from([ROLE_ADMIN.to_string()])) } diff --git a/examples/actix-web/enum-role/src/main.rs b/actix-web-grants/examples/enum-role/main.rs similarity index 92% rename from examples/actix-web/enum-role/src/main.rs rename to actix-web-grants/examples/enum-role/main.rs index f6e70db..01ad6f4 100644 --- a/examples/actix-web/enum-role/src/main.rs +++ b/actix-web-grants/examples/enum-role/main.rs @@ -3,6 +3,7 @@ use actix_web::dev::ServiceRequest; use actix_web::{get, middleware, web, App, Error, HttpResponse, HttpServer}; use actix_web_grants::authorities::{AuthDetails, AuthoritiesCheck}; use actix_web_grants::{protect, AuthorityGuard, GrantsMiddleware}; +use std::collections::HashSet; mod role; @@ -46,10 +47,10 @@ async fn main() -> std::io::Result<()> { } // You can specify any of your own type (`PartialEq` + `Clone`) for the return type wrapped in a vector: Result, Error> -async fn extract(_req: &mut ServiceRequest) -> Result, Error> { +async fn extract(_req: &mut ServiceRequest) -> Result, Error> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Ok(vec![Role::Admin]) + Ok(HashSet::from([Role::Admin])) } diff --git a/examples/actix-web/enum-role/src/role.rs b/actix-web-grants/examples/enum-role/role.rs similarity index 70% rename from examples/actix-web/enum-role/src/role.rs rename to actix-web-grants/examples/enum-role/role.rs index 66175e9..17d76b2 100644 --- a/examples/actix-web/enum-role/src/role.rs +++ b/actix-web-grants/examples/enum-role/role.rs @@ -1,5 +1,5 @@ // `Eq` and `Hash` is required -#[derive(Eq, Hash)] +#[derive(Eq, PartialEq, Hash)] pub enum Role { Admin, Manager, diff --git a/examples/actix-web/jwt-httpauth/src/claims.rs b/actix-web-grants/examples/jwt-httpauth/claims.rs similarity index 100% rename from examples/actix-web/jwt-httpauth/src/claims.rs rename to actix-web-grants/examples/jwt-httpauth/claims.rs diff --git a/examples/actix-web/jwt-httpauth/src/main.rs b/actix-web-grants/examples/jwt-httpauth/main.rs similarity index 100% rename from examples/actix-web/jwt-httpauth/src/main.rs rename to actix-web-grants/examples/jwt-httpauth/main.rs diff --git a/actix-web-grants/src/lib.rs b/actix-web-grants/src/lib.rs index 7436be3..9e031c4 100644 --- a/actix-web-grants/src/lib.rs +++ b/actix-web-grants/src/lib.rs @@ -13,7 +13,7 @@ //! //! [`GrantsMiddleware`]: GrantsMiddleware //! [`httpauth`]: https://docs.rs/actix-web-httpauth -//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/actix-web +//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/actix-web-grants/examples //! [`authorities`]: authorities //! [`proc-macro`]: proc_macro //! [`AuthorityGuard`]: AuthorityGuard diff --git a/examples/actix-web/enum-role/Cargo.toml b/examples/actix-web/enum-role/Cargo.toml deleted file mode 100644 index d070b37..0000000 --- a/examples/actix-web/enum-role/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "enum-role" -version = "0.1.0" -description = "Example using actix-web-grants with enum role type" -authors = ["DDtKey "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -actix-web = { version = "4.0" } -actix-web-grants = { path = "../../" } diff --git a/examples/actix-web/jwt-httpauth/Cargo.toml b/examples/actix-web/jwt-httpauth/Cargo.toml deleted file mode 100644 index 4111a0e..0000000 --- a/examples/actix-web/jwt-httpauth/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "jwt-httpauth" -version = "0.1.0" -description = "Example using actix-web-grants with actix-web-httpauth via JWT authorization" -authors = ["DDtKey "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -actix-web = { version = "4.1.0" } -actix-web-grants = { path = "../../" } -actix-web-httpauth = "0.8.0" -jsonwebtoken = "8" -serde = {version = "1", features = ["derive"] } -chrono = "0.4.19" diff --git a/examples/poem/enum-role/Cargo.toml b/examples/poem/enum-role/Cargo.toml deleted file mode 100644 index 72c6d71..0000000 --- a/examples/poem/enum-role/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "enum-role" -version = "0.1.0" -description = "Example using poem-grants with enum role type" -authors = ["DDtKey "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -poem = "1" -poem-grants = { path = "../../" } -tokio = "1.34.0" diff --git a/examples/poem/jwt-auth/Cargo.toml b/examples/poem/jwt-auth/Cargo.toml deleted file mode 100644 index 45cafb6..0000000 --- a/examples/poem/jwt-auth/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "jwt-auth" -version = "0.1.0" -description = "Example using poem-grants with JWT auth middleware" -authors = ["DDtKey "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -chrono = "0.4.19" -jsonwebtoken = "7" -poem = "1" -poem-grants = { path = "../../" } -serde = {version = "1.0", features = ["derive"] } -tokio = "1.34.0" diff --git a/examples/rocket/enum-role/Cargo.toml b/examples/rocket/enum-role/Cargo.toml deleted file mode 100644 index 5ffae15..0000000 --- a/examples/rocket/enum-role/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "enum-role" -version = "0.1.0" -description = "Example using rocket-grants with enum role type" -authors = ["DDtKey "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -rocket = "0.5.0" -rocket-grants = { path = "../../" } -tokio = "1.34.0" diff --git a/examples/rocket/jwt-auth/Cargo.toml b/examples/rocket/jwt-auth/Cargo.toml deleted file mode 100644 index 97b8ba0..0000000 --- a/examples/rocket/jwt-auth/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "jwt-auth" -version = "0.1.0" -description = "Example using rocket-grants with JWT auth" -authors = ["DDtKey "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -chrono = "0.4.19" -jsonwebtoken = "7" -rocket = { version = "0.5.0", features = ["json"] } -rocket-grants = { path = "../../../rocket-grants" } -serde = {version = "1.0", features = ["derive"] } -tokio = "1.34.0" diff --git a/poem-grants/Cargo.toml b/poem-grants/Cargo.toml index cbe6b80..87ba6d5 100644 --- a/poem-grants/Cargo.toml +++ b/poem-grants/Cargo.toml @@ -29,3 +29,5 @@ poem = {version = "1", features = ["test"]} poem-openapi = "3.0.6" serde = {version = "1.0", features = ["derive"]} tokio = {version = "1.34.0", features = ["rt-multi-thread"]} +jsonwebtoken = "9.1.0" +chrono = "0.4.19" diff --git a/poem-grants/README.md b/poem-grants/README.md index 91def46..4c2b744 100644 --- a/poem-grants/README.md +++ b/poem-grants/README.md @@ -36,7 +36,7 @@ Route::new() .with(GrantsMiddleware::with_extractor(extract)) ``` -> Steps 1 and 2 can be replaced by custom middleware or integration with another libraries. Take a look at an [jwt-auth example](../examples/poem/jwt-auth/src/main.rs) +> Steps 1 and 2 can be replaced by custom middleware or integration with another libraries. Take a look at an [jwt-auth example](examples/jwt-auth/main.rs) 3. Protect your endpoints in any convenient way from the examples below: @@ -79,7 +79,7 @@ Here is an example using the `ty` and `expr` attributes. But these are independe `expr` allows you to include some checks in the macro based on function params, it can be combined with authorities by using `all`/`any`. `ty` allows you to use a custom type for th authorities (then the middleware needs to be configured). -Take a look at an [enum-role example](../examples/poem/enum-role/src/main.rs) +Take a look at an [enum-role example](examples/enum-role/main.rs) ```rust,ignore use poem::{Response, http::StatusCode, web}; @@ -120,8 +120,8 @@ You can find more [`examples`] in the git repository folder and [`documentation` ## Supported `poem` versions * For `poem-grants: 1.*` supported version of `poem` is `1.*` -[`jwt-auth`]: https://github.com/DDtKey/protect-endpoints/blob/main/examples/poem/jwt-auth -[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/poem +[`jwt-auth`]: https://github.com/DDtKey/protect-endpoints/blob/main/poem-grants/examples/jwt-auth +[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/poem-grants/examples [`documentation`]: https://docs.rs/poem-grants [`poem`]: https://github.com/poem-web/poem [`poem-openapi`]: https://github.com/poem-web/poem/tree/master/poem-openapi diff --git a/examples/poem/base_example.rs b/poem-grants/examples/base_example.rs similarity index 92% rename from examples/poem/base_example.rs rename to poem-grants/examples/base_example.rs index 635bffc..90e4925 100644 --- a/examples/poem/base_example.rs +++ b/poem-grants/examples/base_example.rs @@ -3,6 +3,7 @@ use poem::listener::TcpListener; use poem::{get, web, EndpointExt, Request, Response, Route, Server}; use poem_grants::authorities::{AuthDetails, AuthoritiesCheck}; use poem_grants::GrantsMiddleware; +use std::collections::HashSet; const ROLE_ADMIN: &str = "ROLE_ADMIN"; const ADMIN_RESPONSE: &str = "Hello Admin!"; @@ -58,10 +59,10 @@ async fn main() -> Result<(), std::io::Error> { } // You can use both `&Request` and `&mut Request` -async fn extract(_req: &mut Request) -> poem::Result> { +async fn extract(_req: &mut Request) -> poem::Result> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Ok(vec![ROLE_ADMIN.to_string()]) + Ok(HashSet::from([ROLE_ADMIN.to_string()])) } diff --git a/examples/poem/enum-role/src/main.rs b/poem-grants/examples/enum-role/main.rs similarity index 92% rename from examples/poem/enum-role/src/main.rs rename to poem-grants/examples/enum-role/main.rs index f46bd01..5c9b082 100644 --- a/examples/poem/enum-role/src/main.rs +++ b/poem-grants/examples/enum-role/main.rs @@ -3,6 +3,7 @@ use poem::listener::TcpListener; use poem::{get, http::StatusCode, EndpointExt, Request, Response, Route, Server}; use poem_grants::authorities::{AuthDetails, AuthoritiesCheck}; use poem_grants::GrantsMiddleware; +use std::collections::HashSet; mod role; @@ -38,10 +39,10 @@ async fn main() -> Result<(), std::io::Error> { } // You can specify any of your own type (`PartialEq` + `Clone`) for the return type wrapped in a vector: poem::Result> -async fn extract(_req: &mut Request) -> poem::Result> { +async fn extract(_req: &mut Request) -> poem::Result> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Ok(vec![Role::ADMIN]) + Ok(HashSet::from([Role::ADMIN])) } diff --git a/examples/poem/enum-role/src/role.rs b/poem-grants/examples/enum-role/role.rs similarity index 78% rename from examples/poem/enum-role/src/role.rs rename to poem-grants/examples/enum-role/role.rs index 1e420ca..63540fa 100644 --- a/examples/poem/enum-role/src/role.rs +++ b/poem-grants/examples/enum-role/role.rs @@ -1,5 +1,5 @@ // `Eq` and `Hash` is required -#[derive(Eq, Hash)] +#[derive(Eq, PartialEq, Hash)] #[allow(clippy::upper_case_acronyms)] pub enum Role { ADMIN, diff --git a/examples/poem/jwt-auth/src/claims.rs b/poem-grants/examples/jwt-auth/claims.rs similarity index 100% rename from examples/poem/jwt-auth/src/claims.rs rename to poem-grants/examples/jwt-auth/claims.rs diff --git a/examples/poem/jwt-auth/src/jwt_middleware.rs b/poem-grants/examples/jwt-auth/jwt_middleware.rs similarity index 100% rename from examples/poem/jwt-auth/src/jwt_middleware.rs rename to poem-grants/examples/jwt-auth/jwt_middleware.rs diff --git a/examples/poem/jwt-auth/src/main.rs b/poem-grants/examples/jwt-auth/main.rs similarity index 100% rename from examples/poem/jwt-auth/src/main.rs rename to poem-grants/examples/jwt-auth/main.rs diff --git a/examples/poem/openapi_example.rs b/poem-grants/examples/openapi_example.rs similarity index 93% rename from examples/poem/openapi_example.rs rename to poem-grants/examples/openapi_example.rs index 71b243f..6765e3a 100644 --- a/examples/poem/openapi_example.rs +++ b/poem-grants/examples/openapi_example.rs @@ -4,6 +4,7 @@ use poem_grants::authorities::{AuthDetails, AuthoritiesCheck}; use poem_grants::GrantsMiddleware; use poem_openapi::payload::PlainText; use poem_openapi::{OpenApi, OpenApiService}; +use std::collections::HashSet; const ROLE_ADMIN: &str = "ROLE_ADMIN"; const ADMIN_RESPONSE: &str = "Hello Admin!"; @@ -62,10 +63,10 @@ async fn main() -> Result<(), std::io::Error> { } // You can use both `&Request` and `&mut Request` -async fn extract(_req: &mut Request) -> poem::Result> { +async fn extract(_req: &mut Request) -> poem::Result> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Ok(vec![ROLE_ADMIN.to_string()]) + Ok(HashSet::from([ROLE_ADMIN.to_string()])) } diff --git a/poem-grants/src/lib.rs b/poem-grants/src/lib.rs index ab48409..12a36ee 100644 --- a/poem-grants/src/lib.rs +++ b/poem-grants/src/lib.rs @@ -12,7 +12,7 @@ //! You can find more [`examples`] in the git repository. //! //! [`GrantsMiddleware`]: GrantsMiddleware -//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/poem +//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/poem-grants/examples //! [`authorities`]: authorities //! [`proc-macro`]: proc_macro #![doc = include_str!("../README.md")] diff --git a/rocket-grants/Cargo.toml b/rocket-grants/Cargo.toml index ebf0c84..b7ba6cc 100644 --- a/rocket-grants/Cargo.toml +++ b/rocket-grants/Cargo.toml @@ -29,3 +29,5 @@ rocket = { version = "0.5.0", features = ["json"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1.34.0", features = ["rt-multi-thread"] } +chrono = "0.4.19" +jsonwebtoken = "9.1.0" diff --git a/rocket-grants/README.md b/rocket-grants/README.md index defeca7..59e10b1 100644 --- a/rocket-grants/README.md +++ b/rocket-grants/README.md @@ -57,7 +57,7 @@ Here is an example using the `ty` and `expr` attributes. But these are independe `expr` allows you to include some checks in the macro based on function params, it can be combined with authorities by using `all`/`any`. `ty` allows you to use a custom type for the authority (then the fairing needs to be configured). -Take a look at an [enum-role example](../examples/rocket/enum-role/src/main.rs) +Take a look at an [enum-role example](examples/enum-role/main.rs) ```rust,ignore use enums::Role::{self, ADMIN}; @@ -108,7 +108,7 @@ You can set up custom responses for: ## Supported `rocket` versions * For `rocket-grants: 0.1.*` supported version of `rocket` is `0.5.*` -[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/rocket +[`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/rocket-grants/examples [`documentation`]: https://docs.rs/rocket-grants [`rocket`]: https://github.com/SergioBenitez/Rocket [`poem-grants`]: https://github.com/DDtKey/protect-endpoints/tree/main/poem-grants diff --git a/examples/rocket/base_example.rs b/rocket-grants/examples/base_example.rs similarity index 94% rename from examples/rocket/base_example.rs rename to rocket-grants/examples/base_example.rs index 500dc18..5fc6ab6 100644 --- a/examples/rocket/base_example.rs +++ b/rocket-grants/examples/base_example.rs @@ -2,6 +2,7 @@ use rocket::serde::json::Json; use rocket::{get, post}; use rocket_grants::authorities::{AuthDetails, AuthoritiesCheck}; use rocket_grants::GrantsFairing; +use std::collections::HashSet; const ROLE_ADMIN: &str = "ROLE_ADMIN"; const ADMIN_RESPONSE: &str = "Hello Admin!"; @@ -60,7 +61,7 @@ async fn rocket() -> _ { ) .attach(GrantsFairing::with_extractor_fn(|_req| { Box::pin(async move { - Some(vec![ROLE_ADMIN.to_string()]) // just a stub + Some(HashSet::from([ROLE_ADMIN.to_string()])) // just a stub }) })) } diff --git a/examples/rocket/enum-role/src/main.rs b/rocket-grants/examples/enum-role/main.rs similarity index 79% rename from examples/rocket/enum-role/src/main.rs rename to rocket-grants/examples/enum-role/main.rs index 662e01b..10c4597 100644 --- a/examples/rocket/enum-role/src/main.rs +++ b/rocket-grants/examples/enum-role/main.rs @@ -1,8 +1,9 @@ -use crate::role::Role::{self, ADMIN}; +use crate::role::Role::{self, Admin}; use rocket::http::Status; use rocket::Request; use rocket_grants::authorities::{AuthDetails, AuthoritiesCheck}; use rocket_grants::GrantsFairing; +use std::collections::HashSet; mod role; @@ -17,7 +18,7 @@ async fn macro_secured() -> Status { // An example of programmable protection with custom type #[rocket::get("/manual")] async fn manual_secure(details: AuthDetails) -> &'static str { - if details.has_authority(&Role::ADMIN) { + if details.has_authority(&Role::Admin) { return "Hello Admin!"; } "Hello!" @@ -28,14 +29,16 @@ async fn manual_secure(details: AuthDetails) -> &'static str { async fn rocket() -> _ { rocket::build() .mount("/api", rocket::routes![macro_secured, manual_secure]) - .attach(GrantsFairing::with_extractor_fn(|req| Box::pin(extract(req)))) + .attach(GrantsFairing::with_extractor_fn(|req| { + Box::pin(extract(req)) + })) } // You can specify any of your own type (`PartialEq` + `Clone`) for the return type wrapped in a vector: rocket::Result> -async fn extract(_req: &mut Request<'_>) -> Option> { +async fn extract(_req: &mut Request<'_>) -> Option> { // Here is a place for your code to get user permissions/roles/authorities from a request // For example from a token or database // Stub example - Some(vec![Role::ADMIN]) + Some(HashSet::from([Role::Admin])) } diff --git a/examples/rocket/enum-role/src/role.rs b/rocket-grants/examples/enum-role/role.rs similarity index 100% rename from examples/rocket/enum-role/src/role.rs rename to rocket-grants/examples/enum-role/role.rs diff --git a/examples/rocket/jwt-auth/src/claims.rs b/rocket-grants/examples/jwt-auth/claims.rs similarity index 89% rename from examples/rocket/jwt-auth/src/claims.rs rename to rocket-grants/examples/jwt-auth/claims.rs index 6b0be95..2eacebe 100644 --- a/examples/rocket/jwt-auth/src/claims.rs +++ b/rocket-grants/examples/jwt-auth/claims.rs @@ -1,6 +1,7 @@ use chrono::{Duration, Utc}; use jsonwebtoken::{self, DecodingKey, EncodingKey, Header, Validation}; use serde::{Deserialize, Serialize}; +use std::collections::HashSet; // Token lifetime and Secret key are hardcoded for clarity const JWT_EXPIRATION_HOURS: i64 = 24; @@ -9,12 +10,12 @@ const SECRET: &str = "SECRET"; #[derive(Debug, Serialize, Deserialize)] pub(crate) struct Claims { pub username: String, - pub permissions: Vec, + pub permissions: HashSet, exp: i64, } impl Claims { - pub fn new(username: String, permissions: Vec) -> Self { + pub fn new(username: String, permissions: HashSet) -> Self { Self { username, permissions, diff --git a/examples/rocket/jwt-auth/src/main.rs b/rocket-grants/examples/jwt-auth/main.rs similarity index 91% rename from examples/rocket/jwt-auth/src/main.rs rename to rocket-grants/examples/jwt-auth/main.rs index 4bb7f1c..5ca3750 100644 --- a/examples/rocket/jwt-auth/src/main.rs +++ b/rocket-grants/examples/jwt-auth/main.rs @@ -4,6 +4,7 @@ use rocket::serde::json::Json; use rocket::Request; use rocket_grants::GrantsFairing; use serde::Deserialize; +use std::collections::HashSet; use crate::claims::Claims; @@ -41,10 +42,12 @@ async fn rocket() -> _ { "/api", rocket::routes![permission_secured, manager_secured, create_token], ) - .attach(GrantsFairing::with_extractor_fn(|req| Box::pin(extract_from_jwt(req)))) + .attach(GrantsFairing::with_extractor_fn(|req| { + Box::pin(extract_from_jwt(req)) + })) } -async fn extract_from_jwt(req: &mut Request<'_>) -> Option> { +async fn extract_from_jwt(req: &mut Request<'_>) -> Option> { req.headers() .get(AUTHORIZATION.as_str()) .next() @@ -79,5 +82,5 @@ pub async fn create_token(info: Json) -> Result, + pub permissions: HashSet, } diff --git a/rocket-grants/src/lib.rs b/rocket-grants/src/lib.rs index 1db30f6..6b5a21c 100644 --- a/rocket-grants/src/lib.rs +++ b/rocket-grants/src/lib.rs @@ -12,7 +12,7 @@ //! You can find more [`examples`] in the git repository. //! //! [`GrantsFairing`]: GrantsFairing -//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/examples/rocket +//! [`examples`]: https://github.com/DDtKey/protect-endpoints/tree/main/rocket-grants/examples //! [`permissions`]: authorities //! [`proc-macro`]: proc_macro #![doc = include_str!("../README.md")]