Browse Source

Reference PolySharp and replace Substring() calls by the range syntax

pull/1553/head
Kévin Chalet 3 years ago
parent
commit
7ca9be1428
  1. 9
      Directory.Build.props
  2. 4
      gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs
  3. 1
      sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj
  4. 1
      sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj
  5. 16
      src/OpenIddict.Client.Owin/OpenIddictClientOwinHandlers.cs
  6. 7
      src/OpenIddict.Client/OpenIddictClientConfiguration.cs
  7. 2
      src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs
  8. 4
      src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs
  9. 1
      src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj
  10. 1
      src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj
  11. 1
      src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj
  12. 8
      src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs
  13. 32
      src/OpenIddict.Server.Owin/OpenIddictServerOwinHandlers.cs
  14. 4
      src/OpenIddict.Server/OpenIddictServerConfiguration.cs
  15. 4
      src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs
  16. 2
      src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs
  17. 2
      src/OpenIddict.Server/OpenIddictServerHandlers.cs
  18. 2
      src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs
  19. 10
      src/OpenIddict.Validation.Owin/OpenIddictValidationOwinHandlers.cs
  20. 3
      src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs
  21. 2
      src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs
  22. 4
      test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs
  23. 4
      test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs

9
Directory.Build.props

@ -64,6 +64,15 @@
<Serviceable>false</Serviceable> <Serviceable>false</Serviceable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<!--
Note: OpenIddict uses PolySharp to dynamically generate polyfills for types that are not available on
some of the targeted TFMs (e.g Index, Range or nullable attributes on .NET Framework/.NET Standard).
-->
<PackageReference Include="PolySharp" Version="1.3.0" IsImplicitlyDefined="true"
PrivateAssets="all" Condition=" '$(DisablePolySharp)' != 'true' " />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectCapability Include="DynamicDependentFile" /> <ProjectCapability Include="DynamicDependentFile" />
<ProjectCapability Include="DynamicFileNesting" /> <ProjectCapability Include="DynamicFileNesting" />

4
gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs

