SEBWIN-405: Implemented detection of Moodle session identifier and server disconnection.

This commit is contained in:
Damian Büchel 2020-07-31 13:24:42 +02:00
parent 22f6e8b664
commit facc8c9442
6 changed files with 52 additions and 14 deletions

View file

@ -10,6 +10,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Timers; using System.Timers;
using CefSharp; using CefSharp;
using CefSharp.WinForms; using CefSharp.WinForms;
@ -353,8 +354,7 @@ namespace SafeExamBrowser.Browser
var task = manager.VisitAllCookiesAsync(); var task = manager.VisitAllCookiesAsync();
var cookies = task.GetAwaiter().GetResult(); var cookies = task.GetAwaiter().GetResult();
var edxLogin = cookies.FirstOrDefault(c => c.Name == "edxloggedin"); var edxLogin = cookies.FirstOrDefault(c => c.Name == "edxloggedin");
var moodleSession = cookies.FirstOrDefault(c => c.Name == "MoodleSession");
// TODO: MoodleSession
if (edxLogin != default(Cookie)) if (edxLogin != default(Cookie))
{ {
@ -363,9 +363,15 @@ namespace SafeExamBrowser.Browser
if (edxSession != default(Cookie) && !sessionCookies.Contains(edxSession.Domain)) if (edxSession != default(Cookie) && !sessionCookies.Contains(edxSession.Domain))
{ {
sessionCookies.Add(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) catch (Exception e)
{ {

View file

@ -354,7 +354,6 @@ namespace SafeExamBrowser.Client
while (!response.Success) while (!response.Success)
{ {
logger.Error($"Failed to communicate session identifier with server! {response.Message}"); 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); Thread.Sleep(Settings.Server.RequestAttemptInterval);
response = Server.SendSessionIdentifier(identifier); response = Server.SendSessionIdentifier(identifier);
} }

View file

@ -241,7 +241,7 @@ namespace SafeExamBrowser.Client
private IOperation BuildServerOperation() 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); var operation = new ServerOperation(actionCenter, context, logger, server, taskbar);
context.Server = server; context.Server = server;

View file

@ -70,9 +70,9 @@ namespace SafeExamBrowser.Client.Operations
logger.Info("Finalizing server..."); logger.Info("Finalizing server...");
StatusChanged?.Invoke(TextKey.OperationStatus_FinalizeServer); StatusChanged?.Invoke(TextKey.OperationStatus_FinalizeServer);
// TODO: Stop sending pings and logs (or in controller?)
server.StopConnectivity();
// TODO: Stop action center and taskbar notifications // TODO: Stop action center and taskbar notifications
server.StopConnectivity();
} }
return result; return result;

View file

@ -19,12 +19,12 @@ namespace SafeExamBrowser.Server.Contracts
public interface IServerProxy public interface IServerProxy
{ {
/// <summary> /// <summary>
/// Attempts to initialize a connection to the server. /// Attempts to initialize a connection with the server.
/// </summary> /// </summary>
ServerResponse Connect(); ServerResponse Connect();
/// <summary> /// <summary>
/// TODO /// Terminates a connection with the server.
/// </summary> /// </summary>
ServerResponse Disconnect(); ServerResponse Disconnect();
@ -39,7 +39,7 @@ namespace SafeExamBrowser.Server.Contracts
ServerResponse<Uri> GetConfigurationFor(Exam exam); ServerResponse<Uri> GetConfigurationFor(Exam exam);
/// <summary> /// <summary>
/// Retrieves the information required to establish a connection with this server. /// Retrieves the information required to establish a connection with the server.
/// </summary> /// </summary>
ConnectionInfo GetConnectionInfo(); ConnectionInfo GetConnectionInfo();
@ -54,7 +54,7 @@ namespace SafeExamBrowser.Server.Contracts
void Initialize(string api, string connectionToken, string examId, string oauth2Token, ServerSettings settings); void Initialize(string api, string connectionToken, string examId, string oauth2Token, ServerSettings settings);
/// <summary> /// <summary>
/// TODO /// Sends the given user session identifier of a LMS and thus establishes a connection with the server.
/// </summary> /// </summary>
ServerResponse SendSessionIdentifier(string identifier); ServerResponse SendSessionIdentifier(string identifier);

View file

@ -80,7 +80,23 @@ namespace SafeExamBrowser.Server
public ServerResponse Disconnect() 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<IEnumerable<Exam>> GetAvailableExams() public ServerResponse<IEnumerable<Exam>> GetAvailableExams()
@ -186,7 +202,24 @@ namespace SafeExamBrowser.Server
public ServerResponse SendSessionIdentifier(string identifier) 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() public void StartConnectivity()
@ -352,7 +385,7 @@ namespace SafeExamBrowser.Server
try try
{ {
response = httpClient.SendAsync(request).GetAwaiter().GetResult(); 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) catch (TaskCanceledException)
{ {