Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Started writing OAuth2 code for FB & Google
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed Jun 24, 2012
1 parent 96fb4d3 commit 6c666f6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Requirements.txt
Expand Up @@ -5,3 +5,4 @@ markdown
mox
flask-testing
blinker
rauth>=0.4.12
Empty file added auth/__init__.py
Empty file.
101 changes: 101 additions & 0 deletions auth/services.py
@@ -0,0 +1,101 @@

from rauth.service import OAuth2Service


class BaseOAuth2(OAuth2Service):
'''
BaseOAuth2 class that handles functionality common to
OAuth2Services, that isn't covered by the rauth library
'''

def __init__(self, name, config, redirect_uri, **kwargs):
'''
Constructor
Params:
name The name of this service
config Config object to extract configuration from
redirect_uri The uri for the service to redirect to
authorize_url The url for authorizing
access_token_url The url for obtaining an access token
from an authorization code
'''
self.redirect_uri = redirect_uri
upperName = config.upper()
super( BaseOAuth2, self ).__init__(
name=name,
authorize_url='',
access_token_url='',
consumer_key=config[ upperName + '_OAUTH_KEY' ],
consumer_secret=config[ upperName + '_OAUTH_SECRET' ]
)

def get_authorize_url(self, state=''):
'''
Gets the authorize url
Params:
state Optional state variable to use in the url
'''
return super( BaseOAuth2, self ).get_authorize_url(
redirect_uri=self.redirect_uri,
scope='',
state=state
)

def get_access_token(self, auth_code):
'''
Gets the access token from the authorization code
Params:
auth_code The auth code supplied by the client
'''
data = {
'code': auth_code,
'redirect_uri': self.redirect_uri
}
return super( BaseOAuth2, self ).get_access_token( data=data )


class FacebookService(BaseOAuth2):
'''
Facebook auth service
This is basically the same as the BaseOAuth2 class, but requests it's
access token using GET instead of POST
'''

def get_access_token(self, **kwargs):
return super( FacebookService, self ).get_access_token(
method='GET', **kwargs
)

_googleService = None
_facebookService = None


def GetGoogleService(config, redirect_url):
global _googleService
if _googleService is None:
_googleService = BaseOAuth2(
'google',
config,
redirect_url,
authorize_url='https://accounts.google.com/o/oauth2/auth',
access_token_url='https://accounts.google.com/o/oauth2/token'
)
return _googleService


def GetFacebookService(config, redirect_url):
global _facebookService
if _facebookService is None:
_facebookService = FacebookService(
'facebook',
config,
redirect_url,
authorize_url='https://www.facebook.com/dialog/oauth',
access_token_url='https://graph.facebook.com/oauth/access_token'
)
return _facebookService

0 comments on commit 6c666f6

Please sign in to comment.