@ -346,7 +346,7 @@ public partial class OpenIddictClientWebIntegrationBuilder
Collection = (bool?) setting.Attribute("Collection") ?? false, Collection = (bool?) setting.Attribute("Collection") ?? false,
Description = (string) setting.Attribute("Description") is string description ? Description = (string) setting.Attribute("Description") is string description ?
char.ToLower(description[0], CultureInfo.GetCultureInfo("en-US")) + description.Substring(1) : null, char.ToLower(description[0], CultureInfo.GetCultureInfo("en-US")) + description[1..] : null,
ClrType = (string) setting.Attribute("Type") switch ClrType = (string) setting.Attribute("Type") switch
{ {
"EncryptionKey" when (string) setting.Element("EncryptionAlgorithm").Attribute("Value") "EncryptionKey" when (string) setting.Element("EncryptionAlgorithm").Attribute("Value")
@ -875,7 +875,7 @@ public partial class OpenIddictClientWebIntegrationOptions
Collection = (bool?) setting.Attribute("Collection") ?? false, Collection = (bool?) setting.Attribute("Collection") ?? false,
Description = (string) setting.Attribute("Description") is string description ? Description = (string) setting.Attribute("Description") is string description ?
char.ToLower(description[0], CultureInfo.GetCultureInfo("en-US")) + description.Substring(1) : null, char.ToLower(description[0], CultureInfo.GetCultureInfo("en-US")) + description[1..] : null,
ClrType = (string) setting.Attribute("Type") switch ClrType = (string) setting.Attribute("Type") switch
{ {
"EncryptionKey" when (string) setting.Element("EncryptionAlgorithm").Attribute("Value") "EncryptionKey" when (string) setting.Element("EncryptionAlgorithm").Attribute("Value")

1
sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj

@ -8,6 +8,7 @@
<MvcBuildViews>false</MvcBuildViews> <MvcBuildViews>false</MvcBuildViews>
<ImplicitUsings>disable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<NoWarn>CA3147</NoWarn> <NoWarn>CA3147</NoWarn>
<DisablePolySharp>true</DisablePolySharp>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

1
sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj

@ -8,6 +8,7 @@
<MvcBuildViews>false</MvcBuildViews> <MvcBuildViews>false</MvcBuildViews>
<ImplicitUsings>disable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<NoWarn>CA3147</NoWarn> <NoWarn>CA3147</NoWarn>
<DisablePolySharp>true</DisablePolySharp>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

16
src/OpenIddict.Client.Owin/OpenIddictClientOwinHandlers.cs

@ -506,22 +506,22 @@ public static partial class OpenIddictClientOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default
@ -706,22 +706,22 @@ public static partial class OpenIddictClientOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default

7
src/OpenIddict.Client/OpenIddictClientConfiguration.cs

@ -82,8 +82,7 @@ public class OpenIddictClientConfiguration : IPostConfigureOptions<OpenIddictCli
if (registration.MetadataAddress.OriginalString.StartsWith("/", StringComparison.Ordinal)) if (registration.MetadataAddress.OriginalString.StartsWith("/", StringComparison.Ordinal))
{ {
registration.MetadataAddress = new Uri(registration.MetadataAddress.OriginalString.Substring( registration.MetadataAddress = new Uri(registration.MetadataAddress.OriginalString[1..], UriKind.Relative);
1, registration.MetadataAddress.OriginalString.Length - 1), UriKind.Relative);
} }
registration.MetadataAddress = new Uri(issuer, registration.MetadataAddress); registration.MetadataAddress = new Uri(issuer, registration.MetadataAddress);
@ -196,7 +195,7 @@ public class OpenIddictClientConfiguration : IPostConfigureOptions<OpenIddictCli
// Only use the 40 first chars of the base64url-encoded modulus. // Only use the 40 first chars of the base64url-encoded modulus.
var identifier = Base64UrlEncoder.Encode(parameters.Modulus); var identifier = Base64UrlEncoder.Encode(parameters.Modulus);
return identifier.Substring(0, Math.Min(identifier.Length, 40)).ToUpperInvariant(); return identifier[..Math.Min(identifier.Length, 40)].ToUpperInvariant();
} }
#if SUPPORTS_ECDSA #if SUPPORTS_ECDSA
@ -209,7 +208,7 @@ public class OpenIddictClientConfiguration : IPostConfigureOptions<OpenIddictCli
// Only use the 40 first chars of the base64url-encoded X coordinate. // Only use the 40 first chars of the base64url-encoded X coordinate.
var identifier = Base64UrlEncoder.Encode(parameters.Q.X); var identifier = Base64UrlEncoder.Encode(parameters.Q.X);
return identifier.Substring(0, Math.Min(identifier.Length, 40)).ToUpperInvariant(); return identifier[..Math.Min(identifier.Length, 40)].ToUpperInvariant();
} }
#endif #endif

2
src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs

@ -119,7 +119,7 @@ public static partial class OpenIddictClientHandlers
{ AbsolutePath: "/", Query.Length: 0, Fragment.Length: 0 } issuer => new[] { AbsolutePath: "/", Query.Length: 0, Fragment.Length: 0 } issuer => new[]
{ {
issuer.AbsoluteUri, // Uri.AbsoluteUri is normalized and always contains a trailing slash. issuer.AbsoluteUri, // Uri.AbsoluteUri is normalized and always contains a trailing slash.
issuer.AbsoluteUri.Substring(0, issuer.AbsoluteUri.Length - 1) issuer.AbsoluteUri[..^1]
}, },
Uri issuer => new[] { issuer.AbsoluteUri } Uri issuer => new[] { issuer.AbsoluteUri }

4
src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs

@ -1433,10 +1433,10 @@ public class OpenIddictApplicationManager<TApplication> : IOpenIddictApplication
BinaryPrimitives.WriteUInt32BigEndian(payload.Slice(9, 12), (uint) salt.Length); BinaryPrimitives.WriteUInt32BigEndian(payload.Slice(9, 12), (uint) salt.Length);
// Write the salt. // Write the salt.
salt.CopyTo(payload.Slice(13)); salt.CopyTo(payload[13..]);
// Write the subkey. // Write the subkey.
key.CopyTo(payload.Slice(13 + salt.Length)); key.CopyTo(payload[(13 + salt.Length)..]);
return payload; return payload;
} }

