Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
test-it
test_oauth2_django
Commits
07cde8cf
Commit
07cde8cf
authored
Nov 06, 2017
by
Sharad Heft
Browse files
Allow a list of verifiers to check if users are valid.
parent
84eb1fe4
Changes
2
Hide whitespace changes
Inline
Side-by-side
test_oauth/backend.py
View file @
07cde8cf
...
...
@@ -4,11 +4,16 @@ from django.contrib.auth.backends import ModelBackend
from
django.conf
import
settings
from
django.db
import
transaction
from
test_oauth
import
verifiers
from
test_oauth.models
import
TESTOAuth2Data
,
Character
from
test_oauth.session
import
TESTOAuth2Session
DEFAULT_VERIFIERS
=
[
verifiers
.
TESTMembershipVerifier
()
]
class
TESTOAuth2Backend
(
ModelBackend
):
def
authenticate
(
self
,
token
=
None
,
**
kwargs
):
if
token
is
None
:
...
...
@@ -16,6 +21,8 @@ class TESTOAuth2Backend(ModelBackend):
profile
=
TESTOAuth2Session
(
token
=
token
).
profile
self
.
run_verifiers
(
profile
)
user
,
_
=
get_user_model
().
objects
.
get_or_create
(
pk
=
profile
[
'id'
],
username
=
profile
[
'username'
]
...
...
@@ -44,6 +51,16 @@ class TESTOAuth2Backend(ModelBackend):
return
user
def
run_verifiers
(
self
,
profile
):
verifiers
=
getattr
(
settings
,
'TEST_OAUTH_VERIFIERS'
,
DEFAULT_VERIFIERS
)
for
v
in
verifiers
:
v
(
**
profile
)
def
get_user
(
self
,
user_id
):
UserModel
=
get_user_model
()
...
...
test_oauth/verifiers.py
0 → 100644
View file @
07cde8cf
from
django.core.exceptions
import
PermissionDenied
class
Verifier
(
object
):
def
__init__
(
self
,
**
kwargs
):
for
key
,
value
in
kwargs
.
items
():
setattr
(
self
,
key
,
value
)
def
__call__
(
self
,
**
kwargs
):
if
not
self
.
valid
(
**
kwargs
):
raise
PermissionDenied
(
self
.
message
)
class
GroupMembershipVerifier
(
Verifier
):
message
=
"You are not in the correct groups to log in to this site."
def
valid
(
self
,
groups
,
**
kwargs
):
for
g
in
groups
:
if
g
[
'id'
]
==
self
.
group
:
return
True
return
False
class
TESTMembershipVerifier
(
GroupMembershipVerifier
):
message
=
"You are not in TEST so you are not allowed to log in."
group
=
6
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment