public OperationResponse HandleAuthenticate(OperationRequest operationRequest, SendParameters sendParameters)
        {
            // validate operation request
            var authenticateRequest = new AuthenticateRequest(this.Protocol, operationRequest);

            if (authenticateRequest.IsValid == false)
            {
                return(OperationHandlerBase.HandleInvalidOperation(authenticateRequest, log));
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat(
                    "HandleAuthenticateRequest:appId={0};version={1};region={2};type={3};userId={4}",
                    authenticateRequest.ApplicationId,
                    authenticateRequest.ApplicationVersion,
                    authenticateRequest.Region,
                    authenticateRequest.ClientAuthenticationType,
                    authenticateRequest.UserId);
            }

            if (authenticateRequest.ClientAuthenticationType == 255 || !string.IsNullOrEmpty(authenticateRequest.Token))
            {
                var response = this.HandleAuthenticateTokenRequest(authenticateRequest);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("HandleAuthenticateRequest - Token Authentication done. Result: {0}; msg={1}", response.ReturnCode, response.DebugMessage);
                }

                if (response.ReturnCode == 0)
                {
                    this.SetCurrentOperationHandler(OperationHandlerDefault.Instance);
                    this.OnAuthSuccess(authenticateRequest);
                }

                return(response);
            }

            // if authentication data is used it must be either a byte array or a string value
            if (authenticateRequest.ClientAuthenticationData != null)
            {
                var dataType = authenticateRequest.ClientAuthenticationData.GetType();
                if (dataType != typeof(byte[]) && dataType != typeof(string))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("HandleAuthenticateRequest - invalid type for auth data (datatype = {0}), request: {1}", dataType, operationRequest.ToString());
                    }

                    return(new OperationResponse
                    {
                        OperationCode = operationRequest.OperationCode,
                        ReturnCode = (short)ErrorCode.OperationInvalid,
                        DebugMessage = ErrorMessages.InvalidTypeForAuthData
                    });
                }
            }

            var app = (MasterApplication)ApplicationBase.Instance;

            // check if custom client authentication is required
            if (app.CustomAuthHandler.IsClientAuthenticationEnabled)
            {
                if (app.TokenCreator == null)
                {
                    log.WarnFormat("No custom authentication supported: AuthTokenKey not specified in config.");

                    var response = new OperationResponse(authenticateRequest.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)ErrorCode.InvalidAuthentication,
                        DebugMessage = ErrorMessages.AuthTokenTypeNotSupported
                    };

                    return(response);
                }


                this.SetCurrentOperationHandler(OperationHandlerAuthenticating.Instance);

                var authSettings = new AuthSettings
                {
                    IsAnonymousAccessAllowed = app.CustomAuthHandler.IsAnonymousAccessAllowed,
                };


                //string AuthenticationValues .AuthGetParameters



                log.DebugFormat("Douglas :: TESTE 1 :: MasterClientPeer.cs");



                //Custom Authenticate external http
                app.CustomAuthHandler.AuthenticateClient(this, authenticateRequest, authSettings, new SendParameters(), authSettings);
                return(null);
            }

            log.DebugFormat("Douglas :: TESTE 2 :: MasterClientPeer.cs");

            // TBD: centralizing setting of userid
            this.UserId = authenticateRequest.UserId;


            // apply application to the peer
            this.SetCurrentOperationHandler(OperationHandlerDefault.Instance);

            this.OnAuthSuccess(authenticateRequest);

            // publish operation response
            return(new OperationResponse(operationRequest.OperationCode));
        }