1
src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<DisablePolySharp>true</DisablePolySharp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>

1
src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<DisablePolySharp>true</DisablePolySharp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>

1
src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<DisablePolySharp>true</DisablePolySharp>
<SignAssembly>false</SignAssembly> <SignAssembly>false</SignAssembly>
<PublicSign>false</PublicSign> <PublicSign>false</PublicSign>
</PropertyGroup> </PropertyGroup>

8
src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs

@ -760,7 +760,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers
try try
{ {
var value = header.Substring("Basic ".Length).Trim(); var value = header["Basic ".Length..].Trim();
var data = Encoding.ASCII.GetString(Convert.FromBase64String(value)); var data = Encoding.ASCII.GetString(Convert.FromBase64String(value));
var index = data.IndexOf(':'); var index = data.IndexOf(':');
@ -775,8 +775,8 @@ public static partial class OpenIddictServerAspNetCoreHandlers
} }
// Attach the basic authentication credentials to the request message. // Attach the basic authentication credentials to the request message.
context.Transaction.Request.ClientId = UnescapeDataString(data.Substring(0, index)); context.Transaction.Request.ClientId = UnescapeDataString(data[..index]);
context.Transaction.Request.ClientSecret = UnescapeDataString(data.Substring(index + 1)); context.Transaction.Request.ClientSecret = UnescapeDataString(data[(index + 1)..]);
return default; return default;
} }
@ -843,7 +843,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers
} }
// Attach the access token to the request message. // Attach the access token to the request message.
context.Transaction.Request.AccessToken = header.Substring("Bearer ".Length); context.Transaction.Request.AccessToken = header["Bearer ".Length..];
return default; return default;
} }

32
src/OpenIddict.Server.Owin/OpenIddictServerOwinHandlers.cs

@ -318,22 +318,22 @@ public static partial class OpenIddictServerOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default
@ -438,22 +438,22 @@ public static partial class OpenIddictServerOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default
@ -517,22 +517,22 @@ public static partial class OpenIddictServerOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default
@ -815,7 +815,7 @@ public static partial class OpenIddictServerOwinHandlers
try try
{ {
var value = header.Substring("Basic ".Length).Trim(); var value = header["Basic ".Length..].Trim();
var data = Encoding.ASCII.GetString(Convert.FromBase64String(value)); var data = Encoding.ASCII.GetString(Convert.FromBase64String(value));
var index = data.IndexOf(':'); var index = data.IndexOf(':');
@ -830,8 +830,8 @@ public static partial class OpenIddictServerOwinHandlers
} }
// Attach the basic authentication credentials to the request message. // Attach the basic authentication credentials to the request message.
context.Transaction.Request.ClientId = UnescapeDataString(data.Substring(0, index)); context.Transaction.Request.ClientId = UnescapeDataString(data[..index]);
context.Transaction.Request.ClientSecret = UnescapeDataString(data.Substring(index + 1)); context.Transaction.Request.ClientSecret = UnescapeDataString(data[(index + 1)..]);
return default; return default;
} }
@ -898,7 +898,7 @@ public static partial class OpenIddictServerOwinHandlers
} }
// Attach the access token to the request message. // Attach the access token to the request message.
context.Transaction.Request.AccessToken = header.Substring("Bearer ".Length); context.Transaction.Request.AccessToken = header["Bearer ".Length..];
return default; return default;
} }

4
src/OpenIddict.Server/OpenIddictServerConfiguration.cs

