{"id":13598414,"url":"https://github.com/simonw/asgi-auth-github","last_synced_at":"2025-04-19T17:50:31.692Z","repository":{"id":43694591,"uuid":"271315812","full_name":"simonw/asgi-auth-github","owner":"simonw","description":"ASGI middleware that authenticates users against GitHub","archived":false,"fork":false,"pushed_at":"2022-02-23T11:58:30.000Z","size":72,"stargazers_count":35,"open_issues_count":3,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-18T07:53:23.702Z","etag":null,"topics":["asgi","asgi-middleware","authentication","github-api"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-10T15:34:26.000Z","updated_at":"2024-10-11T17:31:16.000Z","dependencies_parsed_at":"2022-09-16T17:10:17.637Z","dependency_job_id":null,"html_url":"https://github.com/simonw/asgi-auth-github","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasgi-auth-github","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasgi-auth-github/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasgi-auth-github/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fasgi-auth-github/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonw","download_url":"https://codeload.github.com/simonw/asgi-auth-github/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249753003,"owners_count":21320645,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","asgi-middleware","authentication","github-api"],"created_at":"2024-08-01T17:00:52.374Z","updated_at":"2025-04-19T17:50:31.648Z","avatar_url":"https://github.com/simonw.png","language":"Python","funding_links":[],"categories":["Authentication"],"sub_categories":[],"readme":"# asgi-auth-github\n\n[![PyPI](https://img.shields.io/pypi/v/asgi-auth-github.svg)](https://pypi.org/project/asgi-auth-github/)\n[![CircleCI](https://circleci.com/gh/simonw/asgi-auth-github.svg?style=svg)](https://circleci.com/gh/simonw/asgi-auth-github)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/asgi-auth-github/blob/master/LICENSE)\n\nASGI middleware that authenticates users against GitHub.\n\n(Originally part of [datasette-auth-github](https://github.com/simonw/datasette-auth-github), now split off as a separate project.)\n\n## Setup instructions\n\n* Install the package - `pip install asgi-auth-github`\n* Create a GitHub OAuth app: https://github.com/settings/applications/new\n* Set the Authorization callback URL to `http://127.0.0.1:8001/-/auth-callback`\n\n## Adding this to your ASGI application\n\n```python\nfrom asgi_auth_github import GitHubAuth\nfrom your_asgi_app import asgi_app\n\n\napp = GitHubAuth(\n    asgi_app,\n    client_id=\"github_client_id\",\n    client_secret=\"github_client_secret\",\n    require_auth=True,\n    # Other options:\n    # cookie_ttl=24 * 60 * 60,\n    # disable_auto_login=True,\n    # allow_users=[\"simonw\"],\n    # allow_orgs=[\"my-org\"],\n    # allow_teams=[\"my-org/engineering\"],\n)\n```\n\nSee the [datasette-auth-github 0.12 documentation](https://github.com/simonw/datasette-auth-github/blob/0.12/README.md) for documentation of the other parameters.\n\nOnce wrapped in this way, your application will redirect users to GitHub to authenticate if they are not yet signed in. Authentication is recorded using a signed cookie.\n\nThe middleware adds a new `\"auth\"` key to the scope containing details of the signed-in user, which is then passed to your application. The contents of the `scope[\"auth\"]` key will look like this:\n\n```json\n{\n    \"id\": \"1234 (their GitHub user ID)\",\n    \"name\": \"Their Display Name\",\n    \"username\": \"their-github-username\",\n    \"email\": \"their-github@email-address.com\",\n    \"ts\": 1562602415\n}\n```\nThe `\"ts\"` value is an integer `time.time()` timestamp representing when the user last signed in.\n\nIf the user is not signed in (and you are not using required authentication) the `\"auth\"` scope key will be set to `None`.\n\n## Example using Starlette\n\nHere's an example using the [Starlette](https://www.starlette.io/) ASGI framework. You'll need to add your `client_id` and `client_secret` to this code before running it.\n\nSave the following as `starlette_demo.py`:\n\n```python\nfrom asgi_auth_github import GitHubAuth\nfrom starlette.applications import Starlette\nfrom starlette.responses import JSONResponse\nfrom starlette.routing import Route\nimport uvicorn\n\napp = Starlette(debug=True)\n\n\nasync def homepage(request):\n    return JSONResponse({\"auth\": request.scope[\"auth\"]})\n\n\napp = Starlette(debug=True, routes=[Route(\"/\", homepage),])\n\n\nauthenticated_app = GitHubAuth(\n    app,\n    client_id=\"...\",\n    client_secret=\"...\",\n    require_auth=True,\n)\n\nif __name__ == \"__main__\":\n    uvicorn.run(authenticated_app, host=\"0.0.0.0\", port=8001)\n```\n\nInstall the dependencies like this:\n\n    pip install uvicorn starlette asgi-auth-github\n\nThen run it with:\n\n    python starlette_demo.py\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fasgi-auth-github","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonw%2Fasgi-auth-github","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fasgi-auth-github/lists"}