-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# とりあえず未実装。OAuthは:neko_dakara_wakannyai2: |