Ejemplo n.º 1
0
        public async Task ProcessResponse_PasswordAcceptedAfterUserNameInInitialRequest()
        {
            TestMocks mocks = new TestMocks();

            mocks.ServerBehaviour.Setup(sb => sb.ValidateAuthenticationCredentials(It.IsAny <IConnection>(), It.IsAny <IAuthenticationCredentials>())).Returns(Task.FromResult(AuthenticationResult.Success));


            LoginMechanismProcessor      processor = this.Setup(mocks);
            AuthMechanismProcessorStatus result    = await processor.ProcessResponse(EncodeBase64("rob")).ConfigureAwait(false);

            Assert.Equal(AuthMechanismProcessorStatus.Continue, result);

            mocks.Connection.Verify(c =>
                                    c.WriteResponse(
                                        It.Is <SmtpResponse>(r =>
                                                             VerifyBase64Response(r.Message, "Password:"******"password")).ConfigureAwait(false);

            Assert.Equal(AuthMechanismProcessorStatus.Success, result);
        }
        public void ProcessRepsonse_ChallengeReponse_BadFormat()
        {
            Mocks mocks = new Mocks();

            string challenge = string.Format("{0}.{1}@{2}", FAKERANDOM, FAKEDATETIME, FAKEDOMAIN);

            CramMd5MechanismProcessor    cramMd5MechanismProcessor = Setup(mocks, challenge);
            AuthMechanismProcessorStatus result = cramMd5MechanismProcessor.ProcessResponse("BLAH");
        }
        public async Task ProcessRepsonse_ChallengeReponse_BadFormat()
        {
            await Assert.ThrowsAsync <SmtpServerException>(async() =>
            {
                TestMocks mocks = new TestMocks();

                string challenge = string.Format("{0}.{1}@{2}", FAKERANDOM, FAKEDATETIME, FAKEDOMAIN);

                CramMd5MechanismProcessor cramMd5MechanismProcessor = this.Setup(mocks, challenge);
                AuthMechanismProcessorStatus result = await cramMd5MechanismProcessor.ProcessResponse("BLAH").ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public void ProcessRepsonse_NoUsername_GetUsernameChallenge()
        {
            Mocks mocks = new Mocks();

            LoginMechanismProcessor      processor = Setup(mocks);
            AuthMechanismProcessorStatus result    = processor.ProcessResponse(null);

            Assert.AreEqual(AuthMechanismProcessorStatus.Continue, result);
            mocks.Connection.Verify(c =>
                                    c.WriteResponse(
                                        It.Is <SmtpResponse>(r =>
                                                             r.Code == (int)StandardSmtpResponseCode.AuthenticationContinue &&
                                                             VerifyBase64Response(r.Message, "Username:")
                                                             )
                                        )
                                    );
        }
Ejemplo n.º 5
0
        public async Task ProcessRepsonse_NoUsername_GetUsernameChallenge()
        {
            TestMocks mocks = new TestMocks();

            LoginMechanismProcessor      processor = this.Setup(mocks);
            AuthMechanismProcessorStatus result    = await processor.ProcessResponse(null).ConfigureAwait(false);

            Assert.Equal(AuthMechanismProcessorStatus.Continue, result);
            mocks.Connection.Verify(c =>
                                    c.WriteResponse(
                                        It.Is <SmtpResponse>(r =>
                                                             r.Code == (int)StandardSmtpResponseCode.AuthenticationContinue &&
                                                             VerifyBase64Response(r.Message, "Username:")
                                                             )
                                        )
                                    );
        }
Ejemplo n.º 6
0
        public async Task ProcessRepsonse_Username_GetPasswordChallenge()
        {
            Mocks mocks = new Mocks();

            LoginMechanismProcessor      processor = Setup(mocks);
            AuthMechanismProcessorStatus result    = await processor.ProcessResponseAsync(EncodeBase64("rob"));

            Assert.Equal(AuthMechanismProcessorStatus.Continue, result);

            mocks.Connection.Verify(c =>
                                    c.WriteResponseAsync(
                                        It.Is <SmtpResponse>(r =>
                                                             VerifyBase64Response(r.Message, "Password:") &&
                                                             r.Code == (int)StandardSmtpResponseCode.AuthenticationContinue
                                                             )
                                        )
                                    );
        }
        private async Task ProcessResponseAsync(AuthenticationResult authenticationResult, AuthMechanismProcessorStatus authMechanismProcessorStatus)
        {
            Mocks mocks = new Mocks();
            mocks.ServerBehaviour.Setup(
                b =>
                b.ValidateAuthenticationCredentialsAsync(mocks.Connection.Object, It.IsAny<AnonymousAuthenticationCredentials>()))
                .ReturnsAsync(authenticationResult);

            AnonymousMechanismProcessor anonymousMechanismProcessor = new AnonymousMechanismProcessor(mocks.Connection.Object);
            AuthMechanismProcessorStatus result = await anonymousMechanismProcessor.ProcessResponseAsync(null);

            Assert.Equal(authMechanismProcessorStatus, result);

            if (authenticationResult == AuthenticationResult.Success)
            {
                Assert.IsType(typeof(AnonymousAuthenticationCredentials), anonymousMechanismProcessor.Credentials);
            }
        }
        public void ProcessRepsonse_GetChallenge()
        {
            Mocks mocks = new Mocks();

            CramMd5MechanismProcessor    cramMd5MechanismProcessor = Setup(mocks);
            AuthMechanismProcessorStatus result = cramMd5MechanismProcessor.ProcessResponse(null);

            string expectedResponse = string.Format("{0}.{1}@{2}", FAKERANDOM, FAKEDATETIME, FAKEDOMAIN);

            Assert.AreEqual(AuthMechanismProcessorStatus.Continue, result);
            mocks.Connection.Verify(
                c => c.WriteResponse(
                    It.Is <SmtpResponse>(r =>
                                         r.Code == (int)StandardSmtpResponseCode.AuthenticationContinue &&
                                         VerifyBase64Response(r.Message, expectedResponse)
                                         )
                    )
                );
        }
Ejemplo n.º 9
0
        private async Task ProcessResponseAsync(AuthenticationResult authenticationResult, AuthMechanismProcessorStatus authMechanismProcessorStatus)
        {
            Mocks mocks = new Mocks();

            mocks.ServerBehaviour.Setup(
                b =>
                b.ValidateAuthenticationCredentialsAsync(mocks.Connection.Object, It.IsAny <AnonymousAuthenticationCredentials>()))
            .ReturnsAsync(authenticationResult);

            AnonymousMechanismProcessor  anonymousMechanismProcessor = new AnonymousMechanismProcessor(mocks.Connection.Object);
            AuthMechanismProcessorStatus result = await anonymousMechanismProcessor.ProcessResponseAsync(null);

            Assert.Equal(authMechanismProcessorStatus, result);

            if (authenticationResult == AuthenticationResult.Success)
            {
                Assert.IsType <AnonymousAuthenticationCredentials>(anonymousMechanismProcessor.Credentials);
            }
        }
        private void ProcessResponse(AuthenticationResult authenticationResult, AuthMechanismProcessorStatus authMechanismProcessorStatus)
        {
            Mocks mocks = new Mocks();

            mocks.ServerBehaviour.Setup(
                b =>
                b.ValidateAuthenticationCredentials(mocks.Connection.Object, It.IsAny <AnonymousAuthenticationCredentials>()))
            .Returns(authenticationResult);

            AnonymousMechanismProcessor  anonymousMechanismProcessor = new AnonymousMechanismProcessor(mocks.Connection.Object);
            AuthMechanismProcessorStatus result = anonymousMechanismProcessor.ProcessResponse(null);

            Assert.AreEqual(authMechanismProcessorStatus, result);

            if (authenticationResult == AuthenticationResult.Success)
            {
                Assert.IsInstanceOfType(anonymousMechanismProcessor.Credentials, typeof(AnonymousAuthenticationCredentials));
            }
        }
Ejemplo n.º 11
0
        public async Task ProcessResponse_PasswordAcceptedAfterUserNameInInitialRequest()
        {
            Mocks mocks = new Mocks();

            LoginMechanismProcessor      processor = Setup(mocks);
            AuthMechanismProcessorStatus result    = await processor.ProcessResponseAsync(EncodeBase64("rob"));

            Assert.Equal(AuthMechanismProcessorStatus.Continue, result);

            mocks.Connection.Verify(c =>
                                    c.WriteResponseAsync(
                                        It.Is <SmtpResponse>(r =>
                                                             VerifyBase64Response(r.Message, "Password:"******"password"));

            Assert.Equal(AuthMechanismProcessorStatus.Success, result);
        }
Ejemplo n.º 12
0
        public void Process(IConnection connection, SmtpCommand command)
        {
            if (command.Arguments.Length > 0)
            {
                if (connection.Session.Authenticated)
                {
                    throw new SmtpServerException(new SmtpResponse(StandardSmtpResponseCode.BadSequenceOfCommands,
                                                                   "Already authenticated"));
                }

                string         mechanismId = command.Arguments[0];
                IAuthMechanism mechanism   = AuthExtensionProcessor.MechanismMap.Get(mechanismId);

                if (mechanism == null)
                {
                    throw new SmtpServerException(
                              new SmtpResponse(StandardSmtpResponseCode.CommandParameterNotImplemented,
                                               "Specified AUTH mechanism not supported"));
                }

                if (!AuthExtensionProcessor.IsMechanismEnabled(mechanism))
                {
                    throw new SmtpServerException(
                              new SmtpResponse(StandardSmtpResponseCode.AuthenticationFailure,
                                               "Specified AUTH mechanism not allowed right now (might require secure connection etc)"));
                }

                IAuthMechanismProcessor authMechanismProcessor =
                    mechanism.CreateAuthMechanismProcessor(connection);

                AuthMechanismProcessorStatus status =
                    authMechanismProcessor.ProcessResponse(string.Join(" ", command.Arguments.Skip(1).ToArray()));
                while (status == AuthMechanismProcessorStatus.Continue)
                {
                    string response = connection.ReadLine();

                    if (response == "*")
                    {
                        connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments, "Authentication aborted"));
                        return;
                    }

                    status = authMechanismProcessor.ProcessResponse(response);
                }

                if (status == AuthMechanismProcessorStatus.Success)
                {
                    connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.AuthenitcationOK,
                                                              "Authenticated OK"));
                    connection.Session.Authenticated             = true;
                    connection.Session.AuthenticationCredentials = authMechanismProcessor.Credentials;
                }
                else
                {
                    connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.AuthenticationFailure,
                                                              "Authentication failure"));
                }
            }
            else
            {
                throw new SmtpServerException(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments,
                                                               "Must specify AUTH mechanism as a parameter"));
            }
        }
