Cheap way of soft logout (untested)

This commit is contained in:
Travis Ralston 2019-07-03 14:43:25 -06:00
parent 463b072b12
commit ec84dc5172
4 changed files with 30 additions and 5 deletions

View file

@ -519,6 +519,15 @@ class Auth(object):
if not ret: if not ret:
defer.returnValue(None) defer.returnValue(None)
if ret.get("expired", False):
logger.warn("Doing soft logout on user")
raise AuthError(
401,
"Token soft logged out",
errcode=Codes.UNKNOWN_TOKEN,
softLogout=true,
)
# we use ret.get() below because *lots* of unit tests stub out # we use ret.get() below because *lots* of unit tests stub out
# get_user_by_access_token in a way where it only returns a couple of # get_user_by_access_token in a way where it only returns a couple of
# the fields. # the fields.

View file

@ -85,7 +85,7 @@ class SynapseError(CodeMessageException):
errcode (str): Matrix error code e.g 'M_FORBIDDEN' errcode (str): Matrix error code e.g 'M_FORBIDDEN'
""" """
def __init__(self, code, msg, errcode=Codes.UNKNOWN): def __init__(self, code, msg, errcode=Codes.UNKNOWN, softLogout=False):
"""Constructs a synapse error. """Constructs a synapse error.
Args: Args:
@ -97,7 +97,7 @@ class SynapseError(CodeMessageException):
self.errcode = errcode self.errcode = errcode
def error_dict(self): def error_dict(self):
return cs_error(self.msg, self.errcode) return cs_error(self.msg, self.errcode, self.softLogout)
class ProxiedRequestError(SynapseError): class ProxiedRequestError(SynapseError):
@ -383,7 +383,7 @@ class RequestSendFailed(RuntimeError):
self.can_retry = can_retry self.can_retry = can_retry
def cs_error(msg, code=Codes.UNKNOWN, **kwargs): def cs_error(msg, code=Codes.UNKNOWN, softLogout=False, **kwargs):
""" Utility method for constructing an error response for client-server """ Utility method for constructing an error response for client-server
interactions. interactions.
@ -394,7 +394,7 @@ def cs_error(msg, code=Codes.UNKNOWN, **kwargs):
Returns: Returns:
A dict representing the error response JSON. A dict representing the error response JSON.
""" """
err = {"error": msg, "errcode": code} err = {"error": msg, "errcode": code, "soft_logout": softLogout}
for key, value in iteritems(kwargs): for key, value in iteritems(kwargs):
err[key] = value err[key] = value
return err return err

View file

@ -284,7 +284,7 @@ class RegistrationWorkerStore(SQLBaseStore):
def _query_for_auth(self, txn, token): def _query_for_auth(self, txn, token):
sql = ( sql = (
"SELECT users.name, users.is_guest, access_tokens.id as token_id," "SELECT users.name, users.is_guest, access_tokens.id as token_id,"
" access_tokens.device_id" " access_tokens.device_id, access_tokens.expired"
" FROM users" " FROM users"
" INNER JOIN access_tokens on users.name = access_tokens.user_id" " INNER JOIN access_tokens on users.name = access_tokens.user_id"
" WHERE token = ?" " WHERE token = ?"

View file

@ -0,0 +1,16 @@
/* Copyright 2019 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.
*/
ALTER TABLE access_tokens ADD expired SMALLINT DEFAULT 0 NOT NULL;