# Copyright 2018-2022 The Matrix.org Foundation C.I.C.
#
# 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.
import hashlib
import hmac
import os
import urllib.parse
from binascii import unhexlify
from typing import Dict, List, Optional
from unittest.mock import AsyncMock, Mock, patch
from parameterized import parameterized, parameterized_class
from twisted.test.proto_helpers import MemoryReactor
from twisted.web.resource import Resource
import synapse.rest.admin
from synapse.api.constants import ApprovalNoticeMedium, LoginType, UserTypes
from synapse.api.errors import Codes, HttpResponseException, ResourceLimitError
from synapse.api.room_versions import RoomVersions
from synapse.media.filepath import MediaFilePaths
from synapse.rest.client import (
devices,
login,
logout,
profile,
register,
room,
sync,
user_directory,
)
from synapse.server import HomeServer
from synapse.storage.databases.main.client_ips import LAST_SEEN_GRANULARITY
from synapse.types import JsonDict, UserID, create_requester
from synapse.util import Clock
from tests import unittest
from tests.test_utils import SMALL_PNG
from tests.unittest import override_config
class UserRegisterTestCase(unittest.HomeserverTestCase):
servlets = [
synapse.rest.admin.register_servlets_for_client_rest_resource,
profile.register_servlets,
]
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
self.url = "/_synapse/admin/v1/register"
self.registration_handler = Mock()
self.identity_handler = Mock()
self.login_handler = Mock()
self.device_handler = Mock()
self.device_handler.check_device_registered = Mock(return_value="FAKE")
self.datastore = Mock(return_value=Mock())
self.datastore.get_current_state_deltas = Mock(return_value=(0, []))
self.hs = self.setup_test_homeserver()
self.hs.config.registration.registration_shared_secret = "shared"
self.hs.get_media_repository = Mock() # type: ignore[method-assign]
self.hs.get_deactivate_account_handler = Mock() # type: ignore[method-assign]
return self.hs
def test_disabled(self) -> None:
"""
If there is no shared secret, registration through this method will be
prevented.
"""
self.hs.config.registration.registration_shared_secret = None
channel = self.make_request("POST", self.url, b"{}")
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(
"Shared secret registration is not enabled", channel.json_body["error"]
)
def test_get_nonce(self) -> None:
"""
Calling GET on the endpoint will return a randomised nonce, using the
homeserver's secrets provider.
"""
with patch("secrets.token_hex") as token_hex:
# Patch secrets.token_hex for the duration of this context
token_hex.return_value = "abcd"
channel = self.make_request("GET", self.url)
self.assertEqual(channel.json_body, {"nonce": "abcd"})
def test_expired_nonce(self) -> None:
"""
Calling GET on the endpoint will return a randomised nonce, which will
only last for SALT_TIMEOUT (60s).
"""
channel = self.make_request("GET", self.url)
nonce = channel.json_body["nonce"]
# 59 seconds
self.reactor.advance(59)
body = {"nonce": nonce}
channel = self.make_request("POST", self.url, body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("username must be specified", channel.json_body["error"])
# 61 seconds
self.reactor.advance(2)
channel = self.make_request("POST", self.url, body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("unrecognised nonce", channel.json_body["error"])
def test_register_incorrect_nonce(self) -> None:
"""
Only the provided nonce can be used, as it's checked in the MAC.
"""
channel = self.make_request("GET", self.url)
nonce = channel.json_body["nonce"]
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
want_mac.update(b"notthenonce\x00bob\x00abc123\x00admin")
want_mac_str = want_mac.hexdigest()
body = {
"nonce": nonce,
"username": "bob",
"password": "abc123",
"admin": True,
"mac": want_mac_str,
}
channel = self.make_request("POST", self.url, body)
self.assertEqual(403, channel.code, msg=channel.json_body)
self.assertEqual("HMAC incorrect", channel.json_body["error"])
def test_register_correct_nonce(self) -> None:
"""
When the correct nonce is provided, and the right key is provided, the
user is registered.
"""
channel = self.make_request("GET", self.url)
nonce = channel.json_body["nonce"]
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
want_mac.update(
nonce.encode("ascii") + b"\x00bob\x00abc123\x00admin\x00support"
)
want_mac_str = want_mac.hexdigest()
body = {
"nonce": nonce,
"username": "bob",
"password": "abc123",
"admin": True,
"user_type": UserTypes.SUPPORT,
"mac": want_mac_str,
}
channel = self.make_request("POST", self.url, body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("@bob:test", channel.json_body["user_id"])
def test_nonce_reuse(self) -> None:
"""
A valid unrecognised nonce.
"""
channel = self.make_request("GET", self.url)
nonce = channel.json_body["nonce"]
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
want_mac.update(nonce.encode("ascii") + b"\x00bob\x00abc123\x00admin")
want_mac_str = want_mac.hexdigest()
body = {
"nonce": nonce,
"username": "bob",
"password": "abc123",
"admin": True,
"mac": want_mac_str,
}
channel = self.make_request("POST", self.url, body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("@bob:test", channel.json_body["user_id"])
# Now, try and reuse it
channel = self.make_request("POST", self.url, body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("unrecognised nonce", channel.json_body["error"])
def test_missing_parts(self) -> None:
"""
Synapse will complain if you don't give nonce, username, password, and
mac. Admin and user_types are optional. Additional checks are done for length
and type.
"""
def nonce() -> str:
channel = self.make_request("GET", self.url)
return channel.json_body["nonce"]
#
# Nonce check
#
# Must be an empty body present
channel = self.make_request("POST", self.url, {})
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("nonce must be specified", channel.json_body["error"])
#
# Username checks
#
# Must be present
channel = self.make_request("POST", self.url
没有合适的资源?快使用搜索试试~ 我知道了~
Synapse用 Python,Twisted 编写的 Matrix 家庭服务器 .zip
共1641个文件
py:874个
sql:305个
md:137个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 158 浏览量
2024-11-24
13:24:06
上传
评论
收藏 9.01MB ZIP 举报
温馨提示
突触 Synapse 目前在element-hq/synapse上积极维护Synapse 是一个开源Matrix家庭服务器,于 2019 年至 2023 年作为 Matrix.org 基金会的一部分开发。Matrix.org 基金会无法为 Synapse 提供维护资源,它将 继续由 Element 开发此外,您还可以选择其他 Matrix 家庭服务器。请参阅Synapse 和 Dendrite 的未来 博客文章以了解更多信息。简而言之,Matrix 是互联网通信的开放标准,支持联合、加密和 VoIP。Matrix.org 对Matrix 项目的目标进行了更详细的说明,正式规范描述了技术细节。内容Synapse 目前在 element-hq/synapse 上积极维护安装和配置将反向代理与 Synapse 结合使用升级现有的 Synapse平台依赖项安全说明测试新安装从客户端注册新用户故障排除和支持身份服务器发展安装和配置Synapse 文档介绍了如何安装 Synapse。我们建议使用 Matrix.org 的Docker 镜像或D
资源推荐
资源详情
资源评论
收起资源包目录
Synapse用 Python,Twisted 编写的 Matrix 家庭服务器 .zip (1641个子文件)
synapse_port_db.1 3KB
synctl.1 2KB
hash_password.1 2KB
register_new_matrix_user.1 2KB
synapse_review_recent_signups.1 1KB
16740.bugfix 118B
build_virtualenv 4KB
pylint.cfg 8KB
changelog 66KB
clean 26B
CODEOWNERS 115B
override-hardened.conf 2KB
postgres.supervisord.conf 561B
log.config 3KB
matrix-synapse-py3.config 487B
control 1KB
copyright 4KB
.coveragerc 110B
cpanfile 373B
ca.crt 1KB
mail.css 2KB
version-picker.css 2KB
sso.css 2KB
style.css 1KB
table-of-contents.css 1KB
style.css 641B
remove-nav-buttons.css 252B
indent-section-headers.css 161B
mail-Vector.css 124B
mail-Element.css 124B
mail-expiry.css 63B
test_db.db 18.4MB
dirs 65B
16737.doc 118B
16533.doc 76B
Dockerfile 7KB
Dockerfile 3KB
editable.Dockerfile 2KB
Dockerfile-dhvirtualenv 4KB
Dockerfile-workers 3KB
.dockerignore 199B
auth_chain_diff.dot 843B
.editorconfig 176B
16738.feature 115B
16696.feature 114B
16679.feature 79B
format 13B
spinner.gif 2KB
.git-blame-ignore-revs 1KB
.gitignore 955B
.gitignore 157B
.gitignore 74B
.gitignore 26B
.gitignore 12B
gotestfmt 792B
complement_package.gotpl 3KB
index.hbs 15KB
synapse.html 11KB
index.html 10KB
sso_footer.html 9KB
sso_auth_account_details.html 5KB
sso_error.html 3KB
notif_mail.html 3KB
notif.html 3KB
notice_expiry.html 2KB
sso_redirect_confirm.html 2KB
index.html 2KB
sso_login_idp_picker.html 1KB
recaptcha.html 1KB
room.html 1KB
_base.html 1KB
sso_new_user_consent.html 961B
sso_partial_profile.html 873B
sso_auth_bad_user.html 812B
password_reset_confirmation.html 740B
terms.html 739B
1.0.html 724B
sso_auth_confirm.html 698B
sso_account_deactivated.html 671B
sso_auth_success.html 629B
registration_token.html 536B
auth_success.html 490B
password_reset.html 431B
add_threepid.html 413B
registration.html 392B
account_renewed.html 308B
password_reset_success.html 240B
account_previously_renewed.html 240B
add_threepid_failure.html 230B
password_reset_failure.html 229B
registration_success.html 228B
add_threepid_success.html 228B
registration_failure.html 185B
success.html 154B
invalid_token.html 144B
POTFILES.in 36B
tox.ini 4KB
mypy.ini 3KB
install 56B
workers-shared-extra.yaml.j2 3KB
共 1641 条
- 1
- 2
- 3
- 4
- 5
- 6
- 17
资源评论
徐浪老师
- 粉丝: 8455
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功