From da458bcfb0e2377cf00497b93cceeab6d7cca679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Fri, 24 Feb 2023 21:33:26 +0100 Subject: [PATCH] SEBWIN-608: Refactored server proxy by extracting request implementations. --- .../ConfigurationData/DataProcessor.cs | 4 +- .../IServerProxy.cs | 20 +- SafeExamBrowser.Server/Data/AttributeType.cs | 17 + SafeExamBrowser.Server/Data/Attributes.cs | 2 +- SafeExamBrowser.Server/Parser.cs | 18 +- SafeExamBrowser.Server/Requests/ApiRequest.cs | 42 ++ .../Requests/AppSignatureKeyRequest.cs | 37 ++ .../Requests/AvailableExamsRequest.cs | 63 +++ .../Requests/BaseRequest.cs | 190 ++++++++ .../Requests/ConfirmLockScreenRequest.cs | 45 ++ .../Requests/ContentType.cs | 16 + .../Requests/DisconnectionRequest.cs | 37 ++ .../Requests/ExamConfigurationRequest.cs | 39 ++ SafeExamBrowser.Server/Requests/Header.cs | 17 + .../Requests/LockScreenRequest.cs | 46 ++ SafeExamBrowser.Server/Requests/LogRequest.cs | 42 ++ .../Requests/LowerHandRequest.cs | 45 ++ .../Requests/NetworkAdapterRequest.cs | 49 ++ .../Requests/OAuth2TokenRequest.cs | 32 ++ .../Requests/PingRequest.cs | 45 ++ .../Requests/PowerSupplyRequest.cs | 45 ++ .../Requests/RaiseHandRequest.cs | 46 ++ .../Requests/SelectExamRequest.cs | 40 ++ .../Requests/SessionIdentifierRequest.cs | 37 ++ .../SafeExamBrowser.Server.csproj | 20 + SafeExamBrowser.Server/ServerProxy.cs | 455 +++++------------- 26 files changed, 1085 insertions(+), 364 deletions(-) create mode 100644 SafeExamBrowser.Server/Data/AttributeType.cs create mode 100644 SafeExamBrowser.Server/Requests/ApiRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/BaseRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/ContentType.cs create mode 100644 SafeExamBrowser.Server/Requests/DisconnectionRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/Header.cs create mode 100644 SafeExamBrowser.Server/Requests/LockScreenRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/LogRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/LowerHandRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/PingRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/RaiseHandRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/SelectExamRequest.cs create mode 100644 SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs index c05a7b2b..6a91ef03 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataProcessor.cs @@ -20,14 +20,14 @@ namespace SafeExamBrowser.Configuration.ConfigurationData { internal void Process(IDictionary rawData, AppSettings settings) { - AllowBrowserToolbarForReloading(rawData, settings); + AllowBrowserToolbarForReloading(settings); CalculateConfigurationKey(rawData, settings); HandleBrowserHomeFunctionality(settings); InitializeProctoringSettings(settings); RemoveLegacyBrowsers(settings); } - private void AllowBrowserToolbarForReloading(IDictionary rawData, AppSettings settings) + private void AllowBrowserToolbarForReloading(AppSettings settings) { if (settings.Browser.AdditionalWindow.AllowReloading && settings.Browser.AdditionalWindow.ShowReloadButton) { diff --git a/SafeExamBrowser.Server.Contracts/IServerProxy.cs b/SafeExamBrowser.Server.Contracts/IServerProxy.cs index c4959337..bc951af3 100644 --- a/SafeExamBrowser.Server.Contracts/IServerProxy.cs +++ b/SafeExamBrowser.Server.Contracts/IServerProxy.cs @@ -49,16 +49,16 @@ namespace SafeExamBrowser.Server.Contracts /// event TerminationRequestedEventHandler TerminationRequested; - /// - /// Attempts to initialize a connection with the server. - /// - ServerResponse Connect(); - /// /// Sends a lock screen confirm notification to the server. /// ServerResponse ConfirmLockScreen(); + /// + /// Attempts to initialize a connection with the server. + /// + ServerResponse Connect(); + /// /// Terminates a connection with the server. /// @@ -99,6 +99,11 @@ namespace SafeExamBrowser.Server.Contracts /// ServerResponse LowerHand(); + /// + /// Sends a raise hand notification to the server. + /// + ServerResponse RaiseHand(string message = default); + /// /// Sends the selected exam to the server. /// @@ -118,10 +123,5 @@ namespace SafeExamBrowser.Server.Contracts /// Stops sending ping and log data to the server. /// void StopConnectivity(); - - /// - /// Sends a raise hand notification to the server. - /// - ServerResponse RaiseHand(string message = default); } } diff --git a/SafeExamBrowser.Server/Data/AttributeType.cs b/SafeExamBrowser.Server/Data/AttributeType.cs new file mode 100644 index 00000000..2cd98202 --- /dev/null +++ b/SafeExamBrowser.Server/Data/AttributeType.cs @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Server.Data +{ + internal enum AttributeType + { + None, + Hand, + LockScreen + } +} diff --git a/SafeExamBrowser.Server/Data/Attributes.cs b/SafeExamBrowser.Server/Data/Attributes.cs index 009a25f6..1c1d6e12 100644 --- a/SafeExamBrowser.Server/Data/Attributes.cs +++ b/SafeExamBrowser.Server/Data/Attributes.cs @@ -18,7 +18,7 @@ namespace SafeExamBrowser.Server.Data internal string Message { get; set; } internal bool ReceiveAudio { get; set; } internal bool ReceiveVideo { get; set; } - internal string Type { get; set; } + internal AttributeType Type { get; set; } internal Attributes() { diff --git a/SafeExamBrowser.Server/Parser.cs b/SafeExamBrowser.Server/Parser.cs index 67a6d7f4..c2575c62 100644 --- a/SafeExamBrowser.Server/Parser.cs +++ b/SafeExamBrowser.Server/Parser.cs @@ -151,9 +151,9 @@ namespace SafeExamBrowser.Server return connectionToken != default; } - internal bool TryParseExams(HttpContent content, out IList exams) + internal bool TryParseExams(HttpContent content, out IEnumerable exams) { - exams = new List(); + var list = new List(); try { @@ -161,7 +161,7 @@ namespace SafeExamBrowser.Server foreach (var exam in json.AsJEnumerable()) { - exams.Add(new Exam + list.Add(new Exam { Id = exam["examId"].Value(), LmsName = exam["lmsType"].Value(), @@ -175,6 +175,8 @@ namespace SafeExamBrowser.Server logger.Error("Failed to parse exams!", e); } + exams = list; + return exams.Any(); } @@ -271,7 +273,15 @@ namespace SafeExamBrowser.Server if (attributesJson.ContainsKey("type")) { - attributes.Type = attributesJson["type"].Value(); + switch (attributesJson["type"].Value()) + { + case "lockscreen": + attributes.Type = AttributeType.LockScreen; + break; + case "raisehand": + attributes.Type = AttributeType.Hand; + break; + } } } diff --git a/SafeExamBrowser.Server/Requests/ApiRequest.cs b/SafeExamBrowser.Server/Requests/ApiRequest.cs new file mode 100644 index 00000000..c5202888 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/ApiRequest.cs @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class ApiRequest : BaseRequest + { + internal ApiRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(out ApiVersion1 api, out string message) + { + var success = TryExecute(HttpMethod.Get, settings.ApiUrl, out var response); + + api = new ApiVersion1(); + message = response.ToLogString(); + + if (success) + { + parser.TryParseApi(response.Content, out api); + } + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs b/SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs new file mode 100644 index 00000000..773070e5 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class AppSignatureKeyRequest : BaseRequest + { + internal AppSignatureKeyRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(out string message) + { + var content = $"seb_signature_key={"WINDOWS-TEST-ASK-1234"}"; + var success = TryExecute(new HttpMethod("PATCH"), api.HandshakeEndpoint, out var response, content, ContentType.URL_ENCODED, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs b/SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs new file mode 100644 index 00000000..af36962d --- /dev/null +++ b/SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Collections.Generic; +using System.Net.Http; +using SafeExamBrowser.Configuration.Contracts; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Contracts.Data; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; +using SafeExamBrowser.SystemComponents.Contracts; + +namespace SafeExamBrowser.Server.Requests +{ + internal class AvailableExamsRequest : BaseRequest + { + private readonly AppConfig appConfig; + private readonly ISystemInfo systemInfo; + private readonly IUserInfo userInfo; + + internal AvailableExamsRequest( + ApiVersion1 api, + AppConfig appConfig, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings, + ISystemInfo systemInfo, + IUserInfo userInfo) : base(api, httpClient, logger, parser, settings) + { + this.appConfig = appConfig; + this.systemInfo = systemInfo; + this.userInfo = userInfo; + } + + internal bool TryExecute(string examId, out IEnumerable exams, out string message) + { + var clientInfo = $"client_id={userInfo.GetUserName()}&seb_machine_name={systemInfo.Name}"; + var versionInfo = $"seb_os_name={systemInfo.OperatingSystemInfo}&seb_version={appConfig.ProgramInformationalVersion}"; + var content = $"institutionId={settings.Institution}&{clientInfo}&{versionInfo}{(examId == default ? "" : $"&examId={examId}")}"; + + var success = TryExecute(HttpMethod.Post, api.HandshakeEndpoint, out var response, content, ContentType.URL_ENCODED, Authorization); + + exams = default; + message = response.ToLogString(); + + if (success) + { + var hasExams = parser.TryParseExams(response.Content, out exams); + var hasToken = TryRetrieveConnectionToken(response); + + success = hasExams && hasToken; + } + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/BaseRequest.cs b/SafeExamBrowser.Server/Requests/BaseRequest.cs new file mode 100644 index 00000000..1aea66b6 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/BaseRequest.cs @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal abstract class BaseRequest + { + private static string connectionToken; + private static string oauth2Token; + + private readonly HttpClient httpClient; + + protected readonly ApiVersion1 api; + protected readonly ILogger logger; + protected readonly Parser parser; + protected readonly ServerSettings settings; + + protected (string, string) Authorization => (Header.AUTHORIZATION, $"Bearer {oauth2Token}"); + protected (string, string) Token => (Header.CONNECTION_TOKEN, connectionToken); + + internal static string ConnectionToken + { + get { return connectionToken; } + set { connectionToken = value; } + } + + internal static string Oauth2Token + { + get { return oauth2Token; } + set { oauth2Token = value; } + } + + protected BaseRequest(ApiVersion1 api, HttpClient httpClient, ILogger logger, Parser parser, ServerSettings settings) + { + this.api = api; + this.httpClient = httpClient; + this.logger = logger; + this.parser = parser; + this.settings = settings; + } + + protected bool TryExecute( + HttpMethod method, + string url, + out HttpResponseMessage response, + string content = default, + string contentType = default, + params (string name, string value)[] headers) + { + response = default; + + for (var attempt = 0; attempt < settings.RequestAttempts && (response == default || !response.IsSuccessStatusCode); attempt++) + { + var request = BuildRequest(method, url, content, contentType, headers); + + try + { + response = httpClient.SendAsync(request).GetAwaiter().GetResult(); + + if (request.RequestUri.AbsolutePath != api.LogEndpoint && request.RequestUri.AbsolutePath != api.PingEndpoint) + { + logger.Debug($"Completed request: {request.Method} '{request.RequestUri}' -> {response.ToLogString()}"); + } + + if (response.StatusCode == HttpStatusCode.Unauthorized && parser.IsTokenExpired(response.Content)) + { + logger.Info("OAuth2 token has expired, attempting to retrieve new one..."); + + if (TryRetrieveOAuth2Token(out var message)) + { + headers = UpdateOAuth2Token(headers); + } + } + } + catch (TaskCanceledException) + { + logger.Debug($"Request {request.Method} '{request.RequestUri}' did not complete within {settings.RequestTimeout}ms!"); + break; + } + catch (Exception e) + { + logger.Debug($"Request {request.Method} '{request.RequestUri}' failed due to {e}"); + } + } + + return response != default && response.IsSuccessStatusCode; + } + + protected bool TryRetrieveConnectionToken(HttpResponseMessage response) + { + var success = parser.TryParseConnectionToken(response, out connectionToken); + + if (success) + { + logger.Info("Successfully retrieved connection token."); + } + else + { + logger.Error("Failed to retrieve connection token!"); + } + + return success; + } + + protected bool TryRetrieveOAuth2Token(out string message) + { + var secret = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{settings.ClientName}:{settings.ClientSecret}")); + var authorization = (Header.AUTHORIZATION, $"Basic {secret}"); + var content = "grant_type=client_credentials&scope=read write"; + var success = TryExecute(HttpMethod.Post, api.AccessTokenEndpoint, out var response, content, ContentType.URL_ENCODED, authorization); + + message = response.ToLogString(); + + if (success && parser.TryParseOauth2Token(response.Content, out oauth2Token)) + { + logger.Info("Successfully retrieved OAuth2 token."); + } + else + { + logger.Error("Failed to retrieve OAuth2 token!"); + } + + return success; + } + + private HttpRequestMessage BuildRequest( + HttpMethod method, + string url, + string content = default, + string contentType = default, + params (string name, string value)[] headers) + { + var request = new HttpRequestMessage(method, url); + + if (content != default) + { + request.Content = new StringContent(content, Encoding.UTF8); + + if (contentType != default) + { + request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); + } + } + + request.Headers.Add(Header.ACCEPT, "application/json, */*"); + + foreach (var (name, value) in headers) + { + request.Headers.Add(name, value); + } + + return request; + } + + private (string name, string value)[] UpdateOAuth2Token((string name, string value)[] headers) + { + var result = new List<(string name, string value)>(); + + foreach (var header in headers) + { + if (header.name == Header.AUTHORIZATION) + { + result.Add((Header.AUTHORIZATION, $"Bearer {oauth2Token}")); + } + else + { + result.Add(header); + } + } + + return result.ToArray(); + } + } +} diff --git a/SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs b/SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs new file mode 100644 index 00000000..35b50ff1 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class ConfirmLockScreenRequest : BaseRequest + { + internal ConfirmLockScreenRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(int lockScreenId, out string message) + { + var json = new JObject + { + ["numericValue"] = lockScreenId, + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = "NOTIFICATION_CONFIRMED" + }; + var content = json.ToString(); + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, ContentType.JSON, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/ContentType.cs b/SafeExamBrowser.Server/Requests/ContentType.cs new file mode 100644 index 00000000..6fe9d874 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/ContentType.cs @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Server.Requests +{ + internal static class ContentType + { + internal const string JSON = "application/json;charset=UTF-8"; + internal const string URL_ENCODED = "application/x-www-form-urlencoded"; + } +} diff --git a/SafeExamBrowser.Server/Requests/DisconnectionRequest.cs b/SafeExamBrowser.Server/Requests/DisconnectionRequest.cs new file mode 100644 index 00000000..ab81d37f --- /dev/null +++ b/SafeExamBrowser.Server/Requests/DisconnectionRequest.cs @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class DisconnectionRequest : BaseRequest + { + internal DisconnectionRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(out string message) + { + var content = "delete=true"; + var success = TryExecute(HttpMethod.Delete, api.HandshakeEndpoint, out var response, content, ContentType.URL_ENCODED, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs b/SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs new file mode 100644 index 00000000..e1a91b53 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Contracts.Data; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class ExamConfigurationRequest : BaseRequest + { + internal ExamConfigurationRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(Exam exam, out HttpContent content, out string message) + { + var url = $"{api.ConfigurationEndpoint}?examId={exam.Id}"; + var success = TryExecute(HttpMethod.Get, url, out var response, default, default, Authorization, Token); + + content = response.Content; + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/Header.cs b/SafeExamBrowser.Server/Requests/Header.cs new file mode 100644 index 00000000..92af3945 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/Header.cs @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Server.Requests +{ + internal static class Header + { + internal const string ACCEPT = "Accept"; + internal const string AUTHORIZATION = "Authorization"; + internal const string CONNECTION_TOKEN = "SEBConnectionToken"; + } +} diff --git a/SafeExamBrowser.Server/Requests/LockScreenRequest.cs b/SafeExamBrowser.Server/Requests/LockScreenRequest.cs new file mode 100644 index 00000000..b99028b3 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/LockScreenRequest.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class LockScreenRequest : BaseRequest + { + internal LockScreenRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(int lockScreenId, string text, out string message) + { + var json = new JObject + { + ["numericValue"] = lockScreenId, + ["text"] = $" {text}", + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = "NOTIFICATION" + }; + var content = json.ToString(); + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, ContentType.JSON, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/LogRequest.cs b/SafeExamBrowser.Server/Requests/LogRequest.cs new file mode 100644 index 00000000..84089b07 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/LogRequest.cs @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class LogRequest : BaseRequest + { + internal LogRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(ILogMessage message) + { + var json = new JObject + { + ["text"] = message.Message, + ["timestamp"] = message.DateTime.ToUnixTimestamp(), + ["type"] = message.Severity.ToLogType() + }; + + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out _, json.ToString(), ContentType.JSON, Authorization, Token); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/LowerHandRequest.cs b/SafeExamBrowser.Server/Requests/LowerHandRequest.cs new file mode 100644 index 00000000..291aab88 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/LowerHandRequest.cs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class LowerHandRequest : BaseRequest + { + internal LowerHandRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(int handId, out string message) + { + var json = new JObject + { + ["numericValue"] = handId, + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = "NOTIFICATION_CONFIRMED" + }; + var content = json.ToString(); + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, ContentType.JSON, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs b/SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs new file mode 100644 index 00000000..c5c025e9 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Logging; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class NetworkAdapterRequest : BaseRequest + { + internal NetworkAdapterRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(string text, int? value = default) + { + var json = new JObject + { + ["text"] = text, + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = LogLevel.Info.ToLogType() + }; + + if (value != default) + { + json["numericValue"] = value.Value; + } + + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, json.ToString(), ContentType.JSON, Authorization, Token); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs b/SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs new file mode 100644 index 00000000..6c155d5e --- /dev/null +++ b/SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class OAuth2TokenRequest : BaseRequest + { + internal OAuth2TokenRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(out string message) + { + return TryRetrieveOAuth2Token(out message); + } + } +} diff --git a/SafeExamBrowser.Server/Requests/PingRequest.cs b/SafeExamBrowser.Server/Requests/PingRequest.cs new file mode 100644 index 00000000..32e827d0 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/PingRequest.cs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class PingRequest : BaseRequest + { + internal PingRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(int pingNumber, out HttpContent content, out string message, string confirmation = default) + { + var requestContent = $"timestamp={DateTime.Now.ToUnixTimestamp()}&ping-number={pingNumber}"; + + if (confirmation != default) + { + requestContent = $"{requestContent}&instruction-confirm={confirmation}"; + } + + var success = TryExecute(HttpMethod.Post, api.PingEndpoint, out var response, requestContent, ContentType.URL_ENCODED, Authorization, Token); + + content = response.Content; + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs b/SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs new file mode 100644 index 00000000..bcf4f48d --- /dev/null +++ b/SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Logging; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class PowerSupplyRequest : BaseRequest + { + internal PowerSupplyRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(string text, int value) + { + var json = new JObject + { + ["numericValue"] = value, + ["text"] = text, + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = LogLevel.Info.ToLogType() + }; + + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out _, json.ToString(), ContentType.JSON, Authorization, Token); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/RaiseHandRequest.cs b/SafeExamBrowser.Server/Requests/RaiseHandRequest.cs new file mode 100644 index 00000000..dabf11e2 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/RaiseHandRequest.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class RaiseHandRequest : BaseRequest + { + internal RaiseHandRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(int handId, string text, out string message) + { + var json = new JObject + { + ["numericValue"] = handId, + ["text"] = $" {text}", + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["type"] = "NOTIFICATION" + }; + var content = json.ToString(); + var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, ContentType.JSON, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/SelectExamRequest.cs b/SafeExamBrowser.Server/Requests/SelectExamRequest.cs new file mode 100644 index 00000000..0a14ed01 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/SelectExamRequest.cs @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Contracts.Data; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class SelectExamRequest : BaseRequest + { + internal SelectExamRequest(ApiVersion1 api, HttpClient httpClient, ILogger logger, Parser parser, ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(Exam exam, out string message, out string salt) + { + var content = $"examId={exam.Id}"; + var method = new HttpMethod("PATCH"); + var success = TryExecute(method, api.HandshakeEndpoint, out var response, content, ContentType.URL_ENCODED, Authorization, Token); + + message = response.ToLogString(); + salt = default; + + if (success) + { + parser.TryParseAppSignatureKeySalt(response, out salt); + } + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs b/SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs new file mode 100644 index 00000000..ea081a07 --- /dev/null +++ b/SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Net.Http; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Server.Data; +using SafeExamBrowser.Settings.Server; + +namespace SafeExamBrowser.Server.Requests +{ + internal class SessionIdentifierRequest : BaseRequest + { + internal SessionIdentifierRequest( + ApiVersion1 api, + HttpClient httpClient, + ILogger logger, + Parser parser, + ServerSettings settings) : base(api, httpClient, logger, parser, settings) + { + } + + internal bool TryExecute(string examId, string identifier, out string message) + { + var content = $"examId={examId}&seb_user_session_id={identifier}"; + var success = TryExecute(HttpMethod.Put, api.HandshakeEndpoint, out var response, content, ContentType.URL_ENCODED, Authorization, Token); + + message = response.ToLogString(); + + return success; + } + } +} diff --git a/SafeExamBrowser.Server/SafeExamBrowser.Server.csproj b/SafeExamBrowser.Server/SafeExamBrowser.Server.csproj index 6c520dca..e3454a05 100644 --- a/SafeExamBrowser.Server/SafeExamBrowser.Server.csproj +++ b/SafeExamBrowser.Server/SafeExamBrowser.Server.csproj @@ -60,11 +60,31 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/SafeExamBrowser.Server/ServerProxy.cs b/SafeExamBrowser.Server/ServerProxy.cs index 43921a77..2bfad729 100644 --- a/SafeExamBrowser.Server/ServerProxy.cs +++ b/SafeExamBrowser.Server/ServerProxy.cs @@ -10,21 +10,17 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; using System.Threading.Tasks; using System.Timers; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Server.Contracts; using SafeExamBrowser.Server.Contracts.Data; using SafeExamBrowser.Server.Contracts.Events; using SafeExamBrowser.Server.Data; -using SafeExamBrowser.Settings.Logging; +using SafeExamBrowser.Server.Requests; using SafeExamBrowser.Settings.Server; using SafeExamBrowser.SystemComponents.Contracts; using SafeExamBrowser.SystemComponents.Contracts.Network; @@ -49,16 +45,14 @@ namespace SafeExamBrowser.Server private readonly INetworkAdapter networkAdapter; private ApiVersion1 api; - private string connectionToken; private bool connectedToPowergrid; + private int currentHandId; private int currentLockScreenId; private int currentPowerSupplyValue; - private int currentRaisHandId; private int currentWlanValue; private string examId; private HttpClient httpClient; private int notificationId; - private string oauth2Token; private int pingNumber; private ServerSettings settings; @@ -92,15 +86,32 @@ namespace SafeExamBrowser.Server this.userInfo = userInfo; } + public ServerResponse ConfirmLockScreen() + { + var request = new ConfirmLockScreenRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(currentLockScreenId, out var message); + + if (success) + { + logger.Info($"Successfully sent notification confirmation for lock screen #{currentLockScreenId}."); + } + else + { + logger.Error($"Failed to send notification confirmation for lock screen #{currentLockScreenId}!"); + } + + return new ServerResponse(success, message); + } + public ServerResponse Connect() { - var success = TryExecute(HttpMethod.Get, settings.ApiUrl, out var response); - var message = response.ToLogString(); + var request = new ApiRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(out api, out var message); - if (success && parser.TryParseApi(response.Content, out api)) + if (success) { logger.Info("Successfully loaded server API."); - success = TryRetrieveOAuth2Token(out message); + success = new OAuth2TokenRequest(api, httpClient, logger, parser, settings).TryExecute(out message); } else { @@ -112,13 +123,8 @@ namespace SafeExamBrowser.Server public ServerResponse Disconnect() { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var content = "delete=true"; - var contentType = "application/x-www-form-urlencoded"; - var token = ("SEBConnectionToken", connectionToken); - - var success = TryExecute(HttpMethod.Delete, api.HandshakeEndpoint, out var response, content, contentType, authorization, token); - var message = response.ToLogString(); + var request = new DisconnectionRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(out var message); if (success) { @@ -134,39 +140,16 @@ namespace SafeExamBrowser.Server public ServerResponse> GetAvailableExams(string examId = default) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var clientInfo = $"client_id={userInfo.GetUserName()}&seb_machine_name={systemInfo.Name}"; - var versionInfo = $"seb_os_name={systemInfo.OperatingSystemInfo}&seb_version={appConfig.ProgramInformationalVersion}"; - var content = $"institutionId={settings.Institution}&{clientInfo}&{versionInfo}{(examId == default ? "" : $"&examId={examId}")}"; - var contentType = "application/x-www-form-urlencoded"; - var exams = default(IList); - - var success = TryExecute(HttpMethod.Post, api.HandshakeEndpoint, out var response, content, contentType, authorization); - var message = response.ToLogString(); + var request = new AvailableExamsRequest(api, appConfig, httpClient, logger, parser, settings, systemInfo, userInfo); + var success = request.TryExecute(examId, out var exams, out var message); if (success) { - var hasExams = parser.TryParseExams(response.Content, out exams); - var hasToken = parser.TryParseConnectionToken(response, out connectionToken); - - success = hasExams && hasToken; - - if (success) - { - logger.Info("Successfully retrieved connection token and available exams."); - } - else if (!hasExams) - { - logger.Error("Failed to retrieve available exams!"); - } - else if (!hasToken) - { - logger.Error("Failed to retrieve connection token!"); - } + logger.Info("Successfully retrieved available exams."); } else { - logger.Error("Failed to load connection token and available exams!"); + logger.Error("Failed to retrieve available exams!"); } return new ServerResponse>(success, exams, message); @@ -174,18 +157,15 @@ namespace SafeExamBrowser.Server public ServerResponse GetConfigurationFor(Exam exam) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var token = ("SEBConnectionToken", connectionToken); + var request = new ExamConfigurationRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(exam, out var content, out var message); var uri = default(Uri); - var success = TryExecute(HttpMethod.Get, $"{api.ConfigurationEndpoint}?examId={exam.Id}", out var response, default, default, authorization, token); - var message = response.ToLogString(); - if (success) { logger.Info("Successfully retrieved exam configuration."); - success = fileSystem.TrySaveFile(response.Content, out uri); + success = fileSystem.TrySaveFile(content, out uri); if (success) { @@ -209,8 +189,8 @@ namespace SafeExamBrowser.Server return new ConnectionInfo { Api = JsonConvert.SerializeObject(api), - ConnectionToken = connectionToken, - Oauth2Token = oauth2Token + ConnectionToken = BaseRequest.ConnectionToken, + Oauth2Token = BaseRequest.Oauth2Token }; } @@ -230,26 +210,35 @@ namespace SafeExamBrowser.Server public void Initialize(string api, string connectionToken, string examId, string oauth2Token, ServerSettings settings) { this.api = JsonConvert.DeserializeObject(api); - this.connectionToken = connectionToken; this.examId = examId; - this.oauth2Token = oauth2Token; + + BaseRequest.ConnectionToken = connectionToken; + BaseRequest.Oauth2Token = oauth2Token; Initialize(settings); } + public ServerResponse LockScreen(string text = null) + { + var request = new LockScreenRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(currentLockScreenId = ++notificationId, text, out var message); + + if (success) + { + logger.Info($"Successfully sent notification for lock screen #{currentLockScreenId}."); + } + else + { + logger.Error($"Failed to send notification for lock screen #{currentLockScreenId}!"); + } + + return new ServerResponse(success, message); + } + public ServerResponse LowerHand() { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = "NOTIFICATION_CONFIRMED", - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["numericValue"] = currentRaisHandId, - }; - var content = json.ToString(); - var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); + var request = new LowerHandRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(currentHandId, out var message); if (success) { @@ -260,33 +249,7 @@ namespace SafeExamBrowser.Server logger.Error("Failed to send lower hand notification!"); } - return new ServerResponse(success, response.ToLogString()); - } - - public ServerResponse ConfirmLockScreen() - { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = "NOTIFICATION_CONFIRMED", - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["numericValue"] = currentLockScreenId, - }; - var content = json.ToString(); - var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); - - if (success) - { - logger.Info("Successfully sent lock screen confirm notification."); - } - else - { - logger.Error("Failed to send lock screen confirm notification!"); - } - - return new ServerResponse(success, response.ToLogString()); + return new ServerResponse(success, message); } public void Notify(ILogContent content) @@ -294,20 +257,10 @@ namespace SafeExamBrowser.Server logContent.Enqueue(content); } - public ServerResponse RaiseHand(string message = null) + public ServerResponse RaiseHand(string text = null) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = "NOTIFICATION", - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["numericValue"] = currentRaisHandId = ++notificationId, - ["text"] = $" {message}" - }; - var content = json.ToString(); - var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); + var request = new RaiseHandRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(currentHandId = ++notificationId, text, out var message); if (success) { @@ -318,45 +271,13 @@ namespace SafeExamBrowser.Server logger.Error("Failed to send raise hand notification!"); } - return new ServerResponse(success, response.ToLogString()); - } - - public ServerResponse LockScreen(string message = null) - { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = "NOTIFICATION", - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["numericValue"] = currentLockScreenId = ++notificationId, - ["text"] = $" {message}" - }; - var content = json.ToString(); - var success = TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); - - if (success) - { - logger.Info("Successfully sent lock screen notification."); - } - else - { - logger.Error("Failed to send lock screen notification!"); - } - - return new ServerResponse(success, response.ToLogString()); + return new ServerResponse(success, message); } public ServerResponse SendSelectedExam(Exam exam) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var content = $"examId={exam.Id}"; - var contentType = "application/x-www-form-urlencoded"; - var token = ("SEBConnectionToken", connectionToken); - - var success = TryExecute(new HttpMethod("PATCH"), api.HandshakeEndpoint, out var response, content, contentType, authorization, token); - var message = response.ToLogString(); + var request = new SelectExamRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(exam, out var message, out var salt); if (success) { @@ -367,7 +288,7 @@ namespace SafeExamBrowser.Server logger.Error("Failed to send selected exam!"); } - if (parser.TryParseAppSignatureKeySalt(response, out var salt)) + if (success && salt != default) { logger.Info("App signature key salt detected, performing key exchange..."); success = TrySendAppSignatureKey(out message); @@ -382,13 +303,8 @@ namespace SafeExamBrowser.Server public ServerResponse SendSessionIdentifier(string identifier) { - 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 = response.ToLogString(); + var request = new SessionIdentifierRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(examId, identifier, out var message); if (success) { @@ -453,24 +369,11 @@ namespace SafeExamBrowser.Server { try { - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - while (!logContent.IsEmpty) { if (logContent.TryDequeue(out var c) && c is ILogMessage message) { - // IMPORTANT: The token needs to be read for every request, as it may get updated by another thread! - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var json = new JObject - { - ["type"] = message.Severity.ToLogType(), - ["timestamp"] = message.DateTime.ToUnixTimestamp(), - ["text"] = message.Message - }; - var content = json.ToString(); - - TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); + new LogRequest(api, httpClient, logger, parser, settings).TryExecute(message); } } } @@ -486,43 +389,16 @@ namespace SafeExamBrowser.Server { try { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var content = $"timestamp={DateTime.Now.ToUnixTimestamp()}&ping-number={++pingNumber}"; - var contentType = "application/x-www-form-urlencoded"; - var token = ("SEBConnectionToken", connectionToken); + instructionConfirmations.TryDequeue(out var confirmation); - if (instructionConfirmations.TryDequeue(out var confirmation)) - { - content = $"{content}&instruction-confirm={confirmation}"; - } - - var success = TryExecute(HttpMethod.Post, api.PingEndpoint, out var response, content, contentType, authorization, token); + var request = new PingRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(++pingNumber, out var content, out var message, confirmation); if (success) { - if (parser.TryParseInstruction(response.Content, out var attributes, out var instruction, out var instructionConfirmation)) + if (parser.TryParseInstruction(content, out var attributes, out var instruction, out var instructionConfirmation)) { - switch (instruction) - { - case Instructions.LOCK_SCREEN: - Task.Run(() => LockScreenRequested?.Invoke(attributes.Message)); - break; - case Instructions.NOTIFICATION_CONFIRM when attributes.Type == "lockscreen": - Task.Run(() => LockScreenConfirmed?.Invoke()); - break; - case Instructions.NOTIFICATION_CONFIRM when attributes.Type == "raisehand": - Task.Run(() => HandConfirmed?.Invoke()); - break; - case Instructions.PROCTORING: - Task.Run(() => ProctoringInstructionReceived?.Invoke(attributes.Instruction)); - break; - case Instructions.PROCTORING_RECONFIGURATION: - Task.Run(() => ProctoringConfigurationReceived?.Invoke(attributes.AllowChat, attributes.ReceiveAudio, attributes.ReceiveVideo)); - break; - case Instructions.QUIT: - Task.Run(() => TerminationRequested?.Invoke()); - break; - } + HandleInstruction(attributes, instruction); if (instructionConfirmation != default) { @@ -532,7 +408,7 @@ namespace SafeExamBrowser.Server } else { - logger.Error($"Failed to send ping: {response.ToLogString()}"); + logger.Error($"Failed to send ping: {message}"); } } catch (Exception e) @@ -547,23 +423,25 @@ namespace SafeExamBrowser.Server { try { - var value = Convert.ToInt32(status.BatteryCharge * 100); var connected = status.IsOnline; + var value = Convert.ToInt32(status.BatteryCharge * 100); + var text = default(string); if (value != currentPowerSupplyValue) { var chargeInfo = $"{status.BatteryChargeStatus} at {value}%"; var gridInfo = $"{(status.IsOnline ? "connected to" : "disconnected from")} the power grid"; - var text = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}"; - SendPowerSupplyStatus(text, value); + currentPowerSupplyValue = value; + text = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}"; } else if (connected != connectedToPowergrid) { - var text = $" Device has been {(connected ? "connected to" : "disconnected from")} power grid"; - SendPowerSupplyStatus(text, value); connectedToPowergrid = connected; + text = $" Device has been {(connected ? "connected to" : "disconnected from")} power grid"; } + + new PowerSupplyRequest(api, httpClient, logger, parser, settings).TryExecute(text, value); } catch (Exception e) { @@ -571,23 +449,6 @@ namespace SafeExamBrowser.Server } } - private void SendPowerSupplyStatus(string text, int value) - { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = LogLevel.Info.ToLogType(), - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["text"] = text, - ["numericValue"] = value - }; - var content = json.ToString(); - - TryExecute(HttpMethod.Post, api.LogEndpoint, out _, content, contentType, authorization, token); - } - private void NetworkAdapter_Changed() { const int NOT_CONNECTED = -1; @@ -598,23 +459,20 @@ namespace SafeExamBrowser.Server if (network?.SignalStrength != currentWlanValue) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject { ["type"] = LogLevel.Info.ToLogType(), ["timestamp"] = DateTime.Now.ToUnixTimestamp() }; + var text = default(string); + var value = default(int?); if (network != default(IWirelessNetwork)) { - json["text"] = $" {network.Name}: {network.Status}, {network.SignalStrength}%"; - json["numericValue"] = network.SignalStrength; + text = $" {network.Name}: {network.Status}, {network.SignalStrength}%"; + value = network.SignalStrength; } else { - json["text"] = " not connected"; + text = " not connected"; } - TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, json.ToString(), contentType, authorization, token); - + new NetworkAdapterRequest(api, httpClient, logger, parser, settings).TryExecute(text, value); currentWlanValue = network?.SignalStrength ?? NOT_CONNECTED; } } @@ -624,26 +482,29 @@ namespace SafeExamBrowser.Server } } - private bool TryRetrieveOAuth2Token(out string message) + private void HandleInstruction(Attributes attributes, string instruction) { - var secret = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{settings.ClientName}:{settings.ClientSecret}")); - var authorization = ("Authorization", $"Basic {secret}"); - var content = "grant_type=client_credentials&scope=read write"; - var contentType = "application/x-www-form-urlencoded"; - var success = TryExecute(HttpMethod.Post, api.AccessTokenEndpoint, out var response, content, contentType, authorization); - - message = response.ToLogString(); - - if (success && parser.TryParseOauth2Token(response.Content, out oauth2Token)) + switch (instruction) { - logger.Info("Successfully retrieved OAuth2 token."); + case Instructions.LOCK_SCREEN: + Task.Run(() => LockScreenRequested?.Invoke(attributes.Message)); + break; + case Instructions.NOTIFICATION_CONFIRM when attributes.Type == AttributeType.LockScreen: + Task.Run(() => LockScreenConfirmed?.Invoke()); + break; + case Instructions.NOTIFICATION_CONFIRM when attributes.Type == AttributeType.Hand: + Task.Run(() => HandConfirmed?.Invoke()); + break; + case Instructions.PROCTORING: + Task.Run(() => ProctoringInstructionReceived?.Invoke(attributes.Instruction)); + break; + case Instructions.PROCTORING_RECONFIGURATION: + Task.Run(() => ProctoringConfigurationReceived?.Invoke(attributes.AllowChat, attributes.ReceiveAudio, attributes.ReceiveVideo)); + break; + case Instructions.QUIT: + Task.Run(() => TerminationRequested?.Invoke()); + break; } - else - { - logger.Error("Failed to retrieve OAuth2 token!"); - } - - return success; } private bool TrySendAppSignatureKey(out string message) @@ -651,13 +512,8 @@ namespace SafeExamBrowser.Server // TODO: // keyGenerator.CalculateAppSignatureKey(configurationKey, server.AppSignatureKeySalt) - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var content = $"seb_signature_key={"WINDOWS-TEST-ASK-1234"}"; - var contentType = "application/x-www-form-urlencoded"; - var token = ("SEBConnectionToken", connectionToken); - var success = TryExecute(new HttpMethod("PATCH"), api.HandshakeEndpoint, out var response, content, contentType, authorization, token); - - message = response.ToLogString(); + var request = new AppSignatureKeyRequest(api, httpClient, logger, parser, settings); + var success = request.TryExecute(out message); if (success) { @@ -670,100 +526,5 @@ namespace SafeExamBrowser.Server return success; } - - private bool TryExecute( - HttpMethod method, - string url, - out HttpResponseMessage response, - string content = default, - string contentType = default, - params (string name, string value)[] headers) - { - response = default; - - for (var attempt = 0; attempt < settings.RequestAttempts && (response == default || !response.IsSuccessStatusCode); attempt++) - { - var request = BuildRequest(method, url, content, contentType, headers); - - try - { - response = httpClient.SendAsync(request).GetAwaiter().GetResult(); - - if (request.RequestUri.AbsolutePath != api.LogEndpoint && request.RequestUri.AbsolutePath != api.PingEndpoint) - { - logger.Debug($"Completed request: {request.Method} '{request.RequestUri}' -> {response.ToLogString()}"); - } - - if (response.StatusCode == HttpStatusCode.Unauthorized && parser.IsTokenExpired(response.Content)) - { - logger.Info("OAuth2 token has expired, attempting to retrieve new one..."); - - if (TryRetrieveOAuth2Token(out var message)) - { - headers = UpdateOAuth2Token(headers); - } - } - } - catch (TaskCanceledException) - { - logger.Debug($"Request {request.Method} '{request.RequestUri}' did not complete within {settings.RequestTimeout}ms!"); - break; - } - catch (Exception e) - { - logger.Debug($"Request {request.Method} '{request.RequestUri}' failed due to {e}"); - } - } - - return response != default && response.IsSuccessStatusCode; - } - - private HttpRequestMessage BuildRequest( - HttpMethod method, - string url, - string content = default, - string contentType = default, - params (string name, string value)[] headers) - { - var request = new HttpRequestMessage(method, url); - - if (content != default) - { - request.Content = new StringContent(content, Encoding.UTF8); - - if (contentType != default) - { - request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); - } - } - - request.Headers.Add("Accept", "application/json, */*"); - - foreach (var (name, value) in headers) - { - request.Headers.Add(name, value); - } - - return request; - } - - private (string name, string value)[] UpdateOAuth2Token((string name, string value)[] headers) - { - var result = new List<(string name, string value)>(); - - foreach (var header in headers) - { - if (header.name == "Authorization") - { - result.Add(("Authorization", $"Bearer {oauth2Token}")); - } - else - { - result.Add(header); - } - } - - return result.ToArray(); - } } }