Пример #1
0
        public IActionResult RegistrationComplete([FromBody] RegistrationCompleteRequest registrationCompleteRequest)
        {
            Logger.Debug("Request: {0}", Framework.Common.SerializeJson.ToObject(registrationCompleteRequest));
            DateTime dateRequest   = DateTime.Now;
            var      response      = new IResponse <RegistrationCompleteResponse>();
            string   correlationId = string.Empty;

            try
            {
                #region Authorization Usuario y Contraseña
                if (string.IsNullOrEmpty(Request.Headers["Authorization"]))
                {
                    var validate = Models.Response.Error(null, "NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }

                //AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                //var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':');
                correlationId = Request.Headers["Correlation-Id"].ToString();

                Core.Entity.User user = new Core.Entity.User()
                {
                    Public   = registrationCompleteRequest.PublicToken,
                    UserName = registrationCompleteRequest.UserAplication,
                    Password = registrationCompleteRequest.PasswordAplication
                };
                var userAuthenticate = _user.Authenticate(user);
                if (userAuthenticate.Data == null)
                {
                    var validate = Models.Response.Error("NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                Core.Entity.UserPolicy userPolicy = new Core.Entity.UserPolicy()
                {
                    AppUserId = registrationCompleteRequest.AppUserId,
                    IdUser    = ((Core.Entity.User)userAuthenticate.Data).Id
                };
                Core.Entity.Policy policy = new Core.Entity.Policy()
                {
                    Name = Request.Path.Value
                };
                var userPolicyAuthorize = _userPolicy.Authorize(userPolicy, policy);
                if (userPolicyAuthorize.Data == null)
                {
                    var validate = Models.Response.Error("NotUnauthorized");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                #endregion

                AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialToken        = authHeader.Parameter;
                var responsetokenValidated = _tokenManager.GetPrincipalFromExpiredToken(credentialToken);

                if (responsetokenValidated.Data == null)
                {
                    response.Data    = null;
                    response.Message = responsetokenValidated.Message;
                    response.State   = responsetokenValidated.State;
                    return(BadRequest(response));
                }
                var principal   = (ClaimsPrincipal)responsetokenValidated.Data;
                var claimList   = principal.Claims.ToList();
                var verifyEmail = claimList[2].Value;

                if (!verifyEmail.Equals(registrationCompleteRequest.Email.Trim()))
                {
                    var validate = Models.Response.Error("ClientNotSession");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(BadRequest(response));
                }

                /*if (registrationCompleteRequest.Age <= 18 && (DateTime.Now.Year - registrationCompleteRequest.Birthdate.Year) <= 18)
                 * {
                 *  var validate = Models.Response.Error("UserNotAgeApropiate");
                 *  response.Data = null;
                 *  response.Message = validate.Message;
                 *  response.State = validate.State;
                 *  return BadRequest(response);
                 * }*/
                Core.Entity.Client client = new Core.Entity.Client()
                {
                    Email            = registrationCompleteRequest.Email,
                    NameComplete     = registrationCompleteRequest.NameComplete,
                    Gender           = registrationCompleteRequest.Gender,
                    Birthdate        = registrationCompleteRequest.Birthdate,
                    Age              = registrationCompleteRequest.Age,
                    Department       = registrationCompleteRequest.Department,
                    City             = registrationCompleteRequest.City,
                    Address          = registrationCompleteRequest.Address,
                    CellPhone        = registrationCompleteRequest.CellPhone,
                    Phone            = registrationCompleteRequest.Phone,
                    EducationLevel   = registrationCompleteRequest.EducationLevel,
                    Disability       = registrationCompleteRequest.Disability,
                    TypeDisability   = registrationCompleteRequest.TypeDisability,
                    ReferenceName    = registrationCompleteRequest.ReferenceName,
                    ReferencePhone   = registrationCompleteRequest.ReferencePhone,
                    ModificationUser = registrationCompleteRequest.AppUserId
                };

                var clientCreateAcountInsert = _client.UpdateRegistrationComplete(client, registrationCompleteRequest.AppUserId);
                if (!(Convert.ToInt32(clientCreateAcountInsert.Data) > 0))
                {
                    response.Data    = null;
                    response.Message = clientCreateAcountInsert.Message;
                    response.State   = clientCreateAcountInsert.State;
                    return(BadRequest(response));
                }

                response.Data = new RegistrationCompleteResponse
                {
                    Email = registrationCompleteRequest.Email,
                    RegistrationComplete = true,
                    NameComplete         = registrationCompleteRequest.NameComplete
                };
                response.Message = Models.Response.CommentMenssage("RegistrationCompleted");
                response.State   = "000";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                Logger.Error("Message: {0}; Exception: {1}", ex.Message, Framework.Common.SerializeJson.ToObject(ex));
                response.Data    = null;
                response.Message = "Error General";
                response.State   = "099";
                return(BadRequest(response));
            }
            finally
            {
                DateTime dateResponse = DateTime.Now;
                Core.Entity.ConsumptionHistory consumptionHistory = new Core.Entity.ConsumptionHistory
                {
                    ApiName       = Request.Path.Value,
                    Host          = Dns.GetHostName() + ":" + Request.Host.Port,
                    CorrelationId = correlationId,
                    AppUserId     = registrationCompleteRequest.AppUserId,
                    Request       = Framework.Common.SerializeJson.ToObject(registrationCompleteRequest),
                    DateRequest   = dateRequest,
                    Response      = Framework.Common.SerializeJson.ToObject(response),
                    DateResponse  = dateResponse,
                    CodeResponse  = response.State
                };
                _consumptionHistory.Insert(consumptionHistory);
                Logger.Debug("Request: {0} Response: {1}", registrationCompleteRequest, response);
            }
        }
Пример #2
0
        public IActionResult RevokeToken([FromBody] RevokeRequest revokeRequest)
        {
            Logger.Debug("Request: {0}", Framework.Common.SerializeJson.ToObject(revokeRequest));
            DateTime dateRequest   = DateTime.Now;
            var      response      = new IResponse <RevokeResponse>();
            string   correlationId = string.Empty;

            try
            {
                #region Authorization Usuario y Contraseña
                if (string.IsNullOrEmpty(Request.Headers["Authorization"]))
                {
                    var validate = Models.Response.Error(null, "NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }

                //AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                //var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':');
                correlationId = Request.Headers["Correlation-Id"].ToString();

                Core.Entity.User user = new Core.Entity.User()
                {
                    Public   = revokeRequest.PublicToken,
                    UserName = revokeRequest.UserAplication,
                    Password = revokeRequest.PasswordAplication
                };
                var userAuthenticate = _user.Authenticate(user);
                if (userAuthenticate.Data == null)
                {
                    var validate = Models.Response.Error("NotAuthenticated");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                Core.Entity.UserPolicy userPolicy = new Core.Entity.UserPolicy()
                {
                    AppUserId = revokeRequest.AppUserId,
                    IdUser    = ((Core.Entity.User)userAuthenticate.Data).Id
                };
                Core.Entity.Policy policy = new Core.Entity.Policy()
                {
                    Name = Request.Path.Value
                };
                var userPolicyAuthorize = _userPolicy.Authorize(userPolicy, policy);
                if (userPolicyAuthorize.Data == null)
                {
                    var validate = Models.Response.Error("NotUnauthorized");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(Unauthorized(response));
                }
                #endregion

                AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialToken        = authHeader.Parameter;
                var responsetokenValidated = _tokenManager.GetPrincipalFromExpiredToken(credentialToken);

                if (responsetokenValidated.Data == null)
                {
                    response.Data    = null;
                    response.Message = responsetokenValidated.Message;
                    response.State   = responsetokenValidated.State;
                    return(BadRequest(response));
                }
                var principal   = (ClaimsPrincipal)responsetokenValidated.Data;
                var claimList   = principal.Claims.ToList();
                var verifyEmail = claimList[2].Value;

                if (!verifyEmail.Equals(revokeRequest.Email.Trim()))
                {
                    var validate = Models.Response.Error("ClientNotSession");
                    response.Data    = null;
                    response.Message = validate.Message;
                    response.State   = validate.State;
                    return(BadRequest(response));
                }

                var token = revokeRequest.Token ?? Request.Cookies["refreshToken"];

                string ipAddress = "";
                if (Request.Headers.ContainsKey("X-Forwarded-For"))
                {
                    ipAddress = Request.Headers["X-Forwarded-For"];
                }
                else
                {
                    ipAddress = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
                }
                var revokeToken = _refreshToken.RevokeToken(token, ipAddress);
                if (revokeToken.Data == null)
                {
                    response.Data    = null;
                    response.State   = revokeToken.State;
                    response.Message = revokeToken.Message;

                    return(BadRequest(response));
                }

                response.Data = new RevokeResponse
                {
                    Token = revokeRequest.Token
                };
                response.Message = Models.Response.CommentMenssage("TokenRevokeSuccessful");
                response.State   = "000";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                Logger.Error("Message: {0}; Exception: {1}", ex.Message, Framework.Common.SerializeJson.ToObject(ex));
                response.Data    = null;
                response.Message = "Error General";
                response.State   = "099";
                return(BadRequest(response));
            }
            finally
            {
                DateTime dateResponse = DateTime.Now;
                Core.Entity.ConsumptionHistory consumptionHistory = new Core.Entity.ConsumptionHistory
                {
                    ApiName       = Request.Path.Value,
                    Host          = Dns.GetHostName() + ":" + Request.Host.Port,
                    CorrelationId = correlationId,
                    AppUserId     = revokeRequest.AppUserId,
                    Request       = Framework.Common.SerializeJson.ToObject(revokeRequest),
                    DateRequest   = dateRequest,
                    Response      = Framework.Common.SerializeJson.ToObject(response),
                    DateResponse  = dateResponse,
                    CodeResponse  = response.State
                };
                _consumptionHistory.Insert(consumptionHistory);
                Logger.Debug("Request: {0} Response: {1}", revokeRequest, response);
            }
        }