Ejemplo n.º 1
0
        public SecurityModule(IContainer container, IAuthenticationService authenticationService)
        {
            logger.Debug("Instantiate SecurityModule.");

            Post[RouterPattern.Security.VerifyToken] = (parameters =>
            {
                return(parameters);
            });

            Get[RouterPattern.Security.Authenticate] = (parameters =>
            {
                return("test");
            });

            Post[RouterPattern.Security.Authenticate] = parameters =>
            {
                try
                {
                    var authenticationBody = new StreamReader(this.Request.Body).ReadToEnd();

                    logger.Debug("DeserializeObject IAuthenticationRequest.");
                    var authenticationRequest = JsonIocConvert.DeserializeObject <IAuthenticationRequest>(authenticationBody, container);

                    // check whether the request is valid
                    if (authenticationRequest.IsValid)
                    {
                        logger.Debug("IAuthenticationRequest is valid.");

                        // authenticate the user
                        var authenticatonResult = authenticationService.Authenticate(authenticationRequest.Username, authenticationRequest.Password);

                        // return authentication results as JSON
                        return(Response.AsJson <IAuthenticationResult>(authenticatonResult));
                    }
                    else
                    {
                        logger.Warn("IAuthenticationRequest is not valid.");
                        return(BadRequest(authenticationService.InvalidRequest()));
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e);
                    // TODO move this away from authentication service (inner method call)
                    return(BadRequest(authenticationService.ErrorRequest()));
                }
            };
        }
Ejemplo n.º 2
0
        public void ConversionTest()
        {
            // Given
            IContainer container = new Container();

            container.RegisterForApplication();
            var bodyDyn    = new { username = "******", password = "******" };
            var jsonString = "{\"username\":\"admin\",\"password\":\"success-password\"}";

            // When (Deserialize with actual object
            var result1 = JsonIocConvert.DeserializeObject <IAuthenticationRequest>(jsonString, container);

            // Then
            Assert.AreEqual(result1.Username, "admin");
            Assert.AreEqual(result1.Password, "success-password");
            Assert.IsInstanceOf <AuthenticationRequest>(result1);
        }