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

Commit

Permalink
include sid (if present) in access tokens #3955 (#4202)
Browse files Browse the repository at this point in the history
  • Loading branch information
brockallen authored Mar 25, 2020
1 parent 8f0b87a commit 3046d95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ public virtual async Task<Token> CreateAccessTokenAsync(TokenCreationRequest req
claims.Add(new Claim(JwtClaimTypes.JwtId, CryptoRandom.CreateUniqueId(16)));
}

if (request.ValidatedRequest.SessionId.IsPresent())
{
claims.Add(new Claim(JwtClaimTypes.SessionId, request.ValidatedRequest.SessionId));
}

var issuer = ContextAccessor.HttpContext.GetIdentityServerIssuerUri();
var token = new Token(OidcConstants.TokenTypes.AccessToken)
{
Expand Down Expand Up @@ -236,7 +241,7 @@ public virtual async Task<Token> CreateAccessTokenAsync(TokenCreationRequest req
}
}
}

return token;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using IdentityModel;
using IdentityServer.UnitTests.Common;
using IdentityServer4.Configuration;
using IdentityServer4.Models;
Expand Down Expand Up @@ -114,5 +115,41 @@ public async Task CreateAccessTokenAsync_when_no_apiresources_should_not_include

result.Audiences.Count.Should().Be(0);
}


[Fact]
public async Task CreateAccessTokenAsync_when_no_session_should_not_include_sid()
{
var request = new TokenCreationRequest
{
ValidatedResources = new ResourceValidationResult(),
ValidatedRequest = new ValidatedRequest()
{
Client = new Client { },
SessionId = null
}
};

var result = await _subject.CreateAccessTokenAsync(request);

result.Claims.SingleOrDefault(x => x.Type == JwtClaimTypes.SessionId).Should().BeNull();
}
[Fact]
public async Task CreateAccessTokenAsync_when_session_should_include_sid()
{
var request = new TokenCreationRequest
{
ValidatedResources = new ResourceValidationResult(),
ValidatedRequest = new ValidatedRequest()
{
Client = new Client { },
SessionId = "123"
}
};

var result = await _subject.CreateAccessTokenAsync(request);

result.Claims.SingleOrDefault(x => x.Type == JwtClaimTypes.SessionId).Value.Should().Be("123");
}
}
}

0 comments on commit 3046d95

Please sign in to comment.