Example #1
0
        public async Task <AccountModel> SignUp(SignUpDto signUpForm)
        {
            return(await base.Execute <AccountModel>(async() =>
            {
                using (UnitOfWork db = new UnitOfWork())
                {
                    IRepo <AccountEntity> accountRepo = db.GetRepo <AccountEntity>();

                    if (await accountRepo.FirstOrDefault(acc => acc.login == signUpForm.Login) != null)
                    {
                        throw new LoginDuplicationException();
                    }

                    PublicKeyModel publicKeyModel = signUpForm.PublicKey.ToModel <PublicKeyModel>();
                    string password = null;

                    try
                    {
                        if (signUpForm.PublicKey == null)
                        {
                            throw new InvalidKeyException();
                        }

                        AsymmetricAlgorithm algorithm = KeyService.GetKeyPair(publicKeyModel);
                        password = EncryptionService.Decrypt(signUpForm.PasswordEncrypted, algorithm);
                    }
                    catch (KeyNotFoundException) { throw new InvalidKeyException(); }
                    catch (FormatException) { throw new PostValidationException("Password format was not match Base64"); }
                    catch (CryptographicException) { throw new WrongEncryptionException("Password"); }

                    if (!PasswordPattern.IsMatch(password))
                    {
                        throw new InvalidPasswordException();
                    }

                    KeyService.DestroyKeyPair(publicKeyModel);

                    AccountEntity accountEntity = new AccountEntity()
                    {
                        login = signUpForm.Login,
                        password = HashingService.GetHashHex(password),
                        first_name = signUpForm.FirstName,
                        last_name = signUpForm.LastName
                    };

                    string authorizedRoleName = DefaultRoles.AUTHORIZED.ToName();
                    accountEntity.Roles.Add(
                        await db.GetRepo <RoleEntity>().FirstOrDefault(
                            role => role.is_default && role.name == authorizedRoleName
                            )
                        );

                    AccountEntity inserted = await accountRepo.Create(accountEntity);
                    await db.Save();

                    return inserted.ToModel <AccountModel>();
                }
            }));
        }
Example #2
0
 internal PersonalCard(CardModel virgilCardDto, PublicKeyModel publicKey, PrivateKey privateKey)
 {
     this.VirgilCardDto         = virgilCardDto;
     this.Id                    = virgilCardDto.Id;
     this.Identity              = new Identity(virgilCardDto.Identity);
     this.PublicKey             = new PublishedPublicKey(publicKey);
     this.Hash                  = virgilCardDto.Hash;
     this.CreatedAt             = virgilCardDto.CreatedAt;
     this.PrivateKey            = privateKey;
     this.IsPrivateKeyEncrypted = VirgilKeyPair.IsPrivateKeyEncrypted(privateKey);
 }
Example #3
0
        public IActionResult SendKey(Guid id, [FromBody] PublicKeyModel publicKey)
        {
            TextReader textReader = new StringReader(publicKey.PublicKey);

            Org.BouncyCastle.OpenSsl.PemReader pemReader      = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
            AsymmetricKeyParameter             publicKeyParam = (AsymmetricKeyParameter)pemReader.ReadObject();

            streamQueueManager.Streams[id.ToString()].PublicKey    = publicKeyParam;
            streamQueueManager.Streams[id.ToString()].StreamerName = publicKey.StreamerName;
            return(Ok());
        }
Example #4
0
        public PublicKeyModel GetNewPublicKey()
        {
            RSA rsa = RSA.Create();

            PublicKeyModel publicKey = new PublicKeyModel(
                rsa.ToXmlString(false)
                );

            Codes.Add(publicKey, rsa);

            return(publicKey);
        }
        public async Task InsertInvalidTest()
        {
            PublicKeyModel key = KeyService.GetNewPublicKey();

            RSA rsa = RSA.Create(
                new RSAParameters()
            {
                Exponent = Convert.FromBase64String(key.Exponent),
                Modulus  = Convert.FromBase64String(key.Modulus)
            }
                );

            SignUpDto signUpForm = new SignUpDto()
            {
                FirstName = "testFirstName",
                LastName  = "testLastName",
                Login     = "******" +
                            "testLongLogintestLongLogintestLongLogintestLongLogintestLongLogintestLongLogin" +
                            "testLongLogintestLongLogintestLongLogintestLongLogintestLongLogintestLongLogintestLongLogintestLongLogin" + new Guid().ToString(), //260
                PasswordEncrypted = Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes("123456789"), RSAEncryptionPadding.Pkcs1)),
                PublicKey         = new PublicKeyDto()
                {
                    Exponent = key.Exponent,
                    Modulus  = key.Modulus
                }
            };

            try
            {
                AccountModel account = await AuthService.SignUp(signUpForm);

                using (UnitOfWork db = new UnitOfWork())
                    await db.GetRepo <AccountEntity>().Delete(account.Id);

                Assert.Fail();
            }
            catch (DataValidationException ex)
            {
                Assert.Pass();
            }
        }
Example #6
0
        public IActionResult EditKey([FromBody] PublicKeyModel model)
        {
            User sessionUser = _repository.UserRepository.GetUserByNormalizedName(User.Identity.Name.ToUpper());

            using (IUpdateUnitOfWork <User, int> uow = _repository.UserRepository.Update(sessionUser.Id))
            {
                uow.Entity.PublicKeys.Find((k) => k.Id == model.KeyNumber).KeyData = Encoding.Default.GetBytes(model.KeyData);
                if (model.Active)
                {
                    uow.Entity.PublicKeys.Find((k) => k.Id == model.KeyNumber).Flag = Key.KeyFlag.ACTIVE;
                }
                else
                {
                    uow.Entity.PublicKeys.Find((k) => k.Id == model.KeyNumber).Flag = Key.KeyFlag.OBSOLET;
                }

                uow.Complete();
            }

            return(Ok());
        }
Example #7
0
        public IActionResult AddNewKey([FromBody] PublicKeyModel model)
        {
            Console.Write("Model: " + model.KeyData);

            if (model.KeyData == null)
            {
                return(Ok());
            }

            User sessionUser = _repository.UserRepository.GetUserByNormalizedName(User.Identity.Name.ToUpper());

            using (IUpdateUnitOfWork <User, int> uow = _repository.UserRepository.Update(sessionUser.Id))
            {
                uow.Entity.PublicKeys.Add(new Key()
                {
                    KeyData = Encoding.UTF8.GetBytes(model.KeyData)
                });
                uow.Complete();
            }

            return(Ok());
        }
Example #8
0
 public void DestroyKeyPair(PublicKeyModel publicKey)
 {
     Codes[publicKey].Dispose();
     Codes.Remove(publicKey);
 }
Example #9
0
 public AsymmetricAlgorithm GetKeyPair(PublicKeyModel publicKey)
 {
     return(Codes[publicKey]);
 }
 public PublishedPublicKey(PublicKeyModel publicKeyDto)
 {
     this.Data      = publicKeyDto.Value;
     this.Id        = publicKeyDto.Id;
     this.CreatedAt = publicKeyDto.CreatedAt;
 }