Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from seker212/S/Some-random-fixes
Browse files Browse the repository at this point in the history
S/some random fixes
  • Loading branch information
Dzemoro authored Jan 25, 2022
2 parents 6733880 + 20218db commit 68cc681
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
10 changes: 5 additions & 5 deletions ComeX.Lib.Auth/ConnectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ internal ConnectionCache()
_cacheDict = new ConcurrentDictionary<string, TokenData>();
}

internal bool TryAdd(string connectionId, TokenData tokenData)
internal bool TryAdd(string token, TokenData tokenData)
{
if (_cacheDict.Values.Contains(tokenData))
{
var existingTokenData = _cacheDict.First(x => x.Value.Equals(tokenData)).Value;
existingTokenData.AddConnectionId(connectionId);
return _cacheDict.TryAdd(connectionId, existingTokenData);
existingTokenData.AddConnectionId(token);
return _cacheDict.TryAdd(token, existingTokenData);
}
else
{
if (tokenData.ConnectionIds.Count == 0)
tokenData.AddConnectionId(connectionId);
return _cacheDict.TryAdd(connectionId, tokenData);
tokenData.AddConnectionId(token);
return _cacheDict.TryAdd(token, tokenData);
}
}

Expand Down
10 changes: 6 additions & 4 deletions ComeX.Lib.Auth/ILoginManager.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
namespace ComeX.Lib.Auth
using System.Threading.Tasks;

namespace ComeX.Lib.Auth
{
public interface ILoginManager
{
/// <summary>
/// Tries to login user based on <paramref name="token"/> and assigns a <paramref name="connectionId"/> as a key to the <see cref="IConnectionCache"/> and <see cref="TokenData"/> as a value.
/// Tries to login user based on <paramref name="token"/>.
/// </summary>
/// <param name="connectionId">Connection Id used to send <paramref name="token"/>.</param>
/// <param name="token">Token value.</param>
/// <exception cref="InvalidCredentialsException">Invalid token.</exception>
void Login(string connectionId, string token);

/// <summary>
/// Logs out a connection. Essentially removes <paramref name="connectionId"/> from <see cref="IConnectionCache"/>.
/// Logs out a connection.
/// </summary>
/// <remarks>
/// If a user uses more than one connection only the given connection will be removed.
/// </remarks>
/// <param name="connectionId">Id of the terminated connection.</param>
void Logout(string connectionId);
Task Logout(string connectionId);
}
}
56 changes: 36 additions & 20 deletions ComeX.Lib.Auth/LoginManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using ComeX.Lib.Common.Helpers;
using ComeX.Lib.Common.UserDatabaseAPI;
using System;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace ComeX.Lib.Auth
{
Expand All @@ -19,33 +21,47 @@ internal LoginManager(IUserApiManager userApiManager, ConnectionCache connection

public void Login(string connectionId, string token)
{
string tokenHash;
TokenDataModel tokenReceived;
using (var hashingHelper = new HashingHelper(SHA512.Create()))
if (_connectionCache.ContainsKey(token))
{
tokenHash = hashingHelper.GenerateHash(token);
_connectionCache[token].AddConnectionId(connectionId);
}
try
{
tokenReceived = _userApiManager.GetToken(tokenHash);
}
catch (UserApiException ex) when (ex.ResponseStatusCode.HasValue && ex.ResponseStatusCode.Value == HttpStatusCode.Unauthorized)
{
throw new InvalidCredentialsException("Invalid token.");
}
var tokenData = new TokenData(tokenHash, tokenReceived.UserId, tokenReceived.Username, DateTime.ParseExact(tokenReceived.ValidTo, "MM/dd/yyyy HH:mm:ss", null), connectionId);
if (tokenData.IsValid())
else
{
if (!_connectionCache.TryAdd(token, tokenData))
throw new InvalidCredentialsException("Could not add token to connection cache.");
string tokenHash;
TokenDataModel tokenReceived;
using (var hashingHelper = new HashingHelper(SHA512.Create()))
{
tokenHash = hashingHelper.GenerateHash(token);
}
try
{
tokenReceived = _userApiManager.GetToken(tokenHash);
}
catch (UserApiException ex) when (ex.ResponseStatusCode.HasValue && ex.ResponseStatusCode.Value == HttpStatusCode.Unauthorized)
{
throw new InvalidCredentialsException("Invalid token.");
}
var tokenData = new TokenData(tokenHash, tokenReceived.UserId, tokenReceived.Username, DateTime.SpecifyKind(DateTime.ParseExact(tokenReceived.ValidTo, "MM/dd/yyyy HH:mm:ss", null), DateTimeKind.Utc).ToLocalTime(), connectionId);
if (tokenData.IsValid())
{
if (!_connectionCache.TryAdd(token, tokenData))
throw new InvalidCredentialsException("Could not add token to connection cache.");
}
else
throw new InvalidCredentialsException("Invalid token.");
}
else
throw new InvalidCredentialsException("Invalid token.");
}

public void Logout(string connectionId)
public Task Logout(string connectionId)
{
_connectionCache.TryRemove(connectionId);
return Task.Run(() =>
{
var cacheEntry = _connectionCache.Single(x => x.Value.ConnectionIds.Contains(connectionId));
if (cacheEntry.Value.ConnectionIds.Count == 1)
_connectionCache.TryRemove(cacheEntry.Key);
else
cacheEntry.Value.RemoveConnectionId(connectionId);
});
}
}
}
6 changes: 6 additions & 0 deletions ComeX.Server/Hubs/ComeXHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,5 +718,11 @@ public async Task AddReaction(ReactionMessage msg)
await Clients.Caller.SendAsync("Not_allowed");
}
}

public override Task OnDisconnectedAsync(Exception exception)
{
_loginManager.Logout(Context.ConnectionId);
return base.OnDisconnectedAsync(exception);
}
}
}

0 comments on commit 68cc681

Please sign in to comment.