30 lines
940 B
Python
30 lines
940 B
Python
# -*- encoding: utf-8 -*-
|
|
|
|
import time
|
|
|
|
import keycloak_oidc.views
|
|
|
|
class KeycloakSessionRefreshMiddleware(object):
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
# One-time configuration and initialization.
|
|
|
|
def __call__(self, request):
|
|
# Code to be executed for each request before
|
|
# the view (and later middleware) are called.
|
|
|
|
refresh_expires_at = request.session.get('refresh_expires_at',None)
|
|
#print("refresh_expires_at", refresh_expires_at, time.time())
|
|
|
|
if refresh_expires_at is not None:
|
|
if refresh_expires_at - time.time() < 5*60: # 5 minutes before expiration
|
|
rslt = keycloak_oidc.views.renew_session(request)
|
|
|
|
if rslt != 0:
|
|
# renew not successful
|
|
return keycloak_oidc.views.logout(request, multiSessionFound=False)
|
|
|
|
return self.get_response(request)
|
|
|
|
|