-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddlewares.py
More file actions
40 lines (33 loc) · 1.45 KB
/
middlewares.py
File metadata and controls
40 lines (33 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
* * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* * See LICENSE in the project root for license information.
'''
from models.db import Profile
from utils.shortcuts import render
from django.http import HttpResponseRedirect
from django.utils.deprecation import MiddlewareMixin
from services.auth_service import AuthService
from django.contrib.auth import logout as auth_logout
import os
from services.token_service import RefreshTokenException
class HandleRefreshTokenExceptionMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
context = {
'user': AuthService.get_current_user(request),
'reason': str(exception)
}
if exception.__class__.__name__ == 'RefreshTokenException':
return render(request, 'login0365required.html', context)
class UserUnlinkMonitorMiddleware(MiddlewareMixin):
'''
This middleware will clear o365 user from session, once if detects an unlink (mostly made by admins).
'''
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
user = AuthService.get_current_user(request)
if user.are_linked:
if not Profile.objects.filter(id=user.user_id, o365UserId=user.o365_user_id):
auth_logout(request)
AuthService.set_o365_user(request, user.o365_user)
return self.get_response(request)