Skip to content

Commit

Permalink
Removed redundant codes
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareolatomiwa committed Mar 3, 2024
1 parent 9eed802 commit a89b0dd
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 115 deletions.
16 changes: 0 additions & 16 deletions docs/zoom.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageValidationBaselineVersion>8.0.1</PackageValidationBaselineVersion>
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
</PropertyGroup>

<!-- TODO Enable once this provider is published to NuGet.org -->
<PropertyGroup>
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
</PropertyGroup>

<PropertyGroup>
<Description>ASP.NET Core security middleware enabling Zoom authentication.</Description>
<Authors>Christian Oluwawibe</Authors>
Expand Down
7 changes: 3 additions & 4 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public static class Claims
public static class ProfileFields
{
/// <summary>
/// The unique identifier for the given memembermber. May also be referenced as the <c>personId</c> within a Person URN (<c>urn:li:person:{personId}</c>).
/// The <c>id</c> is unique to your specific developer application. Any attempts to use the <c>id</c> with other developer applications will not succeed.
/// The Unique identifier of the user
/// </summary>
public const string Id = "id";

Expand Down Expand Up @@ -72,7 +71,7 @@ public static class ProfileFields
public const string PhoneNumber = "phone_number";

/// <summary>
/// Picture url of the user.
/// Picture URL of the user.
/// </summary>
public const string PictureUrl = "pic_url";

Expand All @@ -87,7 +86,7 @@ public static class ProfileFields
public const string Verified = "verified";

/// <summary>
/// Personal meeting url of the user.
/// Personal meeting URL of the user.
/// </summary>
public const string PersonalMeetingUrl = "personal_meeting_url";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ public class ZoomAuthenticationDefaults
/// <summary>
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
/// </summary>
public static readonly string UserInformationEndpoint = "https://zoom.us/v2/users/me";
public static readonly string UserInformationEndpoint = "https://api.zoom.us/v2/users/me";
}
80 changes: 0 additions & 80 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;

namespace AspNet.Security.OAuth.Zoom;

Expand Down Expand Up @@ -53,67 +50,6 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
}

protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
{
// eBay uses the RuName for the redirect_uri
return base.BuildChallengeUrl(properties, redirectUri);
}

protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OAuthCodeExchangeContext context)
{
var tokenRequestParameters = new Dictionary<string, string>()
{
["grant_type"] = "authorization_code",
["code"] = context.Code,
["redirect_uri"] = context.RedirectUri,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
if (context.Properties.Items.TryGetValue(OAuthConstants.CodeVerifierKey, out var codeVerifier))
{
tokenRequestParameters.Add(OAuthConstants.CodeVerifierKey, codeVerifier!);
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
}

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

request.Content = new FormUrlEncodedContent(tokenRequestParameters);

using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
await Log.ExchangeCodeErrorAsync(Logger, response, Context.RequestAborted);
return OAuthTokenResponse.Failed(new Exception("An error occurred while retrieving an access token."));
}

var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));

return OAuthTokenResponse.Success(payload);
}

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

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

private static string EscapeDataString(string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}

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

private static partial class Log
{
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
Expand All @@ -125,27 +61,11 @@ internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMes
await response.Content.ReadAsStringAsync(cancellationToken));
}

internal static async Task ExchangeCodeErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
{
ExchangeCodeError(
logger,
response.StatusCode,
response.Headers.ToString(),
await response.Content.ReadAsStringAsync(cancellationToken));
}

[LoggerMessage(1, LogLevel.Error, "An error occurred while retrieving the user profile: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
static partial void UserProfileError(
ILogger logger,
System.Net.HttpStatusCode status,
string headers,
string body);

[LoggerMessage(2, LogLevel.Error, "An error occurred while retrieving an access token: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
static partial void ExchangeCodeError(
ILogger logger,
System.Net.HttpStatusCode status,
string headers,
string body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ZoomAuthenticationOptions()
AuthorizationEndpoint = ZoomAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = ZoomAuthenticationDefaults.TokenEndpoint;
UserInformationEndpoint = ZoomAuthenticationDefaults.UserInformationEndpoint;
UsePkce = true;

ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, ProfileFields.Id);
ClaimActions.MapJsonKey(ClaimTypes.Name, ProfileFields.Name);
Expand Down
17 changes: 5 additions & 12 deletions test/AspNet.Security.OAuth.Providers.Tests/Zoom/ZoomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@ public class ZoomTests(ITestOutputHelper outputHelper) : OAuthTests<ZoomAuthenti

protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
{
builder.AddZoom(options =>
{
ConfigureDefaults(builder, options);
});
}

protected internal override void ConfigureApplication(IApplicationBuilder app)
{
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US"),
});
builder.AddZoom(options => ConfigureDefaults(builder, options));
}

[Theory]
Expand All @@ -35,7 +24,11 @@ protected internal override void ConfigureApplication(IApplicationBuilder app)
[InlineData(ClaimTypes.Email, "[email protected]")]
[InlineData(ClaimTypes.GivenName, "Frodo")]
[InlineData(ClaimTypes.Surname, "Baggins")]
[InlineData(ZoomAuthenticationConstants.Claims.PersonalMeetingUrl, "https://us04web.zoom.us/j/5478221937?pwd=eZUKx2n11af")]
[InlineData(ZoomAuthenticationConstants.Claims.Verified, "0")]
[InlineData(ZoomAuthenticationConstants.Claims.Status, "active")]
[InlineData(ZoomAuthenticationConstants.Claims.Picture, "https://upload.wikimedia.org/wikipedia/en/4/4e/Elijah_Wood_as_Frodo_Baggins.png")]
[InlineData(ClaimTypes.MobilePhone, "+2348012345678")]
public async Task Can_Sign_In_Using_Zoom(string claimType, string claimValue)
{
await AuthenticateUserAndAssertClaimValue(claimType, claimValue);
Expand Down
8 changes: 6 additions & 2 deletions test/AspNet.Security.OAuth.Providers.Tests/Zoom/bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
},
{
"comment": "https://developers.zoom.us/docs/api/rest/reference/user/methods/#tag/Users",
"uri": "https://zoom.us/v2/users/me",
"uri": "https://api.zoom.us/v2/users/me",
"contentFormat": "json",
"contentJson": {
"id": "0ECPVTrOjh",
"email": "[email protected]",
"display_name": "Frodo Baggins",
"first_name": "Frodo",
"last_name": "Baggins",
"pic_url": "https://upload.wikimedia.org/wikipedia/en/4/4e/Elijah_Wood_as_Frodo_Baggins.png"
"pic_url": "https://upload.wikimedia.org/wikipedia/en/4/4e/Elijah_Wood_as_Frodo_Baggins.png",
"personal_meeting_url": "https://us04web.zoom.us/j/5478221937?pwd=eZUKx2n11af",
"verified": 0,
"status": "active",
"phone_number": "+2348012345678"
}
}
]
Expand Down

0 comments on commit a89b0dd

Please sign in to comment.