public void It_gets_client_by_id()
        {
            IdentityClient client;
            IdentityClient clientFromId;

            using (var transaction = _database.GetTransaction())
            {
                CreateClient(client1);
                CreateClient(client2);
                client       = _clientTable.GetClientByName(client2);
                clientFromId = _clientTable.GetClientById(client.Id);
                transaction.Dispose();
            }

            Assert.AreEqual(client.Id, clientFromId.Id);
        }
        // ----- User funcionality --------------------------------------------

        public Task CreateAsync(TUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(IdentityConstants.User);
            }

            if (string.IsNullOrEmpty(user.ClientId))
            {
                throw new NoNullAllowedException(IdentityConstants.ClientId);
            }

            if (_clientTable.GetClientById(user.ClientId) == null)
            {
                throw new NoNullAllowedException(IdentityConstants.Client);
            }

            _userTable.Insert(user);

            return(Task.FromResult <object>(null));
        }
        /// <summary>
        /// Protecting the data by creating a JWT, containing all claims of the Identity.
        /// The Audience Secret (automatically and randomly generated on client creation)
        /// is retrieved from the client entry saved in the Identity database.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Protect(AuthenticationTicket data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(IdentityConstants.Data);
            }

            var clientId =
                data.Properties.Dictionary.ContainsKey(IdentityConstants.ClientPropertyKey)
                                        ? data.Properties.Dictionary[IdentityConstants.ClientPropertyKey]
                                        : null;

            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new InvalidOperationException("AuthenticationTicket.Properties does not include clientid");
            }

            var client = _clientTable.GetClientById(clientId);

            var symmetricKeyAsBase64 = client.Base64Secret;

            var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);

            var signingKey = new HmacSigningCredentials(keyByteArray);

            var issued = data.Properties.IssuedUtc;

            var expires = data.Properties.ExpiresUtc;

            var token = new JwtSecurityToken(_issuer, clientId, data.Identity.Claims,
                                             issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);

            var handler = new JwtSecurityTokenHandler();

            var jwt = handler.WriteToken(token);

            return(jwt);
        }