Ejemplo n.º 1
0
 internal MercuryUser(MercuryUser user)
 {
     _connectionIds = new HashSet <string>(user._connectionIds);
     Authenticated  = user.Authenticated;
     _server        = user._server;
     UserIdentifier = user.UserIdentifier;
 }
Ejemplo n.º 2
0
        private async void OnConnectedInternal(string connectionId)
        {
            MercuryUser user;

            if (_requiresAuth)
            {
                TaskCompletionSource <string> _tcsAuthenticate = new TaskCompletionSource <string>();

                void OnCLientProvidedAuthentication(string connId, string accessToken)
                {
                    if (connId == connectionId)
                    {
                        ClientProvidedAuthentication -= OnCLientProvidedAuthentication;
                        _tcsAuthenticate.SetResult(connId);
                    }
                }

                ClientProvidedAuthentication += OnCLientProvidedAuthentication;
                await _mServer.SendAsync(connectionId, Globals.AuthSelf).ConfigureAwait(false);

                string token = await _tcsAuthenticate.Task;

                ClaimsPrincipal principal = await ValidateAccessToken(token, _defaultScopes).ConfigureAwait(false);

                if (principal != null)
                {
                    string userId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                    AddUserConnectionId(connectionId, userId);
                    user = User(userId);
                }
                else
                {
                    //User is not authorized, closed their connection:
                    await DisconnectClientAsync(connectionId, "AUTH_FAILED").ConfigureAwait(false);

                    return;
                }
            }
            else
            {
                user = new MercuryUser(null, this);
                user.AddConnectionId(connectionId);
            }


            await OnConnectedAsync(user, connectionId).ConfigureAwait(false);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Called whenever a new client connects
 /// </summary>
 /// <param name="connectionId">The unique id of the connected client</param>
 protected virtual Task OnConnectedAsync(MercuryUser user, string connectionId)
 {
     return(Task.CompletedTask);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Called whenever a client disconnects.
 /// </summary>
 /// <param name="connectionId">The unique Id of the new client</param>
 /// <param name="ex">The cause for disconnect, if graceful, returns null</param>
 protected virtual Task OnDisconnectedAsync(MercuryUser user, string connectionId, Exception ex)
 {
     return(Task.CompletedTask);
 }
Ejemplo n.º 5
0
        private async void OnDisconnectedInternal(string connectionId, Exception ex)
        {
            MercuryUser user = UserWithConnectionId(connectionId);

            await OnDisconnectedAsync(user, connectionId, ex);
        }