Add appservice handler and store. Glue together rest > handler > store.

This commit is contained in:
Kegan Dougal 2015-01-27 15:50:28 +00:00
parent 6efdc11cc8
commit 51449e0665
6 changed files with 107 additions and 5 deletions

View File

@ -26,6 +26,7 @@ from .presence import PresenceHandler
from .directory import DirectoryHandler from .directory import DirectoryHandler
from .typing import TypingNotificationHandler from .typing import TypingNotificationHandler
from .admin import AdminHandler from .admin import AdminHandler
from .appservice import ApplicationServicesHandler
class Handlers(object): class Handlers(object):
@ -51,3 +52,4 @@ class Handlers(object):
self.directory_handler = DirectoryHandler(hs) self.directory_handler = DirectoryHandler(hs)
self.typing_notification_handler = TypingNotificationHandler(hs) self.typing_notification_handler = TypingNotificationHandler(hs)
self.admin_handler = AdminHandler(hs) self.admin_handler = AdminHandler(hs)
self.appservice_handler = ApplicationServicesHandler(hs)

View File

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket 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.
from twisted.internet import defer
from ._base import BaseHandler
from synapse.api.errors import StoreError, SynapseError
import logging
logger = logging.getLogger(__name__)
class ApplicationServicesHandler(BaseHandler):
def __init__(self, hs):
super(ApplicationServicesHandler, self).__init__(hs)
@defer.inlineCallbacks
def register(self, base_url, token, namespaces):
# check the token is recognised
try:
app_service = yield self.store.get_app_service(token)
if not app_service:
raise StoreError
except StoreError:
raise SynapseError(
403, "Unrecognised application services token. "
"Consult the home server admin."
)
# update AS entry with base URL
# store namespaces for this AS
defer.returnValue("not_implemented_yet")

View File

@ -45,3 +45,4 @@ class AppServiceRestServlet(RestServlet):
def __init__(self, hs): def __init__(self, hs):
self.hs = hs self.hs = hs
self.handler = hs.get_handlers().appservice_handler

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
"""This module contains REST servlets to do with registration: /register""" """This module contains REST servlets to do with registration: /register"""
from twisted.internet import defer
from base import AppServiceRestServlet, as_path_pattern from base import AppServiceRestServlet, as_path_pattern
from synapse.api.errors import CodeMessageException, SynapseError from synapse.api.errors import CodeMessageException, SynapseError
@ -30,6 +31,7 @@ class RegisterRestServlet(AppServiceRestServlet):
PATTERN = as_path_pattern("/register$") PATTERN = as_path_pattern("/register$")
@defer.inlineCallbacks
def on_POST(self, request): def on_POST(self, request):
params = _parse_json(request) params = _parse_json(request)
@ -56,9 +58,11 @@ class RegisterRestServlet(AppServiceRestServlet):
self._parse_namespace(namespaces, params["namespaces"], "rooms") self._parse_namespace(namespaces, params["namespaces"], "rooms")
self._parse_namespace(namespaces, params["namespaces"], "aliases") self._parse_namespace(namespaces, params["namespaces"], "aliases")
# TODO: pass to the appservice handler hs_token = yield self.handler.register(as_url, as_token, namespaces)
raise CodeMessageException(500, "Not implemented.") defer.returnValue({
"hs_token": hs_token
})
def _parse_namespace(self, target_ns, origin_ns, ns): def _parse_namespace(self, target_ns, origin_ns, ns):
if ns not in target_ns or ns not in origin_ns: if ns not in target_ns or ns not in origin_ns:

View File

@ -18,6 +18,7 @@ from twisted.internet import defer
from synapse.util.logutils import log_function from synapse.util.logutils import log_function
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from .appservice import ApplicationServiceStore
from .directory import DirectoryStore from .directory import DirectoryStore
from .feedback import FeedbackStore from .feedback import FeedbackStore
from .presence import PresenceStore from .presence import PresenceStore
@ -80,8 +81,8 @@ class DataStore(RoomMemberStore, RoomStore,
RegistrationStore, StreamStore, ProfileStore, FeedbackStore, RegistrationStore, StreamStore, ProfileStore, FeedbackStore,
PresenceStore, TransactionStore, PresenceStore, TransactionStore,
DirectoryStore, KeyStore, StateStore, SignatureStore, DirectoryStore, KeyStore, StateStore, SignatureStore,
EventFederationStore, EventFederationStore, MediaRepositoryStore,
MediaRepositoryStore, ApplicationServiceStore
): ):
def __init__(self, hs): def __init__(self, hs):

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket 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.
from twisted.internet import defer
from synapse.api.errors import StoreError
from ._base import SQLBaseStore
class ApplicationServiceStore(SQLBaseStore):
def __init__(self, hs):
super(ApplicationServiceStore, self).__init__(hs)
self.clock = hs.get_clock()
@defer.inlineCallbacks
def get_app_service(self, as_token):
"""Get the application service with the given token.
Args:
token (str): The application service token.
Raises:
StoreError if there was a problem retrieving this.
"""
row = self._simple_select_one(
"application_services", {"token": as_token},
["url", "token"]
)
if not row:
raise StoreError(400, "Bad application services token supplied.")
defer.returnValue(row)