Developer 101: Authorization Basics — Securing Your Web Application

Job Alex Muturi
4 min readJul 19, 2023
Photo by Pete Alexopoulos on Unsplash

Authorization is a critical and fundamental aspect of building secure applications. It ensures that users have the appropriate permissions to access specific resources or perform certain actions. We will explore the basics of authorization, covering key concepts and techniques that apply to applications regardless of the framework or language used.

To follow along you should have a basic understanding of web development concepts, including HTTP, client-server architecture, and session management.

Authorization should not be confused with Authentication. In a previous article in this series we looked at Authentication. Authentication is the process of verifying the identity of a user, while authorization focuses on granting or denying access based on roles and permissions. Authentication involves collecting user credentials and validating them against stored data or an external authentication service. Successful authentication results in the issuance of a session token or a similar mechanism to identify the user in subsequent requests.

To implement user authentication, you can utilize technologies popular strategies like JSON Web Tokens (JWT) or session-based authentication amongst others. With JWT, the server generates a JWT containing user information and signs it with a secret key. The client stores the JWT and sends it along with each request as an authorization header. An alternative approach is session-based authentication. After successful login, the server creates a session for the user and returns a session ID or token. The client includes the session ID in subsequent requests, either as a cookie or in the request header.

Access Control Models:

Access control models define the rules and mechanisms for granting or denying access to resources. Some commonly used models include:

  • Discretionary Access Control (DAC): Allows owners to control access to their resources by granting or revoking permissions. In this model, each resource has an owner who can grant or revoke access permissions to other users or groups. The access permissions typically include read, write, and execute privileges. The resource owner has the discretion to determine who can access their resources and what level of access they have. DAC is commonly used in file systems and database systems.
  • Mandatory Access Control (MAC): Uses labels or categories to determine access based on predefined security policies. It relies on system-wide policies rather than individual resource owners. Each user and resource is assigned a security label or category. The system administrator defines rules that determine which users or categories can access specific resources. These rules are typically based on factors such as sensitivity, confidentiality, or security clearances. MAC is commonly used in highly secure environments like government systems or military applications.
  • Role-Based Access Control (RBAC): Role-Based Access Control is a widely adopted access control model that simplifies access management by assigning permissions to roles and then assigning roles to users. Common roles include “admin,” “user,” or “guest”. Instead of assigning permissions directly to users, permissions are associated with roles, and users inherit those permissions based on their assigned roles. Roles can be hierarchical, allowing for more complex access control structures. RBAC provides flexibility and ease of administration, making it suitable for many applications.
  • Attribute-Based Access Control (ABAC): Attribute-Based Access Control focuses on making access control decisions based on attributes associated with users, resources, and the environment. Attributes can include user attributes (e.g., age, department), resource attributes (e.g., sensitivity, type), and environmental attributes (e.g., time of day, location). Policies in ABAC define rules that evaluate these attributes and make access decisions accordingly. ABAC allows for fine-grained access control and dynamic decision making based on contextual attributes.
  • Rule-Based Access Control (RBAC): Rule-Based Access Control uses a set of rules to determine access permissions. Rules define conditions and actions based on which access decisions are made. These rules can take various forms, such as logical expressions, attribute comparisons, or user-defined functions. RBAC provides flexibility in defining complex access control policies based on specific requirements.

Implementing Authorization:

To implement authorization, start by identifying the protected resources in your application. This can include pages, endpoints, or specific actions. Determine the permissions required to access each resource and map them to user roles. The next step is to implement a user role management mechanism which is built upon an authorization logic securing backend endpoints, frontend component with an element of logging and auditing.

Mostly user roles and permissions are stored in a database or within user session data. When processing requests, validate the user’s role and permissions against the required criteria for the requested resource.

Route Protection: In web applications with multiple routes or pages, route protection ensures that only authenticated users with the necessary roles can access specific routes. This can be achieved using middleware, interceptors, or route guards depending on your framework or architecture.

Route protection mechanisms aim to verify user authentication and authorization before allowing access to protected routes else they redirect unauthorized users to a login page or display an error message.

Fine-Grained Authorization: Fine-grained authorization allows for more granular control over access to specific actions or sections within a resource. This ensures that users only see or can interact with the parts of an application relevant to their permissions.

Using conditional statements or access control directives, frontend code can show or hide UI elements, buttons, or sections based on user roles or permissions.

Conclusion

Implementing robust authorization mechanisms is vital for securing applications. By understanding the basics of authentication, role-based authorization, access control models, and implementing proper authorization techniques, you can build applications that protect sensitive resources and ensure that only authorized users can perform specific actions.

Always stay up to date with security best practices and regularly review and update your authorization implementation as new threats and vulnerabilities emerge.

Happy building and securing your web applications!

--

--

Job Alex Muturi

Angular Developer | Angular Kenya Peer Mentor | Blogger | Tech Speaker