public AuthorizationCode Deserialize(BsonDocument doc)
        {
            var code = new AuthorizationCode();

            code.CreationTime    = doc.GetValueOrDefault("creationTime", code.CreationTime);
            code.IsOpenId        = doc.GetValueOrDefault("isOpenId", code.IsOpenId);
            code.RedirectUri     = doc.GetValueOrDefault("redirectUri", code.RedirectUri);
            code.WasConsentShown = doc.GetValueOrDefault("wasConsentShown", code.WasConsentShown);


            var claimsPrincipal = new ClaimsPrincipal();
            IEnumerable <ClaimsIdentity> identities = doc.GetValueOrDefault("subject", sub =>
            {
                string authenticationType = sub.GetValueOrDefault("authenticationType", (string)null);
                var claims = sub.GetNestedValueOrDefault("claimSet", _claimSetSerializer.Deserialize, new Claim[] {});
                ClaimsIdentity identity = authenticationType == null
                    ? new ClaimsIdentity(claims)
                    : new ClaimsIdentity(claims, authenticationType);
                return(identity);
            }, new ClaimsIdentity[] {});

            claimsPrincipal.AddIdentities(identities);
            code.Subject = claimsPrincipal;

            code.Client = _clientSerializer.Deserialize(doc["client"].AsBsonDocument);

            code.RequestedScopes = doc.GetValueOrDefault(
                "requestedScopes",
                _scopeSerializer.Deserialize,
                code.RequestedScopes);
            return(code);
        }
        public Task <Client> FindClientByIdAsync(string clientId)
        {
            Client       result = null;
            BsonDocument loaded = Collection.FindOneById(clientId);

            if (loaded != null)
            {
                result = _serializer.Deserialize(loaded);
            }

            return(Task.FromResult(result));
        }