authorization: make api key authentication optional

Signed-off-by;  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-11-21 12:31:25 -05:00
parent d4378eb051
commit c83714bfe8
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 5 additions and 3 deletions

View File

@ -76,6 +76,7 @@ class Authorization:
self.login_timeout = config.getint('login_timeout', 90)
self.force_logins = config.getboolean('force_logins', False)
self.default_source = config.get('default_source', "moonraker").lower()
self.enable_api_key = config.getboolean('enable_api_key', True)
if self.default_source not in AUTH_SOURCES:
raise config.error(
"[authorization]: option 'default_source' - Invalid "
@ -734,9 +735,10 @@ class Authorization:
return ost_user
# Check API Key Header
key: Optional[str] = request.headers.get("X-Api-Key")
if key and key == self.api_key:
return self.users[API_USER]
if self.enable_api_key:
key: Optional[str] = request.headers.get("X-Api-Key")
if key and key == self.api_key:
return self.users[API_USER]
# If the force_logins option is enabled and at least one
# user is created this is an unauthorized request