@ -318,7 +318,7 @@ public class OpenIddictServerConfiguration : IPostConfigureOptions<OpenIddictSer
// Only use the 40 first chars of the base64url-encoded modulus. // Only use the 40 first chars of the base64url-encoded modulus.
var identifier = Base64UrlEncoder.Encode(parameters.Modulus); var identifier = Base64UrlEncoder.Encode(parameters.Modulus);
return identifier.Substring(0, Math.Min(identifier.Length, 40)).ToUpperInvariant(); return identifier[..Math.Min(identifier.Length, 40)].ToUpperInvariant();
} }
#if SUPPORTS_ECDSA #if SUPPORTS_ECDSA
@ -331,7 +331,7 @@ public class OpenIddictServerConfiguration : IPostConfigureOptions<OpenIddictSer
// Only use the 40 first chars of the base64url-encoded X coordinate. // Only use the 40 first chars of the base64url-encoded X coordinate.
var identifier = Base64UrlEncoder.Encode(parameters.Q.X); var identifier = Base64UrlEncoder.Encode(parameters.Q.X);
return identifier.Substring(0, Math.Min(identifier.Length, 40)).ToUpperInvariant(); return identifier[..Math.Min(identifier.Length, 40)].ToUpperInvariant();
} }
#endif #endif

4
src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs

@ -1487,9 +1487,7 @@ public static partial class OpenIddictServerHandlers
// Note: response types can be specified in any order. To ensure permissions are correctly // Note: response types can be specified in any order. To ensure permissions are correctly
// checked even if the order differs from the one specified in the request, a HashSet is used. // checked even if the order differs from the one specified in the request, a HashSet is used.
var values = permission.Substring(prefix.Length, permission.Length - prefix.Length) var values = permission[prefix.Length..].Split(Separators.Space, StringSplitOptions.RemoveEmptyEntries);
.Split(Separators.Space, StringSplitOptions.RemoveEmptyEntries);
if (values.Length is not 0 && new HashSet<string>(values, StringComparer.Ordinal).SetEquals(types)) if (values.Length is not 0 && new HashSet<string>(values, StringComparer.Ordinal).SetEquals(types))
{ {
return true; return true;

2
src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs

@ -393,7 +393,7 @@ public static partial class OpenIddictServerHandlers
// for Uri's constructor to correctly compute correct absolute URLs. // for Uri's constructor to correctly compute correct absolute URLs.
if (endpoint.OriginalString.StartsWith("/", StringComparison.Ordinal)) if (endpoint.OriginalString.StartsWith("/", StringComparison.Ordinal))
{ {
endpoint = new Uri(endpoint.OriginalString.Substring(1, endpoint.OriginalString.Length - 1), UriKind.Relative); endpoint = new Uri(endpoint.OriginalString[1..], UriKind.Relative);
} }
return new Uri(issuer, endpoint); return new Uri(issuer, endpoint);

2
src/OpenIddict.Server/OpenIddictServerHandlers.cs

@ -3056,7 +3056,7 @@ public static partial class OpenIddictServerHandlers
// for Uri's constructor to correctly compute correct absolute URLs. // for Uri's constructor to correctly compute correct absolute URLs.
if (endpoint.OriginalString.StartsWith("/", StringComparison.Ordinal)) if (endpoint.OriginalString.StartsWith("/", StringComparison.Ordinal))
{ {
endpoint = new Uri(endpoint.OriginalString.Substring(1, endpoint.OriginalString.Length - 1), UriKind.Relative); endpoint = new Uri(endpoint.OriginalString[1..], UriKind.Relative);
} }
return new Uri(issuer, endpoint); return new Uri(issuer, endpoint);

2
src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs

@ -163,7 +163,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers
string? header = request.Headers[HeaderNames.Authorization]; string? header = request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{ {
context.AccessToken = header.Substring("Bearer ".Length); context.AccessToken = header["Bearer ".Length..];
return default; return default;
} }

10
src/OpenIddict.Validation.Owin/OpenIddictValidationOwinHandlers.cs

@ -163,7 +163,7 @@ public static partial class OpenIddictValidationOwinHandlers
string header = request.Headers[Headers.Authorization]; string header = request.Headers[Headers.Authorization];
if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{ {
context.AccessToken = header.Substring("Bearer ".Length); context.AccessToken = header["Bearer ".Length..];
return default; return default;
} }
@ -322,22 +322,22 @@ public static partial class OpenIddictValidationOwinHandlers
{ {
// If the property ends with #string, represent it as a string parameter. // If the property ends with #string, represent it as a string parameter.
string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.String, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.String.Length), Name: key[..^PropertyTypes.String.Length],
Value: new OpenIddictParameter(property.Value)), Value: new OpenIddictParameter(property.Value)),
// If the property ends with #boolean, return it as a boolean parameter. // If the property ends with #boolean, return it as a boolean parameter.
string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Boolean, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Boolean.Length), Name: key[..^PropertyTypes.Boolean.Length],
Value: new OpenIddictParameter(bool.Parse(property.Value))), Value: new OpenIddictParameter(bool.Parse(property.Value))),
// If the property ends with #integer, return it as an integer parameter. // If the property ends with #integer, return it as an integer parameter.
string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Integer, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Integer.Length), Name: key[..^PropertyTypes.Integer.Length],
Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))), Value: new OpenIddictParameter(long.Parse(property.Value, CultureInfo.InvariantCulture))),
// If the property ends with #json, return it as a JSON parameter. // If the property ends with #json, return it as a JSON parameter.
string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => ( string key when key.EndsWith(PropertyTypes.Json, StringComparison.OrdinalIgnoreCase) => (
Name: key.Substring(0, key.Length - PropertyTypes.Json.Length), Name: key[..^PropertyTypes.Json.Length],
Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))), Value: new OpenIddictParameter(JsonSerializer.Deserialize<JsonElement>(property.Value))),
_ => default _ => default

