Created
October 13, 2019 13:36
-
-
Save ananis25/3642da256bbcdda5bc0e06b5ea4cfad9 to your computer and use it in GitHub Desktop.
asgi app to provide OAuth using Github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uvicorn | |
from http.cookies import SimpleCookie | |
from urllib.parse import parse_qsl | |
from datasette_auth_github import GitHubAuth, utils | |
class GitHubAuthProxy(GitHubAuth): | |
login_path = "/-/login" | |
def make_redirect_cookie(self, request_scope): | |
"""cookie to tell browser where to redirect post authentication""" | |
redirect_cookie = SimpleCookie() | |
qs = dict( | |
parse_qsl(request_scope["query_string"].decode("utf8")) | |
) | |
redirect_cookie[self.redirect_cookie_name] = qs['requestUri'] | |
redirect_cookie[self.redirect_cookie_name]["path"] = "/" | |
return redirect_cookie | |
async def handle_require_auth(self, scope, receive, send): | |
if scope.get("path") == self.login_path: | |
return await super().handle_require_auth(scope, receive, send) | |
else: | |
await utils.send_html(send, 'redirect to login screen', status=401) | |
return | |
async def blank_app(scope, receive, send): | |
await send( | |
{ | |
"type": "http.response.start", | |
"status": 200, | |
"headers": [[b"content-type", b"text/plain"]], | |
} | |
) | |
await send({"type": "http.response.body", "body": b""}) | |
app = GitHubAuthProxy( | |
blank_app, | |
client_id = "xx", | |
client_secret = "xx", | |
require_auth = True, | |
disable_auto_login=True | |
) | |
if __name__ == "__main__": | |
uvicorn.run(app, port=6000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment