Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions data/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server {
listen 80;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
auth_request /auth/check;
}

location /auth {
proxy_pass http://totp-auth:8000; # This is the TOTP Server
proxy_set_header X-Original-URI $request_uri;
}

# This ensures that if the TOTP server returns 401 we redirect to login
error_page 401 = @error401;
location @error401 {
return 302 /auth/login;
}
}
5 changes: 3 additions & 2 deletions main.py → data/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TOKEN_LIFETIME = 60 * 60 * 24
LAST_LOGIN_ATTEMPT = 0
SECRET = open('.totp_secret').read().strip()
SECURE_COOKIE = False # Switch to False for testing on http connections without TLS
FORM = """
<html>
<head>
Expand Down Expand Up @@ -84,7 +85,7 @@ def do_GET(self):
cookie = http.cookies.SimpleCookie()
cookie["token"] = '***'
cookie["token"]["path"] = '/'
cookie["token"]["secure"] = True
cookie["token"]["secure"] = SECURE_COOKIE
self.send_header('Set-Cookie', cookie.output(header=''))
self.send_header('Location', '/')
self.end_headers()
Expand All @@ -111,7 +112,7 @@ def do_POST(self):
cookie = http.cookies.SimpleCookie()
cookie["token"] = TOKEN_MANAGER.generate()
cookie["token"]["path"] = "/"
cookie["token"]["secure"] = True
cookie["token"]["secure"] = SECURE_COOKIE

self.send_response(302)
self.send_header('Set-Cookie', cookie.output(header=''))
Expand Down
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: '2'
services:
totp-auth:
image: alpine
container_name: totp-auth

volumes:
- ./data:/opt
environment:
TZ: Europe/Berlin
SECRET: 3EYUCWBS2LN7AJGV
expose:
- 80

command: >
sh -c -x '
apk add tzdata python3
&& date
&& pip3 install --upgrade pip pyotp
&& cd /opt/
&& echo $$SECRET > .totp_secret
&& python3 /opt/main.py'


totp-example:
image: nginx
container_name: totp-example
volumes:
- ./data/conf.d:/etc/nginx/conf.d
ports:
- "8080:80"