This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin API for creating new users (#3415)
- Loading branch information
Showing
8 changed files
with
569 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
Shared-Secret Registration | ||
========================== | ||
|
||
This API allows for the creation of users in an administrative and | ||
non-interactive way. This is generally used for bootstrapping a Synapse | ||
instance with administrator accounts. | ||
|
||
To authenticate yourself to the server, you will need both the shared secret | ||
(``registration_shared_secret`` in the homeserver configuration), and a | ||
one-time nonce. If the registration shared secret is not configured, this API | ||
is not enabled. | ||
|
||
To fetch the nonce, you need to request one from the API:: | ||
|
||
> GET /_matrix/client/r0/admin/register | ||
|
||
< {"nonce": "thisisanonce"} | ||
|
||
Once you have the nonce, you can make a ``POST`` to the same URL with a JSON | ||
body containing the nonce, username, password, whether they are an admin | ||
(optional, False by default), and a HMAC digest of the content. | ||
|
||
As an example:: | ||
|
||
> POST /_matrix/client/r0/admin/register | ||
> { | ||
"nonce": "thisisanonce", | ||
"username": "pepper_roni", | ||
"password": "pizza", | ||
"admin": true, | ||
"mac": "mac_digest_here" | ||
} | ||
|
||
< { | ||
"access_token": "token_here", | ||
"user_id": "@pepper_roni@test", | ||
"home_server": "test", | ||
"device_id": "device_id_here" | ||
} | ||
|
||
The MAC is the hex digest output of the HMAC-SHA1 algorithm, with the key being | ||
the shared secret and the content being the nonce, user, password, and either | ||
the string "admin" or "notadmin", each separated by NULs. For an example of | ||
generation in Python:: | ||
|
||
import hmac, hashlib | ||
|
||
def generate_mac(nonce, user, password, admin=False): | ||
|
||
mac = hmac.new( | ||
key=shared_secret, | ||
digestmod=hashlib.sha1, | ||
) | ||
|
||
mac.update(nonce.encode('utf8')) | ||
mac.update(b"\x00") | ||
mac.update(user.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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2018 New Vector Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Injectable secrets module for Synapse. | ||
See https://docs.python.org/3/library/secrets.html#module-secrets for the API | ||
used in Python 3.6, and the API emulated in Python 2.7. | ||
""" | ||
|
||
import six | ||
|
||
if six.PY3: | ||
import secrets | ||
|
||
def Secrets(): | ||
return secrets | ||
|
||
|
||
else: | ||
|
||
import os | ||
import binascii | ||
|
||
class Secrets(object): | ||
def token_bytes(self, nbytes=32): | ||
return os.urandom(nbytes) | ||
|
||
def token_hex(self, nbytes=32): | ||
return binascii.hexlify(self.token_bytes(nbytes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.