Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to handle validation logic for users #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist/
*.egg-info
.tox/
.cache/
*.swp
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Step 6: Set up all the correct options (see below for available options)
* OAUTHADMIN_GET_USER: This is function that is given the oauth token and returns
a django.auth.models.User model corresponding to the currently logged-in user.
You can set permissions on this user object and stuff.
* OAUTHADMIN_GET_USER_EXCEPTION_HANDLER: Callable that receives request object, OAuth token
and exception to handle business logics such as unauthorized users. This callable should
return proper response object.
* OAUTHADMIN_CLIENT_ID: Your oAuth client ID
* OAUTHADMIN_CLIENT_SECRET: oAuth client secret
* OAUTHADMIN_BASE_URL: The landing point for all oAuth related queries.
Expand Down
6 changes: 6 additions & 0 deletions oauthadmin/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class OAuthAdminException(Exception):
pass


class GetUserException(OAuthAdminException):
pass
18 changes: 14 additions & 4 deletions oauthadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from urllib.parse import quote_plus

from django.shortcuts import redirect
from django.http import HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect

from oauthadmin.utils import import_by_path
from oauthadmin.settings import app_setting
import oauthadmin.views

from oauthadmin.errors import GetUserException
from oauthadmin.settings import app_setting
from oauthadmin.utils import import_by_path

try:
from django.urls import reverse, NoReverseMatch
except ImportError:
Expand Down Expand Up @@ -82,7 +84,15 @@ def callback(request):
except (MismatchingStateError, InvalidGrantError):
return HttpResponseRedirect(request.build_absolute_uri(reverse(oauthadmin.views.login)))

user = import_by_path(app_setting('GET_USER'))(token)
user_getter = import_by_path(app_setting('GET_USER'))
try:
user = user_getter(token)
except GetUserException as e:
get_user_exception_handler_module = app_setting('GET_USER_EXCEPTION_HANDLER')
if get_user_exception_handler_module:
get_user_exception_handler = import_by_path(get_user_exception_handler_module)
return get_user_exception_handler(request, token, e)
raise

request.session['last_verified_at'] = int(time())
request.session['oauth_token'] = token
Expand Down