コード例 #1
0
        private void HandleTokenlessAuthenticateRequest(
            OperationRequest operationRequest,
            SendParameters sendParameters,
            AuthenticateRequest request)
        {
            this.SetupPeer(request.UserId);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("HandleTokenlessAuthenticateRequest - Token Authentication done. UserId: {0}", this.UserId);
            }

            var response = new OperationResponse { OperationCode = operationRequest.OperationCode };
            this.SendOperationResponse(response, sendParameters);
        }
コード例 #2
0
        private AuthenticationToken GetValidAuthToken(AuthenticateRequest authenticateRequest,
                                                      out OperationResponse operationResponse)
        {
            operationResponse = null;
            if (this.application.TokenCreator == null)
            {
                log.ErrorFormat("No custom authentication supported: AuthTokenKey not specified in config.");

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

                return null;
            }

            // validate the authentication token
            if (string.IsNullOrEmpty(authenticateRequest.Token))
            {
                operationResponse = new OperationResponse(authenticateRequest.OperationRequest.OperationCode)
                {
                    ReturnCode = (short)ErrorCode.InvalidAuthentication,
                    DebugMessage = ErrorMessages.AuthTokenMissing
                };

                return null;
            }

            AuthenticationToken authToken;
            var tokenCreator = this.application.TokenCreator;
            if (!tokenCreator.DecryptAuthenticationToken(authenticateRequest.Token, out authToken))
            {
                log.WarnFormat("Could not decrypt authenticaton token: {0}", authenticateRequest.Token);

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

                return null;
            }

            if (authToken.ValidToTicks < DateTime.UtcNow.Ticks)
            {
                operationResponse = new OperationResponse(authenticateRequest.OperationRequest.OperationCode)
                {
                    ReturnCode = (short)Photon.Common.ErrorCode.AuthenticationTokenExpired,
                    DebugMessage = ErrorMessages.AuthTokenExpired
                };
                return null;
            }

            return authToken;
        }
コード例 #3
0
        private OperationResponse HandleAuthenticateTokenRequest(AuthenticateRequest request)
        {
            OperationResponse operationResponse;

            var authToken = this.GetValidAuthToken(request, out operationResponse);
            if (operationResponse != null || authToken == null)
            {
                return operationResponse;
            }

            this.SetupPeer(authToken);
            // publish operation response
            var responseObject = new AuthenticateResponse { QueuePosition = 0 };
            return new OperationResponse(request.OperationRequest.OperationCode, responseObject);
        }
コード例 #4
0
        protected virtual void HandleAuthenticateOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var request = new AuthenticateRequest(this.Protocol, operationRequest);
            if (this.ValidateOperation(request, sendParameters) == false)
            {
                return;
            }

            if (request.UserId != null)
            {
                this.PeerId = request.UserId;
            }

            var response = new OperationResponse { OperationCode = operationRequest.OperationCode };
            this.SendOperationResponse(response, sendParameters);
        }
コード例 #5
0
        protected virtual void HandleAuthenticateOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            var request = new AuthenticateRequest(this.Protocol, operationRequest);
            if (this.ValidateOperation(request, sendParameters) == false)
            {
                return;
            }

            if (request.ClientAuthenticationType == 255
                || !string.IsNullOrEmpty(request.Token)
                || AuthSettings.Default.Enabled
                )
            {
                var response = this.HandleAuthenticateTokenRequest(request);

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

                this.SendOperationResponse(response, sendParameters);
                return;
            }

            this.HandleTokenlessAuthenticateRequest(operationRequest, sendParameters, request);
        }
コード例 #6
0
        private OperationResponse HandleAuthenticateTokenRequest(AuthenticateRequest request)
        {
            OperationResponse operationResponse;

            var authToken = this.GetValidAuthToken(request, out operationResponse);
            if (operationResponse != null || authToken == null)
            {
                return operationResponse;
            }

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

            // publish operation response
            operationResponse = new OperationResponse(request.OperationRequest.OperationCode, new AuthenticateResponse { QueuePosition = 0 });
            operationResponse.Parameters.Add((byte)ParameterCode.Token, this.GetEncryptedAuthenticationToken(request));
            //operationResponse.Parameters.Add((byte)ParameterCode.Nickname, authToken.Nickname);
            //operationResponse.Parameters.Add((byte)ParameterCode.UserId, this.UserId);
            return operationResponse;
        }
コード例 #7
0
        private void OnAuthSuccess(AuthenticateRequest request)
        {
            var app = (MasterApplication)ApplicationBase.Instance;
            this.Application = app.DefaultApplication;

            // check if the peer wants to receive lobby statistic events
            if (request.ReceiveLobbyStatistics)
            {
                this.Application.LobbyStatsPublisher.Subscribe(this);
            }
        }
