This project demonstrates implementing a multi-level authentication system using Apache APISIX as the API gateway and OAuth2 Proxy for authentication.
This is not used APISix's Multi-Auth plugin, since its not support the forward auth plugin.
graph TD
User[User Browser] -->|Request| APISIX[APISIX Gateway]
%% Home service flow
APISIX -->|1.Forward Auth Check| OAuth2Proxy1[OAuth2 Proxy for Home]
OAuth2Proxy1 -->|2.Auth Success| APISIX
APISIX -->|3.If Authenticated| Home[Home Service]
%% Vault service flow
APISIX -->|1.Request to /vault| OAuth2Proxy2[OAuth2 Proxy for Vault]
OAuth2Proxy2 -->|2.Re-authentication| User
OAuth2Proxy2 -->|3.If Authenticated| SecretVault[Secret Vault Service]
%% Auth stores
OAuth2Proxy1 -.->|Validates against| HtpasswdHome[.htpasswd]
OAuth2Proxy2 -.->|Validates against| HtpasswdVault[.htpasswd_vault]
%% Style definitions
classDef service fill:#c8e6c9,stroke:#43a047,color:#1b5e20
classDef auth fill:#bbdefb,stroke:#1976d2,color:#0d47a1
classDef user fill:#f5f5f5,stroke:#616161,color:#212121
classDef store fill:#ffecb3,stroke:#ffa000,color:#ff6f00
class Home,SecretVault service
class OAuth2Proxy1,OAuth2Proxy2,APISIX auth
class User user
class HtpasswdHome,HtpasswdVault store
The system implements two levels of authentication:
-
Single Authentication (Home Service):
- Users need to authenticate once to access the home service.
- Authentication is handled by the first OAuth2 Proxy instance.
- Credentials: username:
admin
, password:TopSecret
-
Double Authentication (Vault Service):
- Access to the sensitive vault service requires re-authentication.
- After successfully logging into the home service, users must authenticate again with different credentials.
- The second layer is handled by a separate OAuth2 Proxy instance with its own credentials store.
- Credentials: username:
user
, password:TopSecret
This multi-level authentication pattern is useful for:
- Protecting highly sensitive areas of an application
- Implementing the principle of defense in depth
- Requiring explicit consent before accessing critical resources
- Reducing the risk of session hijacking attacks
- Docker and Docker Compose installed on your system
-
Clone this repository (or ensure you have all the configuration files in place)
-
Navigate to the project directory:
cd /path/to/multiauth
-
Start the services using Docker Compose:
docker compose up -d
-
Verify all services are running:
docker compose ps
-
Open your browser and navigate to:
http://localhost/
-
You'll be redirected to the authentication page
-
Log in with:
- Username:
admin
- Password:
TopSecret
- Username:
-
After successful authentication, you'll see the home page with a link to the vault service.
-
Click the vault link from the home page or navigate directly to:
http://localhost/vault/?rd=/vault/
-
Even though you're already logged into the home service, you'll be prompted to authenticate again with different credentials
-
Log in with:
- Username:
user
- Password:
TopSecret
- Username:
-
After this second authentication, you'll gain access to the vault service.
- APISIX: Acts as the API gateway, routing requests and implementing forward authentication
- OAuth2 Proxy: Provides the authentication layer with custom login templates
- Forward Auth Plugin: APISIX uses this plugin to delegate authentication to OAuth2 Proxy
- Separate Credential Stores: Different .htpasswd files for the two authentication layers
.htpasswd
: Credentials for the home service.htpasswd_vault
: Credentials for the vault serviceapisix.yaml
: APISIX routes and service configurationconfig.yaml
: APISIX global configurationdocker-compose.yml
: Service definitions for the stacktemplates/
: Custom OAuth2 Proxy templates
- If you encounter authentication issues, check that the
.htpasswd
files have the correct permissions. - Verify the cookie settings in the OAuth2 Proxy configurations if sessions aren't persisting correctly.
- For login issues, check the browser's developer tools for cookie and redirect problems.
This is a demonstration setup. For production use, consider:
- Using HTTPS for all communications
- Implementing proper cookie security
- Setting appropriate session timeouts
- Using a proper OAuth2 provider instead of htpasswd authentication