2018-11-09 06:54:38 +00:00
|
|
|
# coding:utf-8
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import logout
|
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
|
|
|
from django.contrib.auth import BACKEND_SESSION_KEY
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-02-28 03:58:48 +00:00
|
|
|
from .utils import new_client
|
|
|
|
from .models import OIDT_ACCESS_TOKEN
|
2018-11-09 06:54:38 +00:00
|
|
|
|
2019-02-28 07:12:45 +00:00
|
|
|
BACKEND_OPENID_AUTH_CODE = 'OpenIDAuthorizationCodeBackend'
|
2018-11-09 06:54:38 +00:00
|
|
|
logger = get_logger(__file__)
|
2019-02-28 03:58:48 +00:00
|
|
|
__all__ = ['OpenIDAuthenticationMiddleware']
|
2018-11-09 06:54:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OpenIDAuthenticationMiddleware(MiddlewareMixin):
|
|
|
|
"""
|
|
|
|
Check openid user single logout (with access_token)
|
|
|
|
"""
|
|
|
|
def process_request(self, request):
|
|
|
|
# Don't need openid auth if AUTH_OPENID is False
|
|
|
|
if not settings.AUTH_OPENID:
|
|
|
|
return
|
2019-07-01 03:04:15 +00:00
|
|
|
# Don't need openid auth if no shared session enabled
|
|
|
|
if not settings.AUTH_OPENID_SHARE_SESSION:
|
|
|
|
return
|
2018-11-09 06:54:38 +00:00
|
|
|
# Don't need check single logout if user not authenticated
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return
|
2019-04-03 04:18:58 +00:00
|
|
|
elif not request.session[BACKEND_SESSION_KEY].endswith(
|
2019-02-28 07:12:45 +00:00
|
|
|
BACKEND_OPENID_AUTH_CODE):
|
2018-11-09 06:54:38 +00:00
|
|
|
return
|
|
|
|
# Check openid user single logout or not with access_token
|
|
|
|
try:
|
2019-07-01 03:04:15 +00:00
|
|
|
client = new_client()
|
|
|
|
client.get_userinfo(token=request.session.get(OIDT_ACCESS_TOKEN))
|
2018-11-09 06:54:38 +00:00
|
|
|
except Exception as e:
|
|
|
|
logout(request)
|
|
|
|
logger.error(e)
|