Skip to content

Commit

Permalink
SEBWIN-972: Fixed issue with base address of HttpClient and paths of …
Browse files Browse the repository at this point in the history
…APIs for bundled setup of server and service.
  • Loading branch information
dbuechel committed Nov 12, 2024
1 parent 657cb7e commit 7d10de0
Show file tree
Hide file tree
Showing 28 changed files with 170 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="ScreenProctoring\Service\Requests\OAuth2TokenRequest.cs" />
<Compile Include="ScreenProctoring\Service\Requests\Request.cs" />
<Compile Include="ScreenProctoring\Service\Requests\ScreenShotRequest.cs" />
<Compile Include="ScreenProctoring\Service\Sanitizer.cs" />
<Compile Include="ScreenProctoring\Service\ServiceProxy.cs" />
<Compile Include="ScreenProctoring\Service\ServiceResponse.cs" />
<Compile Include="ScreenProctoring\TransmissionSpooler.cs" />
Expand Down
8 changes: 4 additions & 4 deletions SafeExamBrowser.Proctoring/ScreenProctoring/Service/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ internal class Api

internal Api()
{
AccessTokenEndpoint = "/oauth/token";
HealthEndpoint = "/health";
ScreenShotEndpoint = $"/seb-api/v1/session/{SESSION_ID}/screenshot";
SessionEndpoint = "/seb-api/v1/session";
AccessTokenEndpoint = "oauth/token";
HealthEndpoint = "health";
ScreenShotEndpoint = $"seb-api/v1/session/{SESSION_ID}/screenshot";
SessionEndpoint = "seb-api/v1/session";
}
}
}
32 changes: 32 additions & 0 deletions SafeExamBrowser.Proctoring/ScreenProctoring/Service/Sanitizer.cs
Original file line number Diff line number Diff line change
@@ -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/.
*/

using System;
using System.Reflection;

namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service
{
internal class Sanitizer
{
internal Uri Sanitize(string serviceUrl)
{
return new Uri(serviceUrl.EndsWith("/") ? serviceUrl : $"{serviceUrl}/");
}

internal void Sanitize(Api api)
{
foreach (var property in api.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
var value = property.GetValue(api) as string;
var sanitized = value.TrimStart('/');

property.SetValue(api, sanitized);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class ServiceProxy
private readonly Api api;
private readonly ILogger logger;
private readonly Parser parser;
private readonly Sanitizer sanitizer;

private HttpClient httpClient;

Expand All @@ -31,16 +32,19 @@ internal ServiceProxy(ILogger logger)
this.api = new Api();
this.logger = logger;
this.parser = new Parser(logger);
this.sanitizer = new Sanitizer();
}

internal ServiceResponse Connect(string clientId, string clientSecret, string serviceUrl)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(serviceUrl),
BaseAddress = sanitizer.Sanitize(serviceUrl),
Timeout = TimeSpan.FromSeconds(10)
};

sanitizer.Sanitize(api);

var request = new OAuth2TokenRequest(api, httpClient, logger, parser);
var success = request.TryExecute(clientId, clientSecret, out var message);

Expand Down
30 changes: 30 additions & 0 deletions SafeExamBrowser.Server/Data/Api.cs
Original file line number Diff line number Diff line change
@@ -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 Newtonsoft.Json;

namespace SafeExamBrowser.Server.Data
{
internal class Api
{
[JsonProperty]
internal string AccessTokenEndpoint { get; set; }

[JsonProperty]
internal string HandshakeEndpoint { get; set; }

[JsonProperty]
internal string ConfigurationEndpoint { get; set; }

[JsonProperty]
internal string PingEndpoint { get; set; }

[JsonProperty]
internal string LogEndpoint { get; set; }
}
}
19 changes: 0 additions & 19 deletions SafeExamBrowser.Server/Data/ApiVersion1.cs

This file was deleted.

4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ internal bool IsTokenExpired(HttpContent content)
return isExpired;
}

internal bool TryParseApi(HttpContent content, out ApiVersion1 api)
internal bool TryParseApi(HttpContent content, out Api api)
{
var success = false;

api = new ApiVersion1();
api = new Api();

try
{
Expand Down
13 changes: 9 additions & 4 deletions SafeExamBrowser.Server/Requests/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@

namespace SafeExamBrowser.Server.Requests
{
internal class ApiRequest : BaseRequest
internal class ApiRequest : Request
{
private readonly Sanitizer sanitizer;

internal ApiRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Sanitizer sanitizer,
ServerSettings settings) : base(api, httpClient, logger, parser, settings)
{
this.sanitizer = sanitizer;
}

internal bool TryExecute(out ApiVersion1 api, out string message)
internal bool TryExecute(out Api api, out string message)
{
var success = TryExecute(HttpMethod.Get, settings.ApiUrl, out var response);

api = new ApiVersion1();
api = new Api();
message = response.ToLogString();

if (success)
{
parser.TryParseApi(response.Content, out api);
sanitizer.Sanitize(api);
}

return success;
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/AppSignatureKeyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class AppSignatureKeyRequest : BaseRequest
internal class AppSignatureKeyRequest : Request
{
internal AppSignatureKeyRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/AvailableExamsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

namespace SafeExamBrowser.Server.Requests
{
internal class AvailableExamsRequest : BaseRequest
internal class AvailableExamsRequest : Request
{
private readonly AppConfig appConfig;
private readonly ISystemInfo systemInfo;
private readonly IUserInfo userInfo;

internal AvailableExamsRequest(
ApiVersion1 api,
Api api,
AppConfig appConfig,
HttpClient httpClient,
ILogger logger,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/ConfirmLockScreenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class ConfirmLockScreenRequest : BaseRequest
internal class ConfirmLockScreenRequest : Request
{
internal ConfirmLockScreenRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/DisconnectionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class DisconnectionRequest : BaseRequest
internal class DisconnectionRequest : Request
{
internal DisconnectionRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/ExamConfigurationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class ExamConfigurationRequest : BaseRequest
internal class ExamConfigurationRequest : Request
{
internal ExamConfigurationRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Text;
using SafeExamBrowser.Settings.Logging;

namespace SafeExamBrowser.Server
namespace SafeExamBrowser.Server.Requests
{
internal static class Extensions
{
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/LockScreenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class LockScreenRequest : BaseRequest
internal class LockScreenRequest : Request
{
internal LockScreenRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/LogRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class LogRequest : BaseRequest
internal class LogRequest : Request
{
internal LogRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/LowerHandRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class LowerHandRequest : BaseRequest
internal class LowerHandRequest : Request
{
internal LowerHandRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/NetworkAdapterRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class NetworkAdapterRequest : BaseRequest
internal class NetworkAdapterRequest : Request
{
internal NetworkAdapterRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/OAuth2TokenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class OAuth2TokenRequest : BaseRequest
internal class OAuth2TokenRequest : Request
{
internal OAuth2TokenRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/PingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class PingRequest : BaseRequest
internal class PingRequest : Request
{
internal PingRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/PowerSupplyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class PowerSupplyRequest : BaseRequest
internal class PowerSupplyRequest : Request
{
internal PowerSupplyRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
4 changes: 2 additions & 2 deletions SafeExamBrowser.Server/Requests/RaiseHandRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

namespace SafeExamBrowser.Server.Requests
{
internal class RaiseHandRequest : BaseRequest
internal class RaiseHandRequest : Request
{
internal RaiseHandRequest(
ApiVersion1 api,
Api api,
HttpClient httpClient,
ILogger logger,
Parser parser,
Expand Down
Loading

0 comments on commit 7d10de0

Please sign in to comment.