Skip to content

Commit

Permalink
Merge pull request #191 from ahivert/fix-178
Browse files Browse the repository at this point in the history
fix: keep only the first ip to match postgres inet type
  • Loading branch information
nezhar authored Jul 29, 2024
2 parents 60570e1 + 14081a5 commit 81052dd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ DJANGO_REST_PASSWORDRESET_IP_ADDRESS_HEADER = 'HTTP_X_FORWARDED_FOR'
The same is true for the user agent:

```python
HTTP_USER_AGENT_HEADER = 'HTTP_USER_AGENT'
DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT_HEADER = 'HTTP_USER_AGENT'
```

## Custom Token Generator
Expand Down
2 changes: 1 addition & 1 deletion django_rest_passwordreset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def generate_token_for_email(email, user_agent='', ip_address=''):
return ResetPasswordToken.objects.create(
user=user,
user_agent=user_agent,
ip_address=ip_address,
ip_address=ip_address.split(",")[0],
)


Expand Down
13 changes: 13 additions & 0 deletions tests/test/test_auth_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,19 @@ def test_clear_expired_tokens(self):
# there should be zero tokens
self.assertEqual(ResetPasswordToken.objects.all().count(), 0)

def test_generate_token_for_email_with_multiple_ip_address(self):
"""
Test generating tokens with multiple ip address will keep only the first
one to match inet type
https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-INET
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#syntax
"""
# request a new token with multiple ips
generate_token_for_email(email="[email protected]", ip_address="1.1.1.1, 2.2.2.2")

# there should be one token with only the first ip adress
self.assertEqual(ResetPasswordToken.objects.get().ip_address, "1.1.1.1")

def test_generate_token_for_email(self):
""" Tests generating tokens for a specific email address programmatically """

Expand Down

0 comments on commit 81052dd

Please sign in to comment.