kopia lustrzana https://gitlab.com/jaywink/federation
Add Matrix utility function to register users on Dendrite
rodzic
48be2cbb6e
commit
1b461806f0
|
@ -7,6 +7,9 @@
|
||||||
* Add `federation.hostmeta` generators for Matrix client and server well-known files.
|
* Add `federation.hostmeta` generators for Matrix client and server well-known files.
|
||||||
Django views and url configuration also included for convenience.
|
Django views and url configuration also included for convenience.
|
||||||
|
|
||||||
|
* Add `register_dendrite_user` Matrix protocol utility to register users on Dendrite
|
||||||
|
homeservers using a shared registration secret.
|
||||||
|
|
||||||
## [0.21.0] - 2020-12-20
|
## [0.21.0] - 2020-12-20
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -239,6 +239,8 @@ Some settings need to be set in Django settings. An example is below:
|
||||||
"client_wellknown_other_keys": {
|
"client_wellknown_other_keys": {
|
||||||
"org.foo.key" "barfoo",
|
"org.foo.key" "barfoo",
|
||||||
},
|
},
|
||||||
|
# (Optional) registration shared secret
|
||||||
|
"registration_shared_secret": "supersecretstring",
|
||||||
}
|
}
|
||||||
|
|
||||||
* ``nodeinfo2_function`` (optional) function that returns data for generating a `NodeInfo2 document <https://github.com/jaywink/nodeinfo2>`_. Once configured the path ``/.well-known/x-nodeinfo2`` will automatically generate a NodeInfo2 document. The function should return a ``dict`` corresponding to the NodeInfo2 schema, with the following minimum items:
|
* ``nodeinfo2_function`` (optional) function that returns data for generating a `NodeInfo2 document <https://github.com/jaywink/nodeinfo2>`_. Once configured the path ``/.well-known/x-nodeinfo2`` will automatically generate a NodeInfo2 document. The function should return a ``dict`` corresponding to the NodeInfo2 schema, with the following minimum items:
|
||||||
|
@ -296,6 +298,11 @@ Diaspora
|
||||||
.. autofunction:: federation.utils.diaspora.retrieve_diaspora_hcard
|
.. autofunction:: federation.utils.diaspora.retrieve_diaspora_hcard
|
||||||
.. autofunction:: federation.utils.diaspora.retrieve_diaspora_host_meta
|
.. autofunction:: federation.utils.diaspora.retrieve_diaspora_host_meta
|
||||||
|
|
||||||
|
Matrix
|
||||||
|
......
|
||||||
|
|
||||||
|
.. autofunction:: federation.utils.matrix.register_dendrite_user
|
||||||
|
|
||||||
Network
|
Network
|
||||||
.......
|
.......
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
import hashlib
|
||||||
|
import hmac
|
||||||
|
import uuid
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from federation.utils.django import get_function_from_config
|
||||||
|
|
||||||
|
|
||||||
|
def generate_dendrite_mac(shared_secret: str, username: str, password: str, admin: bool) -> str:
|
||||||
|
"""
|
||||||
|
Generate a MAC for using in registering users with Dendrite.
|
||||||
|
"""
|
||||||
|
# From: https://github.com/matrix-org/dendrite/blob/master/clientapi/routing/register.go
|
||||||
|
mac = hmac.new(
|
||||||
|
key=shared_secret.encode('utf8'),
|
||||||
|
digestmod=hashlib.sha1,
|
||||||
|
)
|
||||||
|
|
||||||
|
mac.update(username.encode('utf8'))
|
||||||
|
mac.update(b"\x00")
|
||||||
|
mac.update(password.encode('utf8'))
|
||||||
|
mac.update(b"\x00")
|
||||||
|
mac.update(b"admin" if admin else b"notadmin")
|
||||||
|
return mac.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def register_dendrite_user(username: str) -> Dict:
|
||||||
|
"""
|
||||||
|
Shared secret registration for Dendrite.
|
||||||
|
|
||||||
|
Note uses the legacy route, see
|
||||||
|
https://github.com/matrix-org/dendrite/issues/1669
|
||||||
|
|
||||||
|
Currently compatible with Django apps only.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{
|
||||||
|
'user_id': '@username:domain.tld',
|
||||||
|
'access_token': 'randomaccesstoken',
|
||||||
|
'home_server': 'domain.tld',
|
||||||
|
'device_id': 'randomdevice'
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
matrix_config_func = get_function_from_config("matrix_config_function")
|
||||||
|
except AttributeError:
|
||||||
|
raise AttributeError("Not configured for Matrix support")
|
||||||
|
matrix_config = matrix_config_func()
|
||||||
|
|
||||||
|
password = str(uuid.uuid4())
|
||||||
|
mac = generate_dendrite_mac(
|
||||||
|
matrix_config["registration_shared_secret"],
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Register using shared secret
|
||||||
|
response = requests.post(
|
||||||
|
f"{matrix_config['homeserver_base_url']}/_matrix/client/api/v1/register?kind=user",
|
||||||
|
json={
|
||||||
|
"type": "org.matrix.login.shared_secret",
|
||||||
|
"mac": mac,
|
||||||
|
"password": password,
|
||||||
|
"user": username,
|
||||||
|
"admin": False,
|
||||||
|
},
|
||||||
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
Ładowanie…
Reference in New Issue