Actix Web Authentication Macro
This project provides a custom procedural macro attribute (#[authentication]) for easily adding JWT (JSON Web Token) authentication to your Actix Web handlers.
Add macro to your project
cargo add nextera_jwt
Prepare your .env file
- JWT_AUDIENCE = your_audience_name
- ACCESS_TOKEN_SECRET = your_access_token_secret
Features
- Automatic
HttpRequestInjection: The macro automatically injects anactix_web::HttpRequestinstance as the first argument of the decorated function, allowing you to access request information. - JWT Authentication: Performs JWT-based authentication by extracting the
Authorizationheader from the request and validating the token against a provided secret key and audience. - Environment Variable Configuration: Retrieves the JWT audience and secret key from environment variables (
JWT_AUDIENCEandACCESS_TOKEN_SECRET), promoting secure configuration management. - Unauthorized Response: Returns an
HttpResponse::Unauthorized(401) response if the authentication fails. - Supports Async Functions: Compatible with asynchronous handlers.
Usage
-
Add the Macro to Your Project:
- Place the macro code (from the provided example) in a separate file (e.g.,
src/lib.rs) within your project. - Add the path to this file in your
Cargo.tomlunder[lib]->path.
- Place the macro code (from the provided example) in a separate file (e.g.,
-
Decorate Your Handlers:
- Apply the
#[authentication]attribute to the handlers that require authentication:
use ; use authentication; async - Apply the
-
Set Environment Variables:
- Before running your application, set the following environment variables:
JWT_AUDIENCE: The intended audience for the JWT.ACCESS_TOKEN_SECRET: The secret key used to sign the JWT.
- Before running your application, set the following environment variables:
-
Run Your Application:
- Build and run your Actix Web application as usual.
Example
See the example directory for a complete, working example demonstrating the usage of the authentication macro with Actix Web.
Important Considerations
- Error Handling: The provided example uses basic error handling. For production environments, implement more robust error handling (e.g., handle missing headers gracefully, return appropriate error responses).
- Security:
- Never hardcode secrets directly in your code. Utilize environment variables or a secrets management solution for secure configuration.
- Regularly rotate your secret keys to enhance security.
- Dependencies: This macro may have dependencies on other crates (e.g., for JWT validation). Ensure these dependencies are correctly listed in your
Cargo.toml.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for any improvements or bug fixes.
This README.md provides a comprehensive overview of the project, its features, usage, and important considerations. Remember to adapt it further based on your specific project needs and any additional functionalities you may implement.