-
Notifications
You must be signed in to change notification settings - Fork 21
[sub pr: #236] Flow revisions #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
craftxbox
wants to merge
1
commit into
unitystation:Hub-Login-revert
Choose a base branch
from
craftxbox:Hub-Login-revert
base: Hub-Login-revert
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace UnitystationLauncher.Models.Api; | ||
|
|
||
| public class ScopeTokenResponse : JsonObject | ||
| { | ||
| [JsonPropertyName("scope_token")] | ||
| public string ScopeToken { get; set; } = string.Empty; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions
21
UnitystationLauncher/Models/Api/ServerConnectionAuthenticationRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| namespace UnitystationLauncher.Models.Api; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace UnitystationLauncher.Models.Api; | ||
|
|
||
| public class ServerConnectionAuthenticationRequest | ||
| { | ||
| public string? ClientFork { get; set; } | ||
|
|
||
| public string? ClientVersion { get; set; } | ||
|
|
||
| public string? GoodFileVersion { get; set; } | ||
| public string? EncryptedSharedSecret { get; set; } | ||
| public string? EncryptedAccountID { get; set; } | ||
|
|
||
| public string? ConnectionPublicServerKey { get; set; } | ||
| [JsonPropertyName("shared_secret")] | ||
| public string SharedSecret { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("unique_identifier")] | ||
| public string UniqueIdentifier { get; set; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("auth_realm")] | ||
| public string? AuthRealm { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| using Org.BouncyCastle.Crypto.Engines; | ||
| using Org.BouncyCastle.Crypto.Parameters; | ||
| using Org.BouncyCastle.Math; | ||
| using Org.BouncyCastle.Math.EC; | ||
| using Org.BouncyCastle.OpenSsl; | ||
| using UnitystationLauncher.Models; | ||
| using UnitystationLauncher.Models.Api; | ||
|
|
@@ -22,9 +23,9 @@ namespace UnitystationLauncher.Services; | |
|
|
||
| public interface IServerAuthenticationService | ||
| { | ||
| public Task<Server> GetServerInfoByIP(string IP); | ||
| public Task<Server> QueryServerInfo(string IP); | ||
|
|
||
| public Task<Dictionary<string, string>> AuthenticateWithServer(string IP, Installation Installation); | ||
| public Task<Dictionary<string, string>> PrenegotiateWithServer(string IP, Installation Installation); | ||
| } | ||
|
|
||
| public class ServerAuthenticationService : IServerAuthenticationService | ||
|
|
@@ -38,13 +39,15 @@ public ServerAuthenticationService(AuthService AuthService) | |
| private readonly AuthService _AuthService; | ||
| private readonly HttpClient _httpClient = new HttpClient(); | ||
|
|
||
| private readonly SHA512 SHA512 = SHA512.Create(); | ||
|
|
||
| public async Task<Server> GetServerInfoByIP(string IP) | ||
| public async Task<Server> QueryServerInfo(string IP) | ||
| { | ||
| try | ||
| { | ||
| string Port = "7778"; | ||
| // check if IP is in the format "IP:Port" | ||
| // if not, assume default port 7778 | ||
| string[] parts = IP.Split(':'); | ||
| string Port = parts.Length == 2 ? parts[1] : "7778"; | ||
|
|
||
| string url = $"http://{IP}:{Port}/"; | ||
|
|
||
| var response = await _httpClient.GetAsync(url); | ||
|
|
@@ -72,50 +75,48 @@ static AsymmetricKeyParameter ImportKeyFromPem(string pem) | |
| } | ||
|
|
||
|
|
||
| public async Task<Dictionary<string, string>> AuthenticateWithServer(string IP, Installation Installation) | ||
| public async Task<Dictionary<string, string>> PrenegotiateWithServer(string IP, Installation Installation) | ||
| { | ||
| var Info = await GetServerInfoByIP(IP); | ||
| var RSAEncrypt = new OaepEncoding( | ||
| new RsaEngine(), | ||
| var Info = await QueryServerInfo(IP); | ||
| var CryptoEncoding = new OaepEncoding( | ||
| new ElGamalEngine(), | ||
| new Sha256Digest() | ||
| ); | ||
|
|
||
| RSAEncrypt.Init(true, ImportKeyFromPem(Info.ServerPublicKey)); // false = decrypt mode | ||
| // wrap the base64 encoded public key in PEM format | ||
| var ServerPublicKey = ImportKeyFromPem("-----BEGIN PUBLIC KEY-----\n" + | ||
| Info.ServerPublicKey + "\n" + | ||
| "-----END PUBLIC KEY-----\n"); | ||
|
|
||
| CryptoEncoding.Init(true, ServerPublicKey); // false = decrypt mode | ||
|
|
||
| byte[] sharedSecret = new byte[32]; // 256-bit key | ||
| RandomNumberGenerator.Fill(sharedSecret); | ||
|
|
||
| // Optional: convert to Base64 if you want to transmit/store it | ||
| string base64Secret = Convert.ToBase64String(sharedSecret); | ||
| var hashInput = new byte[64]; | ||
| Array.Copy(sharedSecret, 0, hashInput, 0, 32); | ||
| Array.Copy(Convert.FromBase64String(Info.ServerPublicKey), 0, hashInput, 32, 32); | ||
|
|
||
| var SHA512Check = Convert.ToBase64String( | ||
| SHA512.ComputeHash( | ||
| Encoding.UTF8.GetBytes( | ||
| base64Secret | ||
| + Info.ServerPublicKey | ||
| + Installation.BuildVersion.ToString() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to send all this crap as part of the token since the malicious server can spoof these to be something else |
||
| + Installation.ForkName.ToString() | ||
| + Installation.GoodFileVersion.ToString() | ||
| + Info.ServerConnectionPublicKey))); | ||
| _AuthService.RegisterJoiningServerWithSecret(SHA512Check); | ||
| var ConnectionChallenge = Convert.ToHexString(SHA512.HashData(hashInput)); | ||
|
|
||
| var ScopeToken = await _AuthService.RegisterConnectionChallenge(ConnectionChallenge, Installation.ForkName); | ||
|
|
||
|
|
||
| var ToSend = new ServerConnectionAuthenticationRequest | ||
| { | ||
| ConnectionPublicServerKey = Info.ServerConnectionPublicKey, | ||
| ClientVersion = Installation.BuildVersion.ToString(), | ||
| GoodFileVersion = Installation.GoodFileVersion.ToString(), | ||
| ClientFork = Installation.ForkName.ToString(), | ||
| EncryptedSharedSecret = EncryptString(RSAEncrypt, base64Secret), | ||
| EncryptedAccountID = EncryptString(RSAEncrypt, _AuthService.AccountLoginResponse.Account.UniqueIdentifier) | ||
| SharedSecret = Convert.ToBase64String(sharedSecret), | ||
| UniqueIdentifier = _AuthService.AccountLoginResponse.Account.UniqueIdentifier, | ||
| AuthRealm = _AuthService | ||
| }; | ||
|
|
||
|
|
||
| // Serialize the object to JSON | ||
| string json = JsonConvert.SerializeObject(ToSend); | ||
|
|
||
| // Wrap it in a StringContent with JSON media type | ||
| var content = new StringContent(json, Encoding.UTF8, "application/json"); | ||
| var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); | ||
|
|
||
| //TODO make this into a function like QueryServerIP | ||
|
|
||
| string Port = "7778"; | ||
| string url = $"http://{IP}:{Port}/"; | ||
|
|
@@ -128,17 +129,19 @@ public async Task<Dictionary<string, string>> AuthenticateWithServer(string IP, | |
| { | ||
| throw new AuthenticationException(contentBack + $" When trying to authenticate with {url}"); | ||
| } | ||
| //end of todo | ||
|
|
||
| return new Dictionary<string, string> | ||
| { | ||
| {"-SharedSecret", base64Secret}, | ||
| {"-ServerPublicConnectionKey", Info.ServerConnectionPublicKey}, | ||
| {"-ServerPublicConnectionKey", Info.DEPRECATEME_ServerConnectionPublicKey}, | ||
| {"-ScopedToken", ScopeToken.ScopeToken} | ||
| }; | ||
| } | ||
|
|
||
| private string EncryptString(OaepEncoding rsa, string ToEncrypt) | ||
| private string EncryptString(OaepEncoding encoding, string ToEncrypt) | ||
| { | ||
| var Bytes = Encoding.UTF8.GetBytes(ToEncrypt); | ||
| return Convert.ToBase64String(rsa.ProcessBlock(Bytes, 0, Bytes.Length)); | ||
| return Convert.ToBase64String(encoding.ProcessBlock(Bytes, 0, Bytes.Length)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for launching the client directly if you're hosting all playing single player so you can modify your character settings In that scenario