{"action":"closed","issue":{"url":"https://api.github.com/repos/Bogdanp/molten/issues/17","repository_url":"https://api.github.com/repos/Bogdanp/molten","labels_url":"https://api.github.com/repos/Bogdanp/molten/issues/17/labels{/name}","comments_url":"https://api.github.com/repos/Bogdanp/molten/issues/17/comments","events_url":"https://api.github.com/repos/Bogdanp/molten/issues/17/events","html_url":"https://github.com/Bogdanp/molten/issues/17","id":365310120,"node_id":"MDU6SXNzdWUzNjUzMTAxMjA=","number":17,"title":"APIKeySecuritySchema not functioning in openapi docs","user":{"login":"dgonzo","id":361559,"node_id":"MDQ6VXNlcjM2MTU1OQ==","avatar_url":"https://avatars1.githubusercontent.com/u/361559?v=4","gravatar_id":"","url":"https://api.github.com/users/dgonzo","html_url":"https://github.com/dgonzo","followers_url":"https://api.github.com/users/dgonzo/followers","following_url":"https://api.github.com/users/dgonzo/following{/other_user}","gists_url":"https://api.github.com/users/dgonzo/gists{/gist_id}","starred_url":"https://api.github.com/users/dgonzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgonzo/subscriptions","organizations_url":"https://api.github.com/users/dgonzo/orgs","repos_url":"https://api.github.com/users/dgonzo/repos","events_url":"https://api.github.com/users/dgonzo/events{/privacy}","received_events_url":"https://api.github.com/users/dgonzo/received_events","type":"User","site_admin":false},"labels":[{"id":960227845,"node_id":"MDU6TGFiZWw5NjAyMjc4NDU=","url":"https://api.github.com/repos/Bogdanp/molten/labels/bug","name":"bug","color":"d73a4a","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/Bogdanp/molten/milestones/1","html_url":"https://github.com/Bogdanp/molten/milestone/1","labels_url":"https://api.github.com/repos/Bogdanp/molten/milestones/1/labels","id":3716921,"node_id":"MDk6TWlsZXN0b25lMzcxNjkyMQ==","number":1,"title":"v0.6.0","description":"","creator":{"login":"Bogdanp","id":43347,"node_id":"MDQ6VXNlcjQzMzQ3","avatar_url":"https://avatars2.githubusercontent.com/u/43347?v=4","gravatar_id":"","url":"https://api.github.com/users/Bogdanp","html_url":"https://github.com/Bogdanp","followers_url":"https://api.github.com/users/Bogdanp/followers","following_url":"https://api.github.com/users/Bogdanp/following{/other_user}","gists_url":"https://api.github.com/users/Bogdanp/gists{/gist_id}","starred_url":"https://api.github.com/users/Bogdanp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bogdanp/subscriptions","organizations_url":"https://api.github.com/users/Bogdanp/orgs","repos_url":"https://api.github.com/users/Bogdanp/repos","events_url":"https://api.github.com/users/Bogdanp/events{/privacy}","received_events_url":"https://api.github.com/users/Bogdanp/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":1,"state":"open","created_at":"2018-10-06T17:33:07Z","updated_at":"2018-10-06T17:43:37Z","due_on":"2018-10-06T07:00:00Z","closed_at":null},"comments":2,"created_at":"2018-10-01T05:04:28Z","updated_at":"2018-10-06T17:43:37Z","closed_at":"2018-10-06T17:43:37Z","author_association":"NONE","body":"## Checklist\r\n\r\n* [x] Does your title concisely summarize the problem?\r\n* [x] Did you include a minimal, reproducible example?\r\n* [x] What OS are you using?\r\n* [x] What version of molten are you using?\r\n* [x] What did you do?\r\n* [x] What did you expect would happen?\r\n* [x] What happened?\r\n\r\n\r\n## What OS are you using?\r\nmacOS 10.13.6\r\n\r\n## What version of molten are you using?\r\n0.5.2\r\n\r\n## What did you do?\r\nI enabled `APIKeySecurityScheme` in the petstore example using the following modified `petstore.app` (see both variations of `security_schemes`):\r\n```python\r\n\"\"\"An example **molten** application that automatically exposes an\r\nOpenAPI document to represent its structure.\r\n\"\"\"\r\nfrom typing import Any, Callable, Optional, Tuple\r\n\r\nfrom molten import (\r\n    App, Header, Include, ResponseRendererMiddleware, Route, annotate,\r\n    Request, HTTP_200, HTTPError, HTTP_401)\r\nfrom molten.openapi import Metadata, OpenAPIHandler, OpenAPIUIHandler, APIKeySecurityScheme\r\n\r\nfrom . import categories, pets, tags\r\nfrom .database import DatabaseComponent\r\n\r\n\r\ndef auth_middleware(handler: Callable[..., Any]) -> Callable[..., Any]:\r\n    def middleware(x_api_key: Optional[Header]) -> Callable[..., Any]:\r\n        if x_api_key == '58be92dd-61a9-4a27-8efa-b6a0e025439e' or getattr(handler, \"no_auth\", False):\r\n            return handler()\r\n        else:\r\n            raise HTTPError(HTTP_401, {\"error\": \"bad credentials\"})\r\n\r\n    return middleware\r\n\r\n\r\ndef u(request: Request) -> Tuple[str, dict]:\r\n    return HTTP_200, {\"req\": f\"{request.headers!r}\"}\r\n\r\n\r\ndef setup_app():\r\n    get_schema = OpenAPIHandler(\r\n        metadata=Metadata(\r\n            title=\"Pet Store\",\r\n            description=__doc__,\r\n            version=\"0.0.0\",\r\n        ),\r\n        # Option 1\r\n        security_schemes=[APIKeySecurityScheme(name=\"X-API-KEY\", in_=\"header\")],\r\n        # Option 2\r\n        security_schemes=[APIKeySecurityScheme(name=\"apiKey\", in_=\"header\")],\r\n        default_security_scheme=\"apiKey\",\r\n    )\r\n\r\n    get_schema = annotate(no_auth=True)(get_schema)\r\n    get_docs = annotate(no_auth=True)(OpenAPIUIHandler())\r\n\r\n    return App(\r\n        components=[\r\n            DatabaseComponent(),\r\n            categories.CategoryManagerComponent(),\r\n            tags.TagManagerComponent(),\r\n            pets.PetManagerComponent(),\r\n        ],\r\n\r\n        middleware=[\r\n            ResponseRendererMiddleware(),\r\n            auth_middleware,\r\n        ],\r\n\r\n        routes=[\r\n            Include(\"/v1/categories\", categories.routes),\r\n            Include(\"/v1/pets\", pets.routes),\r\n            Include(\"/v1/tags\", tags.routes),\r\n            Route(\"/u\", u),\r\n\r\n            Route(\"/_docs\", get_docs),\r\n            Route(\"/_schema\", get_schema),\r\n        ],\r\n    )\r\n\r\n```\r\n\r\n## What did you expect would happen?\r\nFor either \"Option 1\" or \"Option 2\" above I expected that the apiKey would be used to authenticate calls to secure routes. Instead 'X-API-KEY' is absent from the headers and a \"bad credentials\" error is returned. \r\n\r\nOption 1 results in an empty \"Authorizers available\" dialogue box when clicking on the lock icon for a path but I'd expect that dialogue to offer a place to put the \"X-API-KEY\". The main authorize box does offer the expected dialogue box but it is not applied when executing calls from the docs.\r\n\r\nOption 2 results in the expected dialogue boxes but the apiKey is not applied to calls to routes.\r\n\r\nWhen I exercise the api outside of swagger (e.g. `http :8000/v1/categories X-API-KEY:58be92dd-61a9-4a27-8efa-b6a0e025439e`) authentication functions properly. So, only within swagger is the header not being properly applied.\r\n\r\n## What happened?\r\nI noticed that the [security schemes](https://swagger.io/docs/specification/authentication/api-keys/) in openapi have, effectively, two names. The name of the security and the name of the header:\r\n\r\n```yaml\r\ncomponents:\r\n  securitySchemes:\r\n    ApiKeyAuth:        # arbitrary name for the security scheme\r\n      type: apiKey\r\n      in: header       # can be \"header\", \"query\" or \"cookie\"\r\n      name: X-API-KEY  # name of the header, query parameter or cookie\r\n```\r\n\r\nWhen the arbitraty name is \"apiKey\" the dialogue boxes in the docs function as expected.\r\n\r\nTo get the arbitrary name set in [APIKeySecurityScheme](https://github.com/Bogdanp/molten/blob/2470bfebc9f934f7cfe1f5582046f41f933244f0/molten/openapi/documents.py#L122) the `name` attribute must be set to \"apiKey\" but that results in an incorrect header name."}}
{"actor":{"display_login":"Bogdanp"}}