コード例 #8
0
        private void DoCustomAuthenticationResult(CustomAuthenticationResult customAuthResult, 
            AuthenticateRequest authRequest, SendParameters sendParameters, AuthSettings customAuthSettings)
        {
            if (this.Connected == false)
            {
                return;
            }

            try
            {
                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Client custom authentication callback: result={0}, msg={1}, userId={2}",
                        customAuthResult.ResultCode,
                        customAuthResult.Message,
                        this.UserId);
                }

                var operationResponse = new OperationResponse((byte)Hive.Operations.OperationCode.Authenticate)
                {
                    DebugMessage = customAuthResult.Message,
                    Parameters = new Dictionary<byte, object>()
                };

                switch (customAuthResult.ResultCode)
                {
                    default:
                        operationResponse.ReturnCode = (short)Photon.Common.ErrorCode.CustomAuthenticationFailed;
                        this.SendOperationResponse(operationResponse, sendParameters);
                        this.SetCurrentOperationHandler(OperationHandlerInitial.Instance);
                        return;

                    case CustomAuthenticationResultCode.Data:
                        operationResponse.Parameters = new Dictionary<byte, object> { { (byte)ParameterCode.Data, customAuthResult.Data } };
                        this.SendOperationResponse(operationResponse, sendParameters);
                        this.SetCurrentOperationHandler(OperationHandlerInitial.Instance);
                        return;

                    case CustomAuthenticationResultCode.Ok:
                        //apply user id from custom auth result
                        if (!string.IsNullOrEmpty(customAuthResult.UserId))
                        {
                            this.UserId = customAuthResult.UserId;
                        }
                        else if (!string.IsNullOrEmpty(authRequest.UserId))
                        {
                            this.UserId = authRequest.UserId;
                        }
                        else
                        {
                            this.UserId = Guid.NewGuid().ToString();
                        }
                        // create auth token and send response
                        this.CreateAuthTokenAndSendResponse(customAuthResult, authRequest, sendParameters, customAuthSettings, operationResponse);
                        this.SetCurrentOperationHandler(OperationHandlerDefault.Instance);
                        this.OnAuthSuccess(authRequest);
                        break;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                var errorResponse = new OperationResponse((byte)Hive.Operations.OperationCode.Authenticate) { ReturnCode = (short)ErrorCode.InternalServerError };
                this.SendOperationResponse(errorResponse, sendParameters);
                this.SetCurrentOperationHandler(OperationHandlerInitial.Instance);
            }
        }
コード例 #9
0
        protected virtual void CreateAuthTokenAndSendResponse(CustomAuthenticationResult customAuthResult, AuthenticateRequest authRequest,
            SendParameters sendParameters, AuthSettings authSettings, OperationResponse operationResponse)
        {
            var app = (MasterApplication)ApplicationBase.Instance;
            this.unencryptedAuthToken = app.TokenCreator.CreateAuthenticationToken(
                authRequest,
                authSettings,
                this.UserId,
                customAuthResult.AuthCookie);

            operationResponse.Parameters.Add((byte) ParameterCode.Token, this.GetEncryptedAuthenticationToken(authRequest));
            operationResponse.Parameters.Add((byte) ParameterCode.Data, customAuthResult.Data);
            operationResponse.Parameters.Add((byte)ParameterCode.Nickname, customAuthResult.Nickname);
            operationResponse.Parameters.Add((byte)ParameterCode.UserId, this.UserId);
            this.SendOperationResponse(operationResponse, sendParameters);
        }
コード例 #10
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);
            }

            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,
                                   };

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

            // 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);
        }
コード例 #11
0
        public virtual string GetEncryptedAuthenticationToken(AuthenticateRequest request)
        {
            var app = (MasterApplication)ApplicationBase.Instance;

            if (this.unencryptedAuthToken == null)
            {
                this.unencryptedAuthToken = app.TokenCreator.CreateAuthenticationToken(this.UserId, request);
            }

            return app.TokenCreator.EncryptAuthenticationToken(this.unencryptedAuthToken, true);
        }
コード例 #12
0
        private OperationResponse HandleAuthenticate(OperationRequest operationRequest)
        {
            OperationResponse response;

            var request = new AuthenticateRequest(this.Protocol, operationRequest);
            if (!OperationHelper.ValidateOperation(request, log, out response))
            {
                return response;
            }

            this.UserId = request.UserId;

            this.Application = ((MasterApplication) ApplicationBase.Instance).DefaultApplication;

            // publish operation response
            var responseObject = new AuthenticateResponse { QueuePosition = 0 };
            return new OperationResponse(operationRequest.OperationCode, responseObject);
        }