Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leastprivilege committed Jan 5, 2018
1 parent 28ff73d commit 6d61254
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/IdentityModel/Client/AuthorizeResponse.cs
@@ -1,6 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using IdentityModel.Internal;
using System;
using System.Collections.Generic;
using System.Net;
Expand Down Expand Up @@ -108,7 +109,7 @@ public AuthorizeResponse(string raw)
/// <value>
/// <c>true</c> if the response is an error; otherwise, <c>false</c>.
/// </value>
public bool IsError => !string.IsNullOrEmpty(Error);
public bool IsError => Error.IsPresent();

/// <summary>
/// Gets the expires in.
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityModel/Client/DiscoveryClient.cs
Expand Up @@ -129,7 +129,7 @@ public DiscoveryClient(string authority, HttpMessageHandler innerHandler = null)
/// <returns></returns>
public virtual async Task<DiscoveryResponse> GetAsync(CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(Policy.Authority))
if (Policy.Authority.IsMissing())
{
Policy.Authority = Authority;
}
Expand Down
16 changes: 10 additions & 6 deletions src/IdentityModel/Client/DiscoveryResponse.cs
Expand Up @@ -100,7 +100,7 @@ public DiscoveryResponse(string raw, DiscoveryPolicy policy = null)
Json = JObject.Parse(raw);
var validationError = Validate(policy);

if (!string.IsNullOrEmpty(validationError))
if (validationError.IsPresent())
{
IsError = true;
Json = null;
Expand Down Expand Up @@ -186,7 +186,7 @@ private string Validate(DiscoveryPolicy policy)
}

var error = ValidateEndpoints(Json, policy);
if (!string.IsNullOrEmpty(error)) return error;
if (error.IsPresent()) return error;

return string.Empty;
}
Expand Down Expand Up @@ -223,12 +223,16 @@ public bool ValidateIssuerName(string issuer, string authority, StringComparison
public string ValidateEndpoints(JObject json, DiscoveryPolicy policy)
{
// allowed hosts
var allowedHosts = new HashSet<string>(policy.AdditionalEndpointBaseAddresses.Select(e => new Uri(e).Authority));
allowedHosts.Add(new Uri(policy.Authority).Authority);
var allowedHosts = new HashSet<string>(policy.AdditionalEndpointBaseAddresses.Select(e => new Uri(e).Authority))
{
new Uri(policy.Authority).Authority
};

// allowed authorities (hosts + base address)
var allowedAuthorities = new HashSet<string>(policy.AdditionalEndpointBaseAddresses);
allowedAuthorities.Add(policy.Authority);
var allowedAuthorities = new HashSet<string>(policy.AdditionalEndpointBaseAddresses)
{
policy.Authority
};

foreach (var element in json)
{
Expand Down
3 changes: 2 additions & 1 deletion src/IdentityModel/Client/DynamicRegistrationClient.cs
@@ -1,6 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using IdentityModel.Internal;
using Newtonsoft.Json;
using System;
using System.Net;
Expand Down Expand Up @@ -94,7 +95,7 @@ public virtual async Task<RegistrationResponse> RegisterAsync(RegistrationReques
Content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")
};

if (!string.IsNullOrEmpty(token))
if (token.IsPresent())
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
Expand Down
5 changes: 3 additions & 2 deletions src/IdentityModel/Client/RefreshTokenHandler.cs
@@ -1,6 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using IdentityModel.Internal;
using System;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -115,7 +116,7 @@ public RefreshTokenHandler(TokenClient client, string refreshToken, string acces
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var accessToken = await GetAccessTokenAsync(cancellationToken);
if (string.IsNullOrEmpty(accessToken))
if (accessToken.IsMissing())
{
if (await RefreshTokensAsync(cancellationToken) == false)
{
Expand Down Expand Up @@ -159,7 +160,7 @@ protected override void Dispose(bool disposing)
private async Task<bool> RefreshTokensAsync(CancellationToken cancellationToken)
{
var refreshToken = RefreshToken;
if (string.IsNullOrEmpty(refreshToken))
if (refreshToken.IsMissing())
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityModel/Client/Response.cs
Expand Up @@ -109,7 +109,7 @@ protected Response(HttpStatusCode statusCode, string reason, string content = nu
/// <value>
/// <c>true</c> if an error occurred; otherwise, <c>false</c>.
/// </value>
public bool IsError => !string.IsNullOrEmpty(Error);
public bool IsError => Error.IsPresent();

/// <summary>
/// Gets the type of the error.
Expand Down
4 changes: 2 additions & 2 deletions src/IdentityModel/Client/TokenClient.cs
Expand Up @@ -101,7 +101,7 @@ public TokenClient(string address, string clientId, HttpMessageHandler innerHttp
public TokenClient(string address, string clientId, string clientSecret, HttpMessageHandler innerHttpMessageHandler, AuthenticationStyle style = AuthenticationStyle.BasicAuthentication)
: this(address, innerHttpMessageHandler)
{
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException(nameof(clientId));
if (clientId.IsMissing()) throw new ArgumentNullException(nameof(clientId));

AuthenticationStyle = style;
ClientId = clientId;
Expand Down Expand Up @@ -213,7 +213,7 @@ public virtual async Task<TokenResponse> RequestAsync(IDictionary<string, string
{
merged.Add(OidcConstants.TokenRequest.ClientId, ClientId);

if (!string.IsNullOrEmpty(ClientSecret))
if (ClientSecret.IsPresent())
{
merged.Add(OidcConstants.TokenRequest.ClientSecret, ClientSecret);
}
Expand Down
3 changes: 2 additions & 1 deletion src/IdentityModel/Client/UserInfoClient.cs
@@ -1,6 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using IdentityModel.Internal;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -77,7 +78,7 @@ public TimeSpan Timeout
/// <exception cref="System.ArgumentNullException">token</exception>
public virtual async Task<UserInfoResponse> GetAsync(string token, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(token)) throw new ArgumentNullException(nameof(token));
if (token.IsMissing()) throw new ArgumentNullException(nameof(token));

var request = new HttpRequestMessage(HttpMethod.Get, "");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
Expand Down
4 changes: 2 additions & 2 deletions src/IdentityModel/Identity.cs
Expand Up @@ -61,15 +61,15 @@ public static ClaimsIdentity CreateFromCertificate(X509Certificate2 certificate,
claims.Add(new Claim(ClaimTypes.Thumbprint, thumbprint, ClaimValueTypes.Base64Binary, issuer));

var name = certificate.SubjectName.Name;
if (!string.IsNullOrEmpty(name))
if (name.IsPresent())
{
claims.Add(new Claim(ClaimTypes.X500DistinguishedName, name, ClaimValueTypes.String, issuer));
}

if (includeAllClaims)
{
name = certificate.SerialNumber;
if (!string.IsNullOrEmpty(name))
if (name.IsPresent())
{
claims.Add(new Claim(ClaimTypes.SerialNumber, name, ClaimValueTypes.String, issuer));
}
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityModel/Internal/ValuesHelper.cs
Expand Up @@ -31,7 +31,7 @@ public static class ValuesHelper
foreach (var prop in values.GetType().GetRuntimeProperties())
{
var value = prop.GetValue(values) as string;
if (!string.IsNullOrEmpty(value))
if (value.IsPresent())
{
dictionary.Add(prop.Name, value);
}
Expand Down

0 comments on commit 6d61254

Please sign in to comment.