Even more migrations to mautrix-python
This commit is contained in:
@@ -14,16 +14,18 @@
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from typing import Optional
|
||||
from aiohttp import web
|
||||
from mako.template import Template
|
||||
import pkg_resources
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
|
||||
from ...types import MatrixUserID
|
||||
from mako.template import Template
|
||||
from aiohttp import web
|
||||
import pkg_resources
|
||||
|
||||
from mautrix.types import UserID
|
||||
|
||||
from ...util import sign_token, verify_token
|
||||
from ...user import User
|
||||
from ...puppet import Puppet
|
||||
@@ -31,20 +33,23 @@ from ..common import AuthAPI
|
||||
|
||||
|
||||
class PublicBridgeWebsite(AuthAPI):
|
||||
log = logging.getLogger("mau.web.public") # type: logging.Logger
|
||||
log: logging.Logger = logging.getLogger("mau.web.public")
|
||||
secret_key: str
|
||||
login: Template
|
||||
mx_login: Template
|
||||
app: web.Application
|
||||
|
||||
def __init__(self, loop: asyncio.AbstractEventLoop):
|
||||
super().__init__(loop)
|
||||
self.secret_key = "".join(
|
||||
random.choice(string.ascii_lowercase + string.digits) for _ in range(64)) # type: str
|
||||
self.secret_key = "".join(random.choices(string.ascii_lowercase + string.digits, k=64))
|
||||
|
||||
self.login = Template(pkg_resources.resource_string(
|
||||
"mautrix_telegram", "web/public/login.html.mako")) # type: Template
|
||||
"mautrix_telegram", "web/public/login.html.mako"))
|
||||
|
||||
self.mx_login = Template(pkg_resources.resource_string(
|
||||
"mautrix_telegram", "web/public/matrix-login.html.mako")) # type: Template
|
||||
"mautrix_telegram", "web/public/matrix-login.html.mako"))
|
||||
|
||||
self.app = web.Application(loop=loop) # type: web.Application
|
||||
self.app = web.Application(loop=loop)
|
||||
self.app.router.add_route("GET", "/login", self.get_login)
|
||||
self.app.router.add_route("POST", "/login", self.post_login)
|
||||
self.app.router.add_route("GET", "/matrix-login", self.get_matrix_login)
|
||||
@@ -59,11 +64,11 @@ class PublicBridgeWebsite(AuthAPI):
|
||||
"expiry": int(time.time()) + expires_in,
|
||||
})
|
||||
|
||||
def verify_token(self, token: str, endpoint: str = "/login") -> Optional[MatrixUserID]:
|
||||
def verify_token(self, token: str, endpoint: str = "/login") -> Optional[UserID]:
|
||||
token = verify_token(self.secret_key, token)
|
||||
if token and (token.get("expiry", 0) > int(time.time()) and
|
||||
token.get("endpoint", None) == endpoint):
|
||||
return MatrixUserID(token.get("mxid", None))
|
||||
return UserID(token.get("mxid", None))
|
||||
return None
|
||||
|
||||
async def get_login(self, request: web.Request) -> web.Response:
|
||||
|
||||
Reference in New Issue
Block a user