diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 51dfd0e7..40eecf35 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -371,6 +371,8 @@ def pyrogram_api(): recover_password accept_terms_of_service log_out + get_active_sessions + reset_session """, advanced=""" Advanced @@ -592,6 +594,8 @@ def pyrogram_api(): """, authorization=""" Authorization + ActiveSession + ActiveSessions SentCode TermsOfService """ @@ -751,6 +755,10 @@ def pyrogram_api(): Folder.pin_chat Folder.remove_chat Folder.export_link + """, + active_session=""" + ActiveSession + ActiveSession.reset """ ) diff --git a/compiler/docs/template/bound-methods.rst b/compiler/docs/template/bound-methods.rst index ee6bd2c3..2ec602e5 100644 --- a/compiler/docs/template/bound-methods.rst +++ b/compiler/docs/template/bound-methods.rst @@ -115,7 +115,7 @@ ChatJoinRequest {chat_join_request_toctree} Story ---------------- +----- .. hlist:: :columns: 3 @@ -128,7 +128,7 @@ Story {story_toctree} Folder ---------------- +------ .. hlist:: :columns: 2 @@ -139,3 +139,16 @@ Folder :hidden: {folder_toctree} + +ActiveSession +------------- + +.. hlist:: + :columns: 2 + + {active_session_hlist} + +.. toctree:: + :hidden: + + {active_session_toctree} diff --git a/pyrogram/methods/auth/__init__.py b/pyrogram/methods/auth/__init__.py index ce585648..c97fe89f 100644 --- a/pyrogram/methods/auth/__init__.py +++ b/pyrogram/methods/auth/__init__.py @@ -20,11 +20,13 @@ from .accept_terms_of_service import AcceptTermsOfService from .check_password import CheckPassword from .connect import Connect from .disconnect import Disconnect +from .get_active_sessions import GetActiveSessions from .get_password_hint import GetPasswordHint from .initialize import Initialize from .log_out import LogOut from .recover_password import RecoverPassword from .resend_code import ResendCode +from .reset_session import ResetSession from .send_code import SendCode from .send_recovery_code import SendRecoveryCode from .sign_in import SignIn @@ -38,11 +40,13 @@ class Auth( CheckPassword, Connect, Disconnect, + GetActiveSessions, GetPasswordHint, Initialize, LogOut, RecoverPassword, ResendCode, + ResetSession, SendCode, SendRecoveryCode, SignIn, diff --git a/pyrogram/methods/auth/get_active_sessions.py b/pyrogram/methods/auth/get_active_sessions.py new file mode 100644 index 00000000..9a3ff498 --- /dev/null +++ b/pyrogram/methods/auth/get_active_sessions.py @@ -0,0 +1,39 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw, types + + +class GetActiveSessions: + async def get_active_sessions( + self: "pyrogram.Client" + ) -> "types.ActiveSessions": + """Returns all active sessions of the current user. + + .. include:: /_includes/usable-by/users.rst + + Returns: + :obj:`~pyrogram.types.ActiveSessions`: On success, all the active sessions of the current user is returned. + + """ + r = await self.invoke( + raw.functions.account.GetAuthorizations() + ) + + return types.ActiveSessions._parse(r) diff --git a/pyrogram/methods/auth/reset_session.py b/pyrogram/methods/auth/reset_session.py new file mode 100644 index 00000000..6332794e --- /dev/null +++ b/pyrogram/methods/auth/reset_session.py @@ -0,0 +1,40 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import pyrogram +from pyrogram import raw + + +class ResetSession: + async def reset_session( + self: "pyrogram.Client", + id: int + ) -> bool: + """Log out an active authorized session by its hash. + + .. include:: /_includes/usable-by/users.rst + + Returns: + ``bool``: On success, in case the session is destroyed, True is returned. Otherwise, False is returned. + + """ + r = await self.invoke( + raw.functions.account.ResetAuthorization(hash=id) + ) + + return r diff --git a/pyrogram/types/authorization/__init__.py b/pyrogram/types/authorization/__init__.py index b31ad26a..04a34f2a 100644 --- a/pyrogram/types/authorization/__init__.py +++ b/pyrogram/types/authorization/__init__.py @@ -16,7 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .active_session import ActiveSession +from .active_sessions import ActiveSessions from .sent_code import SentCode from .terms_of_service import TermsOfService -__all__ = ["TermsOfService", "SentCode"] +__all__ = [ + "ActiveSession", + "ActiveSessions", + "SentCode", + "TermsOfService", +] diff --git a/pyrogram/types/authorization/active_session.py b/pyrogram/types/authorization/active_session.py new file mode 100644 index 00000000..174c2d12 --- /dev/null +++ b/pyrogram/types/authorization/active_session.py @@ -0,0 +1,179 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from datetime import datetime + +from pyrogram import raw, utils + +from ..object import Object + + +class ActiveSession(Object): + """Contains information about one session in a Telegram application used by the current user. Sessions must be shown to the user in the returned order. + + Parameters: + id (``int``): + Session identifier. + + device_model (``str``): + Model of the device the application has been run or is running on, as provided by the application. + + platform (``str``): + Operating system the application has been run or is running on, as provided by the application. + + system_version (``str``): + Version of the operating system the application has been run or is running on, as provided by the application. + + api_id (``int``): + Telegram API identifier, as provided by the application. + + application_name (``str``): + Name of the application, as provided by the application. + + application_version (``str``): + The version of the application, as provided by the application. + + log_in_date (:py:obj:`~datetime.datetime`, *optional*): + Point in time (Unix timestamp) when the user has logged in. + + last_active_date (:py:obj:`~datetime.datetime`, *optional*): + Point in time (Unix timestamp) when the session was last used. + + ip_address (``str``): + IP address from which the session was created, in human-readable format. + + location (``str``): + A human-readable description of the location from which the session was created, based on the IP address. + + country (``str``): + Country determined from IP. + + region (``str``): + Region determined from IP. + + is_current (``bool``): + True, if this session is the current session. + + is_password_pending (``bool``): + True, if a 2-step verification password is needed to complete authorization of the session. + + is_unconfirmed (``bool``): + True, if the session wasn't confirmed from another session. + + can_accept_secret_chats (``bool``): + True, if incoming secret chats can be accepted by the session. + + can_accept_calls (``bool``): + True, if incoming calls can be accepted by the session. + + is_official_application (``bool``): + True, if the application is an official application or uses the api_id of an official application. + + """ + + def __init__( + self, + *, + id: int = None, + device_model: str = None, + platform: str = None, + system_version: str = None, + api_id: int = None, + application_name: str = None, + application_version: str = None, + log_in_date: datetime = None, + last_active_date: datetime = None, + ip_address: str = None, + location: str = None, + country: str = None, + region: str = None, + is_current: bool = None, + is_password_pending: bool = None, + is_unconfirmed: bool = None, + can_accept_secret_chats: bool = None, + can_accept_calls: bool = None, + is_official_application: bool = None + ): + super().__init__() + + self.id = id + self.device_model = device_model + self.platform = platform + self.system_version = system_version + self.api_id = api_id + self.application_name = application_name + self.application_version = application_version + self.log_in_date = log_in_date + self.last_active_date = last_active_date + self.ip_address = ip_address + self.location = location + self.country = country + self.region = region + self.is_current = is_current + self.is_password_pending = is_password_pending + self.is_unconfirmed = is_unconfirmed + self.can_accept_secret_chats = can_accept_secret_chats + self.can_accept_calls = can_accept_calls + self.is_official_application = is_official_application + + @staticmethod + def _parse(session: "raw.types.Authorization") -> "ActiveSession": + return ActiveSession( + id=session.hash, + device_model=session.device_model, + platform=session.platform, + system_version=session.system_version, + api_id=session.api_id, + application_name=session.app_name, + application_version=session.app_version, + log_in_date=utils.timestamp_to_datetime(session.date_created), + last_active_date=utils.timestamp_to_datetime(session.date_active), + ip_address=session.ip, + location=session.region, + country=session.country, + region=session.region, + is_current=getattr(session, "current", None), + is_password_pending=getattr(session, "password_pending", None), + is_unconfirmed=getattr(session, "unconfirmed", None), + can_accept_secret_chats=not getattr(session, "encrypted_requests_disabled", False), + can_accept_calls=not getattr(session, "call_requests_disabled", False), + is_official_application=getattr(session, "official_app", None) + ) + + async def reset(self): + """Bound method *reset* of :obj:`~pyrogram.types.ActiveSession`. + + Use as a shortcut for: + + .. code-block:: python + + await client.reset_session(123456789) + + Example: + .. code-block:: python + + await session.reset() + + Returns: + True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + return await self._client.reset_session(self.id) diff --git a/pyrogram/types/authorization/active_sessions.py b/pyrogram/types/authorization/active_sessions.py new file mode 100644 index 00000000..ff196577 --- /dev/null +++ b/pyrogram/types/authorization/active_sessions.py @@ -0,0 +1,56 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import List + +from pyrogram import raw, types + +from ..object import Object + + +class ActiveSessions(Object): + """Contains a list of currently active sessions + + Parameters: + inactive_session_ttl_days (``int``): + Number of days of inactivity before sessions will automatically be terminated; 1-366 days. + + active_sessions (List of :obj:`~pyrogram.types.ActiveSession`): + List of sessions. + """ + + def __init__( + self, + *, + inactive_session_ttl_days: int = None, + active_sessions: List["types.ActiveSession"] = None + ): + super().__init__() + + self.inactive_session_ttl_days = inactive_session_ttl_days + self.active_sessions = active_sessions + + @staticmethod + def _parse(authorizations: "raw.types.account.Authorizations") -> "ActiveSessions": + return ActiveSessions( + inactive_session_ttl_days=authorizations.authorization_ttl_days, + active_sessions=types.List([ + types.ActiveSession._parse(active) + for active in authorizations.authorizations + ]) + )