public virtual void CanAddAndGetSigningCredential()
 {
     using (var uow = DataService.StartUnitOfWork())
     {
         var credential = new SigningCredentialEntity
         {
             Issued   = DateTime.Now,
             Contents = "MyKey"
         };
         uow.SigningCredentialRepository.Add(credential);
         Assert.AreEqual(credential.Contents, uow.SigningCredentialRepository.Get(credential.Id).Contents);
     }
 }
예제 #2
0
        /// <summary>	Gets signing credentials asynchronous. </summary>
        /// <returns>	The signing credentials asynchronous. </returns>
        public Task <SigningCredentials> GetSigningCredentialsAsync()
        {
            return(Task <SigningCredentials> .Factory.StartNew(() =>
            {
                SigningCredentials credentials;
                using (var uow = _dataService.StartUnitOfWork())
                {
                    var latest = uow.SigningCredentialRepository.GetLatest();

                    // check if no valid signing credential is available
                    SigningCredentialEntity credential;
                    if (latest == null || latest.Issued.AddDays(_signingOptions.RolloverDays) < DateTime.UtcNow)
                    {
                        var key = CreateNewRsaKey();
                        var json = JsonConvert.SerializeObject(key,
                                                               new JsonSerializerSettings {
                            ContractResolver = new RsaKeyContractResolver()
                        });
                        credential = new SigningCredentialEntity {
                            Issued = DateTime.UtcNow, Contents = json
                        };
                        credential = uow.SigningCredentialRepository.Add(credential);
                        uow.Commit();
                    }
                    else
                    {
                        credential = latest;
                    }

                    var tempKey = JsonConvert.DeserializeObject <TemporaryRsaKey>(credential.Contents);
                    credentials = new SigningCredentials(CreateRsaSecurityKey(tempKey.Parameters, tempKey.KeyId),
                                                         Algorithm);
                }

                return credentials;
            }));
        }