Skip to content

Commit 0bdbd8f

Browse files
author
yapetrichka
committed
feat: Add load keystore from environment variables
1 parent 7030e0b commit 0bdbd8f

16 files changed

+262
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## [1.3.0] - 2024-06-16
4+
- Added load keystore from environment variables
5+
36
## [1.2.3] - 2023-12-12
47
- Changed toolbar path to match Unity submission-guidelines (section 2.5.1.a)
58

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
22
<Style src="project://database/Packages/com.dreamcode.mobile.android-keystore/Editor/Assets/Styles/KeystoreWindow.uss?fileID=7433441132597879392&amp;guid=4c0697299499c2746bd838d8d7bc9c29&amp;type=3#KeystoreWindow" />
33
<ui:VisualElement name="Container" style="height: 100%; color: rgb(255, 255, 255); background-color: rgb(255, 255, 255); justify-content: space-around; flex-grow: 0; align-items: stretch;">
4+
<ui:Label text="Repository" display-tooltip-when-elided="true" name="RepositoryLabel" style="color: rgb(118, 118, 118); -unity-font-style: bold; -unity-text-align: upper-center; font-size: 24px; padding-right: 0; padding-left: 0; padding-top: 0; width: auto; white-space: normal;" />
5+
<ui:EnumField type="UnityEngine.TextAlignment, UnityEngine.TextRenderingModule" value="Center" include-obsolete-values="false" name="Storage" style="align-items: stretch; flex-direction: column; justify-content: flex-start; align-self: auto; width: auto; padding-right: 10%; padding-left: 10%;" />
46
<ui:Label text="Project Keystore" display-tooltip-when-elided="true" name="KeystoreLabel" style="color: rgb(118, 118, 118); -unity-font-style: bold; -unity-text-align: upper-center; font-size: 24px; padding-right: 0; padding-left: 0; padding-top: 0; width: auto; white-space: normal;" />
57
<ui:TextField picking-mode="Ignore" label="Path .keystore" text="path.keystore" multiline="false" name="KeystorePath" class="keystore-text-field" style="padding-top: 0; font-size: 18px;" />
68
<ui:TextField picking-mode="Ignore" label="Password" text="password" multiline="false" name="KeystorePass" class="keystore-text-field" style="flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; font-size: 18px; color: rgb(255, 255, 255); background-color: rgb(255, 255, 255); -unity-font-style: bold; -unity-text-align: upper-left; width: auto; padding-left: 10%; padding-right: 10%; padding-top: 0;" />
79
<ui:Label text="Project Key" display-tooltip-when-elided="true" name="KeyLabel" style="color: rgb(118, 118, 118); -unity-font-style: bold; -unity-text-align: upper-center; font-size: 24px; padding-right: 0; padding-left: 0; padding-top: 0; white-space: normal;" />
810
<ui:TextField picking-mode="Ignore" label="Alias" text="alias" multiline="false" name="KeyaliasName" class="keystore-text-field" style="flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; font-size: 18px; color: rgb(255, 255, 255); background-color: rgb(255, 255, 255); -unity-font-style: bold; -unity-text-align: upper-left; width: auto; padding-left: 10%; padding-right: 10%; padding-top: 0;" />
911
<ui:TextField picking-mode="Ignore" label="Password" text="password" multiline="false" name="KeyaliasPass" class="keystore-text-field" style="flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; font-size: 18px; color: rgb(255, 255, 255); background-color: rgb(255, 255, 255); -unity-font-style: bold; -unity-text-align: upper-left; width: auto; padding-left: 10%; padding-right: 10%; padding-top: 0;" />
1012
<ui:Button text="Save" display-tooltip-when-elided="true" name="SaveBtn" class="save save-button" style="border-left-width: 0; border-right-width: 0; border-top-width: 0; border-bottom-width: 0; margin-top: 0; margin-bottom: 0; -unity-font-style: bold; bottom: auto; top: auto; flex-shrink: 0; flex-grow: 0;" />
13+
<ui:Button text="Donate" display-tooltip-when-elided="true" name="DonateBtn" class="save save-button" style="border-left-width: 0; border-right-width: 0; border-top-width: 0; border-bottom-width: 0; margin-top: 0; margin-bottom: 0; -unity-font-style: bold; bottom: auto; top: auto; flex-shrink: 0; flex-grow: 0; width: auto; align-items: stretch; align-self: auto; left: auto; margin-right: 30%; margin-left: 30%; height: 8%; padding-right: 12px; padding-left: 12px;" />
1114
</ui:VisualElement>
1215
</ui:UXML>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace DreamCode.AutoKeystore.Editor.Configuration
2+
{
3+
public enum KeystoreRepository
4+
{
5+
EditorPrefs,
6+
OsEnvironment
7+
}
8+
}

