2018-11-09 06:54:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.views.generic.base import RedirectView
|
|
|
|
from django.contrib.auth import authenticate, login
|
|
|
|
from django.http.response import (
|
|
|
|
HttpResponseBadRequest,
|
|
|
|
HttpResponseServerError,
|
|
|
|
HttpResponseRedirect
|
|
|
|
)
|
|
|
|
|
2019-02-28 03:58:48 +00:00
|
|
|
from .utils import new_client
|
|
|
|
from .models import Nonce
|
|
|
|
from .signals import post_openid_login_success
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-02-28 03:58:48 +00:00
|
|
|
client = new_client()
|
2018-11-09 06:54:38 +00:00
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
__all__ = ['OpenIDLoginView', 'OpenIDLoginCompleteView']
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
class OpenIDLoginView(RedirectView):
|
2018-11-09 06:54:38 +00:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2019-02-28 06:41:33 +00:00
|
|
|
redirect_uri = settings.BASE_SITE_URL + str(settings.LOGIN_COMPLETE_URL)
|
2018-11-09 06:54:38 +00:00
|
|
|
nonce = Nonce(
|
2019-02-27 00:45:00 +00:00
|
|
|
redirect_uri=redirect_uri,
|
2018-11-09 06:54:38 +00:00
|
|
|
next_path=self.request.GET.get('next')
|
|
|
|
)
|
|
|
|
cache.set(str(nonce.state), nonce, 24*3600)
|
2019-07-01 03:04:15 +00:00
|
|
|
|
2018-11-09 06:54:38 +00:00
|
|
|
self.request.session['openid_state'] = str(nonce.state)
|
2019-07-01 03:04:15 +00:00
|
|
|
authorization_url = client.get_authorization_url(
|
|
|
|
redirect_uri=nonce.redirect_uri,
|
|
|
|
scope='code',
|
|
|
|
state=str(nonce.state)
|
|
|
|
)
|
2018-11-09 06:54:38 +00:00
|
|
|
return authorization_url
|
|
|
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
class OpenIDLoginCompleteView(RedirectView):
|
2018-11-09 06:54:38 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'error' in request.GET:
|
|
|
|
return HttpResponseServerError(self.request.GET['error'])
|
|
|
|
if 'code' not in self.request.GET and 'state' not in self.request.GET:
|
2019-07-01 03:04:15 +00:00
|
|
|
return HttpResponseBadRequest(content='Code or State is empty')
|
2018-11-09 06:54:38 +00:00
|
|
|
if self.request.GET['state'] != self.request.session['openid_state']:
|
2019-07-01 03:04:15 +00:00
|
|
|
return HttpResponseBadRequest(content='State invalid')
|
2018-11-09 06:54:38 +00:00
|
|
|
nonce = cache.get(self.request.GET['state'])
|
|
|
|
if not nonce:
|
2019-07-01 03:04:15 +00:00
|
|
|
return HttpResponseBadRequest(content='State failure')
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
user = authenticate(
|
|
|
|
request=self.request,
|
|
|
|
code=self.request.GET['code'],
|
|
|
|
redirect_uri=nonce.redirect_uri
|
|
|
|
)
|
|
|
|
cache.delete(str(nonce.state))
|
|
|
|
if not user:
|
2019-07-01 03:04:15 +00:00
|
|
|
return HttpResponseBadRequest(content='Authenticate user failed')
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
login(self.request, user)
|
2019-02-28 07:12:45 +00:00
|
|
|
post_openid_login_success.send(
|
|
|
|
sender=self.__class__, user=user, request=self.request
|
|
|
|
)
|
2018-11-09 06:54:38 +00:00
|
|
|
return HttpResponseRedirect(nonce.next_path or '/')
|
|
|
|
|