Exemplo n.º 1
0
        public async Task <IActionResult> PostAsync([FromBody] Models.Credentials credentials)
        {
            Models.Customer customer;
            if (AuthenticatedToken != null) //social auth
            {
                NewRelic.Api.Agent.NewRelic.AddCustomParameter("credentials.email", AuthenticatedToken.Payload["email"].ToString());
                customer = await _customerRepository.Get(AuthenticatedToken.Payload["email"].ToString());

                if (customer == null)
                {
                    customer = new Models.Customer
                    {
                        Nombre = AuthenticatedToken.Payload["name"].ToString(),
                        Mail   = AuthenticatedToken.Payload["email"].ToString(),
                        Tipo   = (int)Models.Credentials.Types.Social, //social user
                        Estado = 2,                                    //initial state
                        Condos = new List <Models.Condo>()
                    };
                    if (!await _customerRepository.CreateOrUpdate(customer))
                    {
                        return(new BadRequestObjectResult(customer)); //problems creating customer on db
                    }
                }
            }
            else
            {
                NewRelic.Api.Agent.NewRelic.AddCustomParameter("credentials.email", credentials.email);
                var getUserInfo = await _customerRepository.CheckPassword(credentials);

                if ((getUserInfo == null || getUserInfo.Tables.Count == 0 || getUserInfo.Tables[0].Rows.Count == 0))
                {
                    return(new UnauthorizedResult());
                }
                customer = getUserInfo.Tables[0].Select().ToCustomer();
            }

            if (customer.Estado > 2)
            {
                return(new ForbidResult()); //user disabled
            }
            var defaultDuration = !Request.Query.TryGetValue("tokenDuration", out StringValues customTokenDuration);
            var tokenDuration   = defaultDuration ? 5 : double.Parse(customTokenDuration);
            var jwt             = Jwt.Create(customer, tokenDuration);

            return(new OkObjectResult(new
            {
                email = customer.Mail,
                firstName = customer.Nombre,
                idToken = jwt,
                name = customer.Nombre,
                photoUrl = customer.Icono,
                provider = customer.Tipo == (int)Models.Credentials.Types.Social ? "social" : "internal",
                state = customer.Estado,
                data = customer.Condos,
                validTo = tokenDuration == 0 ? DateTime.MaxValue.ToUniversalTime().ToString() : DateTime.Now.AddMinutes(tokenDuration).ToUniversalTime().ToString()
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Get()
        {
            var loggedCustomer = getLoggedCustomer();

            if (loggedCustomer == null)
            {
                return(new UnauthorizedResult());
            }

            NewRelic.Api.Agent.NewRelic.AddCustomParameter("customer.email", loggedCustomer.Mail);
            //complete customer data
            var result = await _customerRepository.Get(loggedCustomer.Mail);

            if (result == null)
            {
                return(new NotFoundResult());
            }
            return(new OkObjectResult(result));
        }