https and auth token rollover
This commit is contained in:
@@ -7,7 +7,8 @@ public static class Cupid
|
||||
{
|
||||
private static string serverAddress;
|
||||
public static string ServerAddress => serverAddress;
|
||||
public static string CupidURI => "http://" + serverAddress + ":" + port;
|
||||
private static string cupidBaseUri;
|
||||
public static string CupidURI => cupidBaseUri;
|
||||
|
||||
private static int port;
|
||||
public static int Port => port;
|
||||
@@ -24,10 +25,16 @@ public static class Cupid
|
||||
public static int RoomPort = -1;
|
||||
|
||||
|
||||
public static async Task Init(string _serverAddress, int _port, string _bearerToken)
|
||||
public static async Task Init(string _serverAddress, int _port, string _bearerToken, string _settingsPassword)
|
||||
{
|
||||
serverAddress = _serverAddress;
|
||||
port = _port;
|
||||
if (!TryBuildEndpoints(_serverAddress, _port, out serverAddress, out cupidBaseUri, out port))
|
||||
{
|
||||
Logger.Log("Cupid: invalid or empty server address.");
|
||||
bearerToken = _bearerToken ?? "";
|
||||
settings = null;
|
||||
return;
|
||||
}
|
||||
|
||||
bearerToken = _bearerToken ?? "";
|
||||
|
||||
if (string.IsNullOrEmpty(bearerToken))
|
||||
@@ -37,7 +44,9 @@ public static class Cupid
|
||||
return;
|
||||
}
|
||||
|
||||
using (UnityWebRequest www = UnityWebRequest.Get(CupidURI + "/settings"))
|
||||
string settingsPassword = _settingsPassword ?? "";
|
||||
string settingsUrl = CupidURI + "/settings?password=" + UnityWebRequest.EscapeURL(settingsPassword);
|
||||
using (UnityWebRequest www = UnityWebRequest.Get(settingsUrl))
|
||||
{
|
||||
www.SetRequestHeader("Authorization", "Bearer " + bearerToken);
|
||||
var operation = www.SendWebRequest();
|
||||
@@ -63,6 +72,13 @@ public static class Cupid
|
||||
return;
|
||||
}
|
||||
|
||||
if (body.Trim() == "403 Unauthorized")
|
||||
{
|
||||
Logger.Log("Cupid settings: wrong or missing password query (server rejected /settings).");
|
||||
settings = null;
|
||||
return;
|
||||
}
|
||||
|
||||
settings = JsonUtility.FromJson<CupidSettings>(body);
|
||||
if (settings == null) { throw new NullReferenceException(); }
|
||||
Logger.Log("Cupid init success");
|
||||
@@ -76,6 +92,40 @@ public static class Cupid
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Plain host/IP + HTTP port, or an absolute URL (https://host with no port uses 443).
|
||||
/// Sets <paramref name="hostOnly"/> for game client transport; <paramref name="baseUri"/> has no trailing slash.
|
||||
/// </summary>
|
||||
static bool TryBuildEndpoints(string input, int fallbackHttpPort, out string hostOnly, out string baseUri, out int resolvedPort)
|
||||
{
|
||||
hostOnly = "";
|
||||
baseUri = "";
|
||||
resolvedPort = fallbackHttpPort;
|
||||
input = (input ?? "").Trim();
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return false;
|
||||
|
||||
if (input.IndexOf("://", StringComparison.Ordinal) >= 0
|
||||
&& Uri.TryCreate(input, UriKind.Absolute, out Uri uri)
|
||||
&& (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
|
||||
{
|
||||
hostOnly = uri.Host;
|
||||
string authority = uri.GetLeftPart(UriPartial.Authority);
|
||||
string path = uri.AbsolutePath;
|
||||
if (!string.IsNullOrEmpty(path) && path != "/")
|
||||
baseUri = authority.TrimEnd('/') + path.TrimEnd('/');
|
||||
else
|
||||
baseUri = authority;
|
||||
resolvedPort = uri.Port;
|
||||
return true;
|
||||
}
|
||||
|
||||
hostOnly = input;
|
||||
baseUri = "http://" + input + ":" + fallbackHttpPort;
|
||||
resolvedPort = fallbackHttpPort;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static string RandomUsername{
|
||||
get{
|
||||
|
||||
@@ -15,8 +15,12 @@ public class CupidLobby : MonoBehaviour
|
||||
[SerializeField]private GameObject MatchmakingUI;
|
||||
[SerializeField]private string GameScene;
|
||||
[Header("Server info (matchmaker port should match server settings.json)")]
|
||||
[Tooltip("IP/hostname uses HTTP and cupidPort below. Or full URL: https://match.example.com (default 443, no :port in URL) or http://host/path.")]
|
||||
[SerializeField]private string serverAddress = "xx.xx.xxx.xx";
|
||||
[Tooltip("HTTP port when server address is not a full URL. Ignored for https:// or http:// URLs unless the URL includes an explicit port.")]
|
||||
[SerializeField]private int cupidPort = 2612;
|
||||
[Tooltip("Sent as the password query param on GET /settings (matchmaker settings.json).")]
|
||||
[SerializeField]private string cupidPassword = "HelloWorld";
|
||||
[Tooltip("Used when LoginManager is absent or has no token (e.g. Cupid sample scene).")]
|
||||
[SerializeField]private string bearerTokenFallback = "";
|
||||
|
||||
@@ -59,7 +63,7 @@ public class CupidLobby : MonoBehaviour
|
||||
: bearerTokenFallback;
|
||||
if (LoginManager.instance != null && LoginManager.CurrentUser != null && !string.IsNullOrEmpty(LoginManager.CurrentUser.username))
|
||||
m_username = LoginManager.CurrentUser.username;
|
||||
Cupid.Init(serverAddress, cupidPort, token);
|
||||
Cupid.Init(serverAddress, cupidPort, token, cupidPassword);
|
||||
}
|
||||
bool matchmaking = false;
|
||||
public void Matchmake()
|
||||
|
||||
Reference in New Issue
Block a user