Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

claims on refresh token event #3153 #3185

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/Core/Events/TokenService/RefreshTokenRefreshDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

using System.Collections.Generic;

namespace IdentityServer3.Core.Events
{
/// <summary>
Expand Down Expand Up @@ -52,5 +54,13 @@ public class RefreshTokenRefreshDetails
/// The lifetime.
/// </value>
public int Lifetime { get; set; }

/// <summary>
/// Gets or sets current user claims
/// </summary>
/// <value>
/// The claims
/// </value>
public Dictionary<string, object> Claims { get; set; }
}
}
43 changes: 23 additions & 20 deletions source/Core/Extensions/IEventServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ namespace IdentityServer3.Core.Extensions
{
internal static class IEventServiceExtensions
{
public static async Task RaisePreLoginSuccessEventAsync(this IEventService events,
public static async Task RaisePreLoginSuccessEventAsync(this IEventService events,
string signInMessageId, SignInMessage signInMessage, AuthenticateResult authResult)
{
var evt = new Event<LoginDetails>(
EventConstants.Categories.Authentication,
Resources.Events.PreLoginSuccess,
EventTypes.Success,
EventTypes.Success,
EventConstants.Ids.PreLoginSuccess,
new LoginDetails {
SubjectId = authResult.HasSubject ? authResult.User.GetSubjectId() : null,
new LoginDetails
{
SubjectId = authResult.HasSubject ? authResult.User.GetSubjectId() : null,
Name = authResult.User.Identity.Name,
SignInId = signInMessageId,
SignInMessage = signInMessage,
Expand All @@ -45,7 +46,7 @@ public static async Task RaisePreLoginSuccessEventAsync(this IEventService event
await events.RaiseEventAsync(evt);
}

public static async Task RaisePreLoginFailureEventAsync(this IEventService events,
public static async Task RaisePreLoginFailureEventAsync(this IEventService events,
string signInMessageId, SignInMessage signInMessage, string error)
{
var evt = new Event<LoginDetails>(
Expand All @@ -57,13 +58,13 @@ public static async Task RaisePreLoginFailureEventAsync(this IEventService event
{
SignInId = signInMessageId,
SignInMessage = signInMessage,
},
},
error);

await events.RaiseEventAsync(evt);
}

public static async Task RaiseLocalLoginSuccessEventAsync(this IEventService events,
public static async Task RaiseLocalLoginSuccessEventAsync(this IEventService events,
string username, string signInMessageId, SignInMessage signInMessage, AuthenticateResult authResult)
{
var evt = new Event<LocalLoginDetails>(
Expand All @@ -84,7 +85,7 @@ public static async Task RaiseLocalLoginSuccessEventAsync(this IEventService eve
await events.RaiseEventAsync(evt);
}

public static async Task RaiseLocalLoginFailureEventAsync(this IEventService events,
public static async Task RaiseLocalLoginFailureEventAsync(this IEventService events,
string username, string signInMessageId, SignInMessage signInMessage, string error)
{
var evt = new Event<LocalLoginDetails>(
Expand All @@ -97,13 +98,13 @@ public static async Task RaiseLocalLoginFailureEventAsync(this IEventService eve
SignInId = signInMessageId,
SignInMessage = signInMessage,
LoginUserName = username
},
},
error);

await events.RaiseEventAsync(evt);
}

public static async Task RaiseExternalLoginSuccessEventAsync(this IEventService events,
public static async Task RaiseExternalLoginSuccessEventAsync(this IEventService events,
ExternalIdentity externalIdentity, string signInMessageId, SignInMessage signInMessage, AuthenticateResult authResult)
{
var evt = new Event<ExternalLoginDetails>(
Expand All @@ -125,7 +126,7 @@ public static async Task RaiseExternalLoginSuccessEventAsync(this IEventService
await events.RaiseEventAsync(evt);
}

public static async Task RaiseExternalLoginFailureEventAsync(this IEventService events,
public static async Task RaiseExternalLoginFailureEventAsync(this IEventService events,
ExternalIdentity externalIdentity, string signInMessageId, SignInMessage signInMessage, string error)
{
var evt = new Event<ExternalLoginDetails>(
Expand All @@ -139,7 +140,7 @@ public static async Task RaiseExternalLoginFailureEventAsync(this IEventService
SignInMessage = signInMessage,
Provider = externalIdentity.Provider,
ProviderId = externalIdentity.ProviderId,
},
},
error);

await events.RaiseEventAsync(evt);
Expand All @@ -157,7 +158,7 @@ public static async Task RaiseExternalLoginErrorEventAsync(this IEventService ev
await events.RaiseEventAsync(evt);
}

public static async Task RaiseSuccessfulResourceOwnerFlowAuthenticationEventAsync(this IEventService events,
public static async Task RaiseSuccessfulResourceOwnerFlowAuthenticationEventAsync(this IEventService events,
string userName, string subjectId, SignInMessage message)
{
var evt = new Event<LocalLoginDetails>(
Expand All @@ -175,7 +176,7 @@ public static async Task RaiseSuccessfulResourceOwnerFlowAuthenticationEventAsyn
await events.RaiseEventAsync(evt);
}

public static async Task RaiseFailedResourceOwnerFlowAuthenticationEventAsync(this IEventService events,
public static async Task RaiseFailedResourceOwnerFlowAuthenticationEventAsync(this IEventService events,
string userName, SignInMessage message, string error)
{
var evt = new Event<LocalLoginDetails>(
Expand All @@ -193,7 +194,7 @@ public static async Task RaiseFailedResourceOwnerFlowAuthenticationEventAsync(th
await events.RaiseEventAsync(evt);
}

public static async Task RaisePartialLoginCompleteEventAsync(this IEventService events,
public static async Task RaisePartialLoginCompleteEventAsync(this IEventService events,
ClaimsIdentity subject, string signInMessageId, SignInMessage signInMessage)
{
var evt = new Event<LoginDetails>(
Expand All @@ -212,7 +213,7 @@ public static async Task RaisePartialLoginCompleteEventAsync(this IEventService
await events.RaiseEventAsync(evt);
}

public static async Task RaiseLogoutEventAsync(this IEventService events,
public static async Task RaiseLogoutEventAsync(this IEventService events,
ClaimsPrincipal subject, string signOutId, SignOutMessage signOutMessage)
{
var evt = new Event<LogoutDetails>(
Expand All @@ -239,7 +240,8 @@ public static async Task RaiseCspReportEventAsync(this IEventService events, str
EventTypes.Information,
EventConstants.Ids.CspReport);

evt.DetailsFunc = () => {
evt.DetailsFunc = () =>
{
string subject = null;
string name = null;
if (user != null && user.Identity.IsAuthenticated)
Expand All @@ -253,7 +255,7 @@ public static async Task RaiseCspReportEventAsync(this IEventService events, str
{
reportData = Newtonsoft.Json.JsonConvert.DeserializeObject(report);
}
catch(Newtonsoft.Json.JsonReaderException)
catch (Newtonsoft.Json.JsonReaderException)
{
reportData = "Error reading CSP report JSON";
evt.Message = "Raw Report Data: " + report;
Expand Down Expand Up @@ -400,7 +402,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);
Expand All @@ -412,7 +415,7 @@ public static async Task RaiseUnhandledExceptionEventAsync(this IEventService ev
EventConstants.Categories.InternalError,
"Unhandled exception",
EventTypes.Error,
EventConstants.Ids.UnhandledExceptionError,
EventConstants.Ids.UnhandledExceptionError,
exception.ToString());

await events.RaiseEventAsync(evt);
Expand Down
1 change: 1 addition & 0 deletions source/Tests/UnitTests/Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<Compile Include="Endpoints\Connect\PoP\PoPAsymmetricTestsRefresh.cs" />
<Compile Include="Endpoints\Connect\PoP\PoPAsymmetricTestsCode.cs" />
<Compile Include="Endpoints\Connect\PoP\RsaPublicKeyJwk.cs" />
<Compile Include="Events\RefreshTokenRefreshDetailsTests.cs" />
<Compile Include="Services\Default\DefaultCorsPolicyServiceTests.cs" />
<Compile Include="Services\Default\DefaultLocalizationServiceTests.cs" />
<Compile Include="TestCert.cs" />
Expand Down
58 changes: 58 additions & 0 deletions source/Tests/UnitTests/Events/RefreshTokenRefreshDetailsTests.cs
Original file line number Diff line number Diff line change
@@ -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<ClaimsPrincipal>();
claimsPrincipalMock.Setup(x => x.Claims)
.Returns(new List<Claim>()
{
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<IEventService>();

// When
await eventServiceMock.Object.RaiseSuccessfulRefreshTokenRefreshEventAsync(oldHandle,
newHandle,
refreshToken);

// Then
eventServiceMock.Verify(
x => x.RaiseAsync(
It.Is<Event<RefreshTokenRefreshDetails>>(rt
=> rt.Details.Claims.ContainsKey("claim_type_1")
&& rt.Details.Claims.ContainsKey("claim_type_2")
&& rt.Details.Claims.Count == 2)));
}
}
}