-
-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add HTTP Basic Auth support for nginx reverse proxy #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import json | ||
| import logging | ||
| import os | ||
| import platform | ||
| import requests | ||
| from datetime import datetime, timedelta, timezone | ||
| from time import sleep | ||
|
|
||
|
|
@@ -28,6 +30,34 @@ | |
| td1ms = timedelta(milliseconds=1) | ||
|
|
||
|
|
||
| def _patch_client_auth(client, user, password): | ||
| """Replace aw-client HTTP methods to inject Basic Auth credentials.""" | ||
| from aw_client.client import always_raise_for_request_errors | ||
|
|
||
| auth = requests.auth.HTTPBasicAuth(user, password) | ||
| _url = client._url | ||
|
|
||
| @always_raise_for_request_errors | ||
| def _get(self_ref, endpoint, params=None): | ||
| return requests.get(_url(endpoint), params=params, auth=auth) | ||
|
|
||
| @always_raise_for_request_errors | ||
| def _post(self_ref, endpoint, data, params=None): | ||
| headers = {"Content-type": "application/json", "charset": "utf-8"} | ||
| return requests.post(_url(endpoint), data=bytes(json.dumps(data), "utf8"), headers=headers, params=params, auth=auth) | ||
|
|
||
| @always_raise_for_request_errors | ||
| def _delete(self_ref, endpoint, data=None): | ||
| if data is None: | ||
| data = {} | ||
| headers = {"Content-type": "application/json"} | ||
| return requests.delete(_url(endpoint), data=json.dumps(data), headers=headers, auth=auth) | ||
|
|
||
| client._get = lambda endpoint, params=None: _get(client, endpoint, params) | ||
| client._post = lambda endpoint, data, params=None: _post(client, endpoint, data, params) | ||
| client._delete = lambda endpoint, data=None: _delete(client, endpoint, data) | ||
|
Comment on lines
+33
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| class Settings: | ||
| def __init__(self, config_section, timeout=None, poll_time=None): | ||
| # Time without input before we're considering the user as AFK | ||
|
|
@@ -48,6 +78,11 @@ def __init__(self, args, testing=False): | |
| self.client = ActivityWatchClient( | ||
| "aw-watcher-afk", host=args.host, port=args.port, testing=testing | ||
| ) | ||
|
|
||
| if args.auth_user and args.auth_password: | ||
| _patch_client_auth(self.client, args.auth_user, args.auth_password) | ||
| logger.info("HTTP Basic Auth enabled for user: %s", args.auth_user) | ||
|
Comment on lines
+82
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a user sets |
||
|
|
||
| self.bucketname = "{}_{}".format( | ||
| self.client.client_name, self.client.client_hostname | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ | |
| [aw-watcher-afk] | ||
| timeout = 180 | ||
| poll_time = 5 | ||
| host = "" | ||
| port = "" | ||
| auth_user = "" | ||
| auth_password = "" | ||
|
|
||
| [aw-watcher-afk-testing] | ||
| timeout = 20 | ||
|
|
@@ -26,12 +30,16 @@ def parse_args(): | |
|
|
||
| default_poll_time = config["poll_time"] | ||
| default_timeout = config["timeout"] | ||
| default_host = config.get("host", "") or None | ||
| default_port = config.get("port", "") or None | ||
| default_auth_user = config.get("auth_user", "") | ||
| default_auth_password = config.get("auth_password", "") | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="A watcher for keyboard and mouse input to detect AFK state." | ||
| ) | ||
| parser.add_argument("--host", dest="host") | ||
| parser.add_argument("--port", dest="port") | ||
| parser.add_argument("--host", dest="host", default=default_host) | ||
| parser.add_argument("--port", dest="port", default=default_port) | ||
| parser.add_argument( | ||
| "--testing", dest="testing", action="store_true", help="run in testing mode" | ||
| ) | ||
|
|
@@ -47,5 +55,17 @@ def parse_args(): | |
| parser.add_argument( | ||
| "--poll-time", dest="poll_time", type=float, default=default_poll_time | ||
| ) | ||
| parser.add_argument( | ||
| "--auth-user", | ||
| dest="auth_user", | ||
| default=default_auth_user, | ||
| help="Username for HTTP Basic Auth (for nginx-proxied servers)", | ||
| ) | ||
| parser.add_argument( | ||
| "--auth-password", | ||
| dest="auth_password", | ||
| default=default_auth_password, | ||
| help="Password for HTTP Basic Auth (for nginx-proxied servers)", | ||
| ) | ||
|
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Passing a password via CLI argument makes it readable to any user on the system via |
||
| parsed_args = parser.parse_args() | ||
| return parsed_args | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"charset"key is set as a standalone HTTP header rather than as a parameter inside theContent-Typevalue. Per RFC 7231, charset belongs in the media type parameter:application/json; charset=utf-8. As a separate header namedcharset, most servers will ignore it entirely.