Ejemplo n.º 13
0
        /// <inheritdoc/>
        public async Task Process(IConnection connection, SmtpCommand command)
        {
            ArgumentsParser argumentsParser = new ArgumentsParser(command.ArgumentsText);

            if (argumentsParser.Arguments.Count > 0)
            {
                if (connection.Session.Authenticated)
                {
                    throw new SmtpServerException(new SmtpResponse(
                                                      StandardSmtpResponseCode.BadSequenceOfCommands,
                                                      "Already authenticated"));
                }

                string         mechanismId = argumentsParser.Arguments.First();
                IAuthMechanism mechanism   = this.AuthExtensionProcessor.MechanismMap.Get(mechanismId);

                if (mechanism == null)
                {
                    throw new SmtpServerException(
                              new SmtpResponse(
                                  StandardSmtpResponseCode.CommandParameterNotImplemented,
                                  "Specified AUTH mechanism not supported"));
                }

                if (!await this.AuthExtensionProcessor.IsMechanismEnabled(mechanism).ConfigureAwait(false))
                {
                    throw new SmtpServerException(
                              new SmtpResponse(
                                  StandardSmtpResponseCode.AuthenticationFailure,
                                  "Specified AUTH mechanism not allowed right now (might require secure connection etc)"));
                }

                IAuthMechanismProcessor authMechanismProcessor =
                    mechanism.CreateAuthMechanismProcessor(connection);

                string initialData = null;
                if (argumentsParser.Arguments.Count > 1)
                {
                    initialData = string.Join(" ", argumentsParser.Arguments.Skip(1).ToArray());
                }

                AuthMechanismProcessorStatus status =
                    await authMechanismProcessor.ProcessResponse(initialData).ConfigureAwait(false);

                while (status == AuthMechanismProcessorStatus.Continue)
                {
                    string response = await connection.ReadLine().ConfigureAwait(false);

                    if (response == "*")
                    {
                        await connection.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorInCommandArguments, "Authentication aborted")).ConfigureAwait(false);

                        return;
                    }

                    status = await authMechanismProcessor.ProcessResponse(response).ConfigureAwait(false);
                }

                if (status == AuthMechanismProcessorStatus.Success)
                {
                    await connection.WriteResponse(new SmtpResponse(
                                                       StandardSmtpResponseCode.AuthenticationOK,
                                                       "Authenticated OK")).ConfigureAwait(false);

                    connection.Session.Authenticated             = true;
                    connection.Session.AuthenticationCredentials = authMechanismProcessor.Credentials;
                }
                else
                {
                    await connection.WriteResponse(new SmtpResponse(
                                                       StandardSmtpResponseCode.AuthenticationFailure,
                                                       "Authentication failure")).ConfigureAwait(false);
                }
            }
            else
            {
                throw new SmtpServerException(new SmtpResponse(
                                                  StandardSmtpResponseCode.SyntaxErrorInCommandArguments,
                                                  "Must specify AUTH mechanism as a parameter"));
            }
        }