Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update examples and include into crates #57

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions actix-web-grants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 4 additions & 4 deletions actix-web-grants/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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};
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<Vec<String>, Error> {
async fn extract(_req: &mut ServiceRequest) -> Result<HashSet<String>, 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()]))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Vec<YOUR_TYPE_HERE>, Error>
async fn extract(_req: &mut ServiceRequest) -> Result<Vec<Role>, Error> {
async fn extract(_req: &mut ServiceRequest) -> Result<HashSet<Role>, 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]))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// `Eq` and `Hash` is required
#[derive(Eq, Hash)]
#[derive(Eq, PartialEq, Hash)]
pub enum Role {
Admin,
Manager,
Expand Down
2 changes: 1 addition & 1 deletion actix-web-grants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions examples/actix-web/enum-role/Cargo.toml

This file was deleted.

16 changes: 0 additions & 16 deletions examples/actix-web/jwt-httpauth/Cargo.toml

This file was deleted.

13 changes: 0 additions & 13 deletions examples/poem/enum-role/Cargo.toml

This file was deleted.

16 changes: 0 additions & 16 deletions examples/poem/jwt-auth/Cargo.toml

This file was deleted.

13 changes: 0 additions & 13 deletions examples/rocket/enum-role/Cargo.toml

This file was deleted.

16 changes: 0 additions & 16 deletions examples/rocket/jwt-auth/Cargo.toml

This file was deleted.

2 changes: 2 additions & 0 deletions poem-grants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 4 additions & 4 deletions poem-grants/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -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<Vec<String>> {
async fn extract(_req: &mut Request) -> poem::Result<HashSet<String>> {
// 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()]))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Vec<YOUR_TYPE_HERE>>
async fn extract(_req: &mut Request) -> poem::Result<Vec<Role>> {
async fn extract(_req: &mut Request) -> poem::Result<HashSet<Role>> {
// 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]))
}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -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<Vec<String>> {
async fn extract(_req: &mut Request) -> poem::Result<HashSet<String>> {
// 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()]))
}
2 changes: 1 addition & 1 deletion poem-grants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 2 additions & 0 deletions rocket-grants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions rocket-grants/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -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
})
}))
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Role>) -> &'static str {
if details.has_authority(&Role::ADMIN) {
if details.has_authority(&Role::Admin) {
return "Hello Admin!";
}
"Hello!"
Expand All @@ -28,14 +29,16 @@ async fn manual_secure(details: AuthDetails<Role>) -> &'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<Vec<YOUR_TYPE_HERE>>
async fn extract(_req: &mut Request<'_>) -> Option<Vec<Role>> {
async fn extract(_req: &mut Request<'_>) -> Option<HashSet<Role>> {
// 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]))
}
Loading
Loading