예제 #1
0
        private void HandleAuthResponseMessage(object source, MessageEventArgs e)
        {
            AuthenticatedMessage authenticated = JsonConvert.DeserializeObject <AuthenticatedMessage>(e.Data.ToString());

            if (authenticated.MessageType != MessageTypeNames.Authenticated)
            {
                ResetSavedSession();
                return;
            }

            if (authenticated.Status != AuthenticationStatus.Authenticated)
            {
                Log($"Player login rejected {authenticated.Status} : {authenticated.StatusMessage}");
                ResetSavedSession();
                return;
            }

            _currentPlayer = new PlayerClientInfo
            {
                Name      = authenticated.PlayerName,
                SessionId = authenticated.SessionId
            };

            SaveSession();
            Log($"Player is authenticated as {_currentPlayer.Name} with sessionId = {_currentPlayer.SessionId}");
        }
 public static void Encode(IByteWriter stream, AuthenticatedMessage encodedAuthenticatedMessage) {
 XdrEncoding.EncodeInt32((int)encodedAuthenticatedMessage.Discriminant.InnerValue, stream);
 switch (encodedAuthenticatedMessage.Discriminant.InnerValue) {
 case 0:
 AuthenticatedMessageV0.Encode(stream, encodedAuthenticatedMessage.V0);
 break;
 }
 }
예제 #3
0
 protected override Message ReceiveAuthenticatedMessage(AuthenticatedMessage message)
 {
     if (message is BlocksParityRequest)
     {
         var blockParityRequestMessage = message as BlocksParityRequest;
         var blocksPayload             = message.Payload as Blocks;
         for (int i = 0; i < blocksPayload.BlocksArray.Length; i++)
         {
             var block  = blocksPayload.BlocksArray[i];
             var parity = Cascade.Parity(DestilationBuffer.GetBits(block.Index, block.Length));
             blocksPayload.BlocksArray[i].Parity = parity;
         }
         var mac = MessageAuthenticator.GetMAC(blocksPayload.GetBytes());
         return(new BlocksParityResponse(mac, blocksPayload));
     }
     if (message is CheckParity)
     {
         var checkParityMessage  = message as CheckParity;
         var blockIdentification = checkParityMessage.Payload as BlockIdentifiaction;
         var parity  = Cascade.Parity(DestilationBuffer.GetBits(blockIdentification.Index, blockIdentification.Length));
         var payload = new ParityPayload(blockIdentification.Index, blockIdentification.Length, parity);
         var mac     = MessageAuthenticator.GetMAC(payload.GetBytes());
         return(new Parity(mac, payload));
     }
     if (message is RequestBits)
     {
         var requestBitsMessage  = message as RequestBits;
         var blockIdentification = requestBitsMessage.Payload as BlockIdentifiaction;
         var bytes   = DestilationBuffer.GetBytes(blockIdentification.Index, blockIdentification.Length);
         var payload = new BitsAsBytes(blockIdentification.Index, blockIdentification.Length, bytes);
         var mac     = MessageAuthenticator.GetMAC(payload.GetBytes());
         return(new Bytes(mac, payload, requestBitsMessage.Demand));
     }
     if (message is AddKey)
     {
         var addKeyMessage = message as AddKey;
         var messagPayload = (addKeyMessage.Payload as BlockIdAndKeyAllocation);
         if (messagPayload.BlockId == _currentKeyChunk + 1)
         {
             var key = LocalKeyStore.GetKey();
             CommonKeyStore.AddNewKey(key, messagPayload.KeyAllocation);
             _currentKeyChunk += 1;
             return(PrepareAckForCommonKeyProposition(_currentKeyChunk));
         }
         else
         {
             if (messagPayload.BlockId == _currentKeyChunk)
             {
                 return(PrepareAckForCommonKeyProposition(_currentKeyChunk));
             }
             else
             {
                 return(new NOP());
             }
         }
     }
     return(new NOP());
 }
