Skip to content

Commit

Permalink
Working OAuth2 provider support. Uses oauth2app to implement service …
Browse files Browse the repository at this point in the history
…provider support and provides a test endpoint for bearer token authentication
  • Loading branch information
paulosman committed Feb 13, 2013
1 parent d59b73a commit bd269c9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 1 deletion.
Empty file added lernanta/apps/oauth/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions lernanta/apps/oauth/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.conf.urls.defaults import patterns, url


urlpatterns = patterns('',
url(r'^missing_redirect_uri/?$', 'oauth.views.missing_redirect_uri',
name='oauth_missing_redirect_uri'),

url(r'^authorize/?$', 'oauth.views.authorize',
name='oauth_authorize'),

url(r'^token/?$', 'oauth2app.token.handler',
name='oauth_token_handler'),

url(r'^test/$', 'oauth.views.test',
name='oauth_test'),
)
54 changes: 54 additions & 0 deletions lernanta/apps/oauth/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django import forms
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response

from users.decorators import login_required
from oauth2app.authorize import Authorizer, MissingRedirectURI, AuthorizationException
from oauth2app.authenticate import JSONAuthenticator, AuthenticationException


class AuthorizeForm(forms.Form):
pass

def test(request):
"""Test authentication"""
authenticator = JSONAuthenticator()
try:
authenticator.validate(request)
except AuthenticationException:
return authenticator.error_response()
return authenticator.response({"secret": "information"})

@login_required
def missing_redirect_uri(request):
return render_to_response('oauth2/missing_redirect_uri.html', {},
context_instance=RequestContext(request))

@login_required
def authorize(request):
authorizer = Authorizer()

try:
authorizer.validate(request)
except MissingRedirectURI, e:
return HttpResponseRedirect(reverse('oauth_missing_redirect_uri'))
except AuthorizationException, e:
return authorizer.error_redirect()

if request.method == 'GET':
return render_to_response('oauth/authorize.html', {
'form': AuthorizeForm(),
'form_action': '%s?%s' % (reverse('oauth_authorize'), authorizer.query_string)
}, context_instance=RequestContext(request))
elif request.method == 'POST':
form = AuthorizeForm(request.POST)
if form.is_valid():
print request.POST.get('connect')
if request.POST.get('connect') == "on":
return authorizer.grant_redirect()
else:
return authorizer.error_redirect()

return HttpResponseRedirect('/')
1 change: 1 addition & 0 deletions lernanta/requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ requests==0.13.5
-e git://github.com/jdunck/python-unicodecsv.git#egg=unicodecsv
-e git://github.com/zuzelvp/django-obi.git#egg=django_obi
-e git://github.com/embedly/embedly-python.git#egg=embedly_python
-e git://github.com/hiidef/oauth2app.git@f8205e2146477224a666c03bf115ddf3ff7610ff#egg=oauth2app
4 changes: 3 additions & 1 deletion lernanta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
USE_L10N = True

SUPPORTED_NONLOCALES = ('media', 'static', '.well-known', 'pubsub', 'broadcasts',
'ajax', 'api',)
'ajax', 'api', 'oauth',)

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
Expand Down Expand Up @@ -177,6 +177,8 @@
'lernanta.apps.api',
'tastypie',
'lernanta.apps.media',
'oauth2app',
'oauth',
)

TEMPLATE_CONTEXT_PROCESSORS = (
Expand Down
12 changes: 12 additions & 0 deletions lernanta/templates/oauth/authorize.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block title %}Authorize{% endblock %}

{% block bodyclasses %}signin{% endblock %}
{% block body %}
<form action="{{ form_action }}" method="post">
{% csrf_token %}
<input type="checkbox" name="connect"> Yes, please allow access
<input type="submit" value="Submit" />
</form>
{% endblock %}
9 changes: 9 additions & 0 deletions lernanta/templates/oauth/missing_redirect_uri.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}

{% block title %}Missing Redirect URI{% endblock %}

{% block body %}
<h1>Missing Redirect URI</h1>

<p>The client requesting authorization did not specify a redirect URI so we are unable to complete this request.</p>
{% endblock %}
1 change: 1 addition & 0 deletions lernanta/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
(r'notifications/', include('notifications.urls')),
(r'api/', include('api.urls')),
(r'', include('users.urls')),
(r'^oauth/', include('oauth.urls')),
)

# for serving media when running a local dev server with DEBUG=True
Expand Down

0 comments on commit bd269c9

Please sign in to comment.