1 # Configuring Ejabberd (XMPP Server) to use Pleroma for authentication
3 If you want to give your Pleroma users an XMPP (chat) account, you can configure [Ejabberd](https://github.com/processone/ejabberd) to use your Pleroma server for user authentication, automatically giving every local user an XMPP account.
5 In general, you just have to follow the configuration described at [https://docs.ejabberd.im/admin/configuration/authentication/#external-script](https://docs.ejabberd.im/admin/configuration/authentication/#external-script). Please read this section carefully.
7 Copy the script below to suitable path on your system and set owner and permissions. Also do not forget adjusting `PLEROMA_HOST` and `PLEROMA_PORT`, if necessary.
10 cp pleroma_ejabberd_auth.py /etc/ejabberd/pleroma_ejabberd_auth.py
11 chown ejabberd /etc/ejabberd/pleroma_ejabberd_auth.py
12 chmod 700 /etc/ejabberd/pleroma_ejabberd_auth.py
15 Set external auth params in ejabberd.yaml file:
18 auth_method: [external]
19 extauth_program: "python3 /etc/ejabberd/pleroma_ejabberd_auth.py"
24 Restart / reload your ejabberd service.
26 After restarting your Ejabberd server, your users should now be able to connect with their Pleroma credentials.
33 from base64 import b64encode
37 PLEROMA_HOST = "127.0.0.1"
39 AUTH_ENDPOINT = "/api/v1/accounts/verify_credentials"
40 USER_ENDPOINT = "/api/v1/accounts"
41 LOGFILE = "/var/log/ejabberd/pleroma_auth.log"
43 logging.basicConfig(filename=LOGFILE, level=logging.INFO)
47 def create_connection():
48 return http.client.HTTPConnection(PLEROMA_HOST, PLEROMA_PORT)
51 def verify_credentials(user: str, password: str) -> bool:
52 user_pass_b64 = b64encode("{}:{}".format(
53 user, password).encode('utf-8')).decode("ascii")
56 "Authorization": "Basic {}".format(user_pass_b64)
60 conn = create_connection()
61 conn.request("GET", AUTH_ENDPOINT, params, headers)
63 response = conn.getresponse()
64 if response.status == 200:
68 except Exception as e:
69 logging.info("Can not connect: %s", str(e))
73 def does_user_exist(user: str) -> bool:
74 conn = create_connection()
75 conn.request("GET", "{}/{}".format(USER_ENDPOINT, user))
77 response = conn.getresponse()
78 if response.status == 200:
84 def auth(username: str, server: str, password: str) -> bool:
85 return verify_credentials(username, password)
88 def isuser(username, server):
89 return does_user_exist(username)
93 (pkt_size,) = struct.unpack('>H', bytes(sys.stdin.read(2), encoding='utf8'))
94 pkt = sys.stdin.read(pkt_size)
95 cmd = pkt.split(':')[0]
97 username, server, password = pkt.split(':', 3)[1:]
98 write(auth(username, server, password))
100 username, server = pkt.split(':', 2)[1:]
101 write(isuser(username, server))
102 elif cmd == 'setpass':
103 # u, s, p = pkt.split(':', 3)[1:]
105 elif cmd == 'tryregister':
106 # u, s, p = pkt.split(':', 3)[1:]
108 elif cmd == 'removeuser':
109 # u, s = pkt.split(':', 2)[1:]
111 elif cmd == 'removeuser3':
112 # u, s, p = pkt.split(':', 3)[1:]
120 sys.stdout.write('\x00\x02\x00\x01')
122 sys.stdout.write('\x00\x02\x00\x00')
126 if __name__ == "__main__":
127 logging.info("Starting pleroma ejabberd auth daemon...")
131 except Exception as e:
133 "Error while processing data from ejabberd %s", str(e))