Exemple #2
0
        public OperationResponse HandleAuthenticate(OperationRequest operationRequest, SendParameters sendParameters)
        {
            // validate operation request
            var authenticateRequest = new AuthenticateRequest(this.Protocol, operationRequest);

            if (authenticateRequest.IsValid == false)
            {
                return(OperationHandlerBase.HandleInvalidOperation(authenticateRequest, log));
            }

            this.StopWaitForAuthRequest();

            if (log.IsDebugEnabled)
            {
                log.DebugFormat(
                    "HandleAuthenticateRequest:appId={0};version={1};region={2};type={3};userId={4}",
                    authenticateRequest.ApplicationId,
                    authenticateRequest.ApplicationVersion,
                    authenticateRequest.Region,
                    authenticateRequest.ClientAuthenticationType,
                    authenticateRequest.UserId);
            }

            var app = (MasterApplication)ApplicationBase.Instance;

            if (authenticateRequest.IsTokenAuthUsed)
            {
                OperationResponse response;

                var authToken = GetValidAuthToken(authenticateRequest, out response);
                if (response != null || authToken == null)
                {
                    return(response);
                }

                if (app.DefaultApplication.IsActorExcluded(authToken.UserId))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("HandleAuthenticate, userId '{0}' is excluded", authToken.UserId);
                    }

                    response = new OperationResponse((byte)OperationCode.Authenticate)
                    {
                        DebugMessage = "User blocked",
                        ReturnCode   = (short)ErrorCode.UserBlocked
                    };
                    return(response);
                }

                this.UserId = authToken.UserId;
                this.unencryptedAuthToken = authToken;

                if (!ConnectionRequirementsChecker.Check(this, authenticateRequest.ApplicationId,
                                                         this.unencryptedAuthToken, this.authOnceUsed))
                {
                    log.Warn(secureConnectionLogGuard,
                             $"Client used non secure connection type when it is required. appId:{authenticateRequest.ApplicationId}, Connection: {this.NetworkProtocol}, Reqular Authentication");

                    return(null);
                }
                // publish operation response
                response = new OperationResponse(
                    authenticateRequest.OperationRequest.OperationCode,
                    new AuthenticateResponse
                {
                    QueuePosition = 0
                }
                    );
                response.Parameters.Add((byte)ParameterCode.Token, this.GetEncryptedAuthenticationToken(authenticateRequest));

                if (!string.IsNullOrEmpty(this.UserId))
                {
                    response.Parameters.Add((byte)ParameterCode.UserId, this.UserId);
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("HandleAuthenticateRequest - Token Authentication done. Result: {0}; msg={1}", response.ReturnCode, response.DebugMessage);
                }

                this.SetCurrentOperationHandler(OperationHandlerDefault.Instance);

                this.Application = app.DefaultApplication;

                // check if the peer wants to receive lobby statistic events
                if (authenticateRequest.ReceiveLobbyStatistics)
                {
                    this.Application.LobbyStatsPublisher.Subscribe(this);
                }

                return(response);
            }


            // if authentication data is used it must be either a byte array or a string value
            if (authenticateRequest.ClientAuthenticationData != null)
            {
                var dataType = authenticateRequest.ClientAuthenticationData.GetType();
                if (dataType != typeof(byte[]) && dataType != typeof(string))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("HandleAuthenticateRequest - invalid type for auth data (datatype = {0}), request: {1}", dataType, operationRequest.ToString());
                    }

                    return(new OperationResponse
                    {
                        OperationCode = operationRequest.OperationCode,
                        ReturnCode = (short)ErrorCode.OperationInvalid,
                        DebugMessage = ErrorMessages.InvalidTypeForAuthData
                    });
                }
            }

            // check if custom client authentication is required
            if (app.CustomAuthHandler.IsClientAuthenticationEnabled)
            {
                if (app.TokenCreator == null)
                {
                    log.WarnFormat("No custom authentication supported: AuthTokenKey not specified in config.");

                    var response = new OperationResponse(authenticateRequest.OperationRequest.OperationCode)
                    {
                        ReturnCode   = (short)ErrorCode.InvalidAuthentication,
                        DebugMessage = ErrorMessages.AuthTokenTypeNotSupported
                    };

                    return(response);
                }


                this.SetCurrentOperationHandler(OperationHandlerAuthenticating.Instance);

                var authSettings = new AuthSettings
                {
                    IsAnonymousAccessAllowed = app.CustomAuthHandler.IsAnonymousAccessAllowed,
                };

                app.CustomAuthHandler.AuthenticateClient(this, authenticateRequest, authSettings, new SendParameters(), authSettings);
                return(null);
            }

            var authResponse = new OperationResponse(operationRequest.OperationCode)
            {
                Parameters = new Dictionary <byte, object>()
            };

            // TBD: centralizing setting of userid
            if (string.IsNullOrWhiteSpace(authenticateRequest.UserId))
            {
                this.UserId = Guid.NewGuid().ToString();
                authResponse.Parameters.Add((byte)ParameterCode.UserId, this.UserId);
            }
            else
            {
                this.UserId = authenticateRequest.UserId;
            }

            authResponse.Parameters.Add((byte)ParameterCode.Token, this.GetEncryptedAuthenticationToken(authenticateRequest));

            // apply application to the peer
            this.SetCurrentOperationHandler(OperationHandlerDefault.Instance);

            this.OnAuthSuccess(authenticateRequest);

            // publish operation response
            return(authResponse);
        }