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.
|
||||
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.url = url
|
||||
self.hs_token = hs_token
|
||||
self.namespaces = self._check_namespaces(namespaces)
|
||||
self.txn_id = None
|
||||
self.txn_id = txn_id
|
||||
|
||||
def _check_namespaces(self, namespaces):
|
||||
# Sanity check that it is of the form:
|
||||
|
@ -30,7 +30,6 @@ class ApplicationServiceApi(SimpleHttpClient):
|
||||
|
||||
def __init__(self, hs):
|
||||
super(ApplicationServiceApi, self).__init__(hs)
|
||||
self.hs_token = "_hs_token_" # TODO extract hs token
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def query_user(self, service, user_id):
|
||||
@ -38,7 +37,7 @@ class ApplicationServiceApi(SimpleHttpClient):
|
||||
response = None
|
||||
try:
|
||||
response = yield self.get_json(uri, {
|
||||
"access_token": self.hs_token
|
||||
"access_token": service.hs_token
|
||||
})
|
||||
if response: # just an empty json object
|
||||
defer.returnValue(True)
|
||||
@ -54,7 +53,7 @@ class ApplicationServiceApi(SimpleHttpClient):
|
||||
response = None
|
||||
try:
|
||||
response = yield self.get_json(uri, {
|
||||
"access_token": self.hs_token
|
||||
"access_token": service.hs_token
|
||||
})
|
||||
if response: # just an empty json object
|
||||
defer.returnValue(True)
|
||||
@ -76,9 +75,10 @@ class ApplicationServiceApi(SimpleHttpClient):
|
||||
"events": events
|
||||
},
|
||||
{
|
||||
"access_token": self.hs_token
|
||||
"access_token": service.hs_token
|
||||
})
|
||||
if response: # just an empty json object
|
||||
# TODO: Mark txn as sent successfully
|
||||
defer.returnValue(True)
|
||||
except CodeMessageException as e:
|
||||
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.appservice import ApplicationService
|
||||
from synapse.appservice.api import ApplicationServiceApi
|
||||
import synapse.util.stringutils as stringutils
|
||||
|
||||
import logging
|
||||
|
||||
@ -53,10 +54,9 @@ class ApplicationServicesHandler(object):
|
||||
errcode=Codes.FORBIDDEN
|
||||
)
|
||||
logger.info("Updating application service info...")
|
||||
app_service.hs_token = self._generate_hs_token()
|
||||
yield self.store.update_app_service(app_service)
|
||||
|
||||
logger.info("Sending ping to %s...", app_service.url)
|
||||
yield self.appservice_api.push(app_service, "pinger")
|
||||
defer.returnValue(app_service)
|
||||
|
||||
def unregister(self, 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
|
||||
for service in services:
|
||||
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)
|
||||
|
||||
yield self.handler.register(app_service)
|
||||
hs_token = "_not_implemented_yet" # TODO: Pull this from self.hs?
|
||||
app_service = yield self.handler.register(app_service)
|
||||
hs_token = app_service.hs_token
|
||||
|
||||
defer.returnValue((200, {
|
||||
"hs_token": hs_token
|
||||
|
@ -60,6 +60,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
if service.token == token:
|
||||
service.url = None
|
||||
service.namespaces = None
|
||||
service.hs_token = None
|
||||
|
||||
def _unregister_app_service_txn(self, txn, token):
|
||||
# kill the url to prevent pushes
|
||||
@ -100,6 +101,9 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
if not service.token or not service.url:
|
||||
raise StoreError(400, "Token and url must be specified.")
|
||||
|
||||
if not service.hs_token:
|
||||
raise StoreError(500, "No HS token")
|
||||
|
||||
yield self.runInteraction(
|
||||
"update_app_service",
|
||||
self._update_app_service_txn,
|
||||
@ -126,8 +130,8 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
return False
|
||||
|
||||
txn.execute(
|
||||
"UPDATE application_services SET url=? WHERE id=?",
|
||||
(service.url, as_id,)
|
||||
"UPDATE application_services SET url=?, hs_token=? WHERE id=?",
|
||||
(service.url, service.hs_token, as_id,)
|
||||
)
|
||||
# cleanup regex
|
||||
txn.execute(
|
||||
@ -196,6 +200,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
# 'namespace': enum,
|
||||
# 'as_id': 0,
|
||||
# 'token': "something",
|
||||
# 'hs_token': "otherthing",
|
||||
# 'id': 0
|
||||
# }
|
||||
# ]
|
||||
@ -208,6 +213,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
services[as_token] = {
|
||||
"url": res["url"],
|
||||
"token": as_token,
|
||||
"hs_token": res["hs_token"],
|
||||
"namespaces": {
|
||||
ApplicationService.NS_USERS: [],
|
||||
ApplicationService.NS_ALIASES: [],
|
||||
@ -230,8 +236,9 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||
for service in services.values():
|
||||
logger.info("Found application service: %s", service)
|
||||
self.cache.services.append(ApplicationService(
|
||||
service["token"],
|
||||
service["url"],
|
||||
service["namespaces"]
|
||||
token=service["token"],
|
||||
url=service["url"],
|
||||
namespaces=service["namespaces"],
|
||||
hs_token=service["hs_token"]
|
||||
))
|
||||
|
||||
|
@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS application_services(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
url TEXT,
|
||||
token TEXT,
|
||||
hs_token TEXT,
|
||||
UNIQUE(token) ON CONFLICT ROLLBACK
|
||||
);
|
||||
|
||||
|
@ -46,10 +46,12 @@ class ApplicationServiceStoreTestCase(unittest.TestCase):
|
||||
@defer.inlineCallbacks
|
||||
def test_update_and_retrieval_of_service(self):
|
||||
url = "https://matrix.org/appservices/foobar"
|
||||
hs_token = "hstok"
|
||||
user_regex = ["@foobar_.*:matrix.org"]
|
||||
alias_regex = ["#foobar_.*:matrix.org"]
|
||||
room_regex = []
|
||||
service = ApplicationService(url=url, token=self.as_token, namespaces={
|
||||
service = ApplicationService(
|
||||
url=url, hs_token=hs_token, token=self.as_token, namespaces={
|
||||
ApplicationService.NS_USERS: user_regex,
|
||||
ApplicationService.NS_ALIASES: alias_regex,
|
||||
ApplicationService.NS_ROOMS: room_regex
|
||||
|
Loading…
Reference in New Issue
Block a user