2020-07-13 22:57:19 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2020-07-13 22:57:19 +02:00
|
|
|
|
*
|
|
|
|
|
* 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 SafeExamBrowser.Communication.Contracts.Data;
|
|
|
|
|
using SafeExamBrowser.Configuration.Contracts;
|
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.Cryptography;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
|
|
|
|
|
using SafeExamBrowser.Runtime.Operations.Events;
|
|
|
|
|
using SafeExamBrowser.Settings;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.Operations
|
|
|
|
|
{
|
|
|
|
|
internal abstract class ConfigurationBaseOperation : SessionOperation
|
|
|
|
|
{
|
|
|
|
|
protected string[] commandLineArgs;
|
|
|
|
|
protected IConfigurationRepository configuration;
|
|
|
|
|
|
|
|
|
|
protected string AppDataFilePath => Context.Next.AppConfig.AppDataFilePath;
|
|
|
|
|
protected string ProgramDataFilePath => Context.Next.AppConfig.ProgramDataFilePath;
|
|
|
|
|
|
|
|
|
|
public ConfigurationBaseOperation(string[] commandLineArgs, IConfigurationRepository configuration, SessionContext context) : base(context)
|
|
|
|
|
{
|
|
|
|
|
this.commandLineArgs = commandLineArgs;
|
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void InvokeActionRequired(ActionRequiredEventArgs args);
|
|
|
|
|
|
2024-08-19 12:19:32 +02:00
|
|
|
|
protected LoadStatus? TryLoadSettings(Uri uri, UriSource source, out PasswordParameters passwordParams, out AppSettings settings, string currentPassword = default)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
|
|
|
|
passwordParams = new PasswordParameters { Password = string.Empty, IsHash = true };
|
|
|
|
|
|
|
|
|
|
var status = configuration.TryLoadSettings(uri, out settings, passwordParams);
|
|
|
|
|
|
2024-08-19 12:19:32 +02:00
|
|
|
|
if (status == LoadStatus.PasswordNeeded && currentPassword != default)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
|
|
|
|
passwordParams.Password = currentPassword;
|
|
|
|
|
passwordParams.IsHash = true;
|
|
|
|
|
|
|
|
|
|
status = configuration.TryLoadSettings(uri, out settings, passwordParams);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 12:19:32 +02:00
|
|
|
|
for (var attempts = 0; attempts < 5 && status == LoadStatus.PasswordNeeded; attempts++)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
|
|
|
|
var isLocalConfig = source == UriSource.AppData || source == UriSource.ProgramData;
|
|
|
|
|
var purpose = isLocalConfig ? PasswordRequestPurpose.LocalSettings : PasswordRequestPurpose.Settings;
|
|
|
|
|
var success = TryGetPassword(purpose, out var password);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
passwordParams.Password = password;
|
|
|
|
|
passwordParams.IsHash = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = configuration.TryLoadSettings(uri, out settings, passwordParams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool TryGetPassword(PasswordRequestPurpose purpose, out string password)
|
|
|
|
|
{
|
|
|
|
|
var args = new PasswordRequiredEventArgs { Purpose = purpose };
|
|
|
|
|
|
|
|
|
|
InvokeActionRequired(args);
|
|
|
|
|
password = args.Password;
|
|
|
|
|
|
|
|
|
|
return args.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected enum UriSource
|
|
|
|
|
{
|
|
|
|
|
Undefined,
|
|
|
|
|
AppData,
|
|
|
|
|
CommandLine,
|
|
|
|
|
ProgramData,
|
|
|
|
|
Reconfiguration,
|
|
|
|
|
Server
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|