From facc8c9442c34175fa6cef0851cda90d7e3b23f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Fri, 31 Jul 2020 13:24:42 +0200 Subject: [PATCH] SEBWIN-405: Implemented detection of Moodle session identifier and server disconnection. --- SafeExamBrowser.Browser/BrowserApplication.cs | 12 ++++-- SafeExamBrowser.Client/ClientController.cs | 1 - SafeExamBrowser.Client/CompositionRoot.cs | 2 +- .../Operations/ServerOperation.cs | 4 +- .../IServerProxy.cs | 8 ++-- SafeExamBrowser.Server/ServerProxy.cs | 39 +++++++++++++++++-- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/SafeExamBrowser.Browser/BrowserApplication.cs b/SafeExamBrowser.Browser/BrowserApplication.cs index cc08a4c8..b54e907a 100644 --- a/SafeExamBrowser.Browser/BrowserApplication.cs +++ b/SafeExamBrowser.Browser/BrowserApplication.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Timers; using CefSharp; using CefSharp.WinForms; @@ -353,8 +354,7 @@ namespace SafeExamBrowser.Browser var task = manager.VisitAllCookiesAsync(); var cookies = task.GetAwaiter().GetResult(); var edxLogin = cookies.FirstOrDefault(c => c.Name == "edxloggedin"); - - // TODO: MoodleSession + var moodleSession = cookies.FirstOrDefault(c => c.Name == "MoodleSession"); if (edxLogin != default(Cookie)) { @@ -363,9 +363,15 @@ namespace SafeExamBrowser.Browser if (edxSession != default(Cookie) && !sessionCookies.Contains(edxSession.Domain)) { sessionCookies.Add(edxSession.Domain); - SessionIdentifierDetected?.Invoke(edxSession.Value); + Task.Run(() => SessionIdentifierDetected?.Invoke(edxSession.Value)); } } + + if (moodleSession != default(Cookie) && !sessionCookies.Contains(moodleSession.Domain)) + { + sessionCookies.Add(moodleSession.Domain); + Task.Run(() => SessionIdentifierDetected?.Invoke(moodleSession.Value)); + } } catch (Exception e) { diff --git a/SafeExamBrowser.Client/ClientController.cs b/SafeExamBrowser.Client/ClientController.cs index 70777f35..08b61d45 100644 --- a/SafeExamBrowser.Client/ClientController.cs +++ b/SafeExamBrowser.Client/ClientController.cs @@ -354,7 +354,6 @@ namespace SafeExamBrowser.Client while (!response.Success) { logger.Error($"Failed to communicate session identifier with server! {response.Message}"); - // TODO: Check that is running in separat thread (not UI thread!!) or use different mechanism to wait! Thread.Sleep(Settings.Server.RequestAttemptInterval); response = Server.SendSessionIdentifier(identifier); } diff --git a/SafeExamBrowser.Client/CompositionRoot.cs b/SafeExamBrowser.Client/CompositionRoot.cs index 4b0f0e0f..f50a2bbc 100644 --- a/SafeExamBrowser.Client/CompositionRoot.cs +++ b/SafeExamBrowser.Client/CompositionRoot.cs @@ -241,7 +241,7 @@ namespace SafeExamBrowser.Client private IOperation BuildServerOperation() { - var server = new ServerProxy(context.AppConfig, logger); + var server = new ServerProxy(context.AppConfig, ModuleLogger(nameof(ServerProxy))); var operation = new ServerOperation(actionCenter, context, logger, server, taskbar); context.Server = server; diff --git a/SafeExamBrowser.Client/Operations/ServerOperation.cs b/SafeExamBrowser.Client/Operations/ServerOperation.cs index dd232f71..69a07108 100644 --- a/SafeExamBrowser.Client/Operations/ServerOperation.cs +++ b/SafeExamBrowser.Client/Operations/ServerOperation.cs @@ -70,9 +70,9 @@ namespace SafeExamBrowser.Client.Operations logger.Info("Finalizing server..."); StatusChanged?.Invoke(TextKey.OperationStatus_FinalizeServer); - // TODO: Stop sending pings and logs (or in controller?) - server.StopConnectivity(); // TODO: Stop action center and taskbar notifications + + server.StopConnectivity(); } return result; diff --git a/SafeExamBrowser.Server.Contracts/IServerProxy.cs b/SafeExamBrowser.Server.Contracts/IServerProxy.cs index 8fc5b4bd..6f77539e 100644 --- a/SafeExamBrowser.Server.Contracts/IServerProxy.cs +++ b/SafeExamBrowser.Server.Contracts/IServerProxy.cs @@ -19,12 +19,12 @@ namespace SafeExamBrowser.Server.Contracts public interface IServerProxy { /// - /// Attempts to initialize a connection to the server. + /// Attempts to initialize a connection with the server. /// ServerResponse Connect(); /// - /// TODO + /// Terminates a connection with the server. /// ServerResponse Disconnect(); @@ -39,7 +39,7 @@ namespace SafeExamBrowser.Server.Contracts ServerResponse GetConfigurationFor(Exam exam); /// - /// Retrieves the information required to establish a connection with this server. + /// Retrieves the information required to establish a connection with the server. /// ConnectionInfo GetConnectionInfo(); @@ -54,7 +54,7 @@ namespace SafeExamBrowser.Server.Contracts void Initialize(string api, string connectionToken, string examId, string oauth2Token, ServerSettings settings); /// - /// TODO + /// Sends the given user session identifier of a LMS and thus establishes a connection with the server. /// ServerResponse SendSessionIdentifier(string identifier); diff --git a/SafeExamBrowser.Server/ServerProxy.cs b/SafeExamBrowser.Server/ServerProxy.cs index 3418a8f8..2449a847 100644 --- a/SafeExamBrowser.Server/ServerProxy.cs +++ b/SafeExamBrowser.Server/ServerProxy.cs @@ -80,7 +80,23 @@ namespace SafeExamBrowser.Server public ServerResponse Disconnect() { - return new ServerResponse(false, "TODO!"); + var authorization = ("Authorization", $"Bearer {oauth2Token}"); + var contentType = "application/x-www-form-urlencoded"; + var token = ("SEBConnectionToken", connectionToken); + + var success = TryExecute(HttpMethod.Delete, api.HandshakeEndpoint, out var response, default(string), contentType, authorization, token); + var message = ToString(response); + + if (success) + { + logger.Info("Successfully terminated connection."); + } + else + { + logger.Error("Failed to terminate connection!"); + } + + return new ServerResponse(success, message); } public ServerResponse> GetAvailableExams() @@ -186,7 +202,24 @@ namespace SafeExamBrowser.Server public ServerResponse SendSessionIdentifier(string identifier) { - return new ServerResponse(false, "TODO!"); + var authorization = ("Authorization", $"Bearer {oauth2Token}"); + var content = $"examId={examId}&seb_user_session_id={identifier}"; + var contentType = "application/x-www-form-urlencoded"; + var token = ("SEBConnectionToken", connectionToken); + + var success = TryExecute(HttpMethod.Put, api.HandshakeEndpoint, out var response, content, contentType, authorization, token); + var message = ToString(response); + + if (success) + { + logger.Info("Successfully sent session identifier."); + } + else + { + logger.Error("Failed to send session identifier!"); + } + + return new ServerResponse(success, message); } public void StartConnectivity() @@ -352,7 +385,7 @@ namespace SafeExamBrowser.Server try { response = httpClient.SendAsync(request).GetAwaiter().GetResult(); - logger.Debug($"Request was successful: {request.Method} '{request.RequestUri}' -> {ToString(response)}"); + logger.Debug($"Completed request: {request.Method} '{request.RequestUri}' -> {ToString(response)}"); } catch (TaskCanceledException) {