mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Add hs_token column and generate a different token f.e application service.
This commit is contained in:
parent
a1a4960baf
commit
27091f146a
@ -34,11 +34,13 @@ class ApplicationService(object):
|
|||||||
# values.
|
# values.
|
||||||
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
||||||
|
|
||||||
def __init__(self, token, url=None, namespaces=None, txn_id=None):
|
def __init__(self, token, url=None, namespaces=None, hs_token=None,
|
||||||
|
txn_id=None):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.url = url
|
self.url = url
|
||||||
|
self.hs_token = hs_token
|
||||||
self.namespaces = self._check_namespaces(namespaces)
|
self.namespaces = self._check_namespaces(namespaces)
|
||||||
self.txn_id = None
|
self.txn_id = txn_id
|
||||||
|
|
||||||
def _check_namespaces(self, namespaces):
|
def _check_namespaces(self, namespaces):
|
||||||
# Sanity check that it is of the form:
|
# Sanity check that it is of the form:
|
||||||
|
@ -30,7 +30,6 @@ class ApplicationServiceApi(SimpleHttpClient):
|
|||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
super(ApplicationServiceApi, self).__init__(hs)
|
super(ApplicationServiceApi, self).__init__(hs)
|
||||||
self.hs_token = "_hs_token_" # TODO extract hs token
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def query_user(self, service, user_id):
|
def query_user(self, service, user_id):
|
||||||
@ -38,7 +37,7 @@ class ApplicationServiceApi(SimpleHttpClient):
|
|||||||
response = None
|
response = None
|
||||||
try:
|
try:
|
||||||
response = yield self.get_json(uri, {
|
response = yield self.get_json(uri, {
|
||||||
"access_token": self.hs_token
|
"access_token": service.hs_token
|
||||||
})
|
})
|
||||||
if response: # just an empty json object
|
if response: # just an empty json object
|
||||||
defer.returnValue(True)
|
defer.returnValue(True)
|
||||||
@ -54,7 +53,7 @@ class ApplicationServiceApi(SimpleHttpClient):
|
|||||||
response = None
|
response = None
|
||||||
try:
|
try:
|
||||||
response = yield self.get_json(uri, {
|
response = yield self.get_json(uri, {
|
||||||
"access_token": self.hs_token
|
"access_token": service.hs_token
|
||||||
})
|
})
|
||||||
if response: # just an empty json object
|
if response: # just an empty json object
|
||||||
defer.returnValue(True)
|
defer.returnValue(True)
|
||||||
@ -76,9 +75,10 @@ class ApplicationServiceApi(SimpleHttpClient):
|
|||||||
"events": events
|
"events": events
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"access_token": self.hs_token
|
"access_token": service.hs_token
|
||||||
})
|
})
|
||||||
if response: # just an empty json object
|
if response: # just an empty json object
|
||||||
|
# TODO: Mark txn as sent successfully
|
||||||
defer.returnValue(True)
|
defer.returnValue(True)
|
||||||
except CodeMessageException as e:
|
except CodeMessageException as e:
|
||||||
logger.warning("push_bulk to %s received %s", uri, e.code)
|
logger.warning("push_bulk to %s received %s", uri, e.code)
|
||||||
|
@ -19,6 +19,7 @@ from ._base import BaseHandler
|
|||||||
from synapse.api.errors import Codes, StoreError, SynapseError
|
from synapse.api.errors import Codes, StoreError, SynapseError
|
||||||
from synapse.appservice import ApplicationService
|
from synapse.appservice import ApplicationService
|
||||||
from synapse.appservice.api import ApplicationServiceApi
|
from synapse.appservice.api import ApplicationServiceApi
|
||||||
|
import synapse.util.stringutils as stringutils
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -53,10 +54,9 @@ class ApplicationServicesHandler(object):
|
|||||||
errcode=Codes.FORBIDDEN
|
errcode=Codes.FORBIDDEN
|
||||||
)
|
)
|
||||||
logger.info("Updating application service info...")
|
logger.info("Updating application service info...")
|
||||||
|
app_service.hs_token = self._generate_hs_token()
|
||||||
yield self.store.update_app_service(app_service)
|
yield self.store.update_app_service(app_service)
|
||||||
|
defer.returnValue(app_service)
|
||||||
logger.info("Sending ping to %s...", app_service.url)
|
|
||||||
yield self.appservice_api.push(app_service, "pinger")
|
|
||||||
|
|
||||||
def unregister(self, token):
|
def unregister(self, token):
|
||||||
logger.info("Unregister as_token=%s", token)
|
logger.info("Unregister as_token=%s", token)
|
||||||
@ -136,3 +136,6 @@ class ApplicationServicesHandler(object):
|
|||||||
# Fork off pushes to these services - XXX First cut, best effort
|
# Fork off pushes to these services - XXX First cut, best effort
|
||||||
for service in services:
|
for service in services:
|
||||||
self.appservice_api.push(service, event)
|
self.appservice_api.push(service, event)
|
||||||
|
|
||||||
|
def _generate_hs_token(self):
|
||||||
|
return stringutils.random_string(18)
|
||||||
|
@ -61,8 +61,8 @@ class RegisterRestServlet(AppServiceRestServlet):
|
|||||||
|
|
||||||
app_service = ApplicationService(as_token, as_url, namespaces)
|
app_service = ApplicationService(as_token, as_url, namespaces)
|
||||||
|
|
||||||
yield self.handler.register(app_service)
|
app_service = yield self.handler.register(app_service)
|
||||||
hs_token = "_not_implemented_yet" # TODO: Pull this from self.hs?
|
hs_token = app_service.hs_token
|
||||||
|
|
||||||
defer.returnValue((200, {
|
defer.returnValue((200, {
|
||||||
"hs_token": hs_token
|
"hs_token": hs_token
|
||||||
|
@ -60,6 +60,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
if service.token == token:
|
if service.token == token:
|
||||||
service.url = None
|
service.url = None
|
||||||
service.namespaces = None
|
service.namespaces = None
|
||||||
|
service.hs_token = None
|
||||||
|
|
||||||
def _unregister_app_service_txn(self, txn, token):
|
def _unregister_app_service_txn(self, txn, token):
|
||||||
# kill the url to prevent pushes
|
# kill the url to prevent pushes
|
||||||
@ -100,6 +101,9 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
if not service.token or not service.url:
|
if not service.token or not service.url:
|
||||||
raise StoreError(400, "Token and url must be specified.")
|
raise StoreError(400, "Token and url must be specified.")
|
||||||
|
|
||||||
|
if not service.hs_token:
|
||||||
|
raise StoreError(500, "No HS token")
|
||||||
|
|
||||||
yield self.runInteraction(
|
yield self.runInteraction(
|
||||||
"update_app_service",
|
"update_app_service",
|
||||||
self._update_app_service_txn,
|
self._update_app_service_txn,
|
||||||
@ -126,8 +130,8 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
txn.execute(
|
txn.execute(
|
||||||
"UPDATE application_services SET url=? WHERE id=?",
|
"UPDATE application_services SET url=?, hs_token=? WHERE id=?",
|
||||||
(service.url, as_id,)
|
(service.url, service.hs_token, as_id,)
|
||||||
)
|
)
|
||||||
# cleanup regex
|
# cleanup regex
|
||||||
txn.execute(
|
txn.execute(
|
||||||
@ -196,6 +200,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
# 'namespace': enum,
|
# 'namespace': enum,
|
||||||
# 'as_id': 0,
|
# 'as_id': 0,
|
||||||
# 'token': "something",
|
# 'token': "something",
|
||||||
|
# 'hs_token': "otherthing",
|
||||||
# 'id': 0
|
# 'id': 0
|
||||||
# }
|
# }
|
||||||
# ]
|
# ]
|
||||||
@ -208,6 +213,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
services[as_token] = {
|
services[as_token] = {
|
||||||
"url": res["url"],
|
"url": res["url"],
|
||||||
"token": as_token,
|
"token": as_token,
|
||||||
|
"hs_token": res["hs_token"],
|
||||||
"namespaces": {
|
"namespaces": {
|
||||||
ApplicationService.NS_USERS: [],
|
ApplicationService.NS_USERS: [],
|
||||||
ApplicationService.NS_ALIASES: [],
|
ApplicationService.NS_ALIASES: [],
|
||||||
@ -230,8 +236,9 @@ class ApplicationServiceStore(SQLBaseStore):
|
|||||||
for service in services.values():
|
for service in services.values():
|
||||||
logger.info("Found application service: %s", service)
|
logger.info("Found application service: %s", service)
|
||||||
self.cache.services.append(ApplicationService(
|
self.cache.services.append(ApplicationService(
|
||||||
service["token"],
|
token=service["token"],
|
||||||
service["url"],
|
url=service["url"],
|
||||||
service["namespaces"]
|
namespaces=service["namespaces"],
|
||||||
|
hs_token=service["hs_token"]
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS application_services(
|
|||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
url TEXT,
|
url TEXT,
|
||||||
token TEXT,
|
token TEXT,
|
||||||
|
hs_token TEXT,
|
||||||
UNIQUE(token) ON CONFLICT ROLLBACK
|
UNIQUE(token) ON CONFLICT ROLLBACK
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -46,13 +46,15 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
|
|||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_update_and_retrieval_of_service(self):
|
def test_update_and_retrieval_of_service(self):
|
||||||
url = "https://matrix.org/appservices/foobar"
|
url = "https://matrix.org/appservices/foobar"
|
||||||
|
hs_token = "hstok"
|
||||||
user_regex = ["@foobar_.*:matrix.org"]
|
user_regex = ["@foobar_.*:matrix.org"]
|
||||||
alias_regex = ["#foobar_.*:matrix.org"]
|
alias_regex = ["#foobar_.*:matrix.org"]
|
||||||
room_regex = []
|
room_regex = []
|
||||||
service = ApplicationService(url=url, token=self.as_token, namespaces={
|
service = ApplicationService(
|
||||||
ApplicationService.NS_USERS: user_regex,
|
url=url, hs_token=hs_token, token=self.as_token, namespaces={
|
||||||
ApplicationService.NS_ALIASES: alias_regex,
|
ApplicationService.NS_USERS: user_regex,
|
||||||
ApplicationService.NS_ROOMS: room_regex
|
ApplicationService.NS_ALIASES: alias_regex,
|
||||||
|
ApplicationService.NS_ROOMS: room_regex
|
||||||
})
|
})
|
||||||
yield self.store.update_app_service(service)
|
yield self.store.update_app_service(service)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user