<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 FluentAssertions;
using IdentityModel.Client;
using IdentityServer4.Core.Models;
using IdentityServer4.Core.Services.InMemory;
using IdentityServer4.Tests.Common;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using Xunit;


namespace IdentityServer4.Tests.Conformance.Basic
{
    public class ClientAuthenticationTests : AuthorizeEndpointTestBase
    {
        const string Category = "Conformance.Basic.ClientAuthenticationTests";

        public ClientAuthenticationTests()
        {
            Scopes.Add(StandardScopes.OpenId);
            Clients.Add(new Client
            {
                Enabled = true,
                ClientId = "code_client",
                ClientSecrets = new List<Secret>
                {
                    new Secret("secret".Sha512())
                },

                Flow = Flows.AuthorizationCode,
                AllowAccessToAllScopes = true,

                RequireConsent = false,
                RedirectUris = new List<string>
                {
                    "https://code_client/callback",
                    "https://code_client/callback?foo=bar&baz=quux"
                }
            });
            Users.Add(new InMemoryUser
            {
                Subject = "bob",
                Username = "bob",
                Claims = new Claim[]
                   {
                        new Claim("name", "<NAME>"),
                        new Claim("email", "<EMAIL>"),
                        new Claim("role", "Attorney"),
                   }
            });
        }

        [Fact]
        [Trait("Category", Category)]
        public async Task Token_endpoint_supports_client_authentication_with_basic_authentication_with_POST()
        {
            await LoginAsync("bob");

            var nonce = Guid.NewGuid().ToString();

            _browser.AllowAutoRedirect = false;
            var url = CreateAuthorizeUrl(
                           clientId: "code_client",
                           responseType: "code",
                           scope: "openid",
                           redirectUri: "https://code_client/callback?foo=bar&baz=quux",
                           nonce: nonce);
            var response = await _client.GetAsync(url);

            var authorization = ParseAuthorizationResponseUrl(response.Headers.Location.ToString());
            authorization.Code.Should().NotBeNull();

            var code = authorization.Code;

            // backchannel client
            var tokenClient = new TokenClient(TokenEndpoint, "code_client", "secret", _server.CreateHandler());
            var tokenResult = await tokenClient.RequestAuthorizationCodeAsync(code, "https://code_client/callback?foo=bar&baz=quux");

            tokenResult.IsError.Should().BeFalse();
            tokenResult.IsHttpError.Should().BeFalse();
            tokenResult.TokenType.Should().Be("Bearer");
            tokenResult.AccessToken.Should().NotBeNull();
            tokenResult.ExpiresIn.Should().BeGreaterThan(0);
            tokenResult.IdentityToken.Should().NotBeNull();

            //result.Headers.CacheControl.NoCache.Should().BeTrue();
            //result.Headers.CacheControl.NoStore.Should().BeTrue();
        }

        //[Fact]
        //[Trait("Category", Category)]
        //public void Token_endpoint_supports_client_authentication_with_form_encoded_authentication_in_POST_body()
        //{
        //    host.Login();

        //    var nonce = Guid.NewGuid().ToString();
        //    var query = host.RequestAuthorizationCode(client_id, redirect_uri, "openid", nonce);
        //    var code = query["code"];

        //    host.NewRequest();

        //    var result = host.PostForm(host.GetTokenUrl(),
        //        new
        //        {
        //            grant_type = "authorization_code",
        //            code,
        //            client_id,
        //            client_secret,
        //            redirect_uri,
        //        }
        //    );

        //    result.StatusCode.Should().Be(HttpStatusCode.OK);
        //    result.Headers.CacheControl.NoCache.Should().BeTrue();
        //    result.Headers.CacheControl.NoStore.Should().BeTrue();

        //    var data = result.ReadJsonObject();
        //    data["token_type"].Should().NotBeNull();
        //    data["token_type"].ToString().Should().Be("Bearer");
        //    data["access_token"].Should().NotBeNull();
        //    data["expires_in"].Should().NotBeNull();
        //    data["id_token"].Should().NotBeNull();
        //}
    }
}<commit_msg>port other client authN conformance test<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 FluentAssertions;
using IdentityModel.Client;
using IdentityServer4.Core.Models;
using IdentityServer4.Core.Services.InMemory;
using IdentityServer4.Tests.Common;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using Xunit;