예제 #4
0
 void IHandle <AuthenticatedMessage> .Handle(AuthenticatedMessage message)
 {
     _client.GetAccountVerification()
     .DispatcherSubscribe(x =>
     {
         _appInfo.User = x;
         ActivateItem(_viewModelFactory.TimelinesFactory());
     });
 }
예제 #5
0
 private void OnServerError(ServerErrorMessage serverErrorMessage)
 {
     if (serverErrorMessage.Error.Contains("Requires a user session"))
     {
         File.Delete(App.CurrentPlayerFilename);
         this.authenticated = null; // causes re-login
     }
     Console.WriteLine($"{DateTime.Now}: server error '{serverErrorMessage.Error}'!");
 }
예제 #6
0
 public static void Encode(IByteWriter stream, AuthenticatedMessage encodedAuthenticatedMessage)
 {
     XdrEncoding.EncodeInt32((int)encodedAuthenticatedMessage.Discriminant.InnerValue, stream);
     switch (encodedAuthenticatedMessage.Discriminant.InnerValue)
     {
     case 0:
         AuthenticatedMessageV0.Encode(stream, encodedAuthenticatedMessage.V0);
         break;
     }
 }
예제 #7
0
 public static void Encode(XdrDataOutputStream stream, AuthenticatedMessage encodedAuthenticatedMessage)
 {
     stream.WriteInt(encodedAuthenticatedMessage.Discriminant.InnerValue);
     switch (encodedAuthenticatedMessage.Discriminant.InnerValue)
     {
     case 0:
         AuthenticatedMessageV0.Encode(stream, encodedAuthenticatedMessage.V0);
         break;
     }
 }
 public static AuthenticatedMessage Decode(IByteReader stream) {
   AuthenticatedMessage decodedAuthenticatedMessage = new AuthenticatedMessage();
 decodedAuthenticatedMessage.Discriminant = Uint32.Decode(stream);
 switch (decodedAuthenticatedMessage.Discriminant.InnerValue) {
 case 0:
 decodedAuthenticatedMessage.V0 = AuthenticatedMessageV0.Decode(stream);
 break;
 }
   return decodedAuthenticatedMessage;
 }
예제 #9
0
 private static void Authenticate(AuthenticatedMessage message, AuthenticatedNetworkSession session)
 {
     if (null != message && null != session)
     {
         if (!session.Authenticate(message.AccountName, message.SessionId))
         {
             throw new MessageHandlerException(Resources.ErrorSessionNotAuthenticated);
         }
     }
 }
예제 #10
0
        public static AuthenticatedMessage Decode(IByteReader stream)
        {
            AuthenticatedMessage decodedAuthenticatedMessage = new AuthenticatedMessage();

            decodedAuthenticatedMessage.Discriminant = Uint32.Decode(stream);
            switch (decodedAuthenticatedMessage.Discriminant.InnerValue)
            {
            case 0:
                decodedAuthenticatedMessage.V0 = AuthenticatedMessageV0.Decode(stream);
                break;
            }
            return(decodedAuthenticatedMessage);
        }
예제 #11
0
 public static AuthenticatedMessage Decode(XdrDataInputStream stream)
 {
     var decodedAuthenticatedMessage = new AuthenticatedMessage();
     var discriminant = Uint32.Decode(stream);
     decodedAuthenticatedMessage.Discriminant = discriminant;
     switch (decodedAuthenticatedMessage.Discriminant.InnerValue)
     {
         case 0:
             decodedAuthenticatedMessage.V0 = AuthenticatedMessageV0.Decode(stream);
             break;
     }
     return decodedAuthenticatedMessage;
 }
