|
| 1 | +import pytest |
| 2 | +from starlette.status import HTTP_401_UNAUTHORIZED |
| 3 | + |
| 4 | +from ellar.common import Req, get, guards |
| 5 | +from ellar.core import AppFactory, TestClient |
| 6 | +from ellar.core.guard import ( |
| 7 | + APIKeyCookie, |
| 8 | + APIKeyHeader, |
| 9 | + APIKeyQuery, |
| 10 | + HttpBasicAuth, |
| 11 | + HttpBearerAuth, |
| 12 | + HttpDigestAuth, |
| 13 | +) |
| 14 | +from ellar.exceptions import APIException |
| 15 | +from ellar.openapi import OpenAPIDocumentBuilder |
| 16 | +from ellar.serializer import serialize_object |
| 17 | + |
| 18 | + |
| 19 | +class CustomException(APIException): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class QuerySecretKey(APIKeyQuery): |
| 24 | + async def authenticate(self, connection, key): |
| 25 | + if key == "querysecretkey": |
| 26 | + return key |
| 27 | + |
| 28 | + |
| 29 | +class HeaderSecretKey(APIKeyHeader): |
| 30 | + async def authenticate(self, connection, key): |
| 31 | + if key == "headersecretkey": |
| 32 | + return key |
| 33 | + |
| 34 | + |
| 35 | +class HeaderSecretKeyCustomException(HeaderSecretKey): |
| 36 | + exception_class = CustomException |
| 37 | + |
| 38 | + |
| 39 | +class CookieSecretKey(APIKeyCookie): |
| 40 | + openapi_name = "API Key Auth" |
| 41 | + |
| 42 | + async def authenticate(self, connection, key): |
| 43 | + if key == "cookiesecretkey": |
| 44 | + return key |
| 45 | + |
| 46 | + |
| 47 | +class BasicAuth(HttpBasicAuth): |
| 48 | + openapi_name = "API Authentication" |
| 49 | + |
| 50 | + async def authenticate(self, connection, credentials): |
| 51 | + if credentials.username == "admin" and credentials.password == "secret": |
| 52 | + return credentials.username |
| 53 | + |
| 54 | + |
| 55 | +class BearerAuth(HttpBearerAuth): |
| 56 | + openapi_name = "JWT Authentication" |
| 57 | + |
| 58 | + async def authenticate(self, connection, credentials): |
| 59 | + if credentials.credentials == "bearertoken": |
| 60 | + return credentials.credentials |
| 61 | + |
| 62 | + |
| 63 | +class DigestAuth(HttpDigestAuth): |
| 64 | + async def authenticate(self, connection, credentials): |
| 65 | + if credentials.credentials == "digesttoken": |
| 66 | + return credentials.credentials |
| 67 | + |
| 68 | + |
| 69 | +app = AppFactory.create_app() |
| 70 | + |
| 71 | + |
| 72 | +for _path, auth in [ |
| 73 | + ("apikeyquery", QuerySecretKey()), |
| 74 | + ("apikeyheader", HeaderSecretKey()), |
| 75 | + ("apikeycookie", CookieSecretKey()), |
| 76 | + ("basic", BasicAuth()), |
| 77 | + ("bearer", BearerAuth()), |
| 78 | + ("digest", DigestAuth()), |
| 79 | + ("customexception", HeaderSecretKeyCustomException()), |
| 80 | +]: |
| 81 | + |
| 82 | + @get(f"/{_path}") |
| 83 | + @guards(auth) |
| 84 | + def auth_demo_endpoint(request: Req()): |
| 85 | + return {"authentication": request.user} |
| 86 | + |
| 87 | + app.router.append(auth_demo_endpoint) |
| 88 | + |
| 89 | +client = TestClient(app) |
| 90 | + |
| 91 | +BODY_UNAUTHORIZED_DEFAULT = {"detail": "Not authenticated"} |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + "path,kwargs,expected_code,expected_body", |
| 96 | + [ |
| 97 | + ("/apikeyquery", {}, HTTP_401_UNAUTHORIZED, BODY_UNAUTHORIZED_DEFAULT), |
| 98 | + ( |
| 99 | + "/apikeyquery?key=querysecretkey", |
| 100 | + {}, |
| 101 | + 200, |
| 102 | + dict(authentication="querysecretkey"), |
| 103 | + ), |
| 104 | + ("/apikeyheader", {}, HTTP_401_UNAUTHORIZED, BODY_UNAUTHORIZED_DEFAULT), |
| 105 | + ( |
| 106 | + "/apikeyheader", |
| 107 | + dict(headers={"key": "headersecretkey"}), |
| 108 | + 200, |
| 109 | + dict(authentication="headersecretkey"), |
| 110 | + ), |
| 111 | + ("/apikeycookie", {}, HTTP_401_UNAUTHORIZED, BODY_UNAUTHORIZED_DEFAULT), |
| 112 | + ( |
| 113 | + "/apikeycookie", |
| 114 | + dict(cookies={"key": "cookiesecretkey"}), |
| 115 | + 200, |
| 116 | + dict(authentication="cookiesecretkey"), |
| 117 | + ), |
| 118 | + ("/basic", {}, HTTP_401_UNAUTHORIZED, BODY_UNAUTHORIZED_DEFAULT), |
| 119 | + ( |
| 120 | + "/basic", |
| 121 | + dict(headers={"Authorization": "Basic YWRtaW46c2VjcmV0"}), |
| 122 | + 200, |
| 123 | + dict(authentication="admin"), |
| 124 | + ), |
| 125 | + ( |
| 126 | + "/basic", |
| 127 | + dict(headers={"Authorization": "YWRtaW46c2VjcmV0"}), |
| 128 | + 200, |
| 129 | + dict(authentication="admin"), |
| 130 | + ), |
| 131 | + ( |
| 132 | + "/basic", |
| 133 | + dict(headers={"Authorization": "Basic invalid"}), |
| 134 | + HTTP_401_UNAUTHORIZED, |
| 135 | + {"detail": "Invalid authentication credentials"}, |
| 136 | + ), |
| 137 | + ( |
| 138 | + "/basic", |
| 139 | + dict(headers={"Authorization": "some invalid value"}), |
| 140 | + HTTP_401_UNAUTHORIZED, |
| 141 | + BODY_UNAUTHORIZED_DEFAULT, |
| 142 | + ), |
| 143 | + ("/bearer", {}, 401, BODY_UNAUTHORIZED_DEFAULT), |
| 144 | + ( |
| 145 | + "/bearer", |
| 146 | + dict(headers={"Authorization": "Bearer bearertoken"}), |
| 147 | + 200, |
| 148 | + dict(authentication="bearertoken"), |
| 149 | + ), |
| 150 | + ( |
| 151 | + "/bearer", |
| 152 | + dict(headers={"Authorization": "Invalid bearertoken"}), |
| 153 | + HTTP_401_UNAUTHORIZED, |
| 154 | + {"detail": "Invalid authentication credentials"}, |
| 155 | + ), |
| 156 | + ("/digest", {}, 401, BODY_UNAUTHORIZED_DEFAULT), |
| 157 | + ( |
| 158 | + "/digest", |
| 159 | + dict(headers={"Authorization": "Digest digesttoken"}), |
| 160 | + 200, |
| 161 | + dict(authentication="digesttoken"), |
| 162 | + ), |
| 163 | + ( |
| 164 | + "/digest", |
| 165 | + dict(headers={"Authorization": "Invalid digesttoken"}), |
| 166 | + HTTP_401_UNAUTHORIZED, |
| 167 | + {"detail": "Invalid authentication credentials"}, |
| 168 | + ), |
| 169 | + ("/customexception", {}, HTTP_401_UNAUTHORIZED, BODY_UNAUTHORIZED_DEFAULT), |
| 170 | + ( |
| 171 | + "/customexception", |
| 172 | + dict(headers={"key": "headersecretkey"}), |
| 173 | + 200, |
| 174 | + dict(authentication="headersecretkey"), |
| 175 | + ), |
| 176 | + ], |
| 177 | +) |
| 178 | +def test_auth(path, kwargs, expected_code, expected_body): |
| 179 | + response = client.get(path, **kwargs) |
| 180 | + assert response.status_code == expected_code |
| 181 | + assert response.json() == expected_body |
| 182 | + |
| 183 | + |
| 184 | +def test_auth_schema(): |
| 185 | + document = serialize_object(OpenAPIDocumentBuilder().build_document(app)) |
| 186 | + assert document["components"]["securitySchemes"] == { |
| 187 | + "API Key Auth": {"type": "apiKey", "in": "cookie", "name": "API Key Auth"}, |
| 188 | + "HeaderSecretKey": { |
| 189 | + "type": "apiKey", |
| 190 | + "in": "header", |
| 191 | + "name": "HeaderSecretKey", |
| 192 | + }, |
| 193 | + "QuerySecretKey": {"type": "apiKey", "in": "query", "name": "QuerySecretKey"}, |
| 194 | + "API Authentication": { |
| 195 | + "type": "http", |
| 196 | + "scheme": "basic", |
| 197 | + "name": "API Authentication", |
| 198 | + }, |
| 199 | + "JWT Authentication": { |
| 200 | + "type": "http", |
| 201 | + "scheme": "bearer", |
| 202 | + "name": "JWT Authentication", |
| 203 | + }, |
| 204 | + "HeaderSecretKeyCustomException": { |
| 205 | + "type": "apiKey", |
| 206 | + "in": "header", |
| 207 | + "name": "HeaderSecretKeyCustomException", |
| 208 | + }, |
| 209 | + "DigestAuth": {"type": "http", "scheme": "digest", "name": "DigestAuth"}, |
| 210 | + } |
0 commit comments