From 24e5b7b6bd071eb23f354fda3a9ce41c85768cf1 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Fri, 21 Sep 2018 12:22:37 -0500 Subject: [PATCH 1/3] Using cache to keep the user token, and implementing logout method --- rocketc/api_rocket_chat.py | 11 +++++++++ rocketc/rocketc.py | 42 +++++++++++++++++++++++++++++--- rocketc/static/html/rocketc.html | 7 +++--- rocketc/static/js/src/rocketc.js | 16 ++++++++++++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py index d887e109..37d34c59 100644 --- a/rocketc/api_rocket_chat.py +++ b/rocketc/api_rocket_chat.py @@ -256,3 +256,14 @@ def set_custom_fields(self, room_id, custom_fields): response = self._request_rocket_chat(method, url_path, data) LOG.info("Method set_custom_fields: %s with this data: %s", response, data) return response + + def logout_user(self, user_id, login_token): + """ + This method allows to logout an user + """ + url_path = "logout" + headers = {"X-Auth-Token": login_token, "X-User-Id": user_id} + url = "/".join([self.server_url, self.API_PATH, url_path]) + response = requests.get(url=url, headers=headers) + LOG.info("Method logout User: %s ", response) + return response diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 7d94954b..ac0a4872 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -5,6 +5,7 @@ import json import re import pkg_resources +import hashlib from api_teams import ApiTeams # pylint: disable=relative-import from api_rocket_chat import ApiRocketChat # pylint: disable=relative-import @@ -12,6 +13,8 @@ from django.conf import settings from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ +from django.core.cache import cache +from webob.response import Response from xblock.core import XBlock from xblock.fields import Scope, String, Boolean, DateTime, Integer, Float @@ -23,6 +26,8 @@ LOADER = ResourceLoader(__name__) LOG = logging.getLogger(__name__) +ROCKET_CHAT_DATA = "rocket_chat_data" +CACHE_TIMEOUT = 86400 @XBlock.wants("user") # pylint: disable=too-many-ancestors, too-many-instance-attributes @@ -168,7 +173,8 @@ def student_view(self, context=None): "user_data": self.user_data, "ui_is_block": self.ui_is_block, "team_view": self.team_view, - "public_url_service": self.server_data["public_url_service"] + "public_url_service": self.server_data["public_url_service"], + "key": hashlib.sha1("{}_{}".format(ROCKET_CHAT_DATA, self.user_data["username"])).hexdigest() } frag = Fragment(LOADER.render_template( @@ -353,9 +359,13 @@ def login(self, user_data): rocket_chat_user = api.search_rocket_chat_user(user_data["username"]) LOG.info("Login method: result search user: %s", rocket_chat_user["success"]) - if rocket_chat_user['success']: - data = api.create_token(user_data["username"]) + key = hashlib.sha1("{}_{}".format(ROCKET_CHAT_DATA, user_data["username"])).hexdigest() + data = cache.get(key) + if data: + return data + elif rocket_chat_user['success']: + data = api.create_token(user_data["username"]) else: response = api.create_user(user_data["anonymous_student_id"], user_data[ "email"], user_data["username"]) @@ -364,7 +374,7 @@ def login(self, user_data): data = api.create_token(user_data["username"]) LOG.info("Login method: result create token: %s", data) - + cache.set(key, data, CACHE_TIMEOUT) return data def _add_user_to_course_group(self, group_name, user_id): @@ -730,3 +740,27 @@ def _grading_discussions(self, graded_group): def max_score(self): return self.weight + + @XBlock.handler + def logout_user(self, request=None, suffix=None): + """ + This method allows to invalidate the user token + """ + # pylint: disable=unused-argument + key = request.GET.get("beacon_rc") + data = cache.get(key) + if data: + api = self._api_rocket_chat() + user_data = data.get("data") + login_token = user_data.get("authToken") + user_id = user_data.get("userId") + response = api.logout_user(user_id, login_token) + try: + response = response.json() + if response.get("status") == "success": + cache.delete(key) + return Response(status=202) + except AttributeError: + return Response(status=503) + + return Response(status=404) diff --git a/rocketc/static/html/rocketc.html b/rocketc/static/html/rocketc.html index 6e6b7144..742b7165 100644 --- a/rocketc/static/html/rocketc.html +++ b/rocketc/static/html/rocketc.html @@ -5,9 +5,8 @@ {% if ui_is_block and team_view %} {% if "authToken" in response %} -
- - +
+
@@ -59,7 +58,7 @@ {% else %} {% if "authToken" in response %} -
+
{% else %} diff --git a/rocketc/static/js/src/rocketc.js b/rocketc/static/js/src/rocketc.js index 14aec0f7..d23db277 100644 --- a/rocketc/static/js/src/rocketc.js +++ b/rocketc/static/js/src/rocketc.js @@ -3,6 +3,22 @@ function RocketChatXBlock(runtime, element) { $(function ($) { /* Here's where you'd do things on page load. */ + $( document ).ajaxStop(function() { + var beacon_rc = localStorage.getItem("beacon_rc"); + var beacon = $(".rocketc_block .embed-container").attr("data-beacon-rc"); + if (beacon_rc != null && beacon_rc != beacon) { + var logoutUser= runtime.handlerUrl(element, "logout_user"); + var data = {"key": beacon_rc}; + $.ajax({ + type: "GET", + url: logoutUser, + data: {beacon_rc}, + }); + localStorage.setItem("beacon_rc", beacon); + } else { + localStorage.setItem("beacon_rc", beacon); + } + }); }); var setDefaultChannel = runtime.handlerUrl(element, "set_default_channel"); From a55d112c994e377c8a187e8606ee104db50865d0 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 24 Sep 2018 14:48:55 -0500 Subject: [PATCH 2/3] Changing join by format --- rocketc/api_rocket_chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py index 37d34c59..a168d794 100644 --- a/rocketc/api_rocket_chat.py +++ b/rocketc/api_rocket_chat.py @@ -263,7 +263,7 @@ def logout_user(self, user_id, login_token): """ url_path = "logout" headers = {"X-Auth-Token": login_token, "X-User-Id": user_id} - url = "/".join([self.server_url, self.API_PATH, url_path]) + url = "{}/{}/{}".format(self.server_url, self.API_PATH, url_path) response = requests.get(url=url, headers=headers) LOG.info("Method logout User: %s ", response) return response From 76fda60f1b10b6b5da6034beb1b5b2281415cc4f Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 24 Sep 2018 16:08:38 -0500 Subject: [PATCH 3/3] =?UTF-8?q?Bump=20version:=200.2.22=20=E2=86=92=200.2.?= =?UTF-8?q?23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rocketc/rocketc.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index ac0a4872..230d699f 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -277,7 +277,7 @@ def user_data(self): user_data["email"] = user.emails[0] user_data["role"] = runtime.get_user_role() user_data["course_id"] = runtime.course_id - user_data["course"] = re.sub('[^A-Za-z0-9]+', '', runtime.course_id._to_string()) # pylint: disable=protected-access + user_data["course"] = re.sub('[^A-Za-z0-9]+', '', runtime.course_id.to_deprecated_string()) # pylint: disable=protected-access user_data["username"] = user.opt_attrs['edx-platform.username'] user_data["anonymous_student_id"] = runtime.anonymous_student_id return user_data diff --git a/setup.cfg b/setup.cfg index 96ce0d93..b30e6b6d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.2.22 +current_version = 0.2.23 commit = True tag = True diff --git a/setup.py b/setup.py index b5acad52..e4332b5a 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup -__version__ = '0.2.22' +__version__ = '0.2.23' def package_data(pkg, roots):