Skip to content

Commit

Permalink
added nginx apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
sinawic committed Dec 2, 2023
1 parent 1fa78aa commit 8556b02
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nginx/apikey_auth/apikeys.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
map $http_apikey $api_client_name {
default "";
"KrtKNkLNGcwKQ56la4jcHwxF" "client_one";
"sqj3Ye0vFW/CM/o7LTSMEMM+" "client_two";
"diXnbzglAWMMIvyEEV3rq7Kt" "client_ten";
}
17 changes: 17 additions & 0 deletions nginx/apikey_auth/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'
services:
backend:
image: ealen/echo-server:latest
restart: always
environment:
- ENABLE__ENVIRONMENT=false
ports:
- 8000:80

nginx:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./apikeys.conf:/etc/nginx/apikeys.conf
ports:
- 9999:9999
4 changes: 4 additions & 0 deletions nginx/apikey_auth/hint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# https://swagger.io/docs/specification/authentication/api-keys/

curl -I 'http://localhost:9999/' --header 'apikey: diXnbzglAWMMIvyEEV3rq7Kt'
29 changes: 29 additions & 0 deletions nginx/apikey_auth/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
http{

include apikeys.conf;
# will provide $api_client_name

server{
listen 9999;
location / {
auth_request /_validate_apikey;
proxy_pass http://backend/;
}

location = /_validate_apikey {
internal;

if ($http_apikey = "") {
return 401; # Unauthorized
}

if ($api_client_name = "") {
return 403; # Forbidden
}

return 204; # OK (no content)
}
}
}

events {}

0 comments on commit 8556b02

Please sign in to comment.