Testing Kratos Authentication in STING
This guide walks you through testing the Ory Kratos authentication implementation in STING application.
Overview of Kratos Authentication Flow
Kratos uses a browser-based authentication flow:
- Browser Flow: Browser redirects to Kratos, which returns a session cookie
- API Flow: API-based interactions for applications that can’t use cookies
STING uses a mix of these approaches with the frontend handling the UI portion.
Testing Login Via the UI
Prerequisites
Ensure all services are running:
./manage_sting.sh start
Important: If you encounter errors like
Database is uninitialized and superuser password is not specifiedorOCI runtime exec failed, it likely means environment files are missing or have incorrect permissions. Run the fix script:./fix_env_issues.shThis will create necessary environment files with default values in the
/envdirectory. See the Troubleshooting section below for more details.
### Step 1: Access the Login Page
1. Open your browser and navigate to `https://localhost:8443`
2. You should be redirected to the login page if not already authenticated
### Step 2: Understand the Login Flow
The login flow in Kratos works as follows:
1. User accesses the login page
2. Frontend contacts Kratos to initialize a login flow
3. Kratos returns a flow ID
4. User enters credentials
5. Frontend submits credentials to Kratos
6. Kratos validates credentials and returns a session
### Step 3: Register a New Account
Since this is a fresh installation, you'll need to create a user first:
1. Click "Register" or navigate to `https://localhost:8443/register`
2. You'll be redirected to Kratos to handle the registration
3. Fill in the registration form:
- Email: `test@example.com` (use a unique email each time)
- Password: `TestPassword123!` (minimum 8 characters)
4. Submit the form
5. If successful, you should be redirected to the dashboard or a verification page
6. Check the Mailpit UI at `http://localhost:8025` to find verification emails
For automated testing, you can use the provided scripts:
```bash
# For API-based testing
cd kratos && ./test_kratos_registration.sh
# For browser-based testing with interactive prompts
cd kratos && ./test-browser-registration.sh
Step 4: Log in with the Created Account
- Navigate to
https://localhost:8443/login - You’ll be redirected to Kratos for authentication
- Enter the credentials:
- Email:
test@example.com(the email you registered with) - Password:
TestPassword123!(the password you created)
- Email:
- Click “Sign In”
- You should be redirected to the dashboard if login is successful
If you encounter SSL certificate warnings, click “Advanced” and “Accept Risk and Continue” to proceed.
Step 5: Inspect the Network Requests (Optional)
To understand what’s happening under the hood:
- Open your browser’s developer tools (F12 or Ctrl+Shift+I)
- Go to the Network tab
- Clear the current logs
- Reload the login page
- Observe the requests:
- Request to
/self-service/login/browserto initialize the flow - Request to
/self-service/loginwhen submitting credentials - Redirect to your app with a session cookie
- Request to
Common Issues and Solutions
SSL Certificate Errors
You may see browser warnings about invalid certificates since we’re using self-signed certs in development:
- Click “Advanced” and “Proceed to localhost” in Chrome
- Click “Accept the Risk and Continue” in Firefox
Redirect Issues
If redirects aren’t working properly:
- Verify the
KRATOS_PUBLIC_URLandLOGIN_UI_URLin the Kratos configuration - Check that
defaultReturnTois set correctly in the Kratos config
CORS Errors
If you see CORS errors in the console:
- Ensure Kratos’s CORS settings include your frontend URL
- Check the
allowed_originssetting inkratos.yml
Server Communications
If your frontend can’t reach Kratos:
- Verify Docker network connectivity
- Check that ports are properly exposed
- Ensure the Kratos service is healthy
Debugging Tools
Kratos Admin API
Access the Kratos admin API to inspect current sessions and flows:
curl -k https://localhost:4434/admin/identities
Kratos Logs
View the Kratos service logs:
docker logs $(docker ps | grep kratos | awk '{print $1}')
Test Login Flow Directly
Initialize a login flow directly:
curl -k https://localhost:4433/self-service/login/browser
Troubleshooting
Authentication Issues
For detailed troubleshooting of Kratos authentication issues, refer to the Troubleshooting Guide.
Common issues include:
- SSL certificate problems with self-signed certificates
- CORS errors when accessing Kratos directly from the browser
- Redirect errors if URLs are not properly configured
- Problems with session cookies not being properly set or recognized
You can test authentication directly using the provided scripts:
# Test Kratos registration through API
cd kratos && ./test_kratos_registration.sh
# Test browser-based registration with interactive prompts
cd kratos && ./test-browser-registration.sh
Environment Variable Issues
If you see errors like Database is uninitialized and superuser password is not specified or OCI runtime exec failed when starting services, follow these steps:
Run the environment fix script:
./troubleshooting/fix_env_issues.shThis script will:
- Create missing environment files in the
/envdirectory - Set default values for required variables
- Fix permissions on environment files
- Clean up Docker environment
- Create missing environment files in the
Verify the environment files:
ls -la env/You should see files like:
db.env- Database configurationkratos.env- Kratos configurationfrontend.env- Frontend configuration (important for Kratos URL)- Other service-specific .env files
Check that the frontend environment is correctly set up:
cat env/frontend.env # Should contain something like: # REACT_APP_API_URL=https://localhost:5050 # REACT_APP_KRATOS_PUBLIC_URL=https://localhost:4433 # NODE_ENV=developmentIf problems persist:
# Stop all services ./manage_sting.sh stop # Remove all containers and volumes docker-compose down -v # Clean Docker environment docker system prune -f # Regenerate env files cd conf && python3 config_loader.py -g # Start services again cd .. && ./manage_sting.sh start # Update frontend environment cd frontend && ./update-env.sh cd .. && ./manage_sting.sh restart frontend
Container Health Check Failures
If containers fail to start properly due to health check failures:
Check container logs:
docker logs $(docker ps -a | grep db | awk '{print $1}') docker logs $(docker ps -a | grep kratos | awk '{print $1}')Verify network connectivity:
docker network inspect sting_localTry running with extended health check timeouts:
HEALTH_CHECK_START_PERIOD=180s HEALTH_CHECK_TIMEOUT=10s ./manage_sting.sh start
Database Initialization Issues
If the database fails to initialize correctly:
Check if the database container is running:
docker ps | grep dbVerify the database initialization scripts:
ls -la docker-entrypoint-initdb.d/Try rebuilding the database container:
./manage_sting.sh rebuild db
Resources
- Kratos Documentation
- Ory Developer Guides
- Self-Service Flows & User Interface
- Docker Compose Documentation
- PostgreSQL Environment Variables