Overview
The OpenEyes API uses HTTP Basic Authentication to secure endpoints. Authentication is implemented through theBasicAuthBehavior class attached to API controllers.
Source: protected/modules/Api/behaviors/BasicAuthBehavior.php
Authentication Method
HTTP Basic Authentication
API requests must include valid OpenEyes user credentials in the HTTP Authorization header.How It Works
TheBasicAuthBehavior class (lines 17-42) implements the following flow:
- Extracts credentials from
PHP_AUTH_USERandPHP_AUTH_PWserver variables - Creates a
UserIdentityobject with the provided credentials - Authenticates against the OpenEyes user database
- Checks for the
OprnApiaccess permission - Returns 401 if authentication fails, 403 if permission is denied
Code Reference
protected/modules/Api/behaviors/BasicAuthBehavior.php:26-41
Making Authenticated Requests
Using cURL
Using JavaScript (Fetch API)
Using Python (requests)
Using PHP (cURL)
Required Permissions
OprnApi Role
Users accessing the API must have theOprnApi role assigned. This is checked on every API request:
Granting API Access
To grant API access to a user:- Log in to OpenEyes as an administrator
- Navigate to Admin > Users
- Select the user account
- Assign the “OprnApi” role
- Save changes
Controller Integration
API controllers attach theBasicAuthBehavior in their behaviors() method:
PatientController.php:35-40
Access Control Rules
In addition to authentication, controllers define access rules:PatientController.php:20-33
Authentication Errors
401 Unauthorized
Returned when:- Credentials are missing
- Username or password is incorrect
- User account is disabled
403 Forbidden
Returned when:- Authentication succeeds but user lacks
OprnApipermission - User account exists but doesn’t have API access
Security Considerations
Best Practices
- Use HTTPS: Always use TLS/SSL encryption for API requests
- Dedicated API Users: Create separate user accounts specifically for API access
- Strong Passwords: Enforce strong password policies for API users
- Minimal Permissions: Grant only the
OprnApirole, not additional administrative roles - Credential Storage: Store credentials securely using environment variables or secret management
- Rotate Credentials: Periodically change API user passwords
- Monitor Access: Review API access logs regularly
- IP Restrictions: Consider restricting API access to known IP addresses at the firewall level
Example: Environment Variables
Testing Authentication
Test your credentials with a simple request:Next Steps
Patient Search
Search for patients using authenticated requests
API Overview
Learn about API structure and conventions