We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
fix key error in dictionary
## What type of PR?

(Feature, enhancement, bug-fix, documentation)

## What does this PR do?

### Related issue(s)
- Auto close an issue like: closes #2145

## Prerequisites
Before we can consider review and merge, please make sure the following list is done and checked.
If an entry in not applicable, you can check it or remove it from the list.

- [ ] In case of feature or enhancement: documentation updated accordingly
- [ ] Unless it's docs or a minor change: add [changelog](https://mailu.io/master/contributors/workflow.html#changelog) entry file.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `core/admin/mailu/internal/views/auth.py`
Content:
```
1 from mailu import models, utils
2 from mailu.internal import internal, nginx
3 from flask import current_app as app
4 
5 import flask
6 import flask_login
7 import base64
8 
9 @internal.route("/auth/email")
10 def nginx_authentication():
11     """ Main authentication endpoint for Nginx email server
12     """
13     client_ip = flask.request.headers["Client-Ip"]
14     headers = flask.request.headers
15     if headers["Auth-Port"] == '25' and headers['Auth-Method'] == 'plain':
16         response = flask.Response()
17         response.headers['Auth-Status'] = 'AUTH not supported'
18         response.headers['Auth-Error-Code'] = '502 5.5.1'
19         utils.limiter.rate_limit_ip(client_ip)
20         return response
21     if utils.limiter.should_rate_limit_ip(client_ip):
22         status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')
23         response = flask.Response()
24         response.headers['Auth-Status'] = status
25         response.headers['Auth-Error-Code'] = code
26         if int(flask.request.headers['Auth-Login-Attempt']) < 10:
27             response.headers['Auth-Wait'] = '3'
28         return response
29     headers = nginx.handle_authentication(flask.request.headers)
30     response = flask.Response()
31     for key, value in headers.items():
32         response.headers[key] = str(value)
33     is_valid_user = False
34     is_from_webmail = headers['Auth-Port'] in ['10143', '10025']
35     if response.headers.get("Auth-User-Exists"):
36         username = response.headers["Auth-User"]
37         if utils.limiter.should_rate_limit_user(username, client_ip):
38             # FIXME could be done before handle_authentication()
39             status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')
40             response = flask.Response()
41             response.headers['Auth-Status'] = status
42             response.headers['Auth-Error-Code'] = code
43             if int(flask.request.headers['Auth-Login-Attempt']) < 10:
44                 response.headers['Auth-Wait'] = '3'
45             return response
46         is_valid_user = True
47     if headers.get("Auth-Status") == "OK":
48         utils.limiter.exempt_ip_from_ratelimits(client_ip)
49     elif is_valid_user:
50         utils.limiter.rate_limit_user(username, client_ip)
51     elif not is_from_webmail:
52         utils.limiter.rate_limit_ip(client_ip)
53     return response
54 
55 @internal.route("/auth/admin")
56 def admin_authentication():
57     """ Fails if the user is not an authenticated admin.
58     """
59     if (not flask_login.current_user.is_anonymous
60         and flask_login.current_user.global_admin
61         and flask_login.current_user.enabled):
62         return ""
63     return flask.abort(403)
64 
65 @internal.route("/auth/user")
66 def user_authentication():
67     """ Fails if the user is not authenticated.
68     """
69     if (not flask_login.current_user.is_anonymous
70         and flask_login.current_user.enabled):
71         response = flask.Response()
72         email = flask_login.current_user.get_id()
73         response.headers["X-User"] = models.IdnaEmail.process_bind_param(flask_login, email, "")
74         response.headers["X-User-Token"] = utils.gen_temp_token(email, flask.session)
75         return response
76     return flask.abort(403)
77 
78 
79 @internal.route("/auth/basic")
80 def basic_authentication():
81     """ Tries to authenticate using the Authorization header.
82     """
83     client_ip = flask.request.headers.get('X-Real-IP', flask.request.remote_addr)
84     if utils.limiter.should_rate_limit_ip(client_ip):
85         response = flask.Response(status=401)
86         response.headers["WWW-Authenticate"] = 'Basic realm="Authentication rate limit from one source exceeded"'
87         response.headers['Retry-After'] = '60'
88         return response
89     authorization = flask.request.headers.get("Authorization")
90     if authorization and authorization.startswith("Basic "):
91         encoded = authorization.replace("Basic ", "")
92         user_email, password = base64.b64decode(encoded).split(b":", 1)
93         user_email = user_email.decode("utf8")
94         if utils.limiter.should_rate_limit_user(user_email, client_ip):
95             response = flask.Response(status=401)
96             response.headers["WWW-Authenticate"] = 'Basic realm="Authentication rate limit for this username exceeded"'
97             response.headers['Retry-After'] = '60'
98             return response
99         user = models.User.query.get(user_email)
100         if user and nginx.check_credentials(user, password.decode('utf-8'), client_ip, "web"):
101             response = flask.Response()
102             response.headers["X-User"] = models.IdnaEmail.process_bind_param(flask_login, user.email, "")
103             utils.limiter.exempt_ip_from_ratelimits(client_ip)
104             return response
105         utils.limiter.rate_limit_user(user_email, client_ip) if user else utils.limiter.rate_limit_ip(client_ip)
106     response = flask.Response(status=401)
107     response.headers["WWW-Authenticate"] = 'Basic realm="Login Required"'
108     return response
109
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/core/admin/mailu/internal/views/auth.py b/core/admin/mailu/internal/views/auth.py
--- a/core/admin/mailu/internal/views/auth.py
+++ b/core/admin/mailu/internal/views/auth.py
@@ -18,7 +18,8 @@
         response.headers['Auth-Error-Code'] = '502 5.5.1'
         utils.limiter.rate_limit_ip(client_ip)
         return response
-    if utils.limiter.should_rate_limit_ip(client_ip):
+    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']
+    if not is_from_webmail and utils.limiter.should_rate_limit_ip(client_ip):
         status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')
         response = flask.Response()
         response.headers['Auth-Status'] = status
@@ -31,7 +32,6 @@
     for key, value in headers.items():
         response.headers[key] = str(value)
     is_valid_user = False
-    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']
     if response.headers.get("Auth-User-Exists"):
         username = response.headers["Auth-User"]
         if utils.limiter.should_rate_limit_user(username, client_ip):

{"golden_diff": "diff --git a/core/admin/mailu/internal/views/auth.py b/core/admin/mailu/internal/views/auth.py\n--- a/core/admin/mailu/internal/views/auth.py\n+++ b/core/admin/mailu/internal/views/auth.py\n@@ -18,7 +18,8 @@\n         response.headers['Auth-Error-Code'] = '502 5.5.1'\n         utils.limiter.rate_limit_ip(client_ip)\n         return response\n-    if utils.limiter.should_rate_limit_ip(client_ip):\n+    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']\n+    if not is_from_webmail and utils.limiter.should_rate_limit_ip(client_ip):\n         status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')\n         response = flask.Response()\n         response.headers['Auth-Status'] = status\n@@ -31,7 +32,6 @@\n     for key, value in headers.items():\n         response.headers[key] = str(value)\n     is_valid_user = False\n-    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']\n     if response.headers.get(\"Auth-User-Exists\"):\n         username = response.headers[\"Auth-User\"]\n         if utils.limiter.should_rate_limit_user(username, client_ip):\n", "issue": "fix key error in dictionary\n## What type of PR?\r\n\r\n(Feature, enhancement, bug-fix, documentation)\r\n\r\n## What does this PR do?\r\n\r\n### Related issue(s)\r\n- Auto close an issue like: closes #2145\r\n\r\n## Prerequisites\r\nBefore we can consider review and merge, please make sure the following list is done and checked.\r\nIf an entry in not applicable, you can check it or remove it from the list.\r\n\r\n- [ ] In case of feature or enhancement: documentation updated accordingly\r\n- [ ] Unless it's docs or a minor change: add [changelog](https://mailu.io/master/contributors/workflow.html#changelog) entry file.\r\n\n", "before_files": [{"content": "from mailu import models, utils\nfrom mailu.internal import internal, nginx\nfrom flask import current_app as app\n\nimport flask\nimport flask_login\nimport base64\n\n@internal.route(\"/auth/email\")\ndef nginx_authentication():\n    \"\"\" Main authentication endpoint for Nginx email server\n    \"\"\"\n    client_ip = flask.request.headers[\"Client-Ip\"]\n    headers = flask.request.headers\n    if headers[\"Auth-Port\"] == '25' and headers['Auth-Method'] == 'plain':\n        response = flask.Response()\n        response.headers['Auth-Status'] = 'AUTH not supported'\n        response.headers['Auth-Error-Code'] = '502 5.5.1'\n        utils.limiter.rate_limit_ip(client_ip)\n        return response\n    if utils.limiter.should_rate_limit_ip(client_ip):\n        status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')\n        response = flask.Response()\n        response.headers['Auth-Status'] = status\n        response.headers['Auth-Error-Code'] = code\n        if int(flask.request.headers['Auth-Login-Attempt']) < 10:\n            response.headers['Auth-Wait'] = '3'\n        return response\n    headers = nginx.handle_authentication(flask.request.headers)\n    response = flask.Response()\n    for key, value in headers.items():\n        response.headers[key] = str(value)\n    is_valid_user = False\n    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']\n    if response.headers.get(\"Auth-User-Exists\"):\n        username = response.headers[\"Auth-User\"]\n        if utils.limiter.should_rate_limit_user(username, client_ip):\n            # FIXME could be done before handle_authentication()\n            status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')\n            response = flask.Response()\n            response.headers['Auth-Status'] = status\n            response.headers['Auth-Error-Code'] = code\n            if int(flask.request.headers['Auth-Login-Attempt']) < 10:\n                response.headers['Auth-Wait'] = '3'\n            return response\n        is_valid_user = True\n    if headers.get(\"Auth-Status\") == \"OK\":\n        utils.limiter.exempt_ip_from_ratelimits(client_ip)\n    elif is_valid_user:\n        utils.limiter.rate_limit_user(username, client_ip)\n    elif not is_from_webmail:\n        utils.limiter.rate_limit_ip(client_ip)\n    return response\n\n@internal.route(\"/auth/admin\")\ndef admin_authentication():\n    \"\"\" Fails if the user is not an authenticated admin.\n    \"\"\"\n    if (not flask_login.current_user.is_anonymous\n        and flask_login.current_user.global_admin\n        and flask_login.current_user.enabled):\n        return \"\"\n    return flask.abort(403)\n\n@internal.route(\"/auth/user\")\ndef user_authentication():\n    \"\"\" Fails if the user is not authenticated.\n    \"\"\"\n    if (not flask_login.current_user.is_anonymous\n        and flask_login.current_user.enabled):\n        response = flask.Response()\n        email = flask_login.current_user.get_id()\n        response.headers[\"X-User\"] = models.IdnaEmail.process_bind_param(flask_login, email, \"\")\n        response.headers[\"X-User-Token\"] = utils.gen_temp_token(email, flask.session)\n        return response\n    return flask.abort(403)\n\n\n@internal.route(\"/auth/basic\")\ndef basic_authentication():\n    \"\"\" Tries to authenticate using the Authorization header.\n    \"\"\"\n    client_ip = flask.request.headers.get('X-Real-IP', flask.request.remote_addr)\n    if utils.limiter.should_rate_limit_ip(client_ip):\n        response = flask.Response(status=401)\n        response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Authentication rate limit from one source exceeded\"'\n        response.headers['Retry-After'] = '60'\n        return response\n    authorization = flask.request.headers.get(\"Authorization\")\n    if authorization and authorization.startswith(\"Basic \"):\n        encoded = authorization.replace(\"Basic \", \"\")\n        user_email, password = base64.b64decode(encoded).split(b\":\", 1)\n        user_email = user_email.decode(\"utf8\")\n        if utils.limiter.should_rate_limit_user(user_email, client_ip):\n            response = flask.Response(status=401)\n            response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Authentication rate limit for this username exceeded\"'\n            response.headers['Retry-After'] = '60'\n            return response\n        user = models.User.query.get(user_email)\n        if user and nginx.check_credentials(user, password.decode('utf-8'), client_ip, \"web\"):\n            response = flask.Response()\n            response.headers[\"X-User\"] = models.IdnaEmail.process_bind_param(flask_login, user.email, \"\")\n            utils.limiter.exempt_ip_from_ratelimits(client_ip)\n            return response\n        utils.limiter.rate_limit_user(user_email, client_ip) if user else utils.limiter.rate_limit_ip(client_ip)\n    response = flask.Response(status=401)\n    response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Login Required\"'\n    return response\n", "path": "core/admin/mailu/internal/views/auth.py"}], "after_files": [{"content": "from mailu import models, utils\nfrom mailu.internal import internal, nginx\nfrom flask import current_app as app\n\nimport flask\nimport flask_login\nimport base64\n\n@internal.route(\"/auth/email\")\ndef nginx_authentication():\n    \"\"\" Main authentication endpoint for Nginx email server\n    \"\"\"\n    client_ip = flask.request.headers[\"Client-Ip\"]\n    headers = flask.request.headers\n    if headers[\"Auth-Port\"] == '25' and headers['Auth-Method'] == 'plain':\n        response = flask.Response()\n        response.headers['Auth-Status'] = 'AUTH not supported'\n        response.headers['Auth-Error-Code'] = '502 5.5.1'\n        utils.limiter.rate_limit_ip(client_ip)\n        return response\n    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']\n    if not is_from_webmail and utils.limiter.should_rate_limit_ip(client_ip):\n        status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')\n        response = flask.Response()\n        response.headers['Auth-Status'] = status\n        response.headers['Auth-Error-Code'] = code\n        if int(flask.request.headers['Auth-Login-Attempt']) < 10:\n            response.headers['Auth-Wait'] = '3'\n        return response\n    headers = nginx.handle_authentication(flask.request.headers)\n    response = flask.Response()\n    for key, value in headers.items():\n        response.headers[key] = str(value)\n    is_valid_user = False\n    if response.headers.get(\"Auth-User-Exists\"):\n        username = response.headers[\"Auth-User\"]\n        if utils.limiter.should_rate_limit_user(username, client_ip):\n            # FIXME could be done before handle_authentication()\n            status, code = nginx.get_status(flask.request.headers['Auth-Protocol'], 'ratelimit')\n            response = flask.Response()\n            response.headers['Auth-Status'] = status\n            response.headers['Auth-Error-Code'] = code\n            if int(flask.request.headers['Auth-Login-Attempt']) < 10:\n                response.headers['Auth-Wait'] = '3'\n            return response\n        is_valid_user = True\n    if headers.get(\"Auth-Status\") == \"OK\":\n        utils.limiter.exempt_ip_from_ratelimits(client_ip)\n    elif is_valid_user:\n        utils.limiter.rate_limit_user(username, client_ip)\n    elif not is_from_webmail:\n        utils.limiter.rate_limit_ip(client_ip)\n    return response\n\n@internal.route(\"/auth/admin\")\ndef admin_authentication():\n    \"\"\" Fails if the user is not an authenticated admin.\n    \"\"\"\n    if (not flask_login.current_user.is_anonymous\n        and flask_login.current_user.global_admin\n        and flask_login.current_user.enabled):\n        return \"\"\n    return flask.abort(403)\n\n@internal.route(\"/auth/user\")\ndef user_authentication():\n    \"\"\" Fails if the user is not authenticated.\n    \"\"\"\n    if (not flask_login.current_user.is_anonymous\n        and flask_login.current_user.enabled):\n        response = flask.Response()\n        email = flask_login.current_user.get_id()\n        response.headers[\"X-User\"] = models.IdnaEmail.process_bind_param(flask_login, email, \"\")\n        response.headers[\"X-User-Token\"] = utils.gen_temp_token(email, flask.session)\n        return response\n    return flask.abort(403)\n\n\n@internal.route(\"/auth/basic\")\ndef basic_authentication():\n    \"\"\" Tries to authenticate using the Authorization header.\n    \"\"\"\n    client_ip = flask.request.headers.get('X-Real-IP', flask.request.remote_addr)\n    if utils.limiter.should_rate_limit_ip(client_ip):\n        response = flask.Response(status=401)\n        response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Authentication rate limit from one source exceeded\"'\n        response.headers['Retry-After'] = '60'\n        return response\n    authorization = flask.request.headers.get(\"Authorization\")\n    if authorization and authorization.startswith(\"Basic \"):\n        encoded = authorization.replace(\"Basic \", \"\")\n        user_email, password = base64.b64decode(encoded).split(b\":\", 1)\n        user_email = user_email.decode(\"utf8\")\n        if utils.limiter.should_rate_limit_user(user_email, client_ip):\n            response = flask.Response(status=401)\n            response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Authentication rate limit for this username exceeded\"'\n            response.headers['Retry-After'] = '60'\n            return response\n        user = models.User.query.get(user_email)\n        if user and nginx.check_credentials(user, password.decode('utf-8'), client_ip, \"web\"):\n            response = flask.Response()\n            response.headers[\"X-User\"] = models.IdnaEmail.process_bind_param(flask_login, user.email, \"\")\n            utils.limiter.exempt_ip_from_ratelimits(client_ip)\n            return response\n        utils.limiter.rate_limit_user(user_email, client_ip) if user else utils.limiter.rate_limit_ip(client_ip)\n    response = flask.Response(status=401)\n    response.headers[\"WWW-Authenticate\"] = 'Basic realm=\"Login Required\"'\n    return response\n", "path": "core/admin/mailu/internal/views/auth.py"}]}