From 2d7e0283265291c0ab9d6c18150dca34ce292b82 Mon Sep 17 00:00:00 2001 From: Szymon Gaertig Date: Sun, 4 Sep 2016 12:09:23 +0200 Subject: [PATCH] Current claims available in RefreshTokenRefreshDetails #3153 --- .../RefreshTokenRefreshDetails.cs | 10 ++++ .../Extensions/IEventServiceExtensions.cs | 3 +- source/Tests/UnitTests/Core.Tests.csproj | 1 + .../Events/RefreshTokenRefreshDetailsTests.cs | 58 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 source/Tests/UnitTests/Events/RefreshTokenRefreshDetailsTests.cs diff --git a/source/Core/Events/TokenService/RefreshTokenRefreshDetails.cs b/source/Core/Events/TokenService/RefreshTokenRefreshDetails.cs index b48c719ed..9ea6669c7 100644 --- a/source/Core/Events/TokenService/RefreshTokenRefreshDetails.cs +++ b/source/Core/Events/TokenService/RefreshTokenRefreshDetails.cs @@ -14,6 +14,8 @@ * limitations under the License. */ +using System.Collections.Generic; + namespace IdentityServer3.Core.Events { /// @@ -52,5 +54,13 @@ public class RefreshTokenRefreshDetails /// The lifetime. /// public int Lifetime { get; set; } + + /// + /// Gets or sets the claims. + /// + /// + /// The claims. + /// + public Dictionary Claims { get; set; } } } diff --git a/source/Core/Extensions/IEventServiceExtensions.cs b/source/Core/Extensions/IEventServiceExtensions.cs index 88371506e..5d5bd1850 100644 --- a/source/Core/Extensions/IEventServiceExtensions.cs +++ b/source/Core/Extensions/IEventServiceExtensions.cs @@ -400,7 +400,8 @@ public static async Task RaiseSuccessfulRefreshTokenRefreshEventAsync(this IEven OldHandle = oldHandle, NewHandle = newHandle, ClientId = token.ClientId, - Lifetime = token.LifeTime + Lifetime = token.LifeTime, + Claims = token.Subject.Claims.ToClaimsDictionary() }; await events.RaiseEventAsync(evt); diff --git a/source/Tests/UnitTests/Core.Tests.csproj b/source/Tests/UnitTests/Core.Tests.csproj index 2b0d361e5..f2fc9f567 100644 --- a/source/Tests/UnitTests/Core.Tests.csproj +++ b/source/Tests/UnitTests/Core.Tests.csproj @@ -195,6 +195,7 @@ + diff --git a/source/Tests/UnitTests/Events/RefreshTokenRefreshDetailsTests.cs b/source/Tests/UnitTests/Events/RefreshTokenRefreshDetailsTests.cs new file mode 100644 index 000000000..37cbe060d --- /dev/null +++ b/source/Tests/UnitTests/Events/RefreshTokenRefreshDetailsTests.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Security.Claims; +using System.Threading.Tasks; +using IdentityServer3.Core.Events; +using IdentityServer3.Core.Extensions; +using IdentityServer3.Core.Models; +using IdentityServer3.Core.Services; +using Moq; +using Xunit; + +namespace IdentityServer3.Tests.Events +{ + public class RefreshTokenRefreshDetailsTests + { + [Fact] + public async Task When_claims_are_provided_then_create_event_with_given_claims() + { + // Given + var oldHandle = "old_handle"; + var newHandle = "new_handle"; + + var claimsPrincipalMock = new Mock(); + claimsPrincipalMock.Setup(x => x.Claims) + .Returns(new List() + { + new Claim("claim_type_1", "claims_1_value"), + new Claim("claim_type_2", "claims_2_value") + }); + + var refreshToken = new RefreshToken() + { + AccessToken = new Token() + { + Client = new Client() + { + ClientId = "client_id", + AccessTokenLifetime = 10 + }, + }, + Subject = claimsPrincipalMock.Object + }; + var eventServiceMock = new Mock(); + + // When + await eventServiceMock.Object.RaiseSuccessfulRefreshTokenRefreshEventAsync(oldHandle, + newHandle, + refreshToken); + + // Then + eventServiceMock.Verify( + x => x.RaiseAsync( + It.Is>(rt + => rt.Details.Claims.ContainsKey("claim_type_1") + && rt.Details.Claims.ContainsKey("claim_type_2") + && rt.Details.Claims.Count == 2))); + } + } +}