This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from trevorwang/master
add Github OAuth2 login handler
- Loading branch information
Showing
7 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# coding: utf-8 | ||
# | ||
|
||
from .openid import OpenIdMixin, AuthError | ||
from .openid import OpenIdMixin, AuthError | ||
from .github import GithubOAuth2Mixin |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import urllib.parse | ||
import uuid | ||
|
||
from logzero import logger | ||
|
||
from tornado import httpclient | ||
from tornado import escape | ||
from tornado.auth import OAuth2Mixin | ||
from tornado.httputil import url_concat | ||
from tornado.util import unicode_type | ||
from tornado.web import RequestHandler | ||
|
||
from typing import List, Any, Dict, cast, Iterable, Union, Optional | ||
|
||
|
||
class GithubOAuth2Mixin(OAuth2Mixin): | ||
|
||
_OAUTH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token" | ||
_OAUTH_AUTHORIZE_URL = "https://github.com/login/oauth/authorize" | ||
_OAUTH_SETTINGS_KEY = 'github' | ||
|
||
async def get_authenticated_user(self, redirect_uri: str, code: str, client_id: str, client_secret: str) -> Dict[str, Any]: | ||
http = self.get_auth_http_client() | ||
body = urllib.parse.urlencode( | ||
{ | ||
"redirect_uri": redirect_uri, | ||
"code": code, | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
"grant_type": "authorization_code", | ||
} | ||
) | ||
|
||
response = await http.fetch( | ||
self._OAUTH_ACCESS_TOKEN_URL, | ||
method="POST", | ||
headers={"Content-Type": "application/x-www-form-urlencoded", | ||
"Accept": "application/json"}, | ||
body=body, | ||
) | ||
return escape.json_decode(response.body) |
This file contains 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