Example #1
0
        protected async UniTaskVoid HandleRequestValidateAccessToken(
            RequestHandlerData requestHandler,
            RequestValidateAccessTokenMessage request,
            RequestProceedResultDelegate <ResponseValidateAccessTokenMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            long       connectionId = requestHandler.ConnectionId;
            UITextKeys message      = UITextKeys.NONE;
            string     userId       = request.userId;
            string     accessToken  = request.accessToken;
            ValidateAccessTokenResp validateAccessTokenResp = await DbServiceClient.ValidateAccessTokenAsync(new ValidateAccessTokenReq()
            {
                UserId      = userId,
                AccessToken = accessToken
            });

            if (!validateAccessTokenResp.IsPass)
            {
                message     = UITextKeys.UI_ERROR_INVALID_USER_TOKEN;
                userId      = string.Empty;
                accessToken = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo;
                if (userPeersByUserId.TryGetValue(userId, out userPeerInfo))
                {
                    userPeersByUserId.Remove(userPeerInfo.userId);
                    userPeers.Remove(userPeerInfo.connectionId);
                }
                userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(System.Convert.ToBase64String(System.Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                await DbServiceClient.UpdateAccessTokenAsync(new UpdateAccessTokenReq()
                {
                    UserId      = userPeerInfo.userId,
                    AccessToken = accessToken
                });
            }
            // Response
            result.Invoke(
                message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseValidateAccessTokenMessage()
            {
                message     = message,
                userId      = userId,
                accessToken = accessToken,
            });
#endif
        }
Example #2
0
        protected async UniTaskVoid HandleRequestValidateAccessToken(
            RequestHandlerData requestHandler,
            RequestValidateAccessTokenMessage request,
            RequestProceedResultDelegate <ResponseValidateAccessTokenMessage> result)
        {
            long connectionId = requestHandler.ConnectionId;

            ResponseValidateAccessTokenMessage.Error error = ResponseValidateAccessTokenMessage.Error.None;
            string userId      = request.userId;
            string accessToken = request.accessToken;
            ValidateAccessTokenResp validateAccessTokenResp = await DbServiceClient.ValidateAccessTokenAsync(new ValidateAccessTokenReq()
            {
                UserId      = userId,
                AccessToken = accessToken
            });

            if (!validateAccessTokenResp.IsPass)
            {
                error       = ResponseValidateAccessTokenMessage.Error.InvalidAccessToken;
                userId      = string.Empty;
                accessToken = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo;
                if (userPeersByUserId.TryGetValue(userId, out userPeerInfo))
                {
                    userPeersByUserId.Remove(userPeerInfo.userId);
                    userPeers.Remove(userPeerInfo.connectionId);
                }
                userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(System.Convert.ToBase64String(System.Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                await DbServiceClient.UpdateAccessTokenAsync(new UpdateAccessTokenReq()
                {
                    UserId      = userPeerInfo.userId,
                    AccessToken = accessToken
                });
            }
            // Response
            result.Invoke(
                error == ResponseValidateAccessTokenMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseValidateAccessTokenMessage()
            {
                error       = error,
                userId      = userId,
                accessToken = accessToken,
            });
        }
        public override async UniTask <bool> DeserializeClientReadyData(LiteNetLibIdentity playerIdentity, long connectionId, NetDataReader reader)
        {
            string userId            = reader.GetString();
            string accessToken       = reader.GetString();
            string selectCharacterId = reader.GetString();

            if (ServerUserHandlers.TryGetPlayerCharacter(connectionId, out _))
            {
                if (LogError)
                {
                    Logging.LogError(LogTag, "User trying to hack: " + userId);
                }
                Transport.ServerDisconnect(connectionId);
                return(false);
            }

            ValidateAccessTokenResp validateAccessTokenResp = await DbServiceClient.ValidateAccessTokenAsync(new ValidateAccessTokenReq()
            {
                UserId      = userId,
                AccessToken = accessToken
            });

            if (!validateAccessTokenResp.IsPass)
            {
                if (LogError)
                {
                    Logging.LogError(LogTag, "Invalid access token for user: "******"Not ready to spawn player: " + userId);
                }
                // Add to pending list to spawn player later when map server is ready to instantiate object
                pendingSpawnPlayerCharacters.Add(new PendingSpawnPlayerCharacter()
                {
                    connectionId      = connectionId,
                    userId            = userId,
                    selectCharacterId = selectCharacterId
                });
                return(false);
            }

            SetPlayerReadyRoutine(connectionId, userId, selectCharacterId).Forget();
            return(true);
        }
Example #4
0
        private async UniTaskVoid HandleRequestValidateAccessTokenRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestValidateAccessTokenMessage message = messageHandler.ReadMessage <RequestValidateAccessTokenMessage>();

            ResponseValidateAccessTokenMessage.Error error = ResponseValidateAccessTokenMessage.Error.None;
            string userId      = message.userId;
            string accessToken = message.accessToken;
            ValidateAccessTokenResp validateAccessTokenResp = await DbServiceClient.ValidateAccessTokenAsync(new ValidateAccessTokenReq()
            {
                UserId      = userId,
                AccessToken = accessToken
            });

            if (!validateAccessTokenResp.IsPass)
            {
                error       = ResponseValidateAccessTokenMessage.Error.InvalidAccessToken;
                userId      = string.Empty;
                accessToken = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo;
                if (userPeersByUserId.TryGetValue(userId, out userPeerInfo))
                {
                    userPeersByUserId.Remove(userPeerInfo.userId);
                    userPeers.Remove(userPeerInfo.connectionId);
                }
                userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                await DbServiceClient.UpdateAccessTokenAsync(new UpdateAccessTokenReq()
                {
                    UserId      = userPeerInfo.userId,
                    AccessToken = accessToken
                });
            }
            ResponseValidateAccessTokenMessage responseMessage = new ResponseValidateAccessTokenMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseValidateAccessTokenMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.userId       = userId;
            responseMessage.accessToken  = accessToken;
            ServerSendResponse(connectionId, MMOMessageTypes.ResponseValidateAccessToken, responseMessage);
        }