namespace IdentityServer4.Tests.Conformance.Basic
{
    public class ClientAuthenticationTests : AuthorizeEndpointTestBase
    {
        const string Category = "Conformance.Basic.ClientAuthenticationTests";

        public ClientAuthenticationTests()
        {
            Scopes.Add(StandardScopes.OpenId);
            Clients.Add(new Client
            {
                Enabled = true,
                ClientId = "code_client",
                ClientSecrets = new List<Secret>
                {
                    new Secret("secret".Sha512())
                },

                Flow = Flows.AuthorizationCode,
                AllowAccessToAllScopes = true,

                RequireConsent = false,
                RedirectUris = new List<string>
                {
                    "https://code_client/callback",
                    "https://code_client/callback?foo=bar&baz=quux"
                }
            });
            Users.Add(new InMemoryUser
            {
                Subject = "bob",
                Username = "bob",
                Claims = new Claim[]
                   {
                        new Claim("name", "<NAME>"),
                        new Claim("email", "<EMAIL>"),
                        new Claim("role", "Attorney"),
                   }
            });
        }

        [Fact]
        [Trait("Category", Category)]
        public async Task Token_endpoint_supports_client_authentication_with_basic_authentication_with_POST()
        {
            await LoginAsync("bob");

            var nonce = Guid.NewGuid().ToString();

            _browser.AllowAutoRedirect = false;
            var url = CreateAuthorizeUrl(
                           clientId: "code_client",
                           responseType: "code",
                           scope: "openid",
                           redirectUri: "https://code_client/callback?foo=bar&baz=quux",
                           nonce: nonce);
            var response = await _client.GetAsync(url);

            var authorization = ParseAuthorizationResponseUrl(response.Headers.Location.ToString());
            authorization.Code.Should().NotBeNull();

            var code = authorization.Code;

            // backchannel client
            var tokenClient = new TokenClient(TokenEndpoint, "code_client", "secret", _server.CreateHandler());
            var tokenResult = await tokenClient.RequestAuthorizationCodeAsync(code, "https://code_client/callback?foo=bar&baz=quux");

            tokenResult.IsError.Should().BeFalse();
            tokenResult.IsHttpError.Should().BeFalse();
            tokenResult.TokenType.Should().Be("Bearer");
            tokenResult.AccessToken.Should().NotBeNull();
            tokenResult.ExpiresIn.Should().BeGreaterThan(0);
            tokenResult.IdentityToken.Should().NotBeNull();

            //result.Headers.CacheControl.NoCache.Should().BeTrue();
            //result.Headers.CacheControl.NoStore.Should().BeTrue();
        }

        [Fact]
        [Trait("Category", Category)]
        public async Task Token_endpoint_supports_client_authentication_with_form_encoded_authentication_in_POST_body()
        {
            await LoginAsync("bob");

            var nonce = Guid.NewGuid().ToString();

            _browser.AllowAutoRedirect = false;
            var url = CreateAuthorizeUrl(
                           clientId: "code_client",
                           responseType: "code",
                           scope: "openid",
                           redirectUri: "https://code_client/callback?foo=bar&baz=quux",
                           nonce: nonce);
            var response = await _client.GetAsync(url);

            var authorization = ParseAuthorizationResponseUrl(response.Headers.Location.ToString());
            authorization.Code.Should().NotBeNull();

            var code = authorization.Code;

            // backchannel client
            var tokenClient = new TokenClient(TokenEndpoint, "code_client", "secret", _server.CreateHandler(), AuthenticationStyle.PostValues);
            var tokenResult = await tokenClient.RequestAuthorizationCodeAsync(code, "https://code_client/callback?foo=bar&baz=quux");

            tokenResult.IsError.Should().BeFalse();
            tokenResult.IsHttpError.Should().BeFalse();
            tokenResult.TokenType.Should().Be("Bearer");
            tokenResult.AccessToken.Should().NotBeNull();
            tokenResult.ExpiresIn.Should().BeGreaterThan(0);
            tokenResult.IdentityToken.Should().NotBeNull();
        }
    }
}
{'fraction_non_alphanumeric': 0.11172677815837195, 'fraction_numerical': 0.0021156558533145277, 'mean_word_length': 1.833856694262061, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 10, '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': '6890081', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2430, 'n_words': 427}