def set(self, token, request, *args, **kwargs):
if hasattr(request, '<STR_LIT:user>') and request.user:<EOL><INDENT>user = request.user<EOL><DEDENT>elif self.current_user:<EOL><INDENT>user = self.current_user()<EOL><DEDENT>client = request.client<EOL>tokens = self.query.filter_by(<EOL>client_id=client.client_id,<EOL>user_id=user.id).all()<EOL>if tokens:<EOL><INDENT>for tk in tokens:<EOL><INDENT>self.session.delete(tk)<EOL><DEDENT>self.session.commit()<EOL><DEDENT>expires_in = token.get('<STR_LIT>')<EOL>expires = datetime.utcnow() + timedelta(seconds=expires_in)<EOL>tok = self.model(**token)<EOL>tok.expires = expires<EOL>tok.client_id = client.client_id<EOL>tok.user_id = user.id<EOL>self.session.add(tok)<EOL>self.session.commit()<EOL>return tok<EOL>
Creates a Token object and removes all expired tokens that belong
        to the user

        :param token: token object
        :param request: OAuthlib request object