1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! # Toolkit for Authentication and Authorization in Rust
//!
//! A flexible and extensible authentication and authorization library in Rust,
//! designed to support multiple strategies including **ABAC** (Attribute-Based Access Control),
//! **RBAC** (Role-Based Access Control), and **SBA** (Scope-Based Authorization)
//!
//! This crate is suitable for use in both API servers and embedded authorization layers.
//!
//! ---
//!
//! ## ✨ Features
//!
//! - **Authentication (auth_n)**: Handles register, login, and reset_password.
//! - **Authorization (auth_z)**: Supports **ABAC** (Attribute-Based Access Control),
//! **RBAC** (Role-Based Access Control), and **SBA** (Scope-Based Authorization)
//! - **Scope matching**: Flexible support for OAuth2-style scopes with customizable formats.
//!
//!---
//!
//! ## 🚀 Quick Start
//!
//!
//! ### 🏢 ABAC (Attribute-Based Access Control)
//!
//!```rust
//! use auth_kit::auth::auth_z::Authorization;
//! use auth_kit::error::AuthError;
//! use auth_kit::model::{AuthContext, AuthStrategy, Resource, Role, User};
//!
//! fn main() -> Result<(), AuthError> {
//! let user = User {
//! email: "abac@example.com".to_string(),
//! password_hash: "".to_string(),
//! role: Role {
//! name: "employee".to_string(),
//! permissions: vec![],
//! },
//! department: "engineering".to_string(),
//! clearance_level: 5,
//! };
//!
//! let resource = Resource {
//! department: "engineering".to_string(),
//! required_level: 3,
//! };
//!
//! let context = AuthContext {
//! user: Some(user),
//! claims: None,
//! resource: Some(resource),
//! };
//!
//! let authorized = Authorization::new("ABAC");
//! match authorized {
//! Ok(mut authz) => {
//! let result = authz.authorize(&context, "docs", "read", None);
//! match result {
//! Ok(_) => println!("Access granted via ABAC."),
//! Err(e) => println!("ABAC check failed: {}", e.to_string()),
//! }
//!
//! },
//! Err(e) => {
//! println!("Error initializing Authorization: {}", e.to_string());
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//!
//! ### 🔐 RBAC (Role-Based Access Control)
//!
//!```rust
//! use bcrypt::{hash, DEFAULT_COST};
//! use auth_kit::error::AuthError;
//! use auth_kit::auth::auth_n::Authentication;
//! use auth_kit::auth::auth_z::Authorization;
//! use auth_kit::model::{AuthContext, AuthStrategy, Permission};
//!
//! fn main() -> Result<(), AuthError> {
//!
//! let mut authn = Authentication::new();
//!
//! let password_hash = hash("secret123", DEFAULT_COST)
//! .map_err(|e| AuthError::PasswordHashingFailed(e.to_string()))?;
//!
//! match authn.register("admin@example.com", &password_hash) {
//! Ok(()) => println!("User registered"),
//! Err(AuthError::EmailAlreadyRegistered) => println!("Email already in use"),
//! Err(e) => eprintln!("Registration failed: {:?}", e),
//! }
//!
//! let mut user = authn.users.get("admin@example.com").cloned().expect("User must exist");
//! user.role.permissions.push(Permission::Create);
//!
//! let authorized = Authorization::new("RBAC");
//! match authorized {
//! Ok(mut authz) => {
//! let context = AuthContext {
//! user: Some(user),
//! claims: None,
//! resource: None,
//! };
//! let result = authz.authorize(&context, "service", "create", None);
//! match result {
//! Ok(_) => println!("Access granted via RBAC."),
//! Err(e) => println!("Access denied: {}", e.to_string()),
//! }
//! },
//! Err(e) => {
//! println!("Error initializing Authorization: {}", e.to_string());
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ### 🪪 SBA (Scope-Based Authorization)
//!
//! ```rust
//! use auth_kit::auth::auth_z::Authorization;
//! use auth_kit::error::AuthError;
//! use auth_kit::model::{AuthContext, AuthStrategy, Claims};
//!
//! fn main() -> Result<(), AuthError> {
//! let claims = Claims {
//! email: "jwt@example.com".to_string(),
//! service: "admin_service".to_string(),
//! scopes: vec!["admin_service:create".to_string()],
//! };
//!
//! let context = AuthContext {
//! user: None,
//! claims: Some(claims),
//! resource: None,
//! };
//!
//! let authorized = Authorization::new("SBA");
//! match authorized {
//! Ok(mut authz) => {
//! let result = authz.authorize(&context, "admin_service", "create", Some(":"));
//! match result {
//! Ok(_) => println!("Access granted via SBA."),
//! Err(e) => println!("Access denied via SBA: {}", e.to_string()),
//! }
//! },
//! Err(e) => {
//! println!("Error initializing Authorization: {}", e.to_string());
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ---
//!
//! ## 📜 License
//!
//! Licensed under:
//! - Apache License, Version 2.0 [LICENSE](http://www.apache.org/licenses/LICENSE-2.0.txt)
//!
//! ---
//!
//! ## 🧑💻 Author
//!
//! Created and maintained by [Jerry Maheswara](https://github.com/jerry-maheswara-github)
//!
//! Feel free to reach out for suggestions, issues, or improvements!
//!
//! ---
//!
//! ## ❤️ Built with Love in Rust
//!
//! This project is built with ❤️ using **Rust** — a systems programming language that is safe, fast, and concurrent. Rust is the perfect choice for building reliable and efficient applications.
//!
//! ---
//!
//! ## 👋 Contributing
//!
//! Pull requests, issues, and feedback are welcome!
//! If you find this crate useful, give it a ⭐ and share it with others in the Rust community.
//!
//! ---
/// Error definitions and shared error types.
/// Authentication and authorization logic.
/// Common data types used in authentication and policy evaluation.