Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'github_auth'

Conflicts:
	dash_proj/templates/base.html
  • Loading branch information...
commit 1d43d47c819802813015166ddb4d9e4d7749aabd 2 parents 44dea18 + 4716bfb
@macro authored
View
BIN  dash_proj/dash_dev.db
Binary file not shown
View
3  dash_proj/gitawesome/models.py
@@ -31,6 +31,9 @@ class Profile(models.Model):
blog = models.CharField(max_length=128, default='', blank=True)
date_joined = models.DateTimeField(null=True)
+ github_oauth_token = models.CharField(max_length=200)
+ github_oauth_secret = models.CharField(max_length=200)
+
# stats
points = models.IntegerField(default=0)
View
0  dash_proj/githubauth/__init__.py
No changes.
View
3  dash_proj/githubauth/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
View
16 dash_proj/githubauth/tests.py
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
View
60 dash_proj/githubauth/views.py
@@ -0,0 +1,60 @@
+import cgi
+import urllib
+import urllib2
+
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+from django.conf import settings
+from django.contrib.auth import authenticate, login, logout
+from django.contrib.auth.models import User
+from django.contrib.auth.decorators import login_required
+
+
+GITHUB_REQUEST_TOKEN_URL = 'https://github.com/login/oauth/authorize'
+GITHUB_ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
+
+def github_login(request):
+ url = '%s?%s' % (GITHUB_REQUEST_TOKEN_URL,
+ urllib.urlencode({
+ 'client_id': settings.GITHUB_CLIENT_ID,
+ 'redirect_url': request.build_absolute_uri(reverse('github_login_return')),
+ }))
+ return HttpResponseRedirect(url)
+
+def github_login_return(request):
+ try:
+ code = request.GET['code']
+ except Exception, e:
+ print "*** [DEBUG] ", e
+ return HttpResponseRedirect(reverse('gitawesome_home'))
+
+ # get access token
+ data = {
+ 'client_id': settings.GITHUB_CLIENT_ID,
+ 'redirect_url': reverse('github_login_return'),
+ 'client_secret': settings.GITHUB_SECRET,
+ 'code': code
+ }
+ response = urllib2.urlopen(GITHUB_ACCESS_TOKEN_URL, data)
+
+ access_token = dict(cgi.parse_qsl(response))
+
+ print "*** [DEBUG] ", access_token()
+
+ # FIXME: create user
+ user = User.objects.create_user(
+ )
+ user.profile.github_oauth_token = access_token['oauth_token']
+ user.profile.github_oauth_secret = access_token['oauth_token_secret']
+ user.profile.save()
+
+ user = authenticate(username=access_token['screen_name'],
+ password=access_token['oauth_token_secret'])
+ login(request, user)
+ return HttpResponseRedirect(reverse('gitawesome_home'))
+
+@login_required
+def github_logout(request):
+ logout(request)
+ return HttpResponseRedirect(reverse('gitawesome_home'))
+
View
3  dash_proj/settings.py
@@ -190,6 +190,9 @@
GIT_REPO_ROOT = os.path.join(MEDIA_ROOT, '.git.tmp')
+GITHUB_CLIENT_ID = ''
+GITHUB_SECRET = ''
+
def override_settings(dottedpath):
try:
_m = __import__(dottedpath, fromlist=[None])
View
4 dash_proj/templates/base.html
@@ -35,9 +35,9 @@ <h1 id="branding">gitawesome</h1>
<ul>
{% if request.user.is_authenticated %}
<li><a href="{% url gitawesome_home %}">{{ request.user.username }}</a> |</li>
- <li><a href="{% url logout %}">logout</a></li>
+ <li><a href="{% url github_logout %}">logout</a></li>
{% else %}
- <li><a href="{% url login %}" class="connect"><span>login</span></a></li>
+ <li><a href="{% url github_login %}">login</a></li>
{% endif %}
</ul>
</nav>
View
7 dash_proj/urls.py
@@ -3,12 +3,16 @@
from django.contrib import admin
from django.contrib.auth.views import logout, login
-
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
+ url(r'^login/$', 'githubauth.views.github_login', name='github_login'),
+ url(r'^logout/$', 'githubauth.views.github_logout', name='github_logout'),
+ url(r'^login/return/$', 'githubauth.views.github_login_return',
+ name='github_login_return'),
+
(r'^accounts/login/$', login, {}, 'login'),
(r'^accounts/logout/$', logout, {'next_page': '/'}, 'logout'),
@@ -17,3 +21,4 @@
urlpatterns += staticfiles_urlpatterns()
+
Please sign in to comment.
Something went wrong with that request. Please try again.