3
src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs

@ -126,8 +126,7 @@ public class OpenIddictValidationConfiguration : IPostConfigureOptions<OpenIddic
if (options.MetadataAddress.OriginalString.StartsWith("/", StringComparison.Ordinal)) if (options.MetadataAddress.OriginalString.StartsWith("/", StringComparison.Ordinal))
{ {
options.MetadataAddress = new Uri(options.MetadataAddress.OriginalString.Substring( options.MetadataAddress = new Uri(options.MetadataAddress.OriginalString[1..], UriKind.Relative);
1, options.MetadataAddress.OriginalString.Length - 1), UriKind.Relative);
} }
options.MetadataAddress = new Uri(issuer, options.MetadataAddress); options.MetadataAddress = new Uri(issuer, options.MetadataAddress);

2
src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs

@ -73,7 +73,7 @@ public static partial class OpenIddictValidationHandlers
{ AbsolutePath: "/", Query.Length: 0, Fragment.Length: 0 } issuer => new[] { AbsolutePath: "/", Query.Length: 0, Fragment.Length: 0 } issuer => new[]
{ {
issuer.AbsoluteUri, // Uri.AbsoluteUri is normalized and always contains a trailing slash. issuer.AbsoluteUri, // Uri.AbsoluteUri is normalized and always contains a trailing slash.
issuer.AbsoluteUri.Substring(0, issuer.AbsoluteUri.Length - 1) issuer.AbsoluteUri[..^1]
}, },
Uri issuer => new[] { issuer.AbsoluteUri } Uri issuer => new[] { issuer.AbsoluteUri }

4
test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs

@ -435,13 +435,13 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable
continue; continue;
} }
var name = line.Substring(0, index); var name = line[..index];
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
{ {
continue; continue;
} }
var value = line.Substring(index + 1); var value = line[(index + 1)..];
parameters.Add(new KeyValuePair<string, string>(name, value)); parameters.Add(new KeyValuePair<string, string>(name, value));
} }

4
test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs

@ -435,13 +435,13 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable
continue; continue;
} }
var name = line.Substring(0, index); var name = line[..index];
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
{ {
continue; continue;
} }
var value = line.Substring(index + 1); var value = line[(index + 1)..];
parameters.Add(new KeyValuePair<string, string>(name, value)); parameters.Add(new KeyValuePair<string, string>(name, value));
} }

Loading…
Cancel
Save