Skip to content

Commit

Permalink
Allow passing encrypted credentials to all auth funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jokelbaf committed May 6, 2024
1 parent 33b11f5 commit 29e9617
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
26 changes: 16 additions & 10 deletions genshin/client/components/auth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async def os_login_with_password(
password: str,
*,
port: int = 5000,
encrypted: bool = False,
token_type: typing.Optional[int] = 6,
geetest_solver: typing.Optional[typing.Callable[[SessionMMT], typing.Awaitable[SessionMMTResult]]] = None,
) -> WebLoginResult:
Expand All @@ -51,7 +52,7 @@ async def os_login_with_password(
Note that this will start a webserver if captcha is
triggered and `geetest_solver` is not passed.
"""
result = await self._os_web_login(account, password, token_type=token_type)
result = await self._os_web_login(account, password, encrypted=encrypted, token_type=token_type)

if not isinstance(result, SessionMMT):
# Captcha not triggered
Expand All @@ -62,14 +63,17 @@ async def os_login_with_password(
else:
mmt_result = await server.solve_geetest(result, port=port)

return await self._os_web_login(account, password, token_type=token_type, mmt_result=mmt_result)
return await self._os_web_login(
account, password, encrypted=encrypted, token_type=token_type, mmt_result=mmt_result
)

@base.region_specific(types.Region.CHINESE)
async def cn_login_with_password(
self,
account: str,
password: str,
*,
encrypted: bool = False,
port: int = 5000,
geetest_solver: typing.Optional[typing.Callable[[SessionMMT], typing.Awaitable[SessionMMTResult]]] = None,
) -> CNWebLoginResult:
Expand All @@ -78,7 +82,7 @@ async def cn_login_with_password(
Note that this will start a webserver if captcha is
triggered and `geetest_solver` is not passed.
"""
result = await self._cn_web_login(account, password)
result = await self._cn_web_login(account, password, encrypted=encrypted)

if not isinstance(result, SessionMMT):
# Captcha not triggered
Expand All @@ -89,7 +93,7 @@ async def cn_login_with_password(
else:
mmt_result = await server.solve_geetest(result, port=port)

return await self._cn_web_login(account, password, mmt_result=mmt_result)
return await self._cn_web_login(account, password, encrypted=encrypted, mmt_result=mmt_result)

@base.region_specific(types.Region.OVERSEAS)
async def check_mobile_number_validity(self, mobile: str) -> bool:
Expand All @@ -111,6 +115,7 @@ async def login_with_mobile_number(
self,
mobile: str,
*,
encrypted: bool = False,
port: int = 5000,
) -> MobileLoginResult:
"""Login with mobile number, returns cookies.
Expand Down Expand Up @@ -141,6 +146,7 @@ async def login_with_app_password(
account: str,
password: str,
*,
encrypted: bool = False,
port: int = 5000,
geetest_solver: typing.Optional[typing.Callable[[SessionMMT], typing.Awaitable[SessionMMTResult]]] = None,
) -> AppLoginResult:
Expand All @@ -153,7 +159,7 @@ async def login_with_app_password(
2. Email verification is triggered (can happen if you
first login with a new device).
"""
result = await self._app_login(account, password)
result = await self._app_login(account, password, encrypted=encrypted)

if isinstance(result, SessionMMT):
# Captcha triggered
Expand All @@ -162,7 +168,7 @@ async def login_with_app_password(
else:
mmt_result = await server.solve_geetest(result, port=port)

result = await self._app_login(account, password, mmt_result=mmt_result)
result = await self._app_login(account, password, encrypted=encrypted, mmt_result=mmt_result)

if isinstance(result, ActionTicket):
# Email verification required
Expand All @@ -179,7 +185,7 @@ async def login_with_app_password(
code = await server.enter_code(port=port)
await self._verify_email(code, result)

result = await self._app_login(account, password, ticket=result)
result = await self._app_login(account, password, encrypted=encrypted, ticket=result)

return result

Expand Down Expand Up @@ -257,20 +263,20 @@ async def os_game_login(
account: str,
password: str,
*,
encrypted: bool = False,
port: int = 5000,
geetest_solver: typing.Optional[typing.Callable[[RiskyCheckMMT], typing.Awaitable[RiskyCheckMMTResult]]] = None,
) -> GameLoginResult:
"""Perform a login to the game."""
password = auth_utility.encrypt_geetest_credentials(password, 2)
result = await self._shield_login(account, password)
result = await self._shield_login(account, password, encrypted=encrypted)

if isinstance(result, RiskyCheckMMT):
if geetest_solver:
mmt_result = await geetest_solver(result)
else:
mmt_result = await server.solve_geetest(result, port=port)

result = await self._shield_login(account, password, mmt_result=mmt_result)
result = await self._shield_login(account, password, encrypted=encrypted, mmt_result=mmt_result)

if not result.device_grant_required:
return await self._os_game_login(result.account.uid, result.account.token)
Expand Down
8 changes: 6 additions & 2 deletions genshin/client/components/auth/subclients/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def _app_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: SessionMMTResult,
ticket: None = ...,
) -> typing.Union[AppLoginResult, ActionTicket]: ...
Expand All @@ -42,6 +43,7 @@ async def _app_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: None = ...,
ticket: ActionTicket,
) -> AppLoginResult: ...
Expand All @@ -52,6 +54,7 @@ async def _app_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: None = ...,
ticket: None = ...,
) -> typing.Union[AppLoginResult, SessionMMT, ActionTicket]: ...
Expand All @@ -61,6 +64,7 @@ async def _app_login(
account: str,
password: str,
*,
encrypted: bool = False,
mmt_result: typing.Optional[SessionMMTResult] = None,
ticket: typing.Optional[ActionTicket] = None,
) -> typing.Union[AppLoginResult, SessionMMT, ActionTicket]:
Expand All @@ -83,8 +87,8 @@ async def _app_login(
headers["x-rpc-verify"] = ticket.to_rpc_verify_header()

payload = {
"account": auth_utility.encrypt_geetest_credentials(account, 1),
"password": auth_utility.encrypt_geetest_credentials(password, 1),
"account": account if encrypted else auth_utility.encrypt_geetest_credentials(account, 1),
"password": password if encrypted else auth_utility.encrypt_geetest_credentials(password, 1),
}

async with aiohttp.ClientSession() as session:
Expand Down
15 changes: 13 additions & 2 deletions genshin/client/components/auth/subclients/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async def _shield_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: RiskyCheckMMTResult,
) -> ShieldLoginResponse: ...

Expand All @@ -56,11 +57,17 @@ async def _shield_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: None = ...,
) -> typing.Union[ShieldLoginResponse, RiskyCheckMMT]: ...

