Skip to content

Commit

Permalink
Remove broken hypergryph authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
thesadru committed Jan 30, 2025
1 parent c483177 commit 2cb9e4d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 120 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ Arknights python wrapper.

Interacts directly with the game servers, no delays.

支持**中文和B站**账户!

<!-- Supports Chinese and BiliBili accounts -->

---

Source Code: <https://github.com/thesadru/arkprts>
Expand Down Expand Up @@ -95,16 +91,6 @@ for item_id, subitems in user.consumable.items():
print(item_id, item.ts, item.count)
```

Logging in with email and password to the cn server.

```py
auth = arkprts.HypergryphAuth()
await auth.login("[email protected]", "wordpass12")
client = arkprts.Client(auth=auth)

await client.get_data()
```

Making a new client when a global guest client already exists; without excess overhead.

```py
Expand Down
89 changes: 1 addition & 88 deletions arkprts/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@
- Use the access token and channel uid to get an arknights player uid and u8 token.
- Use the arknights player uid and u8 token to get a session secret.
## CN (HyperGryph)
### Get a permanent token
- Send unhashed username and password to receive an access token.
- Optionally, send the access token to receive a channel uid.
### Authenticate
- Use the access token and channel uid to get an arknights player uid and u8 token.
- Use the arknights player uid and u8 token to get a session secret.
## Bilibili
### Get a permanent token
Expand Down Expand Up @@ -98,7 +86,6 @@
"BilibiliAuth",
"CoreAuth",
"GuestAuth",
"HypergryphAuth",
"LongchengAuth",
"MultiAuth",
"YostarAuth",
Expand Down Expand Up @@ -216,7 +203,7 @@ def create(
if server in ("en", "jp", "kr"):
return YostarAuth(server, network=network)
if server == "cn":
return HypergryphAuth(server, network=network)
raise NotImplementedError("Hypergryph authentication is not implemented.")
if server == "bili":
return BilibiliAuth(server, network=network)
if server == "tw":
Expand Down Expand Up @@ -494,80 +481,6 @@ async def login_as_guest(self, nickname: str | None = None) -> tuple[str, str]:
return channel_uid, yostar_token


class HypergryphAuth(Auth):
"""Authentication client for chinese accounts."""

distributor: typing.Literal["hypergryph"]

def __init__(
self,
server: typing.Literal["cn"] = "cn",
*,
network: netn.NetworkSession | None = None,
) -> None:
super().__init__(server, network=network)

async def _get_hypergryph_access_token(self, username: str, password: str) -> str:
"""Get an access token from a username and password."""
data = {
"account": username,
"password": password,
"deviceId": self.device_ids[0],
"platform": 1,
}
data["sign"] = generate_u8_sign(data)
data = await self.request("as", "user/login", json=data)
return data["token"]

async def _get_hypergryph_uid(self, token: str) -> str:
"""Get a channel uid from a hypergryph access token."""
data = {"token": token}
data["sign"] = generate_u8_sign(data)
data = await self.request("as", "user/auth", json=data)
return data["uid"]

async def login_with_token(self, channel_uid: str, access_token: str) -> None:
"""Login with an access token."""
self.session.uid, u8_token = await self._get_u8_token(channel_uid, access_token)
await self._get_secret(self.session.uid, u8_token)

async def get_token_from_password(
self,
username: str | None = None,
password: str | None = None,
*,
stdin: bool = False,
) -> tuple[str, str]:
"""Get a token from a hypergryph account."""
if not username or not password:
if not stdin:
raise TypeError("Password not provided but stdin is disabled.")

username = input("Enter username: ")
password = input("Enter password: ")

access_token = await self._get_hypergryph_access_token(username, password)
channel_uid = await self._get_hypergryph_uid(access_token)
return channel_uid, access_token

async def login(
self,
username: str | None = None,
password: str | None = None,
*,
stdin: bool = True,
) -> tuple[str, str]:
"""Login with a hypergryph account."""
channel_uid, access_token = await self.get_token_from_password(username, password, stdin=stdin)
await self.login_with_token(channel_uid, access_token)

if stdin:
print(f"Channel UID: {channel_uid} Access token: {access_token}") # noqa: T201
print(f'Usage: login_with_token("{channel_uid}", "{access_token}")') # noqa: T201

return channel_uid, access_token


class BilibiliAuth(Auth):
"""Authentication client for bilibili accounts."""

Expand Down
36 changes: 18 additions & 18 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ async def kr_client(pytestconfig: pytest.Config, network: arkprts.NetworkSession
return arkprts.Client(auth)


@pytest.fixture(scope="session")
async def cn_client(network: arkprts.NetworkSession) -> arkprts.Client:
"""Private cn client."""
channel_uid, access_token = os.environ.get("CN_CHANNEL_UID"), os.environ.get("CN_ACCESS_TOKEN")
if not channel_uid or not access_token:
cn_username, cn_password = os.environ.get("CN_USERNAME"), os.environ.get("CN_PASSWORD")
if cn_username:
auth = arkprts.HypergryphAuth(network=network)
channel_uid, access_token = await auth.login(cn_username, cn_password)
warnings.warn(f"Please use CN_CHANNEL_UID={channel_uid} CN_ACCESS_TOKEN={access_token}")
return arkprts.Client(auth)
# @pytest.fixture(scope="session")
# async def cn_client(network: arkprts.NetworkSession) -> arkprts.Client:
# """Private cn client."""
# channel_uid, access_token = os.environ.get("CN_CHANNEL_UID"), os.environ.get("CN_ACCESS_TOKEN")
# if not channel_uid or not access_token:
# cn_username, cn_password = os.environ.get("CN_USERNAME"), os.environ.get("CN_PASSWORD")
# if cn_username:
# auth = arkprts.HypergryphAuth(network=network)
# channel_uid, access_token = await auth.login(cn_username, cn_password)
# warnings.warn(f"Please use CN_CHANNEL_UID={channel_uid} CN_ACCESS_TOKEN={access_token}")
# return arkprts.Client(auth)

pytest.skip("CN_CHANNEL_UID or CN_ACCESS_TOKEN not set")
# pytest.skip("CN_CHANNEL_UID or CN_ACCESS_TOKEN not set")

auth = await arkprts.Auth.from_token("cn", channel_uid, access_token, network=network)
return arkprts.Client(auth)
# auth = await arkprts.Auth.from_token("cn", channel_uid, access_token, network=network)
# return arkprts.Client(auth)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -145,10 +145,10 @@ def test_kr_client(kr_client: arkprts.Client) -> None:
assert kr_client.auth.session.uid


def test_cn_client(cn_client: arkprts.Client) -> None:
assert isinstance(cn_client.auth, arkprts.Auth)
assert cn_client.auth.server == "cn"
assert cn_client.auth.session.uid
# def test_cn_client(cn_client: arkprts.Client) -> None:
# assert isinstance(cn_client.auth, arkprts.Auth)
# assert cn_client.auth.server == "cn"
# assert cn_client.auth.session.uid


def test_bili_client(bili_client: arkprts.Client) -> None:
Expand Down

0 comments on commit 2cb9e4d

Please sign in to comment.