public void CreateUserGeneratedSecurityPair_ReadAndDeleteIt_SecurityPairShouldGetDeleted()
        {
            ISecurityKeysApplicationService registrationService =
                (ISecurityKeysApplicationService)_applicationContext["SecurityKeysApplicationService"];
            var                systemGeneratedKey     = registrationService.CreateSystemGeneratedKey(1);
            List <string>      securityKeyPermissions = new List <string>();
            IList <Permission> permissions            = _permissionRepository.GetAllPermissions();

            for (int i = 0; i < permissions.Count; i++)
            {
                securityKeyPermissions.Add(permissions[i].PermissionId);
            }
            CreateUserGeneratedSecurityKeyPair command =
                new CreateUserGeneratedSecurityKeyPair(securityKeyPermissions,
                                                       DateTime.Today.AddDays(1).ToString(), DateTime.Today.AddDays(-2).ToString(),
                                                       DateTime.Today.AddDays(-1).ToString(), true, true, true, "123");
            var keys = registrationService.CreateUserGeneratedKey(command, systemGeneratedKey.Item1.Value);

            registrationService.DeleteSecurityKeyPair("123", systemGeneratedKey.Item1.Value);
            var getKeyPair = _securityKeysRepository.GetByKeyDescriptionAndUserId("123", 1);

            Assert.Null(getKeyPair);
            var getKeyPair1 = _securityKeysRepository.GetByApiKey(keys.ApiKey);

            Assert.Null(getKeyPair1);
        }
        /// <summary>
        /// Set the permissions
        /// </summary>
        public bool UpdateSecurityKeyPair(UpdateUserGeneratedSecurityKeyPair updateCommand, string apiKey)
        {
            if (updateCommand.Validate())
            {
                //get security key pair for user name
                var getSecurityKeyPair = _securityKeysRepository.GetByApiKey(apiKey);
                if (getSecurityKeyPair == null)
                {
                    throw new ArgumentException("Invalid api key");
                }
                var keyPair = _securityKeysRepository.GetByApiKey(updateCommand.ApiKey);
                if (keyPair == null)
                {
                    throw new InvalidOperationException("Invalid Api Key");
                }
                var getKeyPair = _securityKeysRepository.GetByKeyDescriptionAndUserId(updateCommand.KeyDescritpion,
                                                                                      getSecurityKeyPair.UserId);
                //check if key description already exist
                if (getKeyPair != null)
                {
                    if (!getKeyPair.ApiKey.Equals(updateCommand.ApiKey))
                    {
                        throw new InvalidOperationException("The key description already exist");
                    }
                }
                //update parameters
                keyPair.UpdateSecuritykeyPair(updateCommand.KeyDescritpion, updateCommand.EnableStartDate,
                                              updateCommand.EnableEndDate, updateCommand.EnableExpirationDate, updateCommand.EndDateTime,
                                              updateCommand.StartDateTime, updateCommand.ExpirationDateTime);

                //update permissions
                List <SecurityKeysPermission> permissions = new List <SecurityKeysPermission>();
                for (int i = 0; i < updateCommand.SecurityKeyPermissions.Length; i++)
                {
                    permissions.Add(new SecurityKeysPermission(keyPair.ApiKey,
                                                               updateCommand.SecurityKeyPermissions[i].Permission,
                                                               updateCommand.SecurityKeyPermissions[i].Allowed));
                }
                keyPair.UpdatePermissions(permissions.ToArray());
                //persist
                _persistRepository.SaveUpdate(keyPair);
                return(true);
            }
            throw new InvalidOperationException("Please assign atleast one permission.");
        }
Esempio n. 3
0
        public void CreateSecurityKeyPair_PersistAndReadFromDatabaseByDescriptionKey_SavedAndReadInfoShouldBeSame()
        {
            SecurityKeysPair digitalSignatureInfo = new SecurityKeysPair("1", "123456", "secretkey", 1, DateTime.Today.AddDays(1), DateTime.Today.AddDays(-20), DateTime.Today, DateTime.Now, true, null);

            _persistenceRepository.SaveUpdate(digitalSignatureInfo);
            var readInfo = _securityKeysPairRepository.GetByKeyDescriptionAndUserId("1", 1);

            Assert.NotNull(readInfo);
            Assert.AreEqual(readInfo.KeyDescription, "1");
            Assert.AreEqual(readInfo.ApiKey, "123456");
            Assert.AreEqual(readInfo.SecretKey, "secretkey");
            Assert.AreEqual(readInfo.UserId, digitalSignatureInfo.UserId);
            Assert.AreEqual(readInfo.SystemGenerated, digitalSignatureInfo.SystemGenerated);
            Assert.AreEqual(readInfo.ExpirationDate, digitalSignatureInfo.ExpirationDate);
            Assert.AreEqual(readInfo.StartDate, digitalSignatureInfo.StartDate);
            Assert.AreEqual(readInfo.EndDate, digitalSignatureInfo.EndDate);
        }
        /// <summary>
        /// Create user generated api key
        /// </summary>
        /// <returns></returns>
        public static SecurityKeysPair UserGeneratedSecurityPair(int userId, string keyDescription, string apiKey, string secretKey, bool enableExpirationDate, string expirationDate, bool enableStartDate, string startDate, bool enableEndDate, string endDate, List <SecurityKeysPermission> keysPermissions, ISecurityKeysRepository repository)
        {
            //check if key description already exist
            if (repository.GetByKeyDescriptionAndUserId(keyDescription, userId) != null)
            {
                throw new ArgumentException("The key description already exist");
            }
            SecurityKeysPair securityKeysPair = new SecurityKeysPair(apiKey, secretKey, keyDescription, userId, false, keysPermissions);

            if (enableExpirationDate)
            {
                securityKeysPair.ExpirationDate = Convert.ToDateTime(expirationDate);
            }
            else
            {
                securityKeysPair.ExpirationDate = null;
            }
            if (enableStartDate)
            {
                securityKeysPair.StartDate = Convert.ToDateTime(startDate);
            }
            else
            {
                securityKeysPair.StartDate = null;
            }
            if (enableEndDate)
            {
                securityKeysPair.EndDate = Convert.ToDateTime(endDate);
            }
            else
            {
                securityKeysPair.EndDate = null;
            }
            securityKeysPair.EnableStartDate      = enableStartDate;
            securityKeysPair.EnableEndDate        = enableEndDate;
            securityKeysPair.EnableExpirationDate = enableExpirationDate;
            return(securityKeysPair);
        }