diff --git a/misspy/ext/auth/legacy.py b/misspy/ext/auth/legacy.py new file mode 100644 index 0000000..e6c0563 --- /dev/null +++ b/misspy/ext/auth/legacy.py @@ -0,0 +1,41 @@ +from typing import Union, List + +import aiohttp +from mitypes.user import User + +from ...core.types.internal import auth_session +from ...core.types.other import App +from ...core.exception import AuthFailed + +class session: + def __init__(self) -> None: + pass + + async def generate( + self, + host: str, + name: str, + description: str, + permission: List[str], + callback: Union[str, None] = None, + ): + async with aiohttp.ClientSession() as session: + async with session.post("https://{}/api/app/create".format(host), data={"name": name, "description": description, "permission": permission, "callbackUrl": callback}) as resp: + authApp = App(**await resp.json()) + async with session.post("https://{}/api/auth/session/generate".format(host), data={"appSecret": authApp.secret}) as resp2: + resp2 = await resp2.json() + return auth_session(host, authApp.secret, resp2["token"], resp2["url"]) + + async def check( + self, + authSession: auth_session + ): + async with aiohttp.ClientSession() as session: + async with session.post("https://{}/api/auth/session/userkey".format(authSession.host)) as resp: + resp = await resp.json() + if resp.get("accessToken") is not None: + resp_i: dict = resp["user"] + resp_i["accessToken"] = resp["accessToken"] + return User(**resp_i) + else: + raise AuthFailed(resp) \ No newline at end of file diff --git a/misspy/ext/auth/miauth.py b/misspy/ext/auth/miauth.py new file mode 100644 index 0000000..de7cdba --- /dev/null +++ b/misspy/ext/auth/miauth.py @@ -0,0 +1,52 @@ +import urllib.parse +from typing import Union, List +import uuid + +import aiohttp + +from ...core.types.internal import miauth_session +from ...core.types.user import User +from ...core.exception import MiAuthFailed, NotFound + +class session: + def __init__(self) -> None: + pass + + async def generate( + self, + host: str, + name: Union[str, None] = None, + icon: Union[str, None] = None, + callback: Union[str, None] = None, + permission: Union[List[str], None] = None, + ): + sessionId = uuid.uuid4() + url = urllib.parse.urlparse("https://{}".format(host)) + params = urllib.parse.parse_qs(url.query) + if name: + params["name"] = name + if icon: + params["icon"] = icon + if callback: + params["callback"] = callback + if permission: + params["permission"] = ",".join(permission) + generated_session = miauth_session(sessionId, host, urllib.parse.urlunparse(url._replace(query=urllib.parse.urlencode(params, doseq=True))), name, icon, callback, permission) + return generated_session + + async def check( + self, + authSession: miauth_session + ): + async with aiohttp.ClientSession() as session: + async with session.post("https://{}/api/miauth/{}/check".format(authSession.host, authSession.sessionId)) as resp: + try: + resp = await resp.json() + except aiohttp.ContentTypeError: + raise NotFound('API endpoint "miauth/{}/check" does not exist. The version of Misskey you are using may be older than 12.27.0.'.format(authSession.sessionId)) + resp_i: dict = resp["user"] + resp_i["token"] = resp["token"] + if resp.get("token") is not None: + return User(**resp_i) + else: + raise MiAuthFailed(resp) \ No newline at end of file diff --git a/misspy/ext/auth/oauth.py b/misspy/ext/auth/oauth.py new file mode 100644 index 0000000..998ef7a --- /dev/null +++ b/misspy/ext/auth/oauth.py @@ -0,0 +1 @@ +# とりあえず未実装。OAuthは:neko_dakara_wakannyai2: \ No newline at end of file