forked from flashingpumpkin/django-socialregistration
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53b848b
commit 18f23a7
Showing
16 changed files
with
207 additions
and
26 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
Empty file.
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,12 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
from django.contrib.sites.models import Site | ||
from socialregistration.contrib.foursquare.models import FoursquareProfile | ||
|
||
class FoursquareAuth(ModelBackend): | ||
def authenticate(self, foursquare=None): | ||
try: | ||
return FoursquareProfile.objects.get( | ||
foursquare=foursquare, | ||
site=Site.objects.get_current()).user | ||
except FoursquareProfile.DoesNotExist: | ||
return None |
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,59 @@ | ||
from django.conf import settings | ||
from django.contrib.sites.models import Site | ||
from django.core.urlresolvers import reverse | ||
from socialregistration.clients.oauth import OAuth2 | ||
import json | ||
|
||
|
||
class Foursquare(OAuth2): | ||
client_id = getattr(settings, 'FOURSQUARE_CLIENT_ID', '') | ||
secret = getattr(settings, 'FOURSQUARE_CLIENT_SECRET', '') | ||
scope = getattr(settings, 'FOURSQUARE_REQUEST_PERMISSIONS', '') | ||
|
||
auth_url = 'https://foursquare.com/oauth2/authorize' | ||
access_token_url = 'https://foursquare.com/oauth2/access_token' | ||
|
||
_user_info = None | ||
|
||
def get_callback_url(self): | ||
if self.is_https(): | ||
return 'https://%s%s' % (Site.objects.get_current().domain, | ||
reverse('socialregistration:foursquare:callback')) | ||
return 'http://%s%s' % (Site.objects.get_current().domain, | ||
reverse('socialregistration:foursquare:callback')) | ||
|
||
def parse_access_token(self, content): | ||
""" | ||
Forsquare returns JSON instead of url encoded data. | ||
""" | ||
return json.loads(content) | ||
|
||
def request_access_token(self, params): | ||
""" | ||
Foursquare does not accept POST requests to retrieve an access token, | ||
so we'll be doing a GET request instead. | ||
""" | ||
return self.request(self.access_token_url, method="GET", params=params) | ||
|
||
def get_access_token(self, **params): | ||
""" | ||
Foursquare requires a `grant_type` parameter when requesting the access | ||
token. | ||
""" | ||
return super(Foursquare, self).get_access_token(grant_type='authorization_code', **params) | ||
|
||
def get_signing_params(self): | ||
""" | ||
Foursquare requires `oauth_token` parameter instead of `access_token` | ||
""" | ||
return dict(oauth_token=self._access_token) | ||
|
||
def get_user_info(self): | ||
if self._user_info is None: | ||
resp, content = self.request('https://api.foursquare.com/v2/users/self') | ||
self._user_info = json.loads(content)['response']['user'] | ||
return self._user_info | ||
|
||
@staticmethod | ||
def get_session_key(): | ||
return 'socialreg:foursquare' |
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,36 @@ | ||
from django.contrib.auth import authenticate | ||
from django.contrib.auth.models import User | ||
from django.contrib.sites.models import Site | ||
from django.db import models | ||
from socialregistration.signals import connect | ||
|
||
class FoursquareProfile(models.Model): | ||
user = models.ForeignKey(User, unique=True) | ||
site = models.ForeignKey(Site, default=Site.objects.get_current) | ||
foursquare = models.CharField(max_length=255) | ||
|
||
def __unicode__(self): | ||
try: | ||
return u'%s: %s' % (self.user, self.foursquare) | ||
except User.DoesNotExist: | ||
return u'None' | ||
|
||
def authenticate(self): | ||
return authenticate(foursquare=self.foursquare) | ||
|
||
class FoursquareAccessToken(models.Model): | ||
profile = models.OneToOneField(FoursquareProfile, related_name='access_token') | ||
access_token = models.CharField(max_length=255) | ||
|
||
def save_foursquare_token(sender, user, profile, client, **kwargs): | ||
try: | ||
FoursquareAccessToken.objects.get(profile=profile).delete() | ||
except FoursquareAccessToken.DoesNotExist: | ||
pass | ||
|
||
FoursquareAccessToken.objects.create(access_token=client.get_access_token(), | ||
profile=profile) | ||
|
||
|
||
connect.connect(save_foursquare_token, sender=FoursquareProfile, | ||
dispatch_uid='socialregistration_github_token') |
3 changes: 3 additions & 0 deletions
3
...alregistration/contrib/foursquare/templates/socialregistration/foursquare/foursquare.html
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,3 @@ | ||
{% if error %} | ||
<p>{{ error }}</p> | ||
{% endif %} |
7 changes: 7 additions & 0 deletions
7
...tration/contrib/foursquare/templates/socialregistration/foursquare/foursquare_button.html
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,7 @@ | ||
<form action="{% url socialregistration:foursquare:redirect %}" method="post"> | ||
{% csrf_token %} | ||
{% if next %} | ||
<input type="hidden" name="next" value="{{ next }}" /> | ||
{% endif %} | ||
<input type="submit" value="Sign in with Foursquare" /> | ||
</form> |
Empty file.
6 changes: 6 additions & 0 deletions
6
socialregistration/contrib/foursquare/templatetags/foursquare.py
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,6 @@ | ||
from django import template | ||
from socialregistration.templatetags import button | ||
|
||
register = template.Library() | ||
|
||
register.tag('foursquare_button', button('socialregistration/foursquare/foursquare_button.html')) |
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,11 @@ | ||
from django.conf import settings | ||
from django.conf.urls.defaults import * | ||
from socialregistration.contrib.foursquare.views import FoursquareRedirect, \ | ||
FoursquareCallback, FoursquareSetup | ||
|
||
|
||
urlpatterns = patterns('', | ||
url('^redirect/$', FoursquareRedirect.as_view(), name='redirect'), | ||
url('^callback/$', FoursquareCallback.as_view(), name='callback'), | ||
url('^setup/$', FoursquareSetup.as_view(), name='setup'), | ||
) |
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,24 @@ | ||
from django.core.urlresolvers import reverse | ||
from socialregistration.contrib.foursquare.client import Foursquare | ||
from socialregistration.contrib.foursquare.models import FoursquareProfile | ||
from socialregistration.views import OAuthRedirect, OAuthCallback, SetupCallback | ||
|
||
class FoursquareRedirect(OAuthRedirect): | ||
client = Foursquare | ||
template_name = 'socialregistration/foursquare/foursquare.html' | ||
|
||
class FoursquareCallback(OAuthCallback): | ||
client = Foursquare | ||
template_name = 'socialregistration/foursquare/foursquare.html' | ||
|
||
def get_redirect(self): | ||
return reverse('socialregistration:foursquare:setup') | ||
|
||
class FoursquareSetup(SetupCallback): | ||
client = Foursquare | ||
profile = FoursquareProfile | ||
template_name = 'socialregistration/foursquare/foursquare.html' | ||
|
||
def get_lookup_kwargs(self, request, client): | ||
return {'foursquare': client.get_user_info()['id']} | ||
|
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