Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
squadserver/oauth.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
65 lines (56 sloc)
2.17 KB
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
from config import OAUTH_CREDENTIALS | |
from rauth import OAuth1Service, OAuth2Service | |
from flask import current_app, url_for, request, redirect, session | |
class OAuthSignIn(object): | |
providers = None | |
def __init__(self, provider_name): | |
self.provider_name = provider_name | |
credentials = OAUTH_CREDENTIALS[provider_name] | |
self.consumer_id = credentials['id'] | |
self.consumer_secret = credentials['secret'] | |
def authorize(self): | |
pass | |
def callback(self): | |
pass | |
def get_callback_url(self): | |
return url_for('oauth_callback', provider=self.provider_name, | |
_external=True) | |
@classmethod | |
def get_provider(self, provider_name): | |
if self.providers is None: | |
self.providers = {} | |
for provider_class in self.__subclasses__(): | |
provider = provider_class() | |
self.providers[provider.provider_name] = provider | |
return self.providers[provider_name] | |
class FacebookSignIn(OAuthSignIn): | |
def __init__(self): | |
super(FacebookSignIn, self).__init__('facebook') | |
self.service = OAuth2Service( | |
name='facebook', | |
client_id=self.consumer_id, | |
client_secret=self.consumer_secret, | |
authorize_url='https://graph.facebook.com/oauth/authorize', | |
access_token_url='https://graph.facebook.com/oauth/access_token', | |
base_url='https://graph.facebook.com/' | |
) | |
def authorize(self): | |
return redirect(self.service.get_authorize_url( | |
scope='public_profile,email', | |
response_type='code', | |
redirect_uri=self.get_callback_url()) | |
) | |
def callback(self): | |
if 'code' not in request.args: | |
return None, None, None | |
oauth_session = self.service.get_auth_session( | |
data={'code': request.args['code'], | |
'grant_type': 'authorization_code', | |
'redirect_uri': self.get_callback_url()} | |
) | |
me = oauth_session.get('me', params={'fields' : 'id,name,email'}).json() | |
return ( | |
me['id'], | |
me.get('name'), | |
me.get('email') | |
) |