119 lines
4.2 KiB
Python
Executable File
119 lines
4.2 KiB
Python
Executable File
"""
|
|
MIT License
|
|
|
|
Copyright (c) 2025 Hopsenn
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
"""
|
|
|
|
from functools import wraps
|
|
from django.contrib.auth import login as auth_login
|
|
from django.http import JsonResponse
|
|
|
|
from . import settings
|
|
from .validators import verify_id_token
|
|
from .utils import (
|
|
get_jwt_tokens,
|
|
verify_jwt_token,
|
|
get_user_from_jwt_claims,
|
|
refresh_access_token
|
|
)
|
|
|
|
|
|
def hopid_protected(view_func):
|
|
@wraps(view_func)
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
refresh_token = None
|
|
is_session_based = False
|
|
|
|
auth_header = request.headers.get('Authorization', '')
|
|
if auth_header.lower().startswith('bearer '):
|
|
access_token = auth_header.split(' ', 1)[1].strip()
|
|
else:
|
|
access_token = request.session.get('access_token')
|
|
refresh_token = request.session.get('refresh_token')
|
|
is_session_based = True
|
|
|
|
user = None
|
|
if access_token:
|
|
claims = verify_jwt_token(access_token)
|
|
user = get_user_from_jwt_claims(claims)
|
|
|
|
if not user and is_session_based and refresh_token:
|
|
tokens = refresh_access_token(refresh_token)
|
|
if tokens:
|
|
access_token = tokens.get('access_token')
|
|
refresh_token = tokens.get('refresh_token')
|
|
|
|
request.session['access_token'] = access_token
|
|
request.session['refresh_token'] = refresh_token
|
|
|
|
claims = verify_jwt_token(access_token)
|
|
user = get_user_from_jwt_claims(claims)
|
|
|
|
if not user:
|
|
return JsonResponse({'detail': 'Invalid or expired token.'}, status=401)
|
|
|
|
request.user = user
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
return _wrapped_view
|
|
|
|
|
|
def hopid_callback(response=None):
|
|
def decorator(view_func):
|
|
@wraps(view_func)
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
def fail(reason):
|
|
if callable(response):
|
|
return response(request, reason)
|
|
return view_func(request, *args, **kwargs, error=reason)
|
|
|
|
code = request.GET.get('code')
|
|
next = request.GET.get('next')
|
|
if not code:
|
|
return fail("No code returned")
|
|
|
|
tokens = get_jwt_tokens(code, request.session.pop('pkce_verifier', ''), next)
|
|
if tokens.get('error'):
|
|
return fail(tokens['error'])
|
|
|
|
access_token = tokens.get('access_token')
|
|
id_token = tokens.get('id_token')
|
|
if not access_token or not id_token:
|
|
return fail("No ID token returned")
|
|
|
|
claims = verify_id_token(id_token, access_token)
|
|
if not claims or claims.get("nonce") != request.session.pop('oidc_nonce', None):
|
|
return fail("Invalid or missing nonce")
|
|
|
|
access_claims = verify_jwt_token(access_token)
|
|
profile = get_user_from_jwt_claims(access_claims)
|
|
if not profile:
|
|
return fail("Invalid access token")
|
|
|
|
request.session['access_token'] = access_token
|
|
request.session['refresh_token'] = tokens.get('refresh_token')
|
|
auth_login(request, profile)
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
return _wrapped_view
|
|
return decorator
|
|
|