# `authentication/no_touch.py` ## Overview This file provides the specialized authentication middleware (`NoTouchMiddleware`) used for securing the "No Touch" (Solar) endpoints (`/V2/*`). It is more tightly coupled with the business logic (`EMIService`) to ensure that users can only access their specific applications. ## Class: `NoTouchMiddleware` ### `main(cls, func)` (Decorator) This decorator secures the "no touch" endpoints. It uniquely utilizes Dependency Injection (`@inject`) inside the decorator to access `EMIService`. - **Workflow**: 1. Reads the `application_id` and `mobile_number` from the request body. If `application_id` is missing but `mobile` is present, it fetches the ID using `emi_service.get_application_details_by_mobile()`. 2. Parses the `HTTP_AUTHORIZATION` Bearer token (if present) and decodes it via `jwt_decode`. 3. **Access Token Flow** (`token_type == 'access'`): - Extracts `user_id` or `applicant_id`. - Cross-references these IDs with the database (using `emi_service`) to find the associated `application_id`. - Validates that the requested `application_id` matches the application ID owned by the user. 4. **Temporary Token Flow** (`token_type == 'temporary'`): - Extracts the `mobile_number` and validates its presence. 5. **Environment Level Auth**: - After JWT validation, it calls `AuthMiddleware('env').main()` to ensure the API caller (e.g., frontend client) has valid environment-level credentials (like a valid Client ID/Secret). 6. Returns `solar_auth_failure_response` upon any validation failure. ### `jwt_decode(token)` (Static Method) - Decodes the provided JWT token without signature verification to quickly inspect the payload (`id`, `token_type`, etc.). ### `main_v1(func, *args, **kwargs)` (Static Method) - Appears to be a deprecated or passthrough decorator intended to bypass authentication temporarily (perhaps during a migration or release phase for the solar app). ## Dependencies - `flask` (`request`) - `jwt` (PyJWT) - `bugsnag` - `dependency_injector` (`Provide`, `inject`) - `AuthMiddleware` (from `auth_middleware`) - `solar_auth_failure_response` - `EMIService` (from `emi_service`)