/// <summary>
        ///     ProcessAsync message
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>If message processing can continue</returns>
        /// <remarks>
        ///     <para>
        ///         Check the <see cref="ModuleResult" /> property to see how the message processing have gone so
        ///         far.
        ///     </para>
        /// </remarks>
        public async Task <ModuleResult> ProcessAsync(IClientContext context)
        {
            var principal = context.ChannelData["Principal"] as IPrincipal;

            if (principal != null)
            {
                Thread.CurrentPrincipal = principal;
                return(ModuleResult.Continue);
            }

            var user = context.ChannelData["AuthenticationUser"] as IUserAccount;

            if (user == null)
            {
                var preRequest = context.RequestMessage as IAuthenticationHandshake;
                if (preRequest == null)
                {
                    context.ResponseMessage =
                        new AuthenticationException(
                            "You need to send a message that implements IClientPreAuthentication.");
                    return(ModuleResult.SendResponse);
                }

                user = await _fetcher.FindUserAsync(preRequest.UserName);

                var msg = _authenticationMessageFactory.CreateServerPreAuthentication(user);
                context.ResponseMessage                   = msg;
                context.ChannelData["SessionSalt"]        = msg.SessionSalt;
                context.ChannelData["AuthenticationUser"] = user;
                return(ModuleResult.SendResponse);
            }


            var authenticateRequest = context.RequestMessage as IAuthenticate;

            if (authenticateRequest == null)
            {
                context.ResponseMessage =
                    new AuthenticationException(
                        "You need to send a message that implements IClientAuthentication as the second message to authenticate properly.");
                return(ModuleResult.SendResponse);
            }

            var sessionSalt = context.ChannelData["SessionSalt"] as string;

            if (sessionSalt == null)
            {
                context.ResponseMessage =
                    new AuthenticationException(
                        "Invalid authentication process (salt not found)..");
                return(ModuleResult.SendResponse);
            }


            var response = await AuthenticateUser(context, user, sessionSalt, authenticateRequest);

            context.ResponseMessage = response;
            return(ModuleResult.SendResponse);
        }
Пример #2
0
        /// <summary>
        ///     ProcessAsync message
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>If message processing can continue</returns>
        /// <remarks>
        ///     <para>
        ///         Check the <see cref="ModuleResult" /> property to see how the message processing have gone so
        ///         far.
        ///     </para>
        /// </remarks>
        public async Task <ModuleResult> ProcessAsync(IClientContext context)
        {
            var principal = context.ChannelData["Principal"] as IPrincipal;

            if (principal != null)
            {
                Thread.CurrentPrincipal = principal;
                return(ModuleResult.Continue);
            }

            var handshake = context.RequestMessage as IAuthenticationHandshake;

            if (handshake != null)
            {
                if (_step != 0)
                {
                    context.ResponseMessage =
                        new AuthenticationException(
                            "Invalid authentication process.");
                    return(ModuleResult.SendResponse);
                }

                var user = await _fetcher.FindUserAsync(handshake.UserName);

                var msg = _authenticationMessageFactory.CreateServerPreAuthentication(user);
                context.ResponseMessage                   = msg;
                context.ChannelData["SessionSalt"]        = msg.SessionSalt;
                context.ChannelData["AuthenticationUser"] = user;
                _step = 1;
                return(ModuleResult.SendResponse);
            }
            if (_step == 0)
            {
                return(ModuleResult.Continue);
            }

            var authenticateRequest = context.RequestMessage as IAuthenticate;

            if (authenticateRequest != null)
            {
                var user = context.ChannelData["AuthenticationUser"] as IUserAccount;
                if (user == null || _step != 1)
                {
                    context.ResponseMessage = new AuthenticationException("Invalid authentication process");
                    _step = 0;
                    return(ModuleResult.SendResponse);
                }
                _step = 0;

                var sessionSalt = context.ChannelData["SessionSalt"] as string;
                if (sessionSalt == null)
                {
                    context.ResponseMessage =
                        new AuthenticationException(
                            "Invalid authentication process (salt not found)..");
                    return(ModuleResult.SendResponse);
                }


                var response = await AuthenticateUser(context, user, sessionSalt, authenticateRequest);

                context.ResponseMessage = response;
                return(ModuleResult.SendResponse);
            }

            return(ModuleResult.Continue);
        }