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

Commit

Permalink
add /auth/oauth2/logout, goto callback after login/logout.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Apr 7, 2015
1 parent 816b4f5 commit 4114dd2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions fake_oauth2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
url(r'^login/oauth/authorize$', views.authorize),
url(r'^login/oauth/access_token$', views.access_token),
url(r'^user$', views.user),
url(r'^logout$', views.logout),
]
6 changes: 6 additions & 0 deletions fake_oauth2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ def user(request):
})

return res

@require_GET
def logout(request):
url = reverse('teach.views.oauth2_callback')
qs = 'logout=true'
return HttpResponseRedirect('%s?%s' % (url, qs))
2 changes: 2 additions & 0 deletions teach/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
'teach.views.oauth2_authorize'),
url(r'^auth/oauth2/callback$',
'teach.views.oauth2_callback'),
url(r'^auth/oauth2/logout$',
'teach.views.oauth2_logout'),

url(r'^api-introduction/', 'teach.views.api_introduction',
name='api-introduction'),
Expand Down
28 changes: 27 additions & 1 deletion teach/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,24 @@ def get_origin(url):
return None
return '%s://%s' % (info.scheme, info.netloc)

def validate_callback(callback):
origin = get_origin(callback)
valid_origins = settings.CORS_API_PERSONA_ORIGINS
if origin and origin in valid_origins:
return callback
if settings.DEBUG and valid_origins == ['*']:
return callback
return None

def set_callback(request):
callback = validate_callback(request.GET.get('callback', ''))
if callback:
request.session['oauth2_callback'] = callback

def oauth2_authorize(request):
set_callback(request)
request.session['oauth2_state'] = get_random_string(length=32)

return HttpResponseRedirect(get_idapi_url("/login/oauth/authorize", {
'client_id': settings.IDAPI_CLIENT_ID,
'response_type': 'code',
Expand All @@ -54,9 +70,13 @@ def oauth2_authorize(request):
}))

def oauth2_callback(request):
callback = request.session.get('oauth2_callback', '/')
expected_state = request.session.get('oauth2_state')
state = request.GET.get('state')
code = request.GET.get('code')
if request.GET.get('logout') == 'true':
django.contrib.auth.logout(request)
return HttpResponseRedirect(callback)
if state is None or expected_state is None or state != expected_state:
return HttpResponse('invalid state')
if code is None:
Expand All @@ -65,7 +85,13 @@ def oauth2_callback(request):
user = django.contrib.auth.authenticate(webmaker_oauth2_code=code)
django.contrib.auth.login(request, user)

return HttpResponse('hello %s' % user.username)
return HttpResponseRedirect(callback)

def oauth2_logout(request):
set_callback(request)
return HttpResponseRedirect(get_idapi_url("/logout", {
'client_id': settings.IDAPI_CLIENT_ID
}))

def check_origin(request):
origin = request.META.get('HTTP_ORIGIN')
Expand Down

0 comments on commit 4114dd2

Please sign in to comment.