-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Git service: attach each service to an allauth provider #11995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
fa53c1c
f031b4b
f7a3917
083cfa0
8e6caa3
7fc1466
27ad8c6
5daf4da
1289298
5319608
c9eb355
d898dbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from datetime import datetime | ||
|
||
import structlog | ||
from django.utils import timezone | ||
from requests_oauthlib import OAuth2Session | ||
|
||
log = structlog.get_logger(__name__) | ||
|
||
|
||
def _get_token_updater(token): | ||
""" | ||
Update token given data from OAuth response. | ||
|
||
Expect the following response into the closure:: | ||
|
||
{ | ||
u'token_type': u'bearer', | ||
u'scopes': u'webhook repository team account', | ||
u'refresh_token': u'...', | ||
u'access_token': u'...', | ||
u'expires_in': 3600, | ||
u'expires_at': 1449218652.558185 | ||
} | ||
""" | ||
|
||
def _updater(data): | ||
token.token = data["access_token"] | ||
token.token_secret = data.get("refresh_token", "") | ||
token.expires_at = timezone.make_aware( | ||
datetime.fromtimestamp(data["expires_at"]), | ||
) | ||
token.save() | ||
log.info("Updated token.", token_id=token.pk) | ||
|
||
return _updater | ||
|
||
|
||
def get_oauth2_client(account): | ||
"""Get an OAuth2 client for the given social account.""" | ||
token = account.socialtoken_set.first() | ||
if token is None: | ||
return None | ||
|
||
token_config = { | ||
"access_token": token.token, | ||
"token_type": "bearer", | ||
} | ||
if token.expires_at is not None: | ||
token_expires = (token.expires_at - timezone.now()).total_seconds() | ||
token_config.update( | ||
{ | ||
"refresh_token": token.token_secret, | ||
"expires_in": token_expires, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little surprised we're setting this internally -- is it so we can update it if needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, allauth doesn't provide an oauth2 client, so we use requests_oauthlib, and we need to glue the token from allauth other lib. Most of this code was just moved. |
||
} | ||
) | ||
|
||
provider = account.get_provider() | ||
social_app = provider.app | ||
oauth2_adapter = provider.get_oauth2_adapter(request=provider.request) | ||
|
||
session = OAuth2Session( | ||
client_id=social_app.client_id, | ||
token=token_config, | ||
auto_refresh_kwargs={ | ||
"client_id": social_app.client_id, | ||
"client_secret": social_app.secret, | ||
}, | ||
auto_refresh_url=oauth2_adapter.access_token_url, | ||
token_updater=_get_token_updater(token), | ||
) | ||
return session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what this is saying -- do you mean that is the expected value of the
token
arg?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this is likely the response you get when the request for a new token is made.