Editor/Configuration/KeystoreRepository.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/KeystoreRepositoryFactory.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using DreamCode.AutoKeystore.Editor.Configuration;
3+
4+
namespace DreamCode.AutoKeystore.Editor
5+
{
6+
public sealed class KeystoreRepositoryFactory
7+
{
8+
public IKeystoreRepository Create(KeystoreRepository repository)
9+
{
10+
return repository switch
11+
{
12+
KeystoreRepository.EditorPrefs => new EditorPrefsRepository(),
13+
KeystoreRepository.OsEnvironment => new OsEnvironmentRepository(),
14+
_ => throw new NotImplementedException($"{nameof(KeystoreRepositoryFactory)}-{repository}")
15+
};
16+
}
17+
}
18+
}

Editor/KeystoreRepositoryFactory.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/KeystoreSettings.cs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using DreamCode.AutoKeystore.Editor.Configuration;
13
using UnityEditor;
24

35
namespace DreamCode.AutoKeystore.Editor
@@ -9,18 +11,32 @@ internal static class KeystoreSettings
911
internal static string AliasName { get; private set; }
1012
internal static string AliasPassword { get; private set; }
1113
private const string KeystoreExt = ".keystore";
14+
1215
private static readonly ICrypter _crypter = new TripleDESCrypter(nameof(KeystoreSettings));
16+
private static readonly KeystoreRepositoryFactory _factory = new();
17+
private static IKeystoreRepository _keystoreRepository;
18+
19+
public static void SetupRepository(KeystoreRepository repository)
20+
{
21+
_keystoreRepository = _factory.Create(repository);
22+
}
1323

1424
public static void Load()
1525
{
1626
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
1727
return;
18-
var projectKeystore = LoadProjectKeystore();
28+
if (_keystoreRepository == null)
29+
throw new InvalidOperationException(
30+
$"{nameof(KeystoreSettings)}-{nameof(_keystoreRepository)} is not configured");
31+
var projectKeystore = _keystoreRepository.LoadProjectKeystore(_crypter);
1932
if (string.IsNullOrEmpty(projectKeystore.name) || string.IsNullOrEmpty(projectKeystore.password))
2033
return;
21-
var projectKey = LoadProjectKey();
34+
projectKeystore.name += KeystoreExt;
35+
36+
var projectKey = _keystoreRepository.LoadProjectKey(_crypter);
2237
if (string.IsNullOrEmpty(projectKey.name) || string.IsNullOrEmpty(projectKey.password))
2338
return;
39+
2440
PlayerSettings.Android.keystoreName = Name = projectKeystore.name;
2541
PlayerSettings.Android.keystorePass = Password = projectKeystore.password;
2642
PlayerSettings.Android.keyaliasName = AliasName = projectKey.name;
@@ -31,48 +47,25 @@ public static void Save(string name, string password, string aliasName, string a
3147
{
3248
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
3349
return;
50+
if (_keystoreRepository == null)
51+
throw new InvalidOperationException(
52+
$"{nameof(KeystoreSettings)}-{nameof(_keystoreRepository)} is not configured");
3453
if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(aliasPassword))
3554
return;
3655
var encryptedPassword = _crypter.Encrypt(password);
3756
var encryptedAliasPassword = _crypter.Encrypt(aliasPassword);
38-
EditorPrefs.SetString($"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Name)}",
39-
name);
40-
EditorPrefs.SetString(
41-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}",
42-
encryptedPassword);
43-
EditorPrefs.SetString(
44-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasName)}", aliasName);
45-
EditorPrefs.SetString(
46-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasPassword)}",
47-
encryptedAliasPassword);
57+
58+
_keystoreRepository.Save(
59+
name,
60+
aliasName,
61+
encryptedPassword,
62+
encryptedAliasPassword
63+
);
64+
4865
PlayerSettings.Android.keystoreName = name + KeystoreExt;
4966
PlayerSettings.Android.keystorePass = password;
5067
PlayerSettings.Android.keyaliasName = aliasName;
5168
PlayerSettings.Android.keyaliasPass = aliasPassword;
5269
}
53-
54-
private static (string name, string password) LoadProjectKeystore()
55-
{
56-
var name = EditorPrefs.GetString(
57-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Name)}");
58-
name += KeystoreExt;
59-
var prefsPassword = EditorPrefs.GetString(
60-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}");
61-
var decryptedPassword = _crypter.Decrypt(prefsPassword);
62-
63-
return (name, decryptedPassword);
64-
}
65-
66-
private static (string name, string password) LoadProjectKey()
67-
{
68-
var name = EditorPrefs.GetString(
69-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasName)}");
70-
var prefsAliasPassword =
71-
EditorPrefs.GetString(
72-
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasPassword)}");
73-
var decryptedAliasPassword = _crypter.Decrypt(prefsAliasPassword);
74-
75-
return (name, decryptedAliasPassword);
76-
}
7770
}
7871
}

