Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
fixes #1831
Browse files Browse the repository at this point in the history
  • Loading branch information
leastprivilege committed Dec 6, 2017
1 parent d950f0f commit 2191d30
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


Expand Down Expand Up @@ -134,7 +134,8 @@ protected virtual async Task<TokenResponse> ProcessAuthorizationCodeRequestAsync
var response = new TokenResponse
{
AccessToken = accessToken,
AccessTokenLifetime = request.ValidatedRequest.AccessTokenLifetime
AccessTokenLifetime = request.ValidatedRequest.AccessTokenLifetime,
Custom = request.CustomResponse
};

//////////////////////////
Expand Down Expand Up @@ -221,7 +222,8 @@ protected virtual async Task<TokenResponse> ProcessRefreshTokenRequestAsync(Toke
IdentityToken = await CreateIdTokenFromRefreshTokenRequestAsync(request.ValidatedRequest, accessTokenString),
AccessToken = accessTokenString,
AccessTokenLifetime = request.ValidatedRequest.AccessTokenLifetime,
RefreshToken = handle
RefreshToken = handle,
Custom = request.CustomResponse
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using FluentAssertions;
using IdentityModel;
using IdentityModel.Client;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace IdentityServer4.IntegrationTests.Clients
{
public class CustomTokenRequestValidatorClient
{
private const string TokenEndpoint = "https://server/connect/token";

private readonly HttpClient _client;
private readonly HttpMessageHandler _handler;

public CustomTokenRequestValidatorClient()
{
var val = new TestCustomTokenRequestValidator();
Startup.CustomTokenRequestValidator = val;

var builder = new WebHostBuilder()
.UseStartup<Startup>();
var server = new TestServer(builder);

_handler = server.CreateHandler();
_client = server.CreateClient();
}

[Fact]
public async Task client_credentials_request_should_contain_custom_response()
{
var client = new TokenClient(
TokenEndpoint,
"client",
"secret",
innerHttpMessageHandler: _handler);

var response = await client.RequestClientCredentialsAsync("api1");

var fields = GetFields(response);
fields.Should().Contain("custom", "custom");
}

[Fact]
public async Task resource_owner_credentials_request_should_contain_custom_response()
{
var client = new TokenClient(
TokenEndpoint,
"roclient",
"secret",
innerHttpMessageHandler: _handler);

var response = await client.RequestResourceOwnerPasswordAsync("bob", "bob", "api1");

var fields = GetFields(response);
fields.Should().Contain("custom", "custom");
}

[Fact]
public async Task refreshing_a_token_should_contain_custom_response()
{
var client = new TokenClient(
TokenEndpoint,
"roclient",
"secret",
innerHttpMessageHandler: _handler);

var response = await client.RequestResourceOwnerPasswordAsync("bob", "bob", "api1 offline_access");
response = await client.RequestRefreshTokenAsync(response.RefreshToken);

var fields = GetFields(response);
fields.Should().Contain("custom", "custom");
}

[Fact]
public async Task extension_grant_request_should_contain_custom_response()
{
var client = new TokenClient(
TokenEndpoint,
"client.custom",
"secret",
innerHttpMessageHandler: _handler);

var customParameters = new Dictionary<string, string>
{
{ "custom_credential", "custom credential"}
};

var response = await client.RequestCustomGrantAsync("custom", "api1", customParameters);

var fields = GetFields(response);
fields.Should().Contain("custom", "custom");
}

private Dictionary<string, object> GetFields(TokenResponse response)
{
return response.Json.ToObject<Dictionary<string, object>>();
}
}
}
9 changes: 9 additions & 0 deletions test/IdentityServer.IntegrationTests/Clients/Setup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
using IdentityServer4.Validation;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace IdentityServer4.IntegrationTests.Clients
{
public class Startup
{
static public ICustomTokenRequestValidator CustomTokenRequestValidator { get; set; }

public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
Expand Down Expand Up @@ -42,6 +45,12 @@ public void ConfigureServices(IServiceCollection services)

builder.AddSecretParser<JwtBearerClientAssertionSecretParser>();
builder.AddSecretValidator<PrivateKeyJwtSecretValidator>();

// add a custom token request validator if set
if (CustomTokenRequestValidator != null)
{
builder.Services.AddTransient(r => CustomTokenRequestValidator);
}
}

public void Configure(IApplicationBuilder app)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using IdentityServer4.Validation;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace IdentityServer4.IntegrationTests.Clients
{
public class TestCustomTokenRequestValidator : ICustomTokenRequestValidator
{
public Task ValidateAsync(CustomTokenRequestValidationContext context)
{
context.Result.CustomResponse = new Dictionary<string, object>
{
{"custom", "custom" }
};

return Task.CompletedTask;
}
}
}

0 comments on commit 2191d30

Please sign in to comment.