๐ก๏ธ Authorization - RBAC, ABAC, & Policies
Donโt sprinkle
if (user.isAdmin)checks throughout your business logic. Authorization should be a decoupled layer or a dedicated service. A Programmer designs for the Principle of Least Privilege, ensuring users only have access to the specific data they need to perform their job.
๐ฅ 1. RBAC (Role-Based Access Control)
Section titled โ๐ฅ 1. RBAC (Role-Based Access Control)โThe most common model. Permissions are grouped into Roles, and Roles are assigned to Users.
- Structure:
User โฎ Role โฎ Permission. - Example: A โEditorโ role has the
update_postanddelete_postpermissions. - Pros: Very easy to understand and audit. Excellent for 80% of application needs.
- Cons: Can lead to โRole Explosionโ (e.g., creating
Manager_Region_A,Manager_Region_B) when permissions need to be more granular.
๐งช 2. ABAC (Attribute-Based Access Control)
Section titled โ๐งช 2. ABAC (Attribute-Based Access Control)โA more dynamic model that uses Attributes (properties) of the user, the resource, and the environment to make a decision.
- Structure:
IF (Subject.Attribute AND Resource.Attribute AND Environment.Attribute) THEN Allow. - Example: โAllow
EngineerstoAccessServersonly ifServer.Project == User.ProjectandTime is during working hours.โ - Pros: Extremely flexible; avoids role explosion.
- Cons: High complexity; can be difficult to reason about and can impact performance if rules are too complex.
๐ 3. Policy-Based Access Control (PBAC / ReGo)
Section titled โ๐ 3. Policy-Based Access Control (PBAC / ReGo)โTaking authorization out of the code and into a configuration or policy engine.
- Opa (Open Policy Agent): A popular industry standard. You write policies in a language called Rego.
- How it works: Your API sends a โqueryโ to the policy engine (e.g., โCan User X do Action Y on Resource Z?โ), and the engine returns a simple
true/false. - Programmerโs Move: This allows security teams to update permissions across multiple microservices without redeploying code.
๐๏ธ 4. ReBAC (Relationship-Based Access Control)
Section titled โ๐๏ธ 4. ReBAC (Relationship-Based Access Control)โPopularized by Googleโs Zanzibar paper. It defines access based on relationships between entities.
- Logic: โUser A can edit Document B because User A is a member of Folder C, and Folder C contains Document B.โ
- Best for: Complex hierarchical structures (like Google Drive, Notion, or Slack).
๐ ๏ธ Implementation Patterns
Section titled โ๐ ๏ธ Implementation PatternsโCentralized vs. Decentralized
Section titled โCentralized vs. Decentralizedโ- Centralized: One โAuthorization Serviceโ handles all checks. Great for consistency, but creates a single point of failure and adds network latency.
- Decentralized: Each microservice handles its own authorization. Faster, but harder to ensure uniform security policies across the company.
The โData Filteringโ Problem
Section titled โThe โData Filteringโ ProblemโAuthorization isnโt just about Allow/Deny. Itโs often about filtering what comes back from the database.
- Junior approach: Fetch all records, then loop through them in the API and remove the ones the user shouldnโt see. (Slow and memory-intensive).
- Senior approach: Inject authorization constraints into the Database Query itself (e.g.,
SELECT * FROM orders WHERE user_id = ?).
Scopes vs. Permissions
Section titled โScopes vs. Permissionsโ- Scopes (OAuth2): High-level โbucketsโ of access (e.g.,
read:profile,write:orders). These are usually what the application is allowed to do. - Permissions: Granular internal checks (e.g.,
can_refund_over_500). These are what the user is allowed to do.
๐ก Note: Always audit your authorization. Implement Access Logging so you can answer the question: โWho accessed this sensitive record and when?โ In highly regulated industries, being able to prove that your authorization works is just as important as the authorization itself.