Update ModuleApi to avoid register(generate_token=True) (#5640)

* Update ModuleApi to avoid register(generate_token=True)

This is the only place this is still used, so I'm trying to kill it off.

* changelog
pull/5641/head
Richard van der Hoff 2019-07-08 14:55:34 +01:00 committed by Amber Brown
parent f9e99f9534
commit 4b1f7febc7
2 changed files with 57 additions and 8 deletions

1
changelog.d/5640.misc Normal file
View File

@ -0,0 +1 @@
Update ModuleApi to avoid register(generate_token=True).

View File

@ -12,10 +12,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
from twisted.internet import defer from twisted.internet import defer
from synapse.types import UserID from synapse.types import UserID
logger = logging.getLogger(__name__)
class ModuleApi(object): class ModuleApi(object):
"""A proxy object that gets passed to password auth providers so they """A proxy object that gets passed to password auth providers so they
@ -76,8 +80,13 @@ class ModuleApi(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def register(self, localpart, displayname=None, emails=[]): def register(self, localpart, displayname=None, emails=[]):
"""Registers a new user with given localpart and optional """Registers a new user with given localpart and optional displayname, emails.
displayname, emails.
Also returns an access token for the new user.
Deprecated: avoid this, as it generates a new device with no way to
return that device to the user. Prefer separate calls to register_user and
register_device.
Args: Args:
localpart (str): The localpart of the new user. localpart (str): The localpart of the new user.
@ -85,15 +94,54 @@ class ModuleApi(object):
emails (List[str]): Emails to bind to the new user. emails (List[str]): Emails to bind to the new user.
Returns: Returns:
Deferred: a 2-tuple of (user_id, access_token) Deferred[tuple[str, str]]: a 2-tuple of (user_id, access_token)
""" """
# Register the user logger.warning(
reg = self.hs.get_registration_handler() "Using deprecated ModuleApi.register which creates a dummy user device."
user_id, access_token = yield reg.register( )
localpart=localpart, default_display_name=displayname, bind_emails=emails user_id = yield self.register_user(localpart, displayname, emails)
_, access_token = yield self.register_device(user_id)
defer.returnValue((user_id, access_token))
@defer.inlineCallbacks
def register_user(self, localpart, displayname=None, emails=[]):
"""Registers a new user with given localpart and optional displayname, emails.
Args:
localpart (str): The localpart of the new user.
displayname (str|None): The displayname of the new user.
emails (List[str]): Emails to bind to the new user.
Returns:
Deferred[str]: user_id
"""
user_id, _ = yield self.hs.get_registration_handler().register(
localpart=localpart,
default_display_name=displayname,
bind_emails=emails,
generate_token=False,
) )
defer.returnValue((user_id, access_token)) defer.returnValue(user_id)
def register_device(self, user_id, device_id=None, initial_display_name=None):
"""Register a device for a user and generate an access token.
Args:
user_id (str): full canonical @user:id
device_id (str|None): The device ID to check, or None to generate
a new one.
initial_display_name (str|None): An optional display name for the
device.
Returns:
defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
"""
return self.hs.get_registration_handler().register_device(
user_id=user_id,
device_id=device_id,
initial_display_name=initial_display_name,
)
@defer.inlineCallbacks @defer.inlineCallbacks
def invalidate_access_token(self, access_token): def invalidate_access_token(self, access_token):