Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-openid-auth
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
django-openid-auth
Commits
e0dd0465
Commit
e0dd0465
authored
Mar 18, 2011
by
Michael Hall
Browse files
Options
Browse Files
Download
Plain Diff
Add tests for strict username restrictions
parents
63b05e4c
bbc87811
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
2 deletions
+72
-2
django_openid_auth/auth.py
+13
-2
django_openid_auth/tests/test_views.py
+59
-0
No files found.
django_openid_auth/auth.py
View file @
e0dd0465
...
...
@@ -42,7 +42,9 @@ from django_openid_auth.models import UserOpenID
class
IdentityAlreadyClaimed
(
Exception
):
pass
class
StrictUsernameViolation
(
Exception
):
pass
class
OpenIDBackend
:
"""A django.contrib.auth backend that authenticates the user based on
an OpenID response."""
...
...
@@ -72,7 +74,10 @@ class OpenIDBackend:
claimed_id__exact
=
openid_response
.
identity_url
)
except
UserOpenID
.
DoesNotExist
:
if
getattr
(
settings
,
'OPENID_CREATE_USERS'
,
False
):
user
=
self
.
create_user_from_openid
(
openid_response
)
try
:
user
=
self
.
create_user_from_openid
(
openid_response
)
except
StrictUsernameViolation
:
return
None
else
:
user
=
user_openid
.
user
...
...
@@ -142,6 +147,12 @@ class OpenIDBackend:
nickname
=
details
[
'nickname'
]
or
'openiduser'
email
=
details
[
'email'
]
or
''
if
getattr
(
settings
,
'OPENID_STRICT_USERNAMES'
,
False
):
if
details
[
'nickname'
]
is
None
or
details
[
'nickname'
]
==
''
:
raise
StrictUsernameViolation
(
"No username"
)
if
User
.
objects
.
filter
(
username__exact
=
nickname
)
.
count
()
>
0
:
raise
StrictUsernameViolation
(
"Duplicate username:
%
s"
%
nickname
)
# Pick a username for the user based on their nickname,
# checking for conflicts.
i
=
1
...
...
django_openid_auth/tests/test_views.py
View file @
e0dd0465
...
...
@@ -131,12 +131,14 @@ class RelyingPartyTests(TestCase):
self
.
old_login_redirect_url
=
getattr
(
settings
,
'LOGIN_REDIRECT_URL'
,
'/accounts/profile/'
)
self
.
old_create_users
=
getattr
(
settings
,
'OPENID_CREATE_USERS'
,
False
)
self
.
old_strict_usernames
=
getattr
(
settings
,
'OPENID_STRICT_USERNAMES'
,
False
)
self
.
old_update_details
=
getattr
(
settings
,
'OPENID_UPDATE_DETAILS_FROM_SREG'
,
False
)
self
.
old_sso_server_url
=
getattr
(
settings
,
'OPENID_SSO_SERVER_URL'
,
None
)
self
.
old_teams_map
=
getattr
(
settings
,
'OPENID_LAUNCHPAD_TEAMS_MAPPING'
,
{})
self
.
old_use_as_admin_login
=
getattr
(
settings
,
'OPENID_USE_AS_ADMIN_LOGIN'
,
False
)
settings
.
OPENID_CREATE_USERS
=
False
settings
.
OPENID_STRICT_USERNAMES
=
False
settings
.
OPENID_UPDATE_DETAILS_FROM_SREG
=
False
settings
.
OPENID_SSO_SERVER_URL
=
None
settings
.
OPENID_LAUNCHPAD_TEAMS_MAPPING
=
{}
...
...
@@ -145,6 +147,7 @@ class RelyingPartyTests(TestCase):
def
tearDown
(
self
):
settings
.
LOGIN_REDIRECT_URL
=
self
.
old_login_redirect_url
settings
.
OPENID_CREATE_USERS
=
self
.
old_create_users
settings
.
OPENID_STRICT_USERNAMES
=
self
.
old_strict_usernames
settings
.
OPENID_UPDATE_DETAILS_FROM_SREG
=
self
.
old_update_details
settings
.
OPENID_SSO_SERVER_URL
=
self
.
old_sso_server_url
settings
.
OPENID_LAUNCHPAD_TEAMS_MAPPING
=
self
.
old_teams_map
...
...
@@ -286,6 +289,62 @@ class RelyingPartyTests(TestCase):
self
.
assertEquals
(
user
.
last_name
,
'User'
)
self
.
assertEquals
(
user
.
email
,
'foo@example.com'
)
def
test_strict_username_no_nickname
(
self
):
settings
.
OPENID_CREATE_USERS
=
True
settings
.
OPENID_STRICT_USERNAMES
=
True
# Posting in an identity URL begins the authentication request:
response
=
self
.
client
.
post
(
'/openid/login/'
,
{
'openid_identifier'
:
'http://example.com/identity'
,
'next'
:
'/getuser/'
})
self
.
assertContains
(
response
,
'OpenID transaction in progress'
)
# Complete the request, passing back some simple registration
# data. The user is redirected to the next URL.
openid_request
=
self
.
provider
.
parseFormPost
(
response
.
content
)
sreg_request
=
sreg
.
SRegRequest
.
fromOpenIDRequest
(
openid_request
)
openid_response
=
openid_request
.
answer
(
True
)
sreg_response
=
sreg
.
SRegResponse
.
extractResponse
(
sreg_request
,
{
'nickname'
:
''
,
# No nickname
'fullname'
:
'Some User'
,
'email'
:
'foo@example.com'
})
openid_response
.
addExtension
(
sreg_response
)
response
=
self
.
complete
(
openid_response
)
# Status code should be 403: Forbidden
self
.
assertEquals
(
403
,
response
.
status_code
)
def
test_strict_username_duplicate_user
(
self
):
settings
.
OPENID_CREATE_USERS
=
True
settings
.
OPENID_STRICT_USERNAMES
=
True
# Create a user with the same name as we'll pass back via sreg.
user
=
User
.
objects
.
create_user
(
'someuser'
,
'someone@example.com'
)
useropenid
=
UserOpenID
(
user
=
user
,
claimed_id
=
'http://example.com/different_identity'
,
display_id
=
'http://example.com/different_identity'
)
useropenid
.
save
()
# Posting in an identity URL begins the authentication request:
response
=
self
.
client
.
post
(
'/openid/login/'
,
{
'openid_identifier'
:
'http://example.com/identity'
,
'next'
:
'/getuser/'
})
self
.
assertContains
(
response
,
'OpenID transaction in progress'
)
# Complete the request, passing back some simple registration
# data. The user is redirected to the next URL.
openid_request
=
self
.
provider
.
parseFormPost
(
response
.
content
)
sreg_request
=
sreg
.
SRegRequest
.
fromOpenIDRequest
(
openid_request
)
openid_response
=
openid_request
.
answer
(
True
)
sreg_response
=
sreg
.
SRegResponse
.
extractResponse
(
sreg_request
,
{
'nickname'
:
'someuser'
,
'fullname'
:
'Some User'
,
'email'
:
'foo@example.com'
})
openid_response
.
addExtension
(
sreg_response
)
response
=
self
.
complete
(
openid_response
)
# Status code should be 403: Forbidden
self
.
assertEquals
(
403
,
response
.
status_code
)
def
test_login_update_details
(
self
):
settings
.
OPENID_UPDATE_DETAILS_FROM_SREG
=
True
user
=
User
.
objects
.
create_user
(
'testuser'
,
'someone@example.com'
)
...
...
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