Skip to content

Commit

Permalink
Add nullable annotations (#466)
Browse files Browse the repository at this point in the history
* first cut

* more

* more

* more

* done
  • Loading branch information
leastprivilege committed Aug 18, 2022
1 parent b31f5f0 commit 44aa2f5
Show file tree
Hide file tree
Showing 48 changed files with 473 additions and 464 deletions.
9 changes: 6 additions & 3 deletions src/ClaimComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ public ClaimComparer(Options options)
}

/// <inheritdoc/>
public override bool Equals(Claim x, Claim y)
public override bool Equals(Claim? x, Claim? y)
{
if (x == null && y == null) return true;
if (x == null && y != null) return false;
if (x != null && y == null) return false;

if (x == null) throw new ArgumentNullException(nameof(x));
if (y == null) throw new ArgumentNullException(nameof(y));

var valueComparison = StringComparison.Ordinal;
if (_options.IgnoreValueCase == true) valueComparison = StringComparison.OrdinalIgnoreCase;

var equal = (String.Equals(x.Type, y.Type, StringComparison.OrdinalIgnoreCase) &&
String.Equals(x.Value, y.Value, valueComparison) &&
String.Equals(x.ValueType, y.ValueType, StringComparison.Ordinal));
String.Equals(x.Value, y.Value, valueComparison) &&
String.Equals(x.ValueType, y.ValueType, StringComparison.Ordinal));

