Skip to content

Commit 20fce1b

Browse files
committed
Introduce GameSwiftConfig and client auth secret
1 parent 8b3ef34 commit 20fce1b

10 files changed

+122
-101
lines changed

Core/GameSwiftConfig.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using UnityEngine;
2+
using System.IO;
3+
#if UNITY_EDITOR
4+
using UnityEditor;
5+
#endif
6+
7+
namespace GameSwiftSDK.Core
8+
{
9+
/// <summary>
10+
/// Multiple account login attempts blocker configuration file.
11+
/// </summary>
12+
public class GameSwiftConfig : ScriptableObject
13+
{
14+
private const string DIRECTORY_IN_RESOURCES = "GameSwiftSDK";
15+
private const string CONFIG_FILE_NAME = "GameSwiftConfig";
16+
17+
/// <summary>
18+
/// Mandatory API authentication secret, distributed by GameSwift. Is is unique for every game using the SDK.
19+
/// </summary>
20+
[field: SerializeField]
21+
private string ClientAuthenticationSecret { get; set; }
22+
23+
/// <summary>
24+
/// Turns on/off multiple account login attempts blocker.
25+
/// </summary>
26+
[field: SerializeField]
27+
public bool CheckMultipleLoginAttempts { get; private set; } = true;
28+
29+
/// <summary>
30+
/// Time rate, measured in seconds, at which "GET /api/{idVersion}/oauth/me" call will be made to maintain multiple account login attempts lock.
31+
/// </summary>
32+
[field: SerializeField, Range(5, 500)]
33+
public int BlockerHeartbeatRate { get; private set; } = 60;
34+
35+
private static GameSwiftConfig _instance;
36+
37+
public string GetClientAuthenticationSecret()
38+
{
39+
if (string.IsNullOrEmpty(ClientAuthenticationSecret))
40+
{
41+
Debug.LogError($"{nameof(ClientAuthenticationSecret)} is not filled! All sdk calls will fail!");
42+
}
43+
44+
return ClientAuthenticationSecret;
45+
}
46+
47+
public static GameSwiftConfig Instance
48+
{
49+
get
50+
{
51+
if (_instance == null)
52+
{
53+
var pathInResources = Path.Combine(DIRECTORY_IN_RESOURCES, CONFIG_FILE_NAME);
54+
_instance = Resources.Load<GameSwiftConfig>(pathInResources);
55+
}
56+
57+
return _instance;
58+
}
59+
}
60+
61+
#if UNITY_EDITOR
62+
[InitializeOnLoadMethod]
63+
private static void AttachAssetCheckToEditorUpdate()
64+
{
65+
EditorApplication.update += InitializeMultipleLoginsBlockerData;
66+
}
67+
68+
private static void InitializeMultipleLoginsBlockerData()
69+
{
70+
var isRefreshing = EditorApplication.isCompiling || EditorApplication.isUpdating;
71+
if (isRefreshing == false)
72+
{
73+
74+
EditorApplication.update -= InitializeMultipleLoginsBlockerData;
75+
if (Instance == null)
76+
{
77+
var createdConfig = CreateInstance<GameSwiftConfig>();
78+
79+
var relativeResourcesPath = Path.Combine("Resources", DIRECTORY_IN_RESOURCES);
80+
Directory.CreateDirectory(Path.Combine(Application.dataPath, relativeResourcesPath));
81+
82+
var resourcesPath = Path.Combine("Assets", relativeResourcesPath);
83+
var createdConfigPath = Path.Combine(resourcesPath, $"{CONFIG_FILE_NAME}.asset");
84+
AssetDatabase.CreateAsset(createdConfig, createdConfigPath);
85+
86+
var oldAsset = Path.Combine(resourcesPath, "MultipleLoginsBlockerData.asset");
87+
AssetDatabase.DeleteAsset(oldAsset);
88+
89+
AssetDatabase.SaveAssets();
90+
Debug.Log($"Config file created successfully at {createdConfigPath}.");
91+
}
92+
}
93+
}
94+
#endif
95+
}
96+
}

Core/GameSwiftSdkCore.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace GameSwiftSDK.Core
1212
/// </summary>
1313
public class GameSwiftSdkCore : MonoBehaviour
1414
{
15+
public const string GS_AUTH_HEADER = "gs-authentication";
16+
1517
private static GameSwiftSdkCore _instance;
1618

1719
/// <summary>
@@ -23,14 +25,20 @@ public static GameSwiftSdkCore Instance
2325
{
2426
if (_instance == null)
2527
{
26-
var go = new GameObject("GameSwiftSDKCore");
27-
_instance = go.AddComponent<GameSwiftSdkCore>();
28-
DontDestroyOnLoad(go);
28+
Instantiate();
2929
}
3030

3131
return _instance;
3232
}
3333
}
34+
35+
[RuntimeInitializeOnLoadMethod]
36+
private static void Instantiate()
37+
{
38+
var go = new GameObject("GameSwiftSDKCore");
39+
_instance = go.AddComponent<GameSwiftSdkCore>();
40+
DontDestroyOnLoad(go);
41+
}
3442

