internal static async Task<AuthorizationCode> FromDbFormat(StoredAuthorizationCode code, IAsyncDocumentSession s, IScopeStore scopeStore)
        {
            var result = new AuthorizationCode
            {
                CreationTime = code.CreationTime,
                IsOpenId = code.IsOpenId,
                RedirectUri = code.RedirectUri,
                WasConsentShown = code.WasConsentShown,
                Nonce = code.Nonce,
                Client = Data.StoredClient.FromDbFormat(await s.LoadAsync<Data.StoredClient>("clients/" + code.Client)),
                CodeChallenge = code.CodeChallenge,
                CodeChallengeMethod = code.CodeChallengeMethod,
                SessionId = code.SessionId,
                RequestedScopes = await scopeStore.FindScopesAsync(code.RequestedScopes)
            };

            var claimsPrinciple = new ClaimsPrincipal();
            foreach (var id in code.Subject)
            {
                claimsPrinciple.AddIdentity(Data.StoredIdentity.FromDbFormat(id));
            }
            result.Subject = claimsPrinciple;

            return result;
        }
        internal static StoredAuthorizationCode ToDbFormat(string key, AuthorizationCode code)
        {
            var result = new StoredAuthorizationCode
            {
                Id = "authorizationcodes/" + key,
                ClientId = code.ClientId,
                Client = code.Client.ClientId,
                CodeChallenge = code.CodeChallenge,
                WasConsentShown = code.WasConsentShown,
                CreationTime = code.CreationTime,
                Expires = code.CreationTime.AddSeconds(code.Client.AuthorizationCodeLifetime),
                RedirectUri = code.RedirectUri,
                IsOpenId = code.IsOpenId,
                Nonce = code.Nonce,
                SessionId = code.SessionId,
                CodeChallengeMethod = code.CodeChallengeMethod,
                SubjectId = code.SubjectId,
                RequestedScopes = (from s in code.RequestedScopes select s.Name).ToList(),
            };

            result.Subject = new List<StoredIdentity>();
            foreach (var id in code.Subject.Identities)
            {
                result.Subject.Add(Data.StoredIdentity.ToDbFormat(id));
            }

            return result;
        }