Editor/Repositories.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using UnityEditor;
2+
3+
namespace DreamCode.AutoKeystore.Editor
4+
{
5+
public sealed class EditorPrefsRepository : IKeystoreRepository
6+
{
7+
public (string name, string password) LoadProjectKeystore(ICrypter crypter)
8+
{
9+
var name = EditorPrefs.GetString(
10+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Name)}");
11+
var prefsPassword = EditorPrefs.GetString(
12+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Password)}");
13+
var decryptedPassword = crypter.Decrypt(prefsPassword);
14+
15+
return (name, decryptedPassword);
16+
}
17+
18+
public (string name, string password) LoadProjectKey(ICrypter crypter)
19+
{
20+
var name = EditorPrefs.GetString(
21+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasName)}");
22+
var prefsAliasPassword =
23+
EditorPrefs.GetString(
24+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasPassword)}");
25+
var decryptedAliasPassword = crypter.Decrypt(prefsAliasPassword);
26+
27+
return (name, decryptedAliasPassword);
28+
}
29+
30+
public void Save(string name, string aliasName, string encryptedPassword, string encryptedAliasPassword)
31+
{
32+
EditorPrefs.SetString(
33+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Name)}",
34+
name);
35+
EditorPrefs.SetString(
36+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Password)}",
37+
encryptedPassword);
38+
EditorPrefs.SetString(
39+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasName)}",
40+
aliasName);
41+
EditorPrefs.SetString(
42+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasPassword)}",
43+
encryptedAliasPassword);
44+
}
45+
}
46+
}

Editor/Repositories/EditorPrefsRepository.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace DreamCode.AutoKeystore.Editor
2+
{
3+
public interface IKeystoreRepository
4+
{
5+
(string name, string password) LoadProjectKeystore(ICrypter crypter);
6+
(string name, string password) LoadProjectKey(ICrypter crypter);
7+
void Save(string name, string aliasName, string encryptedPassword, string encryptedAliasPassword);
8+
}
9+
}

Editor/Repositories/IKeystoreRepository.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using UnityEditor;
3+
4+
namespace DreamCode.AutoKeystore.Editor
5+
{
6+
public sealed class OsEnvironmentRepository : IKeystoreRepository
7+
{
8+
public (string name, string password) LoadProjectKeystore(ICrypter crypter)
9+
{
10+
var name = Environment.GetEnvironmentVariable(
11+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Name)}");
12+
var prefsPassword = Environment.GetEnvironmentVariable(
13+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Password)}");
14+
var decryptedPassword = crypter.Decrypt(prefsPassword);
15+
16+
return (name, decryptedPassword);
17+
}
18+
19+
public (string name, string password) LoadProjectKey(ICrypter crypter)
20+
{
21+
var name = Environment.GetEnvironmentVariable(
22+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasName)}");
23+
var prefsAliasPassword = Environment.GetEnvironmentVariable(
24+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasPassword)}");
25+
var decryptedAliasPassword = crypter.Decrypt(prefsAliasPassword);
26+
27+
return (name, decryptedAliasPassword);
28+
}
29+
30+
public void Save(string name, string aliasName, string encryptedPassword, string encryptedAliasPassword)
31+
{
32+
Environment.SetEnvironmentVariable(
33+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Name)}",
34+
name
35+
);
36+
Environment.SetEnvironmentVariable(
37+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasName)}",
38+
aliasName
39+
);
40+
Environment.SetEnvironmentVariable(
41+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.Password)}",
42+
encryptedPassword
43+
);
44+
Environment.SetEnvironmentVariable(
45+
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(KeystoreSettings.AliasPassword)}",
46+
encryptedAliasPassword
47+
);
48+
}
49+
}
50+
}

Editor/Repositories/OsEnvironmentRepository.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)