protected override void OnAuthenticateClient(
     ICustomAuthPeer peer, 
     IAuthenticateRequest authRequest, 
     AuthSettings authSettings,
     SendParameters sendParameters, object state)
 {
     base.OnAuthenticateClient(peer, authRequest, authSettings, sendParameters, state);
 }
 public virtual AuthenticationToken CreateAuthenticationToken(IAuthenticateRequest authRequest, 
     AuthSettings authSettings, 
     string userId, 
     Dictionary<string, object> authCookie)
 {
     var token = new AuthenticationToken
                     {
                         UserId = userId,
                         AuthCookie = authCookie,
                         Flags = authRequest.Flags,
                     };
     this.SetupToken(token);
     return token;
 }
        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);
            }
        }
        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);
        }
        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);
        }
 public virtual string CreateEncryptedAuthenticationToken(IAuthenticateRequest authRequest, AuthSettings authSettings, string userId, Dictionary<string, object> customParameter)
 {
     var token = this.CreateAuthenticationToken(authRequest, authSettings, userId, customParameter);
     return this.EncryptAuthenticationToken(token, false);
 }
 public virtual string CreateEncryptedAuthenticationToken(IAuthenticateRequest authRequest, AuthSettings authSettings, string userId)
 {
     var token = this.CreateAuthenticationToken(authRequest, authSettings, userId, null);
     return this.EncryptAuthenticationToken(token, false);
 }