Skip to content

Commit a2aba03

Browse files
committed
Add forwardedFor option to password grant login
1 parent 53c326a commit a2aba03

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

auth0/authentication/get_token.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def login(
125125
realm: str | None = None,
126126
audience: str | None = None,
127127
grant_type: str = "http://auth0.com/oauth/grant-type/password-realm",
128+
forwarded_for: str | None = None,
128129
) -> Any:
129130
"""Calls /oauth/token endpoint with password-realm grant type
130131
@@ -152,9 +153,16 @@ def login(
152153
grant_type (str, optional): Denotes the flow you're using. For password realm
153154
use http://auth0.com/oauth/grant-type/password-realm
154155
156+
forwarded_for (str, optional): End-user IP as a string value. Set this if you want
157+
brute-force protection to work in server-side scenarios.
158+
See https://auth0.com/docs/get-started/authentication-and-authorization-flow/avoid-common-issues-with-resource-owner-password-flow-and-attack-protection
159+
155160
Returns:
156161
access_token, id_token
157162
"""
163+
headers = None
164+
if forwarded_for:
165+
headers = {"auth0-forwarded-for": forwarded_for}
158166

159167
return self.authenticated_post(
160168
f"{self.protocol}://{self.domain}/oauth/token",
@@ -167,6 +175,7 @@ def login(
167175
"audience": audience,
168176
"grant_type": grant_type,
169177
},
178+
headers=headers,
170179
)
171180

172181
def refresh_token(

auth0/test/authentication/test_get_token.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ def test_login_simple(self, mock_post):
189189
},
190190
)
191191

192+
@mock.patch("auth0.rest.RestClient.post")
193+
def test_login_with_forwarded_for(self, mock_post):
194+
g = GetToken("my.domain.com", "cid", client_secret="clsec")
195+
196+
g.login(username="usrnm", password="pswd", forwarded_for="192.168.0.1")
197+
198+
args, kwargs = mock_post.call_args
199+
200+
self.assertEqual(args[0], "https://my.domain.com/oauth/token")
201+
self.assertEqual(
202+
kwargs["headers"],
203+
{
204+
"auth0-forwarded-for": "192.168.0.1",
205+
},
206+
)
207+
192208
@mock.patch("auth0.rest.RestClient.post")
193209
def test_refresh_token(self, mock_post):
194210
g = GetToken("my.domain.com", "cid", client_secret="clsec")

0 commit comments

Comments
 (0)