Skip to content

Commit

Permalink
Added internet settings to Paratext registration info form, fixed dis…
Browse files Browse the repository at this point in the history
…abling internet in sensitive locations
  • Loading branch information
tjcouch-sil committed Nov 20, 2024
1 parent cba0e36 commit 18f30c3
Show file tree
Hide file tree
Showing 17 changed files with 1,131 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
*.woff binary
*.woff2 binary
*.ldml binary
*.zip binary
3 changes: 3 additions & 0 deletions assets/localization/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"%about_versionLabel_format%": "Version: {version}",
"%about_licenseLabel_format%": "License: {license}",
"%about_db_ip_attribution_format%": "{intro} {websiteLink} ({license}, {terms})",
"%about_db_ip_attribution_intro%": "Internet safety features use data from",
"%about_db_ip_attribution_terms%": "Terms",
"%downloadResources_errorRegistrationInvalid%": "User registration is not valid. Cannot retrieve resources from DBL.",
"%downloadResources_errorInstallResource_resourceNotFound%": "Resource not available from DBL",
"%downloadResources_errorInstallResource_resourceAlreadyInstalled%": "Resource is already installed and up to date. Installation skipped.",
Expand Down
6 changes: 6 additions & 0 deletions assets/localization/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
},
"%submit%": {
"fallbackKey": "%yes%"
},
"%about_db_ip_attribution_intro%": {
"fallbackKey": "%Paratext.Base.SplashScreen.Splash_Load." Internet safety features use data
from "%"
},
"%about_db_ip_attribution_terms%": {
"fallbackKey": "%Paratext.Base.SplashScreen.Splash_Load."Terms"%"
}
}
Binary file added c-sharp/IP-Country.zip
Binary file not shown.
193 changes: 193 additions & 0 deletions c-sharp/JsonUtils/InternetSettingsMementoConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Paratext.Data;

namespace Paranext.DataProvider.JsonUtils;

public class InternetSettingsMementoConverter
: JsonConverter<InternetAccess.InternetSettingsMemento>
{
private const string SELECTED_SERVER = "selectedServer";
private const string PERMITTED_INTERNET_USE = "permittedInternetUse";
private const string PROXY_PORT = "proxyPort";
private const string PROXY_HOST = "proxyHost";
private const string PROXY_USERNAME = "proxyUsername";
private const string PROXY_PASSWORD = "proxyPassword";
private const string PROXY_MODE = "proxyMode";
private const string OVERRIDE_DBL_SERVER = "overrideDBLServer";
private const string OVERRIDE_DBL_API_SERVER = "overrideDBLApiServer";
private const string OVERRIDE_GBC_SERVER = "overrideGbcServer";
private const string DBL_EMAIL = "dblEmail";
private const string DBL_PASSWORD = "dblPassword";

public override InternetAccess.InternetSettingsMemento Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
ServerType selectedServer = ServerType.Production;
InternetUse permittedInternetUse = InternetUse.VpnRequired;
int proxyPort = 0;
string? proxyHost = null;
string? proxyUsername = null;
string? proxyPassword = null;
string? proxyMode = null;
string? overrideDBLServer = null;
string? overrideDBLApiServer = null;
string? overrideGbcServer = null;
string? dblEmail = null;
string? dblPassword = null;

string? lastPropertyName = null;
// The starting token is consumed before we get the reader
int onObjectLevel = 1;
while (onObjectLevel > 0 && reader.Read())
{
switch (reader.TokenType)
{
case JsonTokenType.StartObject:
case JsonTokenType.StartArray:
onObjectLevel++;
break;
case JsonTokenType.EndObject:
case JsonTokenType.EndArray:
onObjectLevel--;
break;
case JsonTokenType.PropertyName:
lastPropertyName = reader.GetString();
break;
case JsonTokenType.True:
case JsonTokenType.False:
lastPropertyName = null;
break;
case JsonTokenType.Number:
switch (lastPropertyName)
{
case PROXY_PORT:
proxyPort = reader.GetInt32();
break;
}
lastPropertyName = null;
break;
case JsonTokenType.String:
switch (lastPropertyName)
{
case SELECTED_SERVER:
var selectedServerString = reader.GetString();
if (!string.IsNullOrEmpty(selectedServerString))
{
selectedServer = selectedServerString switch
{
"Production" => ServerType.Production,
"QualityAssurance" => ServerType.QualityAssurance,
"Development" => ServerType.Development,
"Test" => ServerType.Test,
_ => throw new JsonException(
$"Invalid selectedServer value {selectedServerString}! Must provide a ServerType value"
),
};
}
break;
case PERMITTED_INTERNET_USE:
var permittedInternetUseString = reader.GetString();
if (!string.IsNullOrEmpty(permittedInternetUseString))
{
permittedInternetUse = permittedInternetUseString switch
{
"Enabled" => InternetUse.Enabled,
"VpnRequired" => InternetUse.VpnRequired,
"Disabled" => InternetUse.Disabled,
"ProxyOnly" => InternetUse.ProxyOnly,
_ => throw new JsonException(
$"Invalid permittedInternetUse value {permittedInternetUseString}! Must provide an InternetUse value"
),
};
}
break;
case PROXY_HOST:
proxyHost = reader.GetString() ?? "";
break;
case PROXY_USERNAME:
proxyUsername = reader.GetString() ?? "";
break;
case PROXY_PASSWORD:
proxyPassword = reader.GetString() ?? "";
break;
case PROXY_MODE:
proxyMode = reader.GetString() ?? "";
break;
case OVERRIDE_DBL_SERVER:
overrideDBLServer = reader.GetString() ?? "";
break;
case OVERRIDE_DBL_API_SERVER:
overrideDBLApiServer = reader.GetString() ?? "";
break;
case OVERRIDE_GBC_SERVER:
overrideGbcServer = reader.GetString() ?? "";
break;
case DBL_EMAIL:
dblEmail = reader.GetString() ?? "";
break;
case DBL_PASSWORD:
dblPassword = reader.GetString() ?? "";
break;
}
lastPropertyName = null;
break;
}
}

return new InternetAccess.InternetSettingsMemento
{
SelectedServer = selectedServer,
PermittedInternetUse = permittedInternetUse,
ProxyPort = proxyPort,
ProxyHost = proxyHost,
ProxyUsername = proxyUsername,
ProxyPassword = proxyPassword,
ProxyMode = proxyMode,
OverrideDBLServer = overrideDBLServer,
OverrideDBLApiServer = overrideDBLApiServer,
OverrideGbcServer = overrideGbcServer,
DBLEmail = dblEmail,
DBLPassword = dblPassword,
};
}