3543
/// <summary>
3644
/// Sends a GET request.
@@ -100,6 +108,8 @@ private static void AddHeaders (UnityWebRequest request, RequestData data)
100108
{
101109
request.SetRequestHeader(header.Key, header.Value);
102110
}
111+
112+
request.SetRequestHeader(GS_AUTH_HEADER, GameSwiftConfig.Instance.GetClientAuthenticationSecret());
103113
}
104114

105115
/// <summary>

ID/GameSwiftSdkId.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using GameSwiftSDK.Id.LoginBlock;
1+
using GameSwiftSDK.Core;
22
using UnityEngine;
33

44
namespace GameSwiftSDK.Id
@@ -49,13 +49,15 @@ public static GameSwiftSdkId Instance
4949

5050
private bool _accessTokenRetrieved;
5151

52+
[RuntimeInitializeOnLoadMethod]
5253
private static void Instantiate ()
5354
{
54-
_instance = Core.GameSwiftSdkCore.Instance.gameObject.AddComponent<GameSwiftSdkId>();
55-
56-
if (MultipleLoginsBlockerData.Instance.CheckMultipleLoginAttempts)
55+
var parentObject = GameSwiftSdkCore.Instance.gameObject;
56+
_instance = parentObject.AddComponent<GameSwiftSdkId>();
57+
58+
if (GameSwiftConfig.Instance.CheckMultipleLoginAttempts)
5759
{
58-
Instance.MultipleLoginsBlocker = Instance.gameObject.AddComponent<MultipleLoginsBlocker>();
60+
Instance.MultipleLoginsBlocker = parentObject.AddComponent<MultipleLoginsBlocker>();
5961
}
6062
}
6163
}

ID/LoginBlock.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

ID/LoginBlock/MultipleLoginsBlockerData.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

ID/LoginBlock/MultipleLoginsBlocker.cs renamed to ID/MultipleLoginsBlocker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using GameSwiftSDK.Id.Responses;
55
using UnityEngine;
66

7-
namespace GameSwiftSDK.Id.LoginBlock
7+
namespace GameSwiftSDK.Id
88
{
99
/// <summary>
1010
/// Component used to prevent multiple account login attempts. If multiple logins blocker is turned on this component
@@ -34,7 +34,7 @@ private IEnumerator CheckForMultipleLoginAttempts ()
3434
{
3535
for (;;)
3636
{
37-
yield return new WaitForSeconds(MultipleLoginsBlockerData.Instance.BlockerHeartbeatRate);
37+
yield return new WaitForSeconds(GameSwiftConfig.Instance.BlockerHeartbeatRate);
3838
SendBlockerHeartbeat();
3939
}
4040
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.gameswift.sdk",
3-
"version": "1.1.2",
3+
"version": "2.0.0",
44
"displayName": "GameSwiftSDK",
55
"description": "Unity package making it easy to connect with GameSwift ecosystem via GameSwift ID.",
66
"documentationUrl": "https://docs.gameswift.io/gameswift-products/sdk",

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ In order to integrate your Unity game with GameSwift gaming ecosystem, [import](
88
https://github.com/GameSwift/gameswift-unity-sdk.git
99
```
1010

11+
After successful import, fill `ClientAuthenticationSecret` field in `Assets/Resources/GameSwiftSDK/GameSwiftConfig.asset`. It is a mandatory api authentication token, which is unique for every game using the SDK. These secrets are [distributed by GameSwift](#contact-us).
12+
1113
You can handle GameSwift login in 2 ways: [with launcher](#logging-in-from-launcher) or [without launcher](#logging-in-without-launcher). You can download GameSwift launcher [here](https://launcher.gameswift.io/).
1214
As long as your game targets Windows or MacOS, we strongly recommend to use data passed from our launcher. By doing so, you won't need to implement any login screen for your game as launcher already handles user credentials in a secure way.
1315
If you are building for mobile or web, you will need to create a login screen and implement connection with GameSwift backend manually.
@@ -122,7 +124,7 @@ Though we highly recommend using the above method, if you want to implement some
122124

123125
### Multiple Logins Blocker
124126
You need to have your client set up to block multiple login attempts for this component to work.
125-
To configure `Multiple Logins Blocker` you need to edit `MultipleLoginsBlockerData.asset` scriptable object which should be automatically created on Unity asset refresh in the `Assets/Resources/GameSwiftSDK/` directory.
127+
To configure `Multiple Logins Blocker` you need to edit `GameSwiftConfig.asset` scriptable object which should be automatically created with package import in the `Assets/Resources/GameSwiftSDK/` directory.
126128
When SDK instance in created this component will start working automatically in the background (if is turned on in the config file). Every specified number of seconds it will be sending hearbeats do the server to keep the lock.
127129
If you don't use our recommended login approaches, remember to call `GameSwiftSdkId.GetOauthUserInformation` method as the last step of login process. This will be your first sent heartbeat and will initialize the process.
128130

0 commit comments

Comments
 (0)