Skip to content

Commit 5053d30

Browse files
Update to IdentityModel v5 (DuendeArchive#127)
* update IdentityModel * replace Json.Net specific plumbing * cleanup
1 parent be9e701 commit 5053d30

9 files changed

Lines changed: 50 additions & 46 deletions

src/IdentityModel.AspNetCore.OAuth2Introspection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</ItemGroup>
3636

3737
<ItemGroup>
38-
<PackageReference Include="IdentityModel" Version="4.6.0" />
38+
<PackageReference Include="IdentityModel" Version="5.0.0" />
3939

4040
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4141
<PackageReference Include="minver" Version="2.4.0" PrivateAssets="All" />

src/Infrastructure/CacheExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33

44
using Microsoft.Extensions.Caching.Distributed;
55
using Microsoft.Extensions.Logging;
6-
using Newtonsoft.Json;
76
using System;
87
using System.Collections.Generic;
98
using System.Linq;
109
using System.Security.Claims;
1110
using System.Text;
11+
using System.Text.Json;
1212
using System.Threading.Tasks;
1313

1414
namespace IdentityModel.AspNetCore.OAuth2Introspection
1515
{
1616
internal static class CacheExtensions
1717
{
18-
internal readonly static JsonSerializerSettings Settings;
18+
internal readonly static JsonSerializerOptions Options;
1919

2020
static CacheExtensions()
2121
{
22-
Settings = new JsonSerializerSettings();
23-
Settings.Converters.Add(new ClaimConverter());
22+
Options = new JsonSerializerOptions
23+
{
24+
IgnoreReadOnlyFields = true,
25+
IgnoreReadOnlyProperties = true,
26+
IgnoreNullValues = true
27+
};
28+
29+
Options.Converters.Add(new ClaimConverter());
2430
}
2531

2632
public static async Task<IEnumerable<Claim>> GetClaimsAsync(this IDistributedCache cache, string cacheKeyPrefix, string token)
@@ -33,7 +39,7 @@ public static async Task<IEnumerable<Claim>> GetClaimsAsync(this IDistributedCac
3339
}
3440

3541
var json = Encoding.UTF8.GetString(bytes);
36-
return JsonConvert.DeserializeObject<IEnumerable<Claim>>(json, Settings);
42+
return JsonSerializer.Deserialize<IEnumerable<Claim>>(json, Options);
3743
}
3844

3945
public static async Task SetClaimsAsync(this IDistributedCache cache, string cacheKeyPrefix, string token, IEnumerable<Claim> claims, TimeSpan duration, ILogger logger)
@@ -66,7 +72,7 @@ public static async Task SetClaimsAsync(this IDistributedCache cache, string cac
6672
absoluteLifetime = now.Add(duration);
6773
}
6874

69-
var json = JsonConvert.SerializeObject(claims, Settings);
75+
var json = JsonSerializer.Serialize(claims, Options);
7076
var bytes = Encoding.UTF8.GetBytes(json);
7177

7278
logger.LogDebug("Setting cache item expiration to {expiration}", absoluteLifetime);
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
1-
// Copyright (c) Dominick Baier & Brock Allen. All rights reserved.
2-
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
34

4-
using Newtonsoft.Json;
55
using System;
66
using System.Security.Claims;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
#pragma warning disable 1591
711

812
namespace IdentityModel.AspNetCore.OAuth2Introspection
913
{
10-
internal class ClaimConverter : JsonConverter
14+
public class ClaimConverter : JsonConverter<Claim>
1115
{
12-
public override bool CanConvert(Type objectType)
13-
{
14-
return typeof(Claim) == objectType;
15-
}
16-
17-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
16+
public override Claim Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1817
{
19-
var source = serializer.Deserialize<ClaimLite>(reader);
18+
var source = JsonSerializer.Deserialize<ClaimLite>(ref reader, options);
2019
var target = new Claim(source.Type, source.Value);
21-
20+
2221
return target;
2322
}
2423

25-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
24+
public override void Write(Utf8JsonWriter writer, Claim value, JsonSerializerOptions options)
2625
{
27-
Claim source = (Claim)value;
28-
2926
var target = new ClaimLite
3027
{
31-
Type = source.Type,
32-
Value = source.Value
28+
Type = value.Type,
29+
Value = value.Value
3330
};
3431

35-
serializer.Serialize(writer, target);
32+
JsonSerializer.Serialize(writer, target, options);
3633
}
3734
}
38-
39-
internal class ClaimLite
40-
{
41-
public string Type { get; set; }
42-
public string Value { get; set; }
43-
}
4435
}

src/Infrastructure/ClaimLite.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
4+
5+
#pragma warning disable 1591
6+
7+
namespace IdentityModel.AspNetCore.OAuth2Introspection
8+
{
9+
public class ClaimLite
10+
{
11+
public string Type { get; set; }
12+
public string Value { get; set; }
13+
}
14+
}

src/Infrastructure/TokenRetrieval.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static Func<HttpRequest, string> FromAuthorizationHeader(string scheme =
4444
/// <returns></returns>
4545
public static Func<HttpRequest, string> FromQueryString(string name = "access_token")
4646
{
47-
return (request) => request.Query[name].FirstOrDefault();
47+
return request => request.Query[name].FirstOrDefault();
4848
}
4949
}
5050
}

src/OAuth2IntrospectionOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Authentication;
77
using Microsoft.AspNetCore.Http;
88
using System;
9-
using System.Collections.Concurrent;
109

1110
namespace IdentityModel.AspNetCore.OAuth2Introspection
1211
{

src/PostConfigureOAuth2IntrospectionOptions.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

44
using System;
5-
using System.Collections.Concurrent;
65
using System.Net.Http;
76
using System.Threading.Tasks;
87
using IdentityModel.AspNetCore.OAuth2Introspection.Infrastructure;
@@ -74,17 +73,14 @@ private async Task<string> GetIntrospectionEndpointFromDiscoveryDocument(OAuth2I
7473

7574
if (disco.IsError)
7675
{
77-
if (disco.ErrorType == ResponseErrorType.Http)
76+
switch (disco.ErrorType)
7877
{
79-
throw new InvalidOperationException($"Discovery endpoint {options.Authority} is unavailable: {disco.Error}");
80-
}
81-
if (disco.ErrorType == ResponseErrorType.PolicyViolation)
82-
{
83-
throw new InvalidOperationException($"Policy error while contacting the discovery endpoint {options.Authority}: {disco.Error}");
84-
}
85-
if (disco.ErrorType == ResponseErrorType.Exception)
86-
{
87-
throw new InvalidOperationException($"Error parsing discovery document from {options.Authority}: {disco.Error}");
78+
case ResponseErrorType.Http:
79+
throw new InvalidOperationException($"Discovery endpoint {options.Authority} is unavailable: {disco.Error}");
80+
case ResponseErrorType.PolicyViolation:
81+
throw new InvalidOperationException($"Policy error while contacting the discovery endpoint {options.Authority}: {disco.Error}");
82+
case ResponseErrorType.Exception:
83+
throw new InvalidOperationException($"Error parsing discovery document from {options.Authority}: {disco.Error}");
8884
}
8985
}
9086

test/Tests/Introspection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.Net;
15-
using System.Security.Claims;
1615
using System.Threading.Tasks;
1716
using Tests.Util;
1817
using Xunit;

test/Tests/Util/IntrospectionEndpointHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Dominick Baier & Brock Allen. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
using IdentityModel;
54
using System;
65
using System.Collections.Generic;
76
using System.Net;

0 commit comments

Comments
 (0)