2015-01-19 18:25:16 +00:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2014-12-19 15:27:43 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
|
|
|
|
from django.shortcuts import render
|
2015-01-29 17:03:17 +00:00
|
|
|
from django.template.loader import render_to_string
|
2014-12-19 15:27:43 +00:00
|
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
from django.views.generic import View
|
2015-02-18 18:07:22 +00:00
|
|
|
from oidc_provider.lib.errors import *
|
|
|
|
from oidc_provider.lib.endpoints.authorize import *
|
|
|
|
from oidc_provider.lib.endpoints.token import *
|
|
|
|
from oidc_provider.lib.endpoints.userinfo import *
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorizeView(View):
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
authorize = AuthorizeEndpoint(request)
|
|
|
|
|
|
|
|
try:
|
|
|
|
authorize.validate_params()
|
|
|
|
|
|
|
|
if request.user.is_authenticated():
|
2015-01-12 22:13:48 +00:00
|
|
|
|
2015-01-29 17:03:17 +00:00
|
|
|
# Generate hidden inputs for the form.
|
2015-01-19 18:25:16 +00:00
|
|
|
context = {
|
2014-12-19 15:27:43 +00:00
|
|
|
'params': authorize.params,
|
2015-01-29 17:03:17 +00:00
|
|
|
}
|
|
|
|
hidden_inputs = render_to_string(
|
2015-02-18 18:07:22 +00:00
|
|
|
'oidc_provider/hidden_inputs.html', context)
|
2015-01-29 17:03:17 +00:00
|
|
|
|
|
|
|
# Remove openid from scope list since we don't need to print it.
|
|
|
|
authorize.params.scope.remove('openid')
|
|
|
|
|
|
|
|
context = {
|
2014-12-19 15:27:43 +00:00
|
|
|
'client': authorize.client,
|
2015-01-29 17:03:17 +00:00
|
|
|
'hidden_inputs': hidden_inputs,
|
|
|
|
'params': authorize.params,
|
2014-12-19 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 18:07:22 +00:00
|
|
|
return render(request, 'oidc_provider/authorize.html', context)
|
2014-12-19 15:27:43 +00:00
|
|
|
else:
|
2015-01-19 18:25:16 +00:00
|
|
|
path = request.get_full_path()
|
2015-02-09 20:36:29 +00:00
|
|
|
return redirect_to_login(path)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
except (ClientIdError, RedirectUriError) as error:
|
2015-01-19 18:25:16 +00:00
|
|
|
context = {
|
2015-01-09 17:59:23 +00:00
|
|
|
'error': error.error,
|
|
|
|
'description': error.description,
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:07:22 +00:00
|
|
|
return render(request, 'oidc_provider/error.html', context)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
except (AuthorizeError) as error:
|
2015-01-12 22:13:48 +00:00
|
|
|
uri = error.create_uri(
|
|
|
|
authorize.params.redirect_uri,
|
|
|
|
authorize.params.state)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
return HttpResponseRedirect(uri)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
authorize = AuthorizeEndpoint(request)
|
|
|
|
|
|
|
|
allow = True if request.POST.get('allow') else False
|
|
|
|
|
|
|
|
try:
|
|
|
|
uri = authorize.create_response_uri(allow)
|
|
|
|
|
|
|
|
return HttpResponseRedirect(uri)
|
|
|
|
|
|
|
|
except (AuthorizeError) as error:
|
2015-01-30 20:20:36 +00:00
|
|
|
uri = error.create_uri(
|
|
|
|
authorize.params.redirect_uri,
|
|
|
|
authorize.params.state)
|
2014-12-19 15:27:43 +00:00
|
|
|
|
|
|
|
return HttpResponseRedirect(uri)
|
|
|
|
|
|
|
|
class TokenView(View):
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
token = TokenEndpoint(request)
|
|
|
|
|
|
|
|
try:
|
|
|
|
token.validate_params()
|
|
|
|
|
|
|
|
dic = token.create_response_dic()
|
|
|
|
|
|
|
|
return TokenEndpoint.response(dic)
|
|
|
|
|
|
|
|
except (TokenError) as error:
|
|
|
|
return TokenEndpoint.response(error.create_dict(), status=400)
|
|
|
|
|
|
|
|
@require_http_methods(['GET', 'POST'])
|
|
|
|
def userinfo(request):
|
|
|
|
|
|
|
|
userinfo = UserInfoEndpoint(request)
|
|
|
|
|
|
|
|
try:
|
|
|
|
userinfo.validate_params()
|
|
|
|
|
|
|
|
dic = userinfo.create_response_dic()
|
|
|
|
|
|
|
|
return UserInfoEndpoint.response(dic)
|
|
|
|
|
|
|
|
except (UserInfoError) as error:
|
|
|
|
return UserInfoEndpoint.error_response(
|
|
|
|
error.code,
|
|
|
|
error.description,
|
2015-01-28 18:19:36 +00:00
|
|
|
error.status)
|