Skip to content

misspy.auth

misspy.auth.app

Class for using the old-type authentication of Misskey.

Source code in misspy/auth.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class app:
    """Class for using the old-type authentication of Misskey.

    """
    def __init__(self, address) -> None:
        if not address.startswith("http://") and not address.startswith("https://"):
            self._address: str = "https://" + address
        else:
            self._address: str = address

    async def create(
        self,
        name: str,
        description: str,
        permission: list = [],
        callbackUrl: str = None,
    ):
        """create auth app.

        Args:
            name (str): application name
            description (str): application description
            permission (list, optional): application permission.
            callbackUrl (str, optional): application callback url. Defaults to None.

        Returns:
            AttrDict: _description_
        """
        res = await request(
            self._address, None, endpoint="app/create",
            jobj={
                    "name": name,
                    "description": description,
                    "permission": permission,
                    "callbackUrl": callbackUrl,
                }
        )
        return AttrDict(res)

    async def generate(self, appSecret):
        """generate auth url

        Args:
            appSecret (str): appSecret. app.create is executed to generate it.

        Returns:
            AttrDict: _description_
        """
        res = await request(
            self._address, None, endpoint="auth/session/generate",
            jobj={
                "appSecret": appSecret,
            }
        )
        return AttrDict(res)

    async def get_token(self, appSecret, token):
        """Get a token for the account with the appSecret generated by app.create and the token generated by app.generate.

        Args:
            appSecret (str): appSecret
            token (str): token

        Returns:
            str: user token.
        """
        res = await request(
            self._address, None, endpoint="auth/session/userkey",
            jobj={"appSecret": appSecret, "token": token}
        )
        return hashlib.sha256(
            (res["accessToken"] + appSecret).encode("utf-8")
        ).hexdigest()

create(name, description, permission=[], callbackUrl=None) async

create auth app.

Parameters:

Name Type Description Default
name str

application name

required
description str

application description

required
permission list

application permission.

[]
callbackUrl str

application callback url. Defaults to None.

None

Returns:

Name Type Description
AttrDict

description

Source code in misspy/auth.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
async def create(
    self,
    name: str,
    description: str,
    permission: list = [],
    callbackUrl: str = None,
):
    """create auth app.

    Args:
        name (str): application name
        description (str): application description
        permission (list, optional): application permission.
        callbackUrl (str, optional): application callback url. Defaults to None.

    Returns:
        AttrDict: _description_
    """
    res = await request(
        self._address, None, endpoint="app/create",
        jobj={
                "name": name,
                "description": description,
                "permission": permission,
                "callbackUrl": callbackUrl,
            }
    )
    return AttrDict(res)

generate(appSecret) async

generate auth url

Parameters:

Name Type Description Default
appSecret str

appSecret. app.create is executed to generate it.

required

Returns:

Name Type Description
AttrDict

description

Source code in misspy/auth.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
async def generate(self, appSecret):
    """generate auth url

    Args:
        appSecret (str): appSecret. app.create is executed to generate it.

    Returns:
        AttrDict: _description_
    """
    res = await request(
        self._address, None, endpoint="auth/session/generate",
        jobj={
            "appSecret": appSecret,
        }
    )
    return AttrDict(res)

get_token(appSecret, token) async

Get a token for the account with the appSecret generated by app.create and the token generated by app.generate.

Parameters:

Name Type Description Default
appSecret str

appSecret

required
token str

token

required

Returns:

Name Type Description
str

user token.

Source code in misspy/auth.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
async def get_token(self, appSecret, token):
    """Get a token for the account with the appSecret generated by app.create and the token generated by app.generate.

    Args:
        appSecret (str): appSecret
        token (str): token

    Returns:
        str: user token.
    """
    res = await request(
        self._address, None, endpoint="auth/session/userkey",
        jobj={"appSecret": appSecret, "token": token}
    )
    return hashlib.sha256(
        (res["accessToken"] + appSecret).encode("utf-8")
    ).hexdigest()

misspy.ext.MiAuth

misspy.ext.MiAuth

MiAuth

MiAuth extension.

Source code in misspy/ext/MiAuth.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class MiAuth:
    """MiAuth extension.
    """
    def __init__(self, address) -> None:
        if not address.startswith("http://") and not address.startswith("https://"):
            self._address: str = "https://" + address
        else:
            self._address: str = address

    def generate_url(
        self,
        name,
        icon=None,
        callback: str=None,
        permission: list = [],
    ):
        """generate MiAuth URL.

        Args:
            name (str): Application name.
            icon (str, optional): Application icon url. Defaults to None.
            callback (str, optional): Application callback url. Defaults to None.
            permission (list | None, optional): Application Permission.

        Returns:
            str: MiAuth url.
        """
        self.session_id = uuid.uuid4()
        if callback is not None:
            callback = f"&callback={callback}"
        else:
            callback = ""
        if icon is not None:
            icon = f"&icon={icon}"
        else:
            icon = ""

        url = f"{self._address}/miauth/{self.session_id}?name={urllib.parse.quote(name)}{urllib.parse.quote(callback)}{icon}&permission={','.join(permission)}"
        return url

    def check(self):
        """If authenticated, AttrDict is returned.

        Raises:
            exception.MiAuthFailed: If not authenticated.

        Returns:
            AttrDict: MiAuth result.
        """
        res = httpx.post(f"{self._address}/api/miauth/{self.session_id}/check").json()
        if res.get("token") is not None:
            return AttrDict(res)
        else:
            raise exception.MiAuthFailed(res)

check()

If authenticated, AttrDict is returned.

Raises:

Type Description
MiAuthFailed

If not authenticated.

Returns:

Name Type Description
AttrDict

MiAuth result.

Source code in misspy/ext/MiAuth.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def check(self):
    """If authenticated, AttrDict is returned.

    Raises:
        exception.MiAuthFailed: If not authenticated.

    Returns:
        AttrDict: MiAuth result.
    """
    res = httpx.post(f"{self._address}/api/miauth/{self.session_id}/check").json()
    if res.get("token") is not None:
        return AttrDict(res)
    else:
        raise exception.MiAuthFailed(res)

generate_url(name, icon=None, callback=None, permission=[])

generate MiAuth URL.

Parameters:

Name Type Description Default
name str

Application name.

required
icon str

Application icon url. Defaults to None.

None
callback str

Application callback url. Defaults to None.

None
permission list | None

Application Permission.

[]

Returns:

Name Type Description
str

MiAuth url.

Source code in misspy/ext/MiAuth.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def generate_url(
    self,
    name,
    icon=None,
    callback: str=None,
    permission: list = [],
):
    """generate MiAuth URL.

    Args:
        name (str): Application name.
        icon (str, optional): Application icon url. Defaults to None.
        callback (str, optional): Application callback url. Defaults to None.
        permission (list | None, optional): Application Permission.

    Returns:
        str: MiAuth url.
    """
    self.session_id = uuid.uuid4()
    if callback is not None:
        callback = f"&callback={callback}"
    else:
        callback = ""
    if icon is not None:
        icon = f"&icon={icon}"
    else:
        icon = ""

    url = f"{self._address}/miauth/{self.session_id}?name={urllib.parse.quote(name)}{urllib.parse.quote(callback)}{icon}&permission={','.join(permission)}"
    return url