Skip to content

Commit

Permalink
認証関連
Browse files Browse the repository at this point in the history
  • Loading branch information
sonyakun committed Mar 18, 2024
1 parent f7f2684 commit b6a798b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
41 changes: 41 additions & 0 deletions misspy/ext/auth/legacy.py
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)
52 changes: 52 additions & 0 deletions misspy/ext/auth/miauth.py
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)
1 change: 1 addition & 0 deletions misspy/ext/auth/oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# とりあえず未実装。OAuthは:neko_dakara_wakannyai2:

0 comments on commit b6a798b

Please sign in to comment.