<commit_before>﻿using System.Collections.Specialized;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Thinktecture.IdentityServer.Core.Services;

namespace Thinktecture.IdentityServer.Core.Connect
{
    [RoutePrefix("connect/token")]
    public class TokenEndpointController : ApiController
    {
        private ILogger _logger;
        private TokenResponseGenerator _generator;

        private TokenRequestValidator _requestValidator;
        private ClientValidator _clientValidator;
        
        public TokenEndpointController(TokenRequestValidator requestValidator, ClientValidator clientValidator, TokenResponseGenerator generator, ILogger logger)
        {
            _requestValidator = requestValidator;
            _clientValidator = clientValidator;

            _generator = generator;
            _logger = logger;
        }

        [Route]
        public async Task<IHttpActionResult> Post()
        {
            return await Process(await Request.Content.ReadAsFormDataAsync());
        }

        private Task<IHttpActionResult> Process(NameValueCollection parameters)
        {
            _logger.Start("OIDC token endpoint.");

            // validate client credentials on the wire
            var credential = _clientValidator.ValidateRequest(Request.Headers.Authorization, parameters);

            if (credential.IsMalformed || !credential.IsPresent)
            {
                _logger.Error("No or malformed client credential found.");
                return Task.FromResult(this.TokenErrorResponse(Constants.TokenErrors.InvalidClient));
            }

            // validate client against configuration store
            var client = _clientValidator.ValidateClient(credential);
            if (client == null)
            {
                _logger.Error("Invalid client credentials. Aborting.");
                return Task.FromResult(this.TokenErrorResponse(Constants.TokenErrors.InvalidClient));
            }

            // validate the token request
            var result = _requestValidator.ValidateRequest(parameters, client);

            if (result.IsError)
            {
                return Task.FromResult(this.TokenErrorResponse(result.Error));
            }

            // return response
            var response = _generator.Process(_requestValidator.ValidatedRequest);
            return Task.FromResult(this.TokenResponse(response));
        }<commit_msg>Refactored token endpoint controller<commit_after>﻿using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using Thinktecture.IdentityServer.Core.Connect.Models;
using Thinktecture.IdentityServer.Core.Services;

namespace Thinktecture.IdentityServer.Core.Connect
{
    [RoutePrefix("connect/token")]
    public class TokenEndpointController : ApiController
    {
        private ILogger _logger;
        private TokenResponseGenerator _generator;

        private TokenRequestValidator _requestValidator;
        private ClientValidator _clientValidator;
        
        public TokenEndpointController(TokenRequestValidator requestValidator, ClientValidator clientValidator, TokenResponseGenerator generator, ILogger logger)
        {
            _requestValidator = requestValidator;
            _clientValidator = clientValidator;

            _generator = generator;
            _logger = logger;
        }

        [Route]
        public async Task<IHttpActionResult> Post()
        {
            return await Process(await Request.Content.ReadAsFormDataAsync());
        }

        private Task<IHttpActionResult> Process(NameValueCollection parameters)
        {
            _logger.Start("OIDC token endpoint.");

            // validate client credentials and client
            var client = ValidateClient(parameters, Request.Headers.Authorization);
            if (client == null)
            {
                return Task.FromResult(this.TokenErrorResponse(Constants.TokenErrors.InvalidClient));
            }

            // validate the token request
            var result = _requestValidator.ValidateRequest(parameters, client);

            if (result.IsError)
            {
                return Task.FromResult(this.TokenErrorResponse(result.Error));
            }

            // return response
            var response = _generator.Process(_requestValidator.ValidatedRequest);
            return Task.FromResult(this.TokenResponse(response));
        }

        private Client ValidateClient(NameValueCollection parameters, AuthenticationHeaderValue header)
        {
            // validate client credentials on the wire
            var credential = _clientValidator.ValidateRequest(header, parameters);

            if (credential.IsMalformed || !credential.IsPresent)
            {
                _logger.Error("No or malformed client credential found.");
                return null;
            }

            // validate client against configuration store
            var client = _clientValidator.ValidateClient(credential);
            if (client == null)
            {
                _logger.Error("Invalid client credentials. Aborting.");
                return null;
            }

            return client;
        }
{'fraction_non_alphanumeric': 0.07233238904627007, 'fraction_numerical': 0.0, 'mean_word_length': 2.3434343434343434, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4936617', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1163, 'n_words': 267}