async def _shield_login(
self, account: str, password: str, *, mmt_result: typing.Optional[RiskyCheckMMTResult] = None
self,
account: str,
password: str,
*,
encrypted: bool = False,
mmt_result: typing.Optional[RiskyCheckMMTResult] = None,
) -> typing.Union[ShieldLoginResponse, RiskyCheckMMT]:
"""Log in with the given account and password.
Expand All @@ -80,7 +87,11 @@ async def _shield_login(
else:
headers["x-rpc-risky"] = auth_utility.generate_risky_header(check_result.id)

payload = {"account": account, "password": password, "is_crypto": True}
payload = {
"account": account,
"password": password if encrypted else auth_utility.encrypt_geetest_credentials(password, 2),
"is_crypto": True,
}
async with aiohttp.ClientSession() as session:
async with session.post(
routes.SHIELD_LOGIN_URL.get_url(self.region, self.default_game), json=payload, headers=headers
Expand Down
21 changes: 14 additions & 7 deletions genshin/client/components/auth/subclients/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def _os_web_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
token_type: typing.Optional[int] = ...,
mmt_result: SessionMMTResult,
) -> WebLoginResult: ...
Expand All @@ -38,6 +39,7 @@ async def _os_web_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
token_type: typing.Optional[int] = ...,
mmt_result: None = ...,
) -> typing.Union[SessionMMT, WebLoginResult]: ...
Expand All @@ -48,6 +50,7 @@ async def _os_web_login(
account: str,
password: str,
*,
encrypted: bool = False,
token_type: typing.Optional[int] = 6,
mmt_result: typing.Optional[SessionMMTResult] = None,
) -> typing.Union[SessionMMT, WebLoginResult]:
Expand All @@ -60,8 +63,8 @@ async def _os_web_login(
headers["x-rpc-aigis"] = mmt_result.to_aigis_header()

payload = {
"account": auth_utility.encrypt_geetest_credentials(account, 1),
"password": auth_utility.encrypt_geetest_credentials(password, 1),
"account": account if encrypted else auth_utility.encrypt_geetest_credentials(account, 1),
"password": password if encrypted else auth_utility.encrypt_geetest_credentials(password, 1),
"token_type": token_type,
}

Expand Down Expand Up @@ -94,6 +97,7 @@ async def _cn_web_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: SessionMMTResult,
) -> CNWebLoginResult: ...

Expand All @@ -103,6 +107,7 @@ async def _cn_web_login( # noqa: D102 missing docstring in overload?
account: str,
password: str,
*,
encrypted: bool = ...,
mmt_result: None = ...,
) -> typing.Union[SessionMMT, CNWebLoginResult]: ...

Expand All @@ -112,6 +117,7 @@ async def _cn_web_login(
account: str,
password: str,
*,
encrypted: bool = False,
mmt_result: typing.Optional[SessionMMTResult] = None,
) -> typing.Union[SessionMMT, CNWebLoginResult]:
"""
Expand All @@ -127,8 +133,8 @@ async def _cn_web_login(
headers["x-rpc-aigis"] = mmt_result.to_aigis_header()

payload = {
"account": auth_utility.encrypt_geetest_credentials(account, 2),
"password": auth_utility.encrypt_geetest_credentials(password, 2),
"account": account if encrypted else auth_utility.encrypt_geetest_credentials(account, 2),
"password": password if encrypted else auth_utility.encrypt_geetest_credentials(password, 2),
}

async with aiohttp.ClientSession() as session:
Expand Down Expand Up @@ -156,6 +162,7 @@ async def _send_mobile_otp(
self,
mobile: str,
*,
encrypted: bool = False,
mmt_result: typing.Optional[SessionMMTResult] = None,
) -> typing.Union[None, SessionMMT]:
"""Attempt to send OTP to the provided mobile number.
Expand All @@ -170,7 +177,7 @@ async def _send_mobile_otp(
headers["x-rpc-aigis"] = mmt_result.to_aigis_header()

payload = {
"mobile": auth_utility.encrypt_geetest_credentials(mobile, 2),
"mobile": mobile if encrypted else auth_utility.encrypt_geetest_credentials(mobile, 2),
"area_code": auth_utility.encrypt_geetest_credentials("+86", 2),
}

Expand All @@ -192,7 +199,7 @@ async def _send_mobile_otp(

return None

async def _login_with_mobile_otp(self, mobile: str, otp: str) -> MobileLoginResult:
async def _login_with_mobile_otp(self, mobile: str, otp: str, *, encrypted: bool = False) -> MobileLoginResult:
"""Login with OTP and mobile number.
Returns cookies if OTP matches the one sent, raises an error otherwise.
Expand All @@ -203,7 +210,7 @@ async def _login_with_mobile_otp(self, mobile: str, otp: str) -> MobileLoginResu
}

payload = {
"mobile": auth_utility.encrypt_geetest_credentials(mobile, 2),
"mobile": mobile if encrypted else auth_utility.encrypt_geetest_credentials(mobile, 2),
"area_code": auth_utility.encrypt_geetest_credentials("+86", 2),
"captcha": otp,
}
Expand Down

0 comments on commit 29e9617

Please sign in to comment.