예제 #12
0
        protected override Message ReceiveAuthenticatedMessage(AuthenticatedMessage message)
        {
            if (message is Bytes)
            {
                var bytesMessage       = message as Bytes;
                var bitsAsBytesPayload = bytesMessage.Payload as BitsAsBytes;

                if (bytesMessage.Demand == Demand.Estimation)
                {
                    var bobBits       = new BitArray(bitsAsBytesPayload.GetBytes().ToList().Skip(8).ToArray());
                    var aliceBits     = new BitArray(DestilationBuffer.GetBytes(bitsAsBytesPayload.Index, bitsAsBytesPayload.Length));
                    var estimatedQBER = Cascade.EstimateQBER(aliceBits, bobBits);
                    DestilationBuffer.SetEstimatedQBER(estimatedQBER);
                }
                if (bytesMessage.Demand == Demand.Confirmation)
                {
                }
            }
            if (message is AddingKeyAck)
            {
                var addingKeyAckMessage = message as AddingKeyAck;

                if (DoesAckCorrespondsToLastSentKeyIndex(addingKeyAckMessage.Payload as BlockIdAndKeyAllocation))
                {
                    _lastReceivedKeyIndexAck = _lastSendKeyIndex;

                    var key = LocalKeyStore.GetKey();
                    CommonKeyStore.AddNewKey(key, (addingKeyAckMessage.Payload as BlockIdAndKeyAllocation).KeyAllocation);

                    if (!LocalKeyStore.IsEmpty())
                    {
                        _lastSendKeyIndex += 1;
                        return(PrepareMessageForNewKeyChunk(_lastSendKeyIndex));
                    }
                    else
                    {
                        return(new NOP());
                    }
                }
                else
                {
                    throw new UnexpectedMessageException();
                }
            }
            return(new NOP());
        }
예제 #13
0
        public AuthenticatedMessage Authenticate(AuthenticateMessage message)
        {
            AuthenticatedMessage response = new AuthenticatedMessage();

            if (String.IsNullOrWhiteSpace(message.PlayerName))
            {
                response.Status = AuthenticationStatus.BadUser;
                _websocket.LogMessage($"Player failed authenticate: bad PlayerName");
                return(response);
            }
            Player player;

            try
            {
                player       = _websocket.GetCurrentSessionPlayer();
                player.Color = message.PlayerColor;
            }
            catch (Exception e)
            {
                _websocket.LogMessage($"Caught exception from GetCurrentPlayer, {e.Message}");
                player = null;
            }

            if (player == null)
            {
                Player prev = Server.Instance.Players.FirstOrDefault(p => p.Name == message.PlayerName);
                if (prev != null)
                {
                    _websocket.LogMessage($"Found existing player matching name {message.PlayerName}, removing...");
                    Server.Instance.Players.Remove(prev);
                }
                player = _websocket.CreatePlayerSession(message.PlayerName.Truncate(MaxPlayerNameLen));
            }

            response.PlayerId    = player.Id.ToString();
            response.PlayerName  = player.Name;
            response.PlayerColor = player.Color;
            response.SessionId   = player.SessionData.SessionId;
            _websocket.LogMessage($"Player authenticated: {JsonConvert.SerializeObject(response)}");
            return(response);
        }
예제 #14
0
        public void ConnectForAuthentication()
        {
            Console.WriteLine("Enter username:"******"{authenticated.Status} : {authenticated.StatusMessage}");
                        return;
                    }

                    _currentPlayer = new PlayerClientInfo
                    {
                        Name      = authenticated.PlayerName,
                        SessionId = authenticated.SessionId
                    };
                    File.WriteAllText(CurrentPlayerFilename, JsonConvert.SerializeObject(_currentPlayer, Formatting.Indented));
                    Console.WriteLine($"Player is authenticated  as {_currentPlayer.Name} with sessionId = {_currentPlayer.SessionId}");
                };

                _webSocket.Connect();

                AuthenticateMessage requestMessage = new AuthenticateMessage();
                requestMessage.PlayerName = username;
                string msg = JsonConvert.SerializeObject(requestMessage);
                _webSocket.Send(msg);

                // wait for the delegate function to set _currentPlayer
                int limit = 10;
                while (limit > 0 && authenticated == null)
                {
                    Thread.Sleep(1000);
                    limit--;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                File.Delete(CurrentPlayerFilename);
                authenticated  = null;
                _currentPlayer = null;
            }


            _webSocket.Close();
            _webSocket = null;
        }