Restore SEBPatch

This commit is contained in:
2025-06-01 11:44:20 +02:00
commit 8c656e3137
1297 changed files with 142172 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Proctoring.ScreenProctoring.Service
{
internal class Api
{
internal const string SESSION_ID = "%%_SESSION_ID_%%";
internal string AccessTokenEndpoint { get; set; }
internal string HealthEndpoint { get; set; }
internal string ScreenShotEndpoint { get; set; }
internal string SessionEndpoint { get; set; }
internal Api()
{
AccessTokenEndpoint = "/oauth/token";
HealthEndpoint = "/health";
ScreenShotEndpoint = $"/seb-api/v1/session/{SESSION_ID}/screenshot";
SessionEndpoint = "/seb-api/v1/session";
}
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service
{
internal class Parser
{
private readonly ILogger logger;
internal Parser(ILogger logger)
{
this.logger = logger;
}
internal bool IsTokenExpired(HttpContent content)
{
var isExpired = false;
try
{
var json = JsonConvert.DeserializeObject(Extract(content)) as JObject;
var error = json["error"].Value<string>();
isExpired = error?.Equals("invalid_token", StringComparison.OrdinalIgnoreCase) == true;
}
catch (Exception e)
{
logger.Error("Failed to parse token expiration content!", e);
}
return isExpired;
}
internal bool TryParseHealth(HttpResponseMessage response, out int health)
{
var success = false;
health = default;
try
{
if (response.Headers.TryGetValues(Header.HEALTH, out var values))
{
success = int.TryParse(values.First(), out health);
}
}
catch (Exception e)
{
logger.Error("Failed to parse health!", e);
}
return success;
}
internal bool TryParseOauth2Token(HttpContent content, out string oauth2Token)
{
oauth2Token = default;
try
{
var json = JsonConvert.DeserializeObject(Extract(content)) as JObject;
oauth2Token = json["access_token"].Value<string>();
}
catch (Exception e)
{
logger.Error("Failed to parse Oauth2 token!", e);
}
return oauth2Token != default;
}
internal bool TryParseSessionId(HttpResponseMessage response, out string sessionId)
{
sessionId = default;
try
{
if (response.Headers.TryGetValues(Header.SESSION_ID, out var values))
{
sessionId = values.First();
}
}
catch (Exception e)
{
logger.Error("Failed to parse session identifier!", e);
}
return sessionId != default;
}
private string Extract(HttpContent content)
{
var task = Task.Run(async () =>
{
return await content.ReadAsStreamAsync();
});
var stream = task.GetAwaiter().GetResult();
var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Proctoring.ScreenProctoring.Service.Requests
{
internal static class ContentType
{
internal const string JSON = "application/json;charset=UTF-8";
internal const string OCTET_STREAM = "application/octet-stream";
internal const string URL_ENCODED = "application/x-www-form-urlencoded";
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal class CreateSessionRequest : Request
{
internal CreateSessionRequest(Api api, HttpClient httpClient, ILogger logger, Parser parser) : base(api, httpClient, logger, parser)
{
}
internal bool TryExecute(string groupId, out string message, out string sessionId)
{
var group = (Header.GROUP_ID, groupId);
var success = TryExecute(HttpMethod.Post, api.SessionEndpoint, out var response, string.Empty, ContentType.URL_ENCODED, Authorization, group);
message = response.ToLogString();
sessionId = default;
if (success)
{
parser.TryParseSessionId(response, out sessionId);
}
return success;
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Text;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal static class Extensions
{
internal static string ToLogString(this HttpResponseMessage response)
{
return response == default ? "No Response" : $"{(int) response.StatusCode} {response.StatusCode} {response.ReasonPhrase}";
}
internal static string ToSummary(this Exception exception)
{
var trimChars = new[] { '.', '!' };
var summary = new StringBuilder(exception.Message?.TrimEnd(trimChars));
for (var inner = exception.InnerException; inner != default; inner = inner.InnerException)
{
summary.Append($" -> {inner.Message?.TrimEnd(trimChars)}");
}
return summary.ToString();
}
internal static long ToUnixTimestamp(this DateTime date)
{
return new DateTimeOffset(date).ToUnixTimeMilliseconds();
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Proctoring.ScreenProctoring.Service.Requests
{
internal static class Header
{
internal const string ACCEPT = "Accept";
internal const string AUTHORIZATION = "Authorization";
internal const string GROUP_ID = "SEB_GROUP_UUID";
internal const string HEALTH = "sps_server_health";
internal const string IMAGE_FORMAT = "imageFormat";
internal const string METADATA = "metaData";
internal const string SESSION_ID = "SEB_SESSION_UUID";
internal const string TIMESTAMP = "timestamp";
internal static class Metadata
{
internal const string ApplicationInfo = "screenProctoringMetadataApplication";
internal const string BrowserInfo = "screenProctoringMetadataBrowser";
internal const string BrowserUrls = "screenProctoringMetadataURL";
internal const string TriggerInfo = "screenProctoringMetadataUserAction";
internal const string WindowTitle = "screenProctoringMetadataWindowTitle";
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal class HealthRequest : Request
{
internal HealthRequest(Api api, HttpClient httpClient, ILogger logger, Parser parser) : base(api, httpClient, logger, parser)
{
}
internal bool TryExecute(out int health, out string message)
{
var url = api.HealthEndpoint;
var success = TryExecute(HttpMethod.Get, url, out var response);
health = default;
message = response.ToLogString();
if (success)
{
parser.TryParseHealth(response, out health);
}
return success;
}
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal class OAuth2TokenRequest : Request
{
internal OAuth2TokenRequest(Api api, HttpClient httpClient, ILogger logger, Parser parser) : base(api, httpClient, logger, parser)
{
}
internal bool TryExecute(string clientId, string clientSecret, out string message)
{
ClientId = clientId;
ClientSecret = clientSecret;
return TryRetrieveOAuth2Token(out message);
}
}
}

View File

@@ -0,0 +1,181 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal abstract class Request
{
private const int ATTEMPTS = 5;
private static string oauth2Token;
private readonly HttpClient httpClient;
private bool hadException;
protected readonly Api api;
protected readonly ILogger logger;
protected readonly Parser parser;
protected static string ClientId { get; set; }
protected static string ClientSecret { get; set; }
protected (string, string) Authorization => (Header.AUTHORIZATION, $"Bearer {oauth2Token}");
protected Request(Api api, HttpClient httpClient, ILogger logger, Parser parser)
{
this.api = api;
this.httpClient = httpClient;
this.logger = logger;
this.parser = parser;
}
protected bool TryExecute(
HttpMethod method,
string url,
out HttpResponseMessage response,
object content = default,
string contentType = default,
params (string name, string value)[] headers)
{
response = default;
for (var attempt = 0; attempt < ATTEMPTS && (response == default || !response.IsSuccessStatusCode); attempt++)
{
var request = BuildRequest(method, url, content, contentType, headers);
try
{
response = httpClient.SendAsync(request).GetAwaiter().GetResult();
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.Warn($"Request {request.Method} '{request.RequestUri}' did not complete within {httpClient.Timeout}!");
break;
}
catch (Exception e)
{
if (IsFirstException())
{
logger.Warn($"Request {request.Method} '{request.RequestUri}' has failed: {e.ToSummary()}!");
}
}
}
return response != default && response.IsSuccessStatusCode;
}
protected bool TryRetrieveOAuth2Token(out string message)
{
var secret = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{ClientId}:{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,
object content = default,
string contentType = default,
params (string name, string value)[] headers)
{
var request = new HttpRequestMessage(method, url);
if (content != default)
{
if (content is string)
{
request.Content = new StringContent(content as string, Encoding.UTF8);
}
if (content is byte[])
{
request.Content = new ByteArrayContent(content as byte[]);
}
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 bool IsFirstException()
{
var isFirst = !hadException;
hadException = true;
return isFirst;
}
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();
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
using System.Net.Http;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Proctoring.ScreenProctoring.Data;
using SafeExamBrowser.Proctoring.ScreenProctoring.Imaging;
using SafeExamBrowser.Settings.Proctoring;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal class ScreenShotRequest : Request
{
internal ScreenShotRequest(Api api, HttpClient httpClient, ILogger logger, Parser parser) : base(api, httpClient, logger, parser)
{
}
internal bool TryExecute(MetaData metaData, ScreenShot screenShot, string sessionId, out int health, out string message)
{
var imageFormat = (Header.IMAGE_FORMAT, ToString(screenShot.Format));
var metdataJson = (Header.METADATA, WebUtility.UrlEncode(metaData.ToJson()));
var timestamp = (Header.TIMESTAMP, screenShot.CaptureTime.ToUnixTimestamp().ToString());
var url = api.ScreenShotEndpoint.Replace(Api.SESSION_ID, sessionId);
var success = TryExecute(HttpMethod.Post, url, out var response, screenShot.Data, ContentType.OCTET_STREAM, Authorization, imageFormat, metdataJson, timestamp);
health = default;
message = response.ToLogString();
if (success)
{
parser.TryParseHealth(response, out health);
}
return success;
}
private string ToString(ImageFormat format)
{
switch (format)
{
case ImageFormat.Bmp:
return "bmp";
case ImageFormat.Gif:
return "gif";
case ImageFormat.Jpg:
return "jpg";
case ImageFormat.Png:
return "png";
default:
throw new NotImplementedException($"Image format {format} is not yet implemented!");
}
}
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{
internal class TerminateSessionRequest : Request
{
internal TerminateSessionRequest(Api api, HttpClient httpClient, ILogger logger, Parser parser) : base(api, httpClient, logger, parser)
{
}
internal bool TryExecute(string sessionId, out string message)
{
var url = $"{api.SessionEndpoint}/{sessionId}";
var success = TryExecute(HttpMethod.Delete, url, out var response, contentType: ContentType.URL_ENCODED, headers: Authorization);
message = response.ToLogString();
return success;
}
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Proctoring.ScreenProctoring.Data;
using SafeExamBrowser.Proctoring.ScreenProctoring.Imaging;
using SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests;
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service
{
internal class ServiceProxy
{
private readonly Api api;
private readonly ILogger logger;
private readonly Parser parser;
private HttpClient httpClient;
internal bool IsConnected => SessionId != default;
internal string SessionId { get; set; }
internal ServiceProxy(ILogger logger)
{
this.api = new Api();
this.logger = logger;
this.parser = new Parser(logger);
}
internal ServiceResponse Connect(string clientId, string clientSecret, string serviceUrl)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(serviceUrl),
Timeout = TimeSpan.FromSeconds(10)
};
var request = new OAuth2TokenRequest(api, httpClient, logger, parser);
var success = request.TryExecute(clientId, clientSecret, out var message);
if (success)
{
logger.Info("Successfully connected to service.");
}
else
{
logger.Error("Failed to connect to service!");
}
return new ServiceResponse(success, message);
}
internal ServiceResponse CreateSession(string groupId)
{
var request = new CreateSessionRequest(api, httpClient, logger, parser);
var success = request.TryExecute(groupId, out var message, out var sessionId);
if (success)
{
SessionId = sessionId;
logger.Info("Successfully created session.");
}
else
{
logger.Error("Failed to create session!");
}
return new ServiceResponse(success, message);
}
internal ServiceResponse<int> GetHealth()
{
var request = new HealthRequest(api, httpClient, logger, parser);
var success = request.TryExecute(out var health, out var message);
if (success)
{
logger.Info($"Successfully queried health (value: {health}).");
}
else
{
logger.Warn("Failed to query health!");
}
return new ServiceResponse<int>(success, health, message);
}
internal ServiceResponse<int> Send(MetaData metaData, ScreenShot screenShot)
{
var request = new ScreenShotRequest(api, httpClient, logger, parser);
var success = request.TryExecute(metaData, screenShot, SessionId, out var health, out var message);
if (success)
{
logger.Info($"Successfully sent screen shot ({screenShot}).");
}
else
{
logger.Error("Failed to send screen shot!");
}
return new ServiceResponse<int>(success, health, message);
}
internal ServiceResponse TerminateSession()
{
var request = new TerminateSessionRequest(api, httpClient, logger, parser);
var success = request.TryExecute(SessionId, out var message);
if (success)
{
SessionId = default;
logger.Info("Successfully terminated session.");
}
else
{
logger.Error("Failed to terminate session!");
}
return new ServiceResponse(success, message);
}
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Proctoring.ScreenProctoring.Service
{
internal class ServiceResponse
{
internal string Message { get; }
internal bool Success { get; }
internal ServiceResponse(bool success, string message = default)
{
Message = message;
Success = success;
}
}
internal class ServiceResponse<T> : ServiceResponse
{
internal T Value { get; }
internal ServiceResponse(bool success, T value, string message = default) : base(success, message)
{
Value = value;
}
}
}