Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable nullable in TokenValidator #1677

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE in the project root for license information.


#nullable enable
using Duende.IdentityModel;
using Duende.IdentityServer.Extensions;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -61,7 +62,7 @@ public TokenValidator(
_log = new TokenValidationLog();
}

public async Task<TokenValidationResult> ValidateIdentityTokenAsync(string token, string clientId = null,
public async Task<TokenValidationResult> ValidateIdentityTokenAsync(string token, string? clientId = null,
bool validateLifetime = true)
{
using var activity = Tracing.BasicActivitySource.StartActivity("TokenValidator.ValidateIdentityToken");
Expand Down Expand Up @@ -124,7 +125,7 @@ public async Task<TokenValidationResult> ValidateIdentityTokenAsync(string token
return customResult;
}

public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token, string expectedScope = null)
public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token, string? expectedScope = null)
{
using var activity = Tracing.BasicActivitySource.StartActivity("TokenValidator.ValidateAccessToken");

Expand Down Expand Up @@ -180,7 +181,7 @@ public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token,
}

// make sure client is still active (if client_id claim is present)
var clientClaim = result.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.ClientId);
var clientClaim = result.Claims?.FirstOrDefault(c => c.Type == JwtClaimTypes.ClientId);
if (clientClaim != null)
{
var client = await _clients.FindEnabledClientByIdAsync(clientClaim.Value);
Expand All @@ -197,18 +198,18 @@ public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token,
}

// make sure user is still active (if sub claim is present)
var subClaim = result.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Subject);
var subClaim = result.Claims?.FirstOrDefault(c => c.Type == JwtClaimTypes.Subject);
if (subClaim != null)
{
var principal = Principal.Create("tokenvalidator", result.Claims.ToArray());
var principal = Principal.Create("tokenvalidator", result.Claims!.ToArray());

if (result.ReferenceTokenId.IsPresent())
{
principal.Identities.First()
.AddClaim(new Claim(JwtClaimTypes.ReferenceTokenId, result.ReferenceTokenId));
}

var isActiveCtx = new IsActiveContext(principal, result.Client,
var isActiveCtx = new IsActiveContext(principal, result.Client!,
IdentityServerConstants.ProfileIsActiveCallers.AccessTokenValidation);
await _profile.IsActiveAsync(isActiveCtx);

Expand All @@ -231,7 +232,7 @@ public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token,
{
SubjectId = sub,
SessionId = sid,
Client = result.Client,
Client = result.Client!,
Type = SessionValidationType.AccessToken
});

Expand All @@ -246,7 +247,7 @@ public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token,
// check expected scope(s)
if (expectedScope.IsPresent())
{
var scope = result.Claims.FirstOrDefault(c =>
var scope = result.Claims?.FirstOrDefault(c =>
c.Type == JwtClaimTypes.Scope && c.Value == expectedScope);
if (scope == null)
{
Expand All @@ -272,7 +273,7 @@ public async Task<TokenValidationResult> ValidateAccessTokenAsync(string token,
}

private async Task<TokenValidationResult> ValidateJwtAsync(string jwtString,
IEnumerable<SecurityKeyInfo> validationKeys, bool validateLifetime = true, string audience = null)
IEnumerable<SecurityKeyInfo> validationKeys, bool validateLifetime = true, string? audience = null)
{
using var activity = Tracing.BasicActivitySource.StartActivity("TokenValidator.ValidateJwt");

Expand Down Expand Up @@ -327,7 +328,7 @@ private async Task<TokenValidationResult> ValidateJwtAsync(string jwtString,
}

// load the client that belongs to the client_id claim
Client client = null;
Client? client = null;
var clientId = id.FindFirst(JwtClaimTypes.ClientId);
if (clientId != null)
{
Expand Down Expand Up @@ -388,7 +389,7 @@ private async Task<TokenValidationResult> ValidateReferenceAccessTokenAsync(stri
}

// load the client that is defined in the token
Client client = null;
Client? client = null;
if (token.ClientId != null)
{
client = await _clients.FindEnabledClientByIdAsync(token.ClientId);
Expand Down Expand Up @@ -444,7 +445,7 @@ private IEnumerable<Claim> ReferenceTokenToClaims(Token token)
return claims;
}

private string GetClientIdFromJwt(string token)
private string? GetClientIdFromJwt(string token)
{
try
{
Expand Down