Skip to content

Commit

Permalink
Use standard-compliant encoding
Browse files Browse the repository at this point in the history
Ensure basic authentication token encoding is standard-compliant.
See dotnet/aspnetcore#10615 (comment).
  • Loading branch information
martincostello committed Jun 4, 2019
1 parent 11a6952 commit 637b515
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
24 changes: 21 additions & 3 deletions src/AspNet.Security.OAuth.Fitbit/FitbitAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]

protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)
{
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}"));

var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
request.Headers.Authorization = CreateAuthorizationHeader();

request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
Expand All @@ -92,5 +90,25 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st

return OAuthTokenResponse.Success(payload);
}

private AuthenticationHeaderValue CreateAuthorizationHeader()
{
string EscapeDataString(string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}

return Uri.EscapeDataString(value).Replace("%20", "+");
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId), ":",
EscapeDataString(Options.ClientSecret))));

return new AuthenticationHeaderValue("Basic", credentials);
}
}
}
24 changes: 21 additions & 3 deletions src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ protected override string FormatScope()

protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)
{
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}"));

var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
request.Headers.Authorization = CreateAuthorizationHeader();

// When a custom user agent is specified in the options, add it to the request headers
// to override the default (generic) user agent used by the OAuth2 base middleware.
Expand Down Expand Up @@ -124,5 +122,25 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st

return OAuthTokenResponse.Success(payload);
}

private AuthenticationHeaderValue CreateAuthorizationHeader()
{
string EscapeDataString(string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}

return Uri.EscapeDataString(value).Replace("%20", "+");
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId), ":",
EscapeDataString(Options.ClientSecret))));

return new AuthenticationHeaderValue("Basic", credentials);
}
}
}
24 changes: 21 additions & 3 deletions src/AspNet.Security.OAuth.Yahoo/YahooAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]

protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)
{
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}"));

var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
request.Headers.Authorization = CreateAuthorizationHeader();

request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
Expand All @@ -92,5 +90,25 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st

return OAuthTokenResponse.Success(payload);
}

private AuthenticationHeaderValue CreateAuthorizationHeader()
{
string EscapeDataString(string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}

return Uri.EscapeDataString(value).Replace("%20", "+");
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId), ":",
EscapeDataString(Options.ClientSecret))));

return new AuthenticationHeaderValue("Basic", credentials);
}
}
}
24 changes: 21 additions & 3 deletions src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]

protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)
{
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}"));

var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
request.Headers.Authorization = CreateAuthorizationHeader();

request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
Expand All @@ -92,5 +90,25 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st

return OAuthTokenResponse.Success(payload);
}

private AuthenticationHeaderValue CreateAuthorizationHeader()
{
string EscapeDataString(string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}

return Uri.EscapeDataString(value).Replace("%20", "+");
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId), ":",
EscapeDataString(Options.ClientSecret))));

return new AuthenticationHeaderValue("Basic", credentials);
}
}
}

0 comments on commit 637b515

Please sign in to comment.