Skip to content

Commit

Permalink
Add forwardedFor option to password grant login (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Jun 26, 2023
2 parents 5c81886 + 171ec01 commit 6e101cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions auth0/authentication/get_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def login(
realm: str | None = None,
audience: str | None = None,
grant_type: str = "http://auth0.com/oauth/grant-type/password-realm",
forwarded_for: str | None = None,
) -> Any:
"""Calls /oauth/token endpoint with password-realm grant type
Expand Down Expand Up @@ -152,9 +153,16 @@ def login(
grant_type (str, optional): Denotes the flow you're using. For password realm
use http://auth0.com/oauth/grant-type/password-realm
forwarded_for (str, optional): End-user IP as a string value. Set this if you want
brute-force protection to work in server-side scenarios.
See https://auth0.com/docs/get-started/authentication-and-authorization-flow/avoid-common-issues-with-resource-owner-password-flow-and-attack-protection
Returns:
access_token, id_token
"""
headers = None
if forwarded_for:
headers = {"auth0-forwarded-for": forwarded_for}

return self.authenticated_post(
f"{self.protocol}://{self.domain}/oauth/token",
Expand All @@ -167,6 +175,7 @@ def login(
"audience": audience,
"grant_type": grant_type,
},
headers=headers,
)

def refresh_token(
Expand Down
16 changes: 16 additions & 0 deletions auth0/test/authentication/test_get_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ def test_login_simple(self, mock_post):
},
)

@mock.patch("auth0.rest.RestClient.post")
def test_login_with_forwarded_for(self, mock_post):
g = GetToken("my.domain.com", "cid", client_secret="clsec")

g.login(username="usrnm", password="pswd", forwarded_for="192.168.0.1")

args, kwargs = mock_post.call_args

self.assertEqual(args[0], "https://my.domain.com/oauth/token")
self.assertEqual(
kwargs["headers"],
{
"auth0-forwarded-for": "192.168.0.1",
},
)

@mock.patch("auth0.rest.RestClient.post")
def test_refresh_token(self, mock_post):
g = GetToken("my.domain.com", "cid", client_secret="clsec")
Expand Down

0 comments on commit 6e101cc

Please sign in to comment.