from flask import Flask, Response, redirect from flask import request from markupsafe import escape from mastodon import Mastodon from email.headerregistry import Address import jwt from gevent.pywsgi import WSGIServer import os import sqlite3 import zulip import random import string SECRET = os.environ["SECRET"] ZULIP = f"https://{os.environ['ZULIP']}/accounts/login/jwt/" REDIRECT = f"https://{os.environ['ZULIP']}/fedi-auth/callback" DB = os.environ.get("DB", "/var/lib/fedi-zulip/db/applications") scopes = ["read"] print(f""" Zulip is: {os.environ['ZULIP']} DB location is: {DB} """) con = sqlite3.connect(DB) cur = con.cursor() zulip_client = zulip.Client() def get_zulip_user(handle): print(f"Querying Zulip for handle: {handle}") zulip_client.call_endpoint( url=f"/users/{handle}", method="GET" ) def create_zulip_user(handle): print(f"Creating Zulip user with handle: {handle}") password = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=40)) full_name = handle.split('@')[0] payload = { "email": handle, "password": password, "full_name": full_name } response = zulip_client.create_user(payload) if response["result"] == "success": print(f"Zulip user created for handle: {handle}") return True elif response["result"] == "error" and response["msg"] == f"Email '{handle}' already in use": print(response["msg"] + ", this is okay.") return True else: print(response["msg"]) return False cur.execute("CREATE TABLE IF NOT EXISTS applications(instance TEXT PRIMARY KEY, client TEXT, secret TEXT, disabled BOOLEAN DEFAULT FALSE)") def get_application(instance): res = cur.execute("SELECT client, secret FROM applications WHERE instance = ?", [instance]) return res.fetchone(); def set_application(instance, client, secret): res = cur.execute("INSERT INTO applications(instance, client, secret) values (?, ?, ?)", (instance, client, secret)); con.commit(); app = Flask(__name__) @app.get("/fedi-auth/") def index(): return f"""
You can use this page to login to {os.environ['ZULIP']} using your
Pleroma, Akkoma or Mastodon handle. Format is nickname@server.tld
.
Please wait while you are logged in...
""" except Exception as e: print(e) pass print("Some field wasn't set.") return Response("fail", status=400)