if (_options.IgnoreIssuer)
{
Expand Down
8 changes: 5 additions & 3 deletions src/Client/AuthorityValidationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ namespace IdentityModel.Client;

public struct AuthorityValidationResult
{
public static readonly AuthorityValidationResult SuccessResult = new AuthorityValidationResult(true, null);
public static readonly AuthorityValidationResult SuccessResult = new(true, null);

public string ErrorMessage { get; }

public bool Success { get; }

private AuthorityValidationResult(bool success, string message)
private AuthorityValidationResult(bool success, string? message)
{
if (!success && string.IsNullOrEmpty(message))
{
throw new ArgumentException("A message must be provided if success=false.", nameof(message));
}

ErrorMessage = message;
ErrorMessage = message!;
Success = success;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Client/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ public abstract class ClientOptions
/// <value>
/// The address.
/// </value>
public string Address { get; set; }
public string Address { get; set; } = default!;

/// <summary>
/// Gets or sets the client identifier.
/// </summary>
/// <value>
/// The client identifier.
/// </value>
public string ClientId { get; set; }
public string ClientId { get; set; } = default!;

/// <summary>
/// Gets or sets the client secret.
/// </summary>
/// <value>
/// The client secret.
/// </value>
public string ClientSecret { get; set; }
public string? ClientSecret { get; set; }

/// <summary>
/// Gets or sets the client assertion.
/// </summary>
/// <value>
/// The assertion.
/// </value>
public ClientAssertion ClientAssertion { get; set; } = new ClientAssertion();
public ClientAssertion? ClientAssertion { get; set; } = new();

/// <summary>
/// Gets or sets the client credential style.
Expand All @@ -71,5 +71,5 @@ public abstract class ClientOptions
/// <value>
/// The parameters.
/// </value>
public Parameters Parameters { get; set; } = new Parameters();
public Parameters Parameters { get; set; } = new();
}
8 changes: 4 additions & 4 deletions src/Client/DiscoveryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace IdentityModel.Client;
public class DiscoveryCache : IDiscoveryCache
{
private DateTime _nextReload = DateTime.MinValue;
private AsyncLazy<DiscoveryDocumentResponse> _lazyResponse;
private AsyncLazy<DiscoveryDocumentResponse>? _lazyResponse;

private readonly DiscoveryPolicy _policy;
private readonly Func<HttpMessageInvoker> _getHttpClient;
Expand All @@ -25,7 +25,7 @@ public class DiscoveryCache : IDiscoveryCache
/// </summary>
/// <param name="authority">Base address or discovery document endpoint.</param>
/// <param name="policy">The policy.</param>
public DiscoveryCache(string authority, DiscoveryPolicy policy = null)
public DiscoveryCache(string authority, DiscoveryPolicy? policy = null)
{
_authority = authority;
_policy = policy ?? new DiscoveryPolicy();
Expand All @@ -38,7 +38,7 @@ public DiscoveryCache(string authority, DiscoveryPolicy policy = null)
/// <param name="authority">Base address or discovery document endpoint.</param>
/// <param name="httpClientFunc">The HTTP client function.</param>
/// <param name="policy">The policy.</param>
public DiscoveryCache(string authority, Func<HttpMessageInvoker> httpClientFunc, DiscoveryPolicy policy = null)
public DiscoveryCache(string authority, Func<HttpMessageInvoker> httpClientFunc, DiscoveryPolicy? policy = null)
{
_authority = authority;
_policy = policy ?? new DiscoveryPolicy();
Expand All @@ -61,7 +61,7 @@ public Task<DiscoveryDocumentResponse> GetAsync()
Refresh();
}

return _lazyResponse.Value;
return _lazyResponse!.Value;
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions src/Client/DiscoveryEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public class DiscoveryEndpoint
/// <exception cref="System.InvalidOperationException">
/// Malformed URL
/// </exception>
public static DiscoveryEndpoint ParseUrl(string input, string path = null)
public static DiscoveryEndpoint ParseUrl(string input, string? path = null)
{
if (input == null) throw new ArgumentNullException(nameof(input));

if (String.IsNullOrEmpty(path))
{
path = OidcConstants.Discovery.DiscoveryEndpoint;
Expand All @@ -30,13 +32,13 @@ public static DiscoveryEndpoint ParseUrl(string input, string path = null)
throw new InvalidOperationException("Malformed URL");
}

if (!DiscoveryEndpoint.IsValidScheme(uri))
if (!DiscoveryEndpoint.IsValidScheme(uri!))
{
throw new InvalidOperationException("Malformed URL");
}

var url = input.RemoveTrailingSlash();
if (path.StartsWith("/"))
if (path!.StartsWith("/"))
{
path = path.Substring(1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Client/DiscoveryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class DiscoveryPolicy
/// <summary>
/// Gets or sets the Authority on which the policy checks will be based on
/// </summary>
public string Authority { get; set; }
public string Authority { get; set; } = default!;

/// <summary>
/// The path of the discovery document. Defaults to /.well-known/openid-configuration.
/// </summary>
public string DiscoveryDocumentPath { get; set; }
public string? DiscoveryDocumentPath { get; set; }

/// <summary>
/// Strategy used to validate issuer name and endpoints based on expected authority.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task<BackchannelAuthenticationResponse> RequestBackchannelAu
{
var clone = request.Clone();

if (request.RequestObject.IsPresent())
if (!string.IsNullOrWhiteSpace(request.RequestObject))
{
clone.Parameters.AddOptional(OidcConstants.BackchannelAuthenticationRequest.Request, request.RequestObject);
}
Expand Down
15 changes: 4 additions & 11 deletions src/Client/Extensions/HttpClientDiscoveryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class HttpClientDiscoveryExtensions
/// <param name="address">The address.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpClient client, string address = null, CancellationToken cancellationToken = default)
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpClient client, string? address = null, CancellationToken cancellationToken = default)
{
return await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = address }, cancellationToken).ConfigureAwait();
}
Expand All @@ -36,13 +36,13 @@ public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(th
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpMessageInvoker client, DiscoveryDocumentRequest request, CancellationToken cancellationToken = default)
{
string address;
if (request.Address.IsPresent())
if (request.Address!.IsPresent())
{
address = request.Address;
address = request.Address!;
}
else if (client is HttpClient httpClient)
{
address = httpClient.BaseAddress.AbsoluteUri;
address = httpClient.BaseAddress!.AbsoluteUri;
}
else
{
Expand Down Expand Up @@ -76,13 +76,6 @@ public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(th

var response = await client.SendAsync(clone, cancellationToken).ConfigureAwait();

string responseContent = null;

if (response.Content != null)
{
responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait();
}

if (!response.IsSuccessStatusCode)
{
return await ProtocolResponse.FromHttpResponseAsync<DiscoveryDocumentResponse>(response, $"Error connecting to {url}: {response.ReasonPhrase}").ConfigureAwait();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static async Task<DynamicClientRegistrationResponse> RegisterClientAsync(
clone.Content = new StringContent(JsonSerializer.Serialize(request.Document, options), Encoding.UTF8, "application/json");
clone.Prepare();

if (request.Token.IsPresent())
if (request.Token!.IsPresent())
{
clone.SetBearerToken(request.Token);
clone.SetBearerToken(request.Token!);
}

HttpResponseMessage response;
Expand Down
12 changes: 3 additions & 9 deletions src/Client/Extensions/HttpClientJsonWebKeySetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class HttpClientJsonWebKeySetExtensions
/// <param name="address"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public static async Task<JsonWebKeySetResponse> GetJsonWebKeySetAsync(this HttpMessageInvoker client, string address = null, CancellationToken cancellationToken = default)
public static async Task<JsonWebKeySetResponse> GetJsonWebKeySetAsync(this HttpMessageInvoker client, string? address = null, CancellationToken cancellationToken = default)
{
return await client.GetJsonWebKeySetAsync(new JsonWebKeySetRequest
{
Expand Down Expand Up @@ -51,15 +51,9 @@ public static async Task<JsonWebKeySetResponse> GetJsonWebKeySetAsync(this HttpM
{
response = await client.SendAsync(clone, cancellationToken).ConfigureAwait();

string responseContent = null;
if (response.Content != null)
{
responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait();
}

if (!response.IsSuccessStatusCode)
{
return await ProtocolResponse.FromHttpResponseAsync<JsonWebKeySetResponse>(response, $"Error connecting to {clone.RequestUri.AbsoluteUri}: {response.ReasonPhrase}").ConfigureAwait();
return await ProtocolResponse.FromHttpResponseAsync<JsonWebKeySetResponse>(response, $"Error connecting to {clone.RequestUri!.AbsoluteUri}: {response.ReasonPhrase}").ConfigureAwait();
}
}
catch (OperationCanceledException)
Expand All @@ -68,7 +62,7 @@ public static async Task<JsonWebKeySetResponse> GetJsonWebKeySetAsync(this HttpM
}
catch (Exception ex)
{
return ProtocolResponse.FromException<JsonWebKeySetResponse>(ex, $"Error connecting to {clone.RequestUri.AbsoluteUri}. {ex.Message}.");
return ProtocolResponse.FromException<JsonWebKeySetResponse>(ex, $"Error connecting to {clone.RequestUri!.AbsoluteUri}. {ex.Message}.");
}

return await ProtocolResponse.FromHttpResponseAsync<JsonWebKeySetResponse>(response).ConfigureAwait();
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Extensions/HttpClientUserInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static class HttpClientUserInfoExtensions
/// <returns></returns>
public static async Task<UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default)
{
if (request.Token.IsMissing()) throw new ArgumentNullException(nameof(request.Token));
if (request.Token!.IsMissing()) throw new ArgumentNullException(nameof(request.Token));

var clone = request.Clone();

clone.Method = HttpMethod.Get;
clone.SetBearerToken(request.Token);
clone.SetBearerToken(request.Token!);
clone.Prepare();

HttpResponseMessage response;
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Extensions/JsonElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class JsonElementExtensions
/// <param name="excludeKeys">Claims that should be excluded.</param>
/// <returns></returns>

public static IEnumerable<Claim> ToClaims(this JsonElement json, string issuer = null, params string[] excludeKeys)
public static IEnumerable<Claim> ToClaims(this JsonElement json, string? issuer = null, params string[] excludeKeys)
{
var claims = new List<Claim>();
var excludeList = excludeKeys.ToList();
Expand Down Expand Up @@ -100,7 +100,7 @@ public static JsonElement TryGetValue(this JsonElement json, string name)
/// <param name="json">The json.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static string TryGetString(this JsonElement json, string name)
public static string? TryGetString(this JsonElement json, string name)
{
JsonElement value = json.TryGetValue(name);
return value.ValueKind == JsonValueKind.Undefined ? null : value.ToString();
Expand Down
36 changes: 18 additions & 18 deletions src/Client/Extensions/RequestUrlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ public static string Create(this RequestUrl request, Parameters parameters)
public static string CreateAuthorizeUrl(this RequestUrl request,
string clientId,
string responseType,
string scope = null,
string redirectUri = null,
string state = null,
string nonce = null,
string loginHint = null,
string acrValues = null,
string prompt = null,
string responseMode = null,
string codeChallenge = null,
string codeChallengeMethod = null,
string display = null,
string? scope = null,
string? redirectUri = null,
string? state = null,
string? nonce = null,
string? loginHint = null,
string? acrValues = null,
string? prompt = null,
string? responseMode = null,
string? codeChallenge = null,
string? codeChallengeMethod = null,
string? display = null,
int? maxAge = null,
string uiLocales = null,
string idTokenHint = null,
Parameters extra = null)
string? uiLocales = null,
string? idTokenHint = null,
Parameters? extra = null)
{
var values = new Parameters
{
Expand Down Expand Up @@ -94,10 +94,10 @@ public static string Create(this RequestUrl request, Parameters parameters)
/// <param name="extra">The extra parameters.</param>
/// <returns></returns>
public static string CreateEndSessionUrl(this RequestUrl request,
string idTokenHint = null,
string postLogoutRedirectUri = null,
string state = null,
Parameters extra = null)
string? idTokenHint = null,
string? postLogoutRedirectUri = null,
string? state = null,
Parameters? extra = null)
{
var values = new Parameters();

Expand Down
6 changes: 3 additions & 3 deletions src/Client/IntrospectionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public IntrospectionClient(Func<HttpMessageInvoker> client, IntrospectionClientO
/// </summary>
/// <param name="request">The request.</param>
/// <param name="parameters">The parameters.</param>
internal void ApplyRequestParameters(TokenIntrospectionRequest request, Parameters parameters)
internal void ApplyRequestParameters(TokenIntrospectionRequest request, Parameters? parameters)
{
request.Address = _options.Address;
request.ClientId = _options.ClientId;
request.ClientSecret = _options.ClientSecret;
request.ClientAssertion = _options.ClientAssertion;
request.ClientAssertion = _options.ClientAssertion!;
request.ClientCredentialStyle = _options.ClientCredentialStyle;
request.AuthorizationHeaderStyle = _options.AuthorizationHeaderStyle;
request.Parameters = new Parameters(_options.Parameters);
Expand All @@ -68,7 +68,7 @@ internal void ApplyRequestParameters(TokenIntrospectionRequest request, Paramete
/// <param name="parameters"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<TokenIntrospectionResponse> Introspect(string token, string tokenTypeHint = null, Parameters parameters = null, CancellationToken cancellationToken = default)
public Task<TokenIntrospectionResponse> Introspect(string token, string? tokenTypeHint = null, Parameters? parameters = null, CancellationToken cancellationToken = default)
{
var request = new TokenIntrospectionRequest
{
Expand Down

0 comments on commit 44aa2f5

Please sign in to comment.