public override void Write(
Utf8JsonWriter writer,
InternetAccess.InternetSettingsMemento value,
JsonSerializerOptions options
)
{
writer.WriteStartObject();
var selectedServer = value.SelectedServer switch
{
ServerType.QualityAssurance => "QualityAssurance",
ServerType.Development => "Development",
ServerType.Test => "Test",
_ => "Production",
};
writer.WriteString(SELECTED_SERVER, selectedServer);
var permittedInternetUse = value.PermittedInternetUse switch
{
InternetUse.Enabled => "Enabled",
InternetUse.Disabled => "Disabled",
InternetUse.ProxyOnly => "ProxyOnly",
_ => "VpnRequired",
};
writer.WriteString(PERMITTED_INTERNET_USE, permittedInternetUse);
writer.WriteNumber(PROXY_PORT, value.ProxyPort);
writer.WriteString(PROXY_HOST, value.ProxyHost);
writer.WriteString(PROXY_USERNAME, value.ProxyUsername);
writer.WriteString(PROXY_PASSWORD, value.ProxyPassword);
writer.WriteString(PROXY_MODE, value.ProxyMode);
writer.WriteString(OVERRIDE_DBL_SERVER, value.OverrideDBLServer);
writer.WriteString(OVERRIDE_DBL_API_SERVER, value.OverrideDBLApiServer);
writer.WriteString(OVERRIDE_GBC_SERVER, value.OverrideGbcServer);
writer.WriteString(DBL_EMAIL, value.DBLEmail);
writer.WriteString(DBL_PASSWORD, value.DBLPassword);
writer.WriteEndObject();
}
}
1 change: 1 addition & 0 deletions c-sharp/JsonUtils/SerializationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static JsonSerializerOptions CreateSerializationOptions()
options.Converters.Add(new CommentConverter());
options.Converters.Add(new VerseRefConverter());
options.Converters.Add(new RegistrationDataConverter());
options.Converters.Add(new InternetSettingsMementoConverter());
return options;
}

Expand Down
9 changes: 9 additions & 0 deletions c-sharp/ParanextDataProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="icon.ico" />
<!--
IP-Country.zip is a file containing information about the country of origin for many ip
ranges. As of 18 Nov 2024, this file is copied from a Paratext 9.4 installation. See
`Paratext/ResourceCreator/Program.cs` and `Paratext/ParatextData/GeoLocationData.cs` for more
information on this file and how to generate it.
-->
<Content Include="IP-Country.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion c-sharp/Projects/LocalParatextProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ internal class LocalParatextProjects
private bool _isInitialized = false;
private readonly object _initializationLock = new();

private readonly List<string> _requiredProjectRootFiles = ["usfm.sty", "Attribution.md"];
private readonly List<string> _requiredProjectRootFiles =
[
"usfm.sty",
"Attribution.md",
"CountryStatuses.xml",
];

private static readonly List<string> s_paratextProjectInterfaces =
[
Expand Down
Loading

0 comments on commit 18f30c3

Please sign in to comment.