SEBWIN-608: Refactored server proxy by extracting request implementations.
This commit is contained in:
parent
ae3755df84
commit
da458bcfb0
26 changed files with 1085 additions and 364 deletions
|
@ -20,14 +20,14 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
|
|||
{
|
||||
internal void Process(IDictionary<string, object> rawData, AppSettings settings)
|
||||
{
|
||||
AllowBrowserToolbarForReloading(rawData, settings);
|
||||
AllowBrowserToolbarForReloading(settings);
|
||||
CalculateConfigurationKey(rawData, settings);
|
||||
HandleBrowserHomeFunctionality(settings);
|
||||
InitializeProctoringSettings(settings);
|
||||
RemoveLegacyBrowsers(settings);
|
||||
}
|
||||
|
||||
private void AllowBrowserToolbarForReloading(IDictionary<string, object> rawData, AppSettings settings)
|
||||
private void AllowBrowserToolbarForReloading(AppSettings settings)
|
||||
{
|
||||
if (settings.Browser.AdditionalWindow.AllowReloading && settings.Browser.AdditionalWindow.ShowReloadButton)
|
||||
{
|
||||
|
|
|
@ -49,16 +49,16 @@ namespace SafeExamBrowser.Server.Contracts
|
|||
/// </summary>
|
||||
event TerminationRequestedEventHandler TerminationRequested;
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to initialize a connection with the server.
|
||||
/// </summary>
|
||||
ServerResponse Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Sends a lock screen confirm notification to the server.
|
||||
/// </summary>
|
||||
ServerResponse ConfirmLockScreen();
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to initialize a connection with the server.
|
||||
/// </summary>
|
||||
ServerResponse Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Terminates a connection with the server.
|
||||
/// </summary>
|
||||
|
@ -99,6 +99,11 @@ namespace SafeExamBrowser.Server.Contracts
|
|||
/// </summary>
|
||||
ServerResponse LowerHand();
|
||||
|
||||
/// <summary>
|
||||
/// Sends a raise hand notification to the server.
|
||||
/// </summary>
|
||||
ServerResponse RaiseHand(string message = default);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the selected exam to the server.
|
||||
/// </summary>
|
||||
|
@ -118,10 +123,5 @@ namespace SafeExamBrowser.Server.Contracts
|
|||
/// Stops sending ping and log data to the server.
|
||||
/// </summary>
|
||||
void StopConnectivity();
|
||||
|
||||
/// <summary>
|
||||
/// Sends a raise hand notification to the server.
|
||||
/// </summary>
|
||||
ServerResponse RaiseHand(string message = default);
|
||||
}
|
||||
}
|
||||
|
|
17
SafeExamBrowser.Server/Data/AttributeType.cs
Normal file
17
SafeExamBrowser.Server/Data/AttributeType.cs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
{
|
||||
|
|
|
@ -151,9 +151,9 @@ namespace SafeExamBrowser.Server
|
|||
return connectionToken != default;
|
||||
}
|
||||
|
||||
internal bool TryParseExams(HttpContent content, out IList<Exam> exams)
|
||||
internal bool TryParseExams(HttpContent content, out IEnumerable<Exam> exams)
|
||||
{
|
||||
exams = new List<Exam>();
|
||||
var list = new List<Exam>();
|
||||
|
||||
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<string>(),
|
||||
LmsName = exam["lmsType"].Value<string>(),
|
||||
|
@ -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<string>();
|
||||
switch (attributesJson["type"].Value<string>())
|
||||
{
|
||||
case "lockscreen":
|
||||
attributes.Type = AttributeType.LockScreen;
|
||||
break;
|
||||
case "raisehand":
|
||||
attributes.Type = AttributeType.Hand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
42
SafeExamBrowser.Server/Requests/ApiRequest.cs
Normal file
42
SafeExamBrowser.Server/Requests/ApiRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
37
SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs
Normal file
37
SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
63
SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs
Normal file
63
SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs
Normal file
|
@ -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<Exam> 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;
|
||||
}
|
||||
}
|
||||
}
|
190
SafeExamBrowser.Server/Requests/BaseRequest.cs
Normal file
190
SafeExamBrowser.Server/Requests/BaseRequest.cs
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
45
SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs
Normal file
45
SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
16
SafeExamBrowser.Server/Requests/ContentType.cs
Normal file
16
SafeExamBrowser.Server/Requests/ContentType.cs
Normal file
|
@ -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";
|
||||
}
|
||||
}
|
37
SafeExamBrowser.Server/Requests/DisconnectionRequest.cs
Normal file
37
SafeExamBrowser.Server/Requests/DisconnectionRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
39
SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs
Normal file
39
SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
17
SafeExamBrowser.Server/Requests/Header.cs
Normal file
17
SafeExamBrowser.Server/Requests/Header.cs
Normal file
|
@ -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";
|
||||
}
|
||||
}
|
46
SafeExamBrowser.Server/Requests/LockScreenRequest.cs
Normal file
46
SafeExamBrowser.Server/Requests/LockScreenRequest.cs
Normal file
|
@ -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"] = $"<lockscreen> {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;
|
||||
}
|
||||
}
|
||||
}
|
42
SafeExamBrowser.Server/Requests/LogRequest.cs
Normal file
42
SafeExamBrowser.Server/Requests/LogRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
45
SafeExamBrowser.Server/Requests/LowerHandRequest.cs
Normal file
45
SafeExamBrowser.Server/Requests/LowerHandRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
49
SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs
Normal file
49
SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
32
SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs
Normal file
32
SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
45
SafeExamBrowser.Server/Requests/PingRequest.cs
Normal file
45
SafeExamBrowser.Server/Requests/PingRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
45
SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs
Normal file
45
SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
46
SafeExamBrowser.Server/Requests/RaiseHandRequest.cs
Normal file
46
SafeExamBrowser.Server/Requests/RaiseHandRequest.cs
Normal file
|
@ -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"] = $"<raisehand> {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;
|
||||
}
|
||||
}
|
||||
}
|
40
SafeExamBrowser.Server/Requests/SelectExamRequest.cs
Normal file
40
SafeExamBrowser.Server/Requests/SelectExamRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
37
SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs
Normal file
37
SafeExamBrowser.Server/Requests/SessionIdentifierRequest.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -60,11 +60,31 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="Data\ApiVersion1.cs" />
|
||||
<Compile Include="Data\Attributes.cs" />
|
||||
<Compile Include="Data\AttributeType.cs" />
|
||||
<Compile Include="Data\Instructions.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="FileSystem.cs" />
|
||||
<Compile Include="Parser.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Requests\ApiRequest.cs" />
|
||||
<Compile Include="Requests\AppSignatureKeyRequest.cs" />
|
||||
<Compile Include="Requests\AvailableExamsRequest.cs" />
|
||||
<Compile Include="Requests\BaseRequest.cs" />
|
||||
<Compile Include="Requests\ConfirmLockScreenRequest.cs" />
|
||||
<Compile Include="Requests\ContentType.cs" />
|
||||
<Compile Include="Requests\DisconnectionRequest.cs" />
|
||||
<Compile Include="Requests\ExamConfigurationRequest.cs" />
|
||||
<Compile Include="Requests\Header.cs" />
|
||||
<Compile Include="Requests\LockScreenRequest.cs" />
|
||||
<Compile Include="Requests\LogRequest.cs" />
|
||||
<Compile Include="Requests\LowerHandRequest.cs" />
|
||||
<Compile Include="Requests\NetworkAdapterRequest.cs" />
|
||||
<Compile Include="Requests\OAuth2TokenRequest.cs" />
|
||||
<Compile Include="Requests\PingRequest.cs" />
|
||||
<Compile Include="Requests\PowerSupplyRequest.cs" />
|
||||
<Compile Include="Requests\RaiseHandRequest.cs" />
|
||||
<Compile Include="Requests\SelectExamRequest.cs" />
|
||||
<Compile Include="Requests\SessionIdentifierRequest.cs" />
|
||||
<Compile Include="ServerProxy.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -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<IEnumerable<Exam>> 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<Exam>);
|
||||
|
||||
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<IEnumerable<Exam>>(success, exams, message);
|
||||
|
@ -174,18 +157,15 @@ namespace SafeExamBrowser.Server
|
|||
|
||||
public ServerResponse<Uri> 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<ApiVersion1>(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"] = $"<raisehand> {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"] = $"<lockscreen> {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 = $"<battery> {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}";
|
||||
SendPowerSupplyStatus(text, value);
|
||||
|
||||
currentPowerSupplyValue = value;
|
||||
text = $"<battery> {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}";
|
||||
}
|
||||
else if (connected != connectedToPowergrid)
|
||||
{
|
||||
var text = $"<battery> Device has been {(connected ? "connected to" : "disconnected from")} power grid";
|
||||
SendPowerSupplyStatus(text, value);
|
||||
connectedToPowergrid = connected;
|
||||
text = $"<battery> 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"] = $"<wlan> {network.Name}: {network.Status}, {network.SignalStrength}%";
|
||||
json["numericValue"] = network.SignalStrength;
|
||||
text = $"<wlan> {network.Name}: {network.Status}, {network.SignalStrength}%";
|
||||
value = network.SignalStrength;
|
||||
}
|
||||
else
|
||||
{
|
||||
json["text"] = "<wlan> not connected";
|
||||
text = "<wlan> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue