Skip to content

Commit fd662b1

Browse files
navidnabaviKludex
andauthored
Implement identity on SimpleUser and UnauthenticatedUser (#3271)
Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 41db6a7 commit fd662b1

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎starlette/authentication.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def is_authenticated(self) -> bool:
138138
def display_name(self) -> str:
139139
return self.username
140140

141+
@property
142+
def identity(self) -> str:
143+
return self.username
144+
141145

142146
class UnauthenticatedUser(BaseUser):
143147
@property
@@ -147,3 +151,7 @@ def is_authenticated(self) -> bool:
147151
@property
148152
def display_name(self) -> str:
149153
return ""
154+
155+
@property
156+
def identity(self) -> str:
157+
return ""

‎tests/test_authentication.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def homepage(request: Request) -> JSONResponse:
4747
{
4848
"authenticated": request.user.is_authenticated,
4949
"user": request.user.display_name,
50+
"identity": request.user.identity,
5051
}
5152
)
5253

@@ -213,11 +214,11 @@ def test_user_interface(test_client_factory: TestClientFactory) -> None:
213214
with test_client_factory(app) as client:
214215
response = client.get("/")
215216
assert response.status_code == 200
216-
assert response.json() == {"authenticated": False, "user": ""}
217+
assert response.json() == {"authenticated": False, "user": "", "identity": ""}
217218

218219
response = client.get("/", auth=("tomchristie", "example"))
219220
assert response.status_code == 200
220-
assert response.json() == {"authenticated": True, "user": "tomchristie"}
221+
assert response.json() == {"authenticated": True, "user": "tomchristie", "identity": "tomchristie"}
221222

222223

223224
def test_authentication_required(test_client_factory: TestClientFactory) -> None:

0 commit comments

Comments
 (0)