Skip to content

Commit

Permalink
feat: Introduce SignOut method to ElympicsLobbyClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Sygocki committed May 1, 2023
1 parent 3a29f97 commit ff3bdd0
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 125 deletions.
27 changes: 27 additions & 0 deletions Runtime/Communication/Authentication/Models/AuthData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Elympics.Models.Authentication
{
public class AuthData
{
public Guid UserId { get; }
public string JwtToken { get; }
public AuthType AuthType { get; }

internal string BearerAuthorization => $"Bearer {JwtToken}";

public AuthData(Guid userId, string jwtToken, AuthType authType = AuthType.None)
{
UserId = userId;
JwtToken = jwtToken;
AuthType = authType;
}

public AuthData(AuthenticationDataResponse response, AuthType authType = AuthType.None)
{
UserId = new Guid(response.userId);
JwtToken = response.jwtToken;
AuthType = authType;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Communication/Authentication/Models/AuthData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Communication/Authentication/Models/AuthType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Elympics.Models.Authentication
[Serializable]
public enum AuthType
{
Unknown = 0,
None = 0,
ClientSecret = 1,
EthAddress = 2,
}
Expand Down
29 changes: 10 additions & 19 deletions Runtime/Communication/Authentication/Models/AuthenticationData.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
using System;
using Elympics.Models.Authentication;

namespace Elympics.Models.Authentication
namespace Elympics
{
// TODO: rename to shorter AuthData (but uhh backwards compatibility) ~dsygocki 2023-04-19
public class AuthenticationData
// TODO: in the process of renaming to shorter AuthData (backwards compatibility) ~dsygocki 2023-04-28
public class AuthenticationData : AuthData
{
public Guid UserId { get; }
public string JwtToken { get; }

internal string BearerAuthorization => $"Bearer {JwtToken}";

public AuthenticationData(Guid userId, string jwtToken)
{
UserId = userId;
JwtToken = jwtToken;
}

public AuthenticationData(AuthenticationDataResponse response)
{
UserId = new Guid(response.userId);
JwtToken = response.jwtToken;
}
public AuthenticationData(AuthData authData)
: base(authData.UserId, authData.JwtToken, authData.AuthType) { }
public AuthenticationData(Guid userId, string jwtToken, AuthType authType = AuthType.None)
: base(userId, jwtToken, authType) { }
public AuthenticationData(AuthenticationDataResponse response, AuthType authType = AuthType.None)
: base(response, authType) { }
}
}
4 changes: 2 additions & 2 deletions Runtime/Communication/IAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Elympics
{
internal interface IAuthClient
{
void AuthenticateWithClientSecret(string clientSecret, Action<Result<AuthenticationData, string>> onResult, CancellationToken ct = default);
void AuthenticateWithEthAddress(IEthSigner ethSigner, Action<Result<AuthenticationData, string>> onResult, CancellationToken ct = default);
void AuthenticateWithClientSecret(string clientSecret, Action<Result<AuthData, string>> onResult, CancellationToken ct = default);
void AuthenticateWithEthAddress(IEthSigner ethSigner, Action<Result<AuthData, string>> onResult, CancellationToken ct = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal LongPollingMatchmakerClient(string baseUrl)
_getMatchLongPollingUrl = uriBuilder.Uri.ToString();
}

internal override void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthenticationData authData, CancellationToken ct = default)
internal override void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthData authData, CancellationToken ct = default)
{
var gameId = joinMatchmakerData.GameId;
var gameVersion = joinMatchmakerData.GameVersion;
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Communication/Matchmaker/MatchmakerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Elympics
{
internal abstract class MatchmakerClient : IMatchmakerEvents
{
internal abstract void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthenticationData authData, CancellationToken ct = default);
internal abstract void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthData authData, CancellationToken ct = default);

#region Matchmaking event emitters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void SetUp(string jwtToken, CancellationToken ct)
_ws.Connect();
}

internal override void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthenticationData authData, CancellationToken ct = default)
internal override void JoinMatchmakerAsync(JoinMatchmakerData joinMatchmakerData, AuthData authData, CancellationToken ct = default)
{
if (_ws != null)
CancelConnection();
Expand Down
20 changes: 10 additions & 10 deletions Runtime/Communication/Remote/RemoteAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ internal RemoteAuthClient()
_ethAddressAuthUrl = uriBuilder.Uri.ToString();
}

public void AuthenticateWithClientSecret(string clientSecret, Action<Result<AuthenticationData, string>> onResult, CancellationToken ct = default)
public void AuthenticateWithClientSecret(string clientSecret, Action<Result<AuthData, string>> onResult, CancellationToken ct = default)
{
void OnResponse(Result<AuthenticationDataResponse, Exception> result)
{
onResult(result.IsSuccess
? Result<AuthenticationData, string>.Success(new AuthenticationData(result.Value))
: Result<AuthenticationData, string>.Failure(result.Error.Message));
? Result<AuthData, string>.Success(new AuthData(result.Value, AuthType.ClientSecret))
: Result<AuthData, string>.Failure(result.Error.Message));
}

var requestModel = new ClientSecretAuthRequest { clientSecret = clientSecret };
ElympicsWebClient.SendJsonPutRequest<AuthenticationDataResponse>(_clientSecretAuthUrl, requestModel, callback: OnResponse, ct: ct);
}

public async void AuthenticateWithEthAddress(IEthSigner ethSigner, Action<Result<AuthenticationData, string>> onResult, CancellationToken ct = default)
public async void AuthenticateWithEthAddress(IEthSigner ethSigner, Action<Result<AuthData, string>> onResult, CancellationToken ct = default)
{
var ethAddress = await ethSigner.ProvideAddressAsync(ct: ct);
if (ethAddress == null)
{
onResult(Result<AuthenticationData, string>.Failure(
onResult(Result<AuthData, string>.Failure(
$"Address provided by {nameof(IEthSigner)}.{nameof(IEthSigner.ProvideAddressAsync)} cannot be null"));
return;
}
Expand All @@ -63,7 +63,7 @@ async void OnNonceResponse(Result<string, Exception> result)
{
if (result.IsFailure)
{
onResult(Result<AuthenticationData, string>.Failure(result.Error.Message));
onResult(Result<AuthData, string>.Failure(result.Error.Message));
return;
}

Expand All @@ -76,13 +76,13 @@ async void OnNonceResponse(Result<string, Exception> result)
}
catch (Exception e)
{
onResult(Result<AuthenticationData, string>.Failure(e.ToString()));
onResult(Result<AuthData, string>.Failure(e.ToString()));
return;
}

if (signature == null)
{
onResult(Result<AuthenticationData, string>.Failure(
onResult(Result<AuthData, string>.Failure(
$"Signature provided by {nameof(IEthSigner)}.{nameof(IEthSigner.SignAsync)} cannot be null"));
return;
}
Expand All @@ -102,8 +102,8 @@ async void OnNonceResponse(Result<string, Exception> result)
void OnAuthResponse(Result<AuthenticationDataResponse, Exception> result)
{
onResult(result.IsSuccess
? Result<AuthenticationData, string>.Success(new AuthenticationData(result.Value))
: Result<AuthenticationData, string>.Failure(result.Error.Message));
? Result<AuthData, string>.Success(new AuthData(result.Value, AuthType.EthAddress))
: Result<AuthData, string>.Failure(result.Error.Message));
}
}

Expand Down

0 comments on commit ff3bdd0

Please sign in to comment.