Exemplo n.º 1
0
        public void VerifyJwtSecurityTokenSuccessMethod()
        {
            AppSettings appSettings = new AppSettings();

            appSettings.Secret = "1234567890 a very long word";
            var _jwtTokenHandler = new JwtTokenHandler(appSettings);
            var user             = _jwtTokenHandler.VerifyJwtSecurityToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjEiLCJuYmYiOjE1Njk5NTUwOTgsImV4cCI6MTU3MDU1OTg5OCwiaWF0IjoxNTY5OTU1MDk4fQ._d2vCroRoYMGfB76AG14gorMaVcowiOpp6mf_s49zuE");

            Assert.Equal("1", user);
        }
        public static (int, ApplicationException) PayloadValidator(AppSettings lockerConfiguration, bool jwtToken, string jwtSecret, string token, string payloadType, string lockerId, string transactionId, string[] compartmentIds, string captureType)
        {
            int statusCode = StatusCode.Status200OK;
            ApplicationException result = null;

            try
            {
                #region Json Web Token

                if (jwtToken)
                {
                    if (string.IsNullOrEmpty(token))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidToken, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidToken)
                        };
                        return(statusCode, result);
                    }
                    var(isVerified, transactionid) = JwtTokenHandler.VerifyJwtSecurityToken(jwtSecret, token);
                    if ((!isVerified) || string.IsNullOrEmpty(transactionid))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidToken, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidToken)
                        };
                        return(statusCode, result);
                    }
                }

                #endregion


                #region Payload Validation

                switch (payloadType)
                {
                case PayloadTypes.OpenCompartment:

                    if (string.IsNullOrEmpty(transactionId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyTransactionId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyTransactionId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (string.IsNullOrEmpty(lockerId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (compartmentIds == null || compartmentIds.Length == 0)
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyCompartmentId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyCompartmentId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (compartmentIds.Length > 0 && !compartmentIds.Contains("All"))
                    {
                        if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId)
                        {
                            statusCode = StatusCode.Status422UnprocessableEntity;
                            result     = new ApplicationException {
                                Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId)
                            };
                            Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                            return(statusCode, result);
                        }
                        if (lockerConfiguration != null && lockerConfiguration.Locker.Compartments.Count() > 0)
                        {
                            foreach (string compartmentId in compartmentIds)
                            {
                                var flag = lockerConfiguration.Locker.Compartments.Any(com => com.CompartmentId == compartmentId);
                                if (flag)
                                {
                                    continue;
                                }
                                else
                                {
                                    statusCode = StatusCode.Status422UnprocessableEntity;
                                    result     = new ApplicationException
                                    {
                                        Code    = ApplicationErrorCodes.InvalidCompartmentId,
                                        Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCompartmentId)
                                    };
                                    Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                                    return(statusCode, result);
                                }
                            }
                        }
                    }
                    Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                    return(statusCode, result);

                case PayloadTypes.CompartmentStatus:

                    if (string.IsNullOrEmpty(lockerId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId)
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    if (lockerConfiguration != null && lockerConfiguration.Locker.Compartments.Count() > 0 && !compartmentIds.Contains("All"))
                    {
                        foreach (string compartmentId in compartmentIds)
                        {
                            var flag = lockerConfiguration.Locker.Compartments.Any(compartment => compartment.CompartmentId == compartmentId);
                            if (flag)
                            {
                                continue;
                            }
                            else
                            {
                                statusCode = StatusCode.Status422UnprocessableEntity;
                                result     = new ApplicationException
                                {
                                    Code    = ApplicationErrorCodes.InvalidCompartmentId,
                                    Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCompartmentId)
                                };
                                Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                                return(statusCode, result);
                            }
                        }
                    }
                    Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                    return(statusCode, result);

                case PayloadTypes.LockerStatus:

                    if (string.IsNullOrEmpty(lockerId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId)
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }

                    Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");
                    return(statusCode, result);

                case PayloadTypes.CaptureImage:

                    if (string.IsNullOrEmpty(lockerId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId)
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (string.IsNullOrEmpty(captureType))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyCaptureType, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyCaptureType)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (!(captureType == CaptureType.Photo || captureType == CaptureType.Screen))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.InvalidCaptureType, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCaptureType)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }
                    else if (string.IsNullOrEmpty(transactionId))
                    {
                        statusCode = StatusCode.Status422UnprocessableEntity;
                        result     = new ApplicationException {
                            Code = ApplicationErrorCodes.EmptyTransactionId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyTransactionId)
                        };
                        Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");

                        return(statusCode, result);
                    }

                    Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");
                    return(statusCode, result);
                }

                #endregion
            }
            catch (Exception)
            {
                statusCode = StatusCode.Status502BadGateway;
                result     = new ApplicationException
                {
                    Code    = ApplicationErrorCodes.UnknownError,
                    Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.UnknownError)
                };
                Log.Error("[HCM][Locker Management Validator]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]");
            }
            return(statusCode, result);
        }
 public void VerifyJwtTokenContainsTransactionId()
 {
     var(isVerified, transactionId) = JwtTokenHandler.VerifyJwtSecurityToken(secret, token);
     Assert.NotEmpty(transactionId);
 }
 public void VerifyJwtSecurityToken()
 {
     var(isVerified, transactionId) = JwtTokenHandler.VerifyJwtSecurityToken(secret, token);
     Assert.True(isVerified);
 }