Skip to content

Commit b56f46c

Browse files
committed
fix: KeyError when last_sign_in_time does not exist in token_data
1 parent 6fd96fe commit b56f46c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/firebase_functions/private/_identity_fn.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ def _auth_user_info_from_token_data(token_data: dict[str, _typing.Any]):
6565
def _auth_user_metadata_from_token_data(token_data: dict[str, _typing.Any]):
6666
from firebase_functions.identity_fn import AuthUserMetadata
6767
creation_time = int(token_data["creation_time"]) / 1000.0
68-
last_sign_in_time = int(token_data["last_sign_in_time"]) / 1000.0
68+
last_sign_in_time = None
69+
if "last_sign_in_time" in token_data:
70+
last_sign_in_time = int(token_data["last_sign_in_time"]) / 1000.0
6971
return AuthUserMetadata(
7072
creation_time=_dt.datetime.utcfromtimestamp(creation_time),
71-
last_sign_in_time=_dt.datetime.utcfromtimestamp(last_sign_in_time),
73+
last_sign_in_time=_dt.datetime.utcfromtimestamp(last_sign_in_time) if last_sign_in_time else None,
7274
)
7375

7476

@@ -89,7 +91,7 @@ def _auth_multi_factor_info_from_token_data(token_data: dict[str, _typing.Any]):
8991

9092

9193
def _auth_multi_factor_settings_from_token_data(token_data: dict[str,
92-
_typing.Any]):
94+
_typing.Any]):
9395
if not token_data:
9496
return None
9597

@@ -214,14 +216,14 @@ def _auth_blocking_event_from_token_data(token_data: dict[str, _typing.Any]):
214216

215217

216218
def _validate_auth_response(
217-
event_type: str,
218-
auth_response,
219+
event_type: str,
220+
auth_response,
219221
) -> dict[str, _typing.Any]:
220222
if auth_response is None:
221223
auth_response = {}
222224

223225
custom_claims: dict[str,
224-
_typing.Any] | None = auth_response.get("custom_claims")
226+
_typing.Any] | None = auth_response.get("custom_claims")
225227
session_claims: dict[str, _typing.Any] | None = auth_response.get(
226228
"session_claims")
227229

@@ -303,9 +305,9 @@ def _validate_auth_response(
303305

304306

305307
def before_operation_handler(
306-
func: _typing.Callable,
307-
event_type: str,
308-
request: _Request,
308+
func: _typing.Callable,
309+
event_type: str,
310+
request: _Request,
309311
) -> _Response:
310312
from firebase_functions.identity_fn import BeforeCreateResponse, BeforeSignInResponse
311313
try:

0 commit comments

Comments
 (0)