File indexing completed on 2024-09-01 04:45:28

0001 # SPDX-License-Identifier: MIT
0002 # SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
0003 
0004 import json
0005 from flask import Flask, request, abort
0006 import os
0007 app = Flask(__name__)
0008 
0009 
0010 @app.route("/_matrix/client/v3/login", methods=["GET"])
0011 def login_get():
0012     result = dict()
0013     result["flows"] = [dict()]
0014     result["flows"][0]["type"] = "m.login.password"
0015     return result
0016 
0017 @app.route("/_matrix/client/v3/account/whoami", methods=["GET"])
0018 def whoami():
0019     result = dict()
0020     result["device_id"] = "device_id_1234"
0021     result["user_id"] = "@user:localhost:1234"
0022     return result
0023 
0024 @app.route("/_matrix/client/v3/login", methods=["POST"])
0025 def login_post():
0026     data = request.get_json()
0027     if data["identifier"]["user"] != "user" or data["password"] != "1234":
0028         abort(403)
0029     print(data)
0030     result = dict()
0031     result["access_token"] = "token_login"
0032     result["device_id"] = "device_1234"
0033     result["user_id"] = "@user:localhost:1234"
0034     return result
0035 
0036 def load_json(name):
0037     parts = __file__.split("/")
0038     parts.pop()
0039     datadir = "/".join(parts)
0040     return json.loads(open(f"{datadir}/data/{name}.json").read())
0041 
0042 
0043 @app.route("/_matrix/client/r0/sync")
0044 def sync():
0045 
0046     result = load_json("sync_response_no_rooms") if ("login" in request.headers.get("Authorization")) else load_json("sync_response_rooms")
0047     return result
0048 
0049 @app.route("/.well-known/matrix/client")
0050 def well_known():
0051     reply = dict()
0052     reply["m.homeserver"] = dict()
0053     reply["m.homeserver"]["base_url"] = "https://localhost:1234"
0054     return reply
0055 
0056 @app.route("/_matrix/client/v3/profile/<id>")
0057 def profile(id):
0058     reply = dict()
0059     reply["avatar_url"] = "mxc://localhost:1234/asdf1234"
0060     reply["displayname"] = "User123"
0061     return reply
0062 
0063 @app.route("/_matrix/client/v3/keys/upload", methods=["POST"])
0064 def upload_keys():
0065     reply = dict()
0066     return reply
0067 
0068 
0069 if __name__ == "__main__":
0070     app.run(ssl_context='adhoc', port=1234)