<commit_before>﻿// Copyright (c) <NAME> & <NAME>. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Duende.IdentityServer;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Validation;
using Microsoft.Extensions.Logging;
using Rsk.Samples.IdentityServer4.AdminUiIntegration.Models;

namespace Rsk.Samples.IdentityServer4.AdminUiIntegration.Services
{
    public class ConsentService
    {
        private readonly IIdentityServerInteractionService interaction;
        private readonly IEventService events;
        private readonly ILogger logger;

        public ConsentService(
            IIdentityServerInteractionService interaction,
            IEventService events,
            ILogger logger)
        {
            this.interaction = interaction;
            this.events = events;
            this.logger = logger;
        }

        public async Task<ProcessConsentResult> ProcessConsent(ConsentInputModel model, string userId)
        {
            var result = new ProcessConsentResult();

            // validate return url is still valid
            var request = await interaction.GetAuthorizationContextAsync(model.ReturnUrl);
            if (request == null) return result;

            ConsentResponse grantedConsent = null;

            // user clicked 'no' - send back the standard 'access_denied' response
            if (model.Button == "no")
            {
                grantedConsent = new ConsentResponse {Error = AuthorizationError.AccessDenied};

                // emit event
                await events.RaiseAsync(new ConsentDeniedEvent(userId, request.Client.ClientId,
                    request.ValidatedResources.RawScopeValues));
            }
            // user clicked 'yes' - validate the data
            else if (model.Button == "yes")
            {
                // if the user consented to some scope, build the response model
                if (model.ScopesConsented != null && model.ScopesConsented.Any())
                {
                    var scopes = model.ScopesConsented;
                    scopes = scopes.Where(x =>
                        x != IdentityServerConstants.StandardScopes.OfflineAccess);

                    grantedConsent = new ConsentResponse
                    {
                        RememberConsent = model.RememberConsent,
                        ScopesValuesConsented = scopes.ToArray(),
                        Description = model.Description
                    };

                    // emit event
                    await events.RaiseAsync(new ConsentGrantedEvent(userId, request.Client.ClientId,
                        request.ValidatedResources.RawScopeValues, grantedConsent.ScopesValuesConsented,
                        grantedConsent.RememberConsent));
                }
                else
                {
                    result.ValidationError = "You must pick at least one permission";
                }
            }
            else
            {
                result.ValidationError = "Invalid selection";
            }

            if (grantedConsent != null)
            {
                // communicate outcome of consent back to identityserver
                await interaction.GrantConsentAsync(request, grantedConsent);<commit_msg>Fixed issue where refresh tokens were not being issued after granting consent to them<commit_after>﻿// Copyright (c) <NAME> & <NAME>. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Duende.IdentityServer;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Validation;
using Microsoft.Extensions.Logging;
using Rsk.Samples.IdentityServer4.AdminUiIntegration.Models;

namespace Rsk.Samples.IdentityServer4.AdminUiIntegration.Services
{
    public class ConsentService
    {
        private readonly IIdentityServerInteractionService interaction;
        private readonly IEventService events;
        private readonly ILogger logger;

        public ConsentService(
            IIdentityServerInteractionService interaction,
            IEventService events,
            ILogger logger)
        {
            this.interaction = interaction;
            this.events = events;
            this.logger = logger;
        }

        public async Task<ProcessConsentResult> ProcessConsent(ConsentInputModel model, string userId)
        {
            var result = new ProcessConsentResult();

            // validate return url is still valid
            var request = await interaction.GetAuthorizationContextAsync(model.ReturnUrl);
            if (request == null) return result;

            ConsentResponse grantedConsent = null;

            // user clicked 'no' - send back the standard 'access_denied' response
            if (model.Button == "no")
            {
                grantedConsent = new ConsentResponse {Error = AuthorizationError.AccessDenied};

                // emit event
                await events.RaiseAsync(new ConsentDeniedEvent(userId, request.Client.ClientId,
                    request.ValidatedResources.RawScopeValues));
            }
            // user clicked 'yes' - validate the data
            else if (model.Button == "yes")
            {
                // if the user consented to some scope, build the response model
                if (model.ScopesConsented != null && model.ScopesConsented.Any())
                {
                    var scopes = model.ScopesConsented;

                    if (!request.ValidatedResources.Resources.OfflineAccess)
                    {
                        scopes = scopes.Where(x =>
                            x != IdentityServerConstants.StandardScopes.OfflineAccess);
                    }
                    
                    grantedConsent = new ConsentResponse
                    {
                        RememberConsent = model.RememberConsent,
                        ScopesValuesConsented = scopes.ToArray(),
                        Description = model.Description
                    };

                    // emit event
                    await events.RaiseAsync(new ConsentGrantedEvent(userId, request.Client.ClientId,
                        request.ValidatedResources.RawScopeValues, grantedConsent.ScopesValuesConsented,
                        grantedConsent.RememberConsent));
                }
                else
                {
                    result.ValidationError = "You must pick at least one permission";
                }
            }
            else
            {
                result.ValidationError = "Invalid selection";
            }

            if (grantedConsent != null)
            {
                // communicate outcome of consent back to identityserver
                await interaction.GrantConsentAsync(request, grantedConsent);
{'fraction_non_alphanumeric': 0.06690528634361234, 'fraction_numerical': 0.0011013215859030838, 'mean_word_length': 1.796381832178599, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7591728', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1604, 'n_words': 426}