{"id":23889556,"url":"https://github.com/o-x-l/nginx-auth-server","last_synced_at":"2026-05-17T15:09:04.929Z","repository":{"id":169131847,"uuid":"644769809","full_name":"O-X-L/nginx-auth-server","owner":"O-X-L","description":"Nginx 'auth_request' server","archived":false,"fork":false,"pushed_at":"2023-05-29T17:10:11.000Z","size":43,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-24T11:42:46.858Z","etag":null,"topics":["auth","authentication","google-authenticator","ldap","libpam-google-authenticator","nginx","pam","totp"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/O-X-L.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-24T08:11:39.000Z","updated_at":"2025-06-28T22:50:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"d908e940-8df1-4d8f-8603-a898f0f303df","html_url":"https://github.com/O-X-L/nginx-auth-server","commit_stats":null,"previous_names":["superstes/nginx-auth-server","o-x-l/nginx-auth-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/O-X-L/nginx-auth-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/O-X-L%2Fnginx-auth-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/O-X-L%2Fnginx-auth-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/O-X-L%2Fnginx-auth-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/O-X-L%2Fnginx-auth-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/O-X-L","download_url":"https://codeload.github.com/O-X-L/nginx-auth-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/O-X-L%2Fnginx-auth-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33143276,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T09:28:26.183Z","status":"ssl_error","status_checked_at":"2026-05-17T09:27:52.702Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","google-authenticator","ldap","libpam-google-authenticator","nginx","pam","totp"],"created_at":"2025-01-04T10:09:48.834Z","updated_at":"2026-05-17T15:09:04.899Z","avatar_url":"https://github.com/O-X-L.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nginx 'auth_request' Server\n\nA minimal [Python3-Flask](https://flask.palletsprojects.com/en/2.3.x/quickstart/) web-server that can be used as 'auth_request' target by Nginx.\n\nWhat authentication methods are supported?\n\n* LDAP using [ldap3](https://pypi.org/project/ldap3/)\n* [PAM](https://github.com/linux-pam/linux-pam) using [python-pam](https://pypi.org/project/python-pam/)\n  * System-User authentication (_local linux users_)\n  * Time-based Tokens (_TOTP_) using [libpam-google-authenticator](https://github.com/google/google-authenticator-libpam)\n  * any custom PAM module\n* 2-Factor Authentication\n\nIf you want to use OAuth2 =\u003e you should look at [oauth2-proxy](https://github.com/oauth2-proxy/oauth2-proxy/blob/master/contrib/local-environment/nginx.conf) instead.\n\n## Install\n\n```bash\n# edit and replace placeholders in:\n#   lib/config.py\n#   lib/templates/login.html\n#   systemd/auth-server.service\n#   pam/*\n\nPATH_LIB='/var/local/lib/nginx_auth'\nPATH_TOTP='/etc/auth/totp'\nPATH_LDAP='/etc/auth/ldap'\nSERVICE='nginx-auth-server'\nAUTH_USER='nginx-auth'\n\nuseradd \"$AUTH_USER\"\nmkdir -p \"$PATH_LIB\"\ncp -r lib/* \"$PATH_LIB\"\nchown -R \"$AUTH_USER\":\"$AUTH_USER\" \"$PATH_LIB\" \nchmod -R 750 \"$PATH_LIB\" \n\n# create virtual environment\napt install python3-pip\npython3 -m pip install virtualenv\npython3 -m virtualenv \"${PATH_LIB}/venv\"\nsource \"${PATH_LIB}/venv/bin/activate\"\npython3 -m pip install flask waitress pycryptodome\n\n# to use TOTP-authentication\napt install libpam-google-authenticator qrencode\nmkdir -p \"$PATH_TOTP\"\nchown -R \"$AUTH_USER\":\"$AUTH_USER\" \"$PATH_TOTP\" \nchmod 750 \"$PATH_TOTP\" \ncp pam/nginx-auth-totp /etc/pam.d/nginx-auth-totp\n## to generate keys:\ngoogle-authenticator -s \"${PATH_TOTP}/${USER}.key\" --time-based --issuer=\"$YOUR_SERVICE\" --label=\"$USER\"\nchown \"$AUTH_USER\":\"$AUTH_USER\" \"${PATH_TOTP}/${USER}.key\"\nchmod 600 \"${PATH_TOTP}/${USER}.key\"\n\n# to use LDAP-authentication\npython3 -m pip install ldap3\nmkdir -p \"$PATH_LDAP\"\nchown -R \"$AUTH_USER\":\"$AUTH_USER\" \"$PATH_LDAP\" \nchmod 750 \"$PATH_LDAP\" \n#   add certificates (at least CA-Cert) to PATH_LDAP\n\n# to use system-user authentication\npython3 -m pip install python-pam six toml\nusermod -a -G shadow \"$AUTH_USER\"\ncp pam/nginx-auth-system /etc/pam.d/nginx-auth-system\n\n# add systemd service\ncp systemd/nginx-auth-server.service \"/etc/systemd/system/${SERVICE}.service\"\nsystemctl daemon-reload\nsystemctl enable \"${SERVICE}.service\"\nsystemctl start \"${SERVICE}.service\"\n```\n\nIf you don't want to install all python-modules - you might need to remove imports to unneeded authentication methods, so you don't get \"ModuleNotFoundError\" exceptions.\n\n## Example\n\n### Authentication config\n\nEdit: lib/config.py\n\n```python3\nENCRYPTION_KEY='YOUR-SECRET-RANDOM-SECRET'\n\nAUTH_USER_TYPE = 'system'  # system,ldap,totp; totp only valid if 'TOKEN_TYPE' is None\nAUTH_TOKEN_TYPE = None  # if 2FA should be used; 'totp' or None\n\nLDAP_CONFIG=dict(\n    tls=True,\n    server='ldap.your.org',\n    port=636,\n    ca='/etc/auth/ldap/ca.crt',  # to validate server-cert\n    use_client_cert=False,\n    # client_cert='/etc/auth/ldap/client.crt',\n    # client_key='/etc/auth/ldap/client.key',\n    # client_key_pwd='',\n    base_dn='OU=Base,DC=YOUR,DC=ORG',\n    bind=dict(\n        user='YOUR-BIND-USER',\n        pwd='YOUR-BIND-PWD',\n    ),\n    filter='(\u0026(mail=%s)(objectClass=person)(memberOf:=CN=nginx,OU=Groups,DC=YOUR,DC=ORG))',\n)\n```\n\n### Nginx config\n\n```\n# you should limit the request-rate on the login location\nlimit_req_zone $binary_remote_addr zone=login_sec:10m rate=5r/s;\nlimit_req_zone $binary_remote_addr zone=login_min:20m rate=30r/m;\nlimit_req_status 429;\n\nserver {\n  ...\n\n  location {\n    ...\n    auth_request /auth;\n    error_page 401 = /auth/login;\n    auth_request_set $user $upstream_http_x_auth_request_user;\n    proxy_set_header X-User $user;\n  }\n\n  # authentication\n  location /auth {\n    # access_log syslog:server=unix:/dev/log,tag=nginx_auth,nohostname,severity=info combined;\n    # error_log  syslog:server=unix:/dev/log,tag=nginx_auth,nohostname,severity=error;\n    internal;\n    proxy_pass http://127.0.0.1:8080;\n    proxy_pass_request_body off;\n    proxy_pass_request_headers on;\n    proxy_set_header Content-Length \"\";\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Scheme $scheme;\n    proxy_set_header X-Auth-Request-Redirect $request_uri;\n  }\n  location /auth/login {\n    # access_log syslog:server=unix:/dev/log,tag=nginx_auth_login,nohostname,severity=info combined;\n    # error_log  syslog:server=unix:/dev/log,tag=nginx_auth_login,nohostname,severity=error;\n    limit_req zone=login_sec;\n    limit_req zone=login_min;\n    proxy_pass http://127.0.0.1:8080;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Scheme $scheme;\n    proxy_set_header X-Auth-Request-Redirect $request_uri;\n  }\n}\n```\n\n## Known issues\n\n### libpam-google-authenticator 'user id/group id'\n\nThis error only occurs if the totp-validation is done by a non-root user.\n\nErrors:\n```\nFailed to change group id for user ...\nFailed to change user id to ... (should not appear if 'user=serviceuser' is used inside the pam-file)\n```\n\nSolution:\n```bash\n# make sure the python-binary inside your virtual-environment is no link\nls -l \"${PATH_LIB}/venv/python\"\n# else you might need to copy the currently linked binary inside your venv\ncp /usr/bin/python3 \"${PATH_LIB}/venv/python/bin/python\"\n# make sure to limit the execution privileges on the binary\nchown \"root:${AUTH_USER}\" \"${PATH_LIB}/venv/python/bin/python\"\nchmod 750 \"${PATH_LIB}/venv/python/bin/python\"\n\n# after that you can add system capabilities to the binary\n## if you encounter the 'Failed to change group id for user' error:\nsudo setcap cap_setgid=+eip \"${PATH_LIB}/venv/python/bin/python\"\n## if you encounter the 'Failed to change user id to' error:\nsudo setcap cap_setuid=+eip \"${PATH_LIB}/venv/python/bin/python\"\n```\n\n**WARNING:**\nEspecially the 'cap_setuid' can lead to major security issues if not handled with care!\n\n## System-user password-check fails\n\nThis error only occurs if the system-user validation is done by a non-root user.\n\nError:\n```\nunix_chkpwd: password check failed for user\n```\n\nSolution:\n```\n# add the $AUTH_USER to the group 'shadow' so it can read the system-users password hashes\nusermod -a -G shadow \"$AUTH_USER\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fo-x-l%2Fnginx-auth-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fo-x-l%2Fnginx-auth-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fo-x-l%2Fnginx-auth-server/lists"}