Skip to content

Commit

Permalink
Add Zoom provider (#836)
Browse files Browse the repository at this point in the history
Add Zoom provider.
  • Loading branch information
softwareolatomiwa authored Jun 14, 2024
1 parent e2d2ced commit 7397cb2
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AspNet.Security.OAuth.Providers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.PingO
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.JumpCloud", "src\AspNet.Security.OAuth.JumpCloud\AspNet.Security.OAuth.JumpCloud.csproj", "{8AF5DDBE-2631-4E71-9045-73A6356CE86B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Zoom", "src\AspNet.Security.OAuth.Zoom\AspNet.Security.OAuth.Zoom.csproj", "{A4DFC94C-0769-41B3-926C-22642185C878}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Typeform", "src\AspNet.Security.OAuth.Typeform\AspNet.Security.OAuth.Typeform.csproj", "{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.Calendly", "src\AspNet.Security.OAuth.Calendly\AspNet.Security.OAuth.Calendly.csproj", "{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69}"
Expand Down Expand Up @@ -686,6 +688,10 @@ Global
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.Build.0 = Release|Any CPU
{A4DFC94C-0769-41B3-926C-22642185C878}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4DFC94C-0769-41B3-926C-22642185C878}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4DFC94C-0769-41B3-926C-22642185C878}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4DFC94C-0769-41B3-926C-22642185C878}.Release|Any CPU.Build.0 = Release|Any CPU
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -807,6 +813,7 @@ Global
{101681FB-569F-4941-B943-2AD380039BE0} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{8AF5DDBE-2631-4E71-9045-73A6356CE86B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{A4DFC94C-0769-41B3-926C-22642185C878} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{31333261-A9C2-4AEB-AA6C-AC66DB4FA966} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{ADAC649F-A8CC-4CF2-8C34-288F7DEBBE69} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{83C37AC5-51FB-47CD-8CBE-77AA114FF6F3} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
| Yandex | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Yandex?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Yandex/ "Download AspNet.Security.OAuth.Yandex from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Yandex?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Yandex "Download AspNet.Security.OAuth.Yandex from MyGet.org") | [Documentation](https://tech.yandex.com/oauth/ "Yandex developer documentation") |
| Zalo | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Zalo?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Zalo/ "Download AspNet.Security.OAuth.Zalo from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Zalo?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Zalo "Download AspNet.Security.OAuth.Zalo from MyGet.org") | [Documentation](https://developers.zalo.me/docs/api/social-api-4 "Zalo developer documentation") |
| Zendesk | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Zendesk?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Zendesk/ "Download AspNet.Security.OAuth.Zendesk from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Zendesk?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Zendesk "Download AspNet.Security.OAuth.Zendesk from MyGet.org") | [Documentation](https://support.zendesk.com/hc/en-us/articles/203663836#topic_ar1_mfs_qk "Zendesk developer documentation") |
| Zoom | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Zoom?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Zoom/ "Download AspNet.Security.OAuth.Zoom from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Zoom?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Zoom "Download AspNet.Security.OAuth.Zoom from MyGet.org") | [Documentation](https://developers.zoom.us/docs/integrations/ "Zoom developer documentation") |

<!--
| CHANGEME | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.CHANGEME?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.CHANGEME/ "Download AspNet.Security.OAuth.CHANGEME from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.CHANGEME?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.CHANGEME "Download AspNet.Security.OAuth.CHANGEME from MyGet.org") | [Documentation](CHANGEME "CHANGEME developer documentation") |
Expand Down
24 changes: 24 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/AspNet.Security.OAuth.Zoom.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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>
<PackageTags>aspnetcore;authentication;zoom;oauth;security</PackageTags>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
</ItemGroup>

</Project>
93 changes: 93 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

namespace AspNet.Security.OAuth.Zoom;

/// <summary>
/// Contains constants specific to the <see cref="ZoomAuthenticationHandler"/>.
/// </summary>
public static class ZoomAuthenticationConstants
{
public static class Claims
{
public const string Picture = "picture";

public const string Email = "email";

public const string NameIdentifier = "id";

public const string Name = "name";

public const string GivenName = "given_name";

public const string FamilyName = "family_name";

public const string PhoneNumber = "phone_number";

public const string Status = "account_status";

public const string Verified = "verified";

public const string PersonalMeetingUrl = "personal_meeting_url";
}

/// <summary>
/// Available profile fields after a Zoom authentication.
/// See <a>https://developers.zoom.us/docs/api/rest/reference/user/methods/#operation/user</a>
/// </summary>
public static class ProfileFields
{
/// <summary>
/// The Unique identifier of the user
/// </summary>
public const string Id = "id";

/// <summary>
/// Display name of the user.
/// </summary>
public const string Name = "display_name";

/// <summary>
/// Given/First name of the user.
/// </summary>
public const string GivenName = "first_name";

/// <summary>
/// Last name of the user.
/// </summary>
public const string FamilyName = "last_name";

/// <summary>
/// Email address of the user.
/// </summary>
public const string Email = "email";

/// <summary>
/// Phone number of the user.
/// </summary>
public const string PhoneNumber = "phone_number";

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

/// <summary>
/// AccountStatus of the user.
/// </summary>
public const string Status = "status";

/// <summary>
/// Verification status of the user.
/// </summary>
public const string Verified = "verified";

/// <summary>
/// Personal meeting URL of the user.
/// </summary>
public const string PersonalMeetingUrl = "personal_meeting_url";
}
}
48 changes: 48 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

namespace AspNet.Security.OAuth.Zoom;

/// <summary>
/// Default values used by the Zoom authentication middleware.
/// </summary>
public class ZoomAuthenticationDefaults
{
/// <summary>
/// Default value for <see cref="AuthenticationScheme.Name"/>.
/// </summary>
public const string AuthenticationScheme = "Zoom";

/// <summary>
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
/// </summary>
public static readonly string DisplayName = "Zoom";

/// <summary>
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
/// </summary>
public static readonly string Issuer = "Zoom";

/// <summary>
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
/// </summary>
public static readonly string CallbackPath = "/signin-zoom";

/// <summary>
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
/// </summary>
public static readonly string AuthorizationEndpoint = "https://zoom.us/oauth/authorize";

/// <summary>
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
/// </summary>
public static readonly string TokenEndpoint = "https://zoom.us/oauth/token";

/// <summary>
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
/// </summary>
public static readonly string UserInformationEndpoint = "https://api.zoom.us/v2/users/me";
}
74 changes: 74 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using AspNet.Security.OAuth.Zoom;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods to add Zoom authentication capabilities to an HTTP application pipeline.
/// </summary>
public static class ZoomAuthenticationExtensions
{
/// <summary>
/// Adds <see cref="ZoomAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Zoom authentication capabilities.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to add the middleware to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static AuthenticationBuilder AddZoom([NotNull] this AuthenticationBuilder builder)
{
return builder.AddZoom(ZoomAuthenticationDefaults.AuthenticationScheme, options => { });
}

/// <summary>
/// Adds <see cref="ZoomAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Zoom authentication capabilities.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to add the middleware to.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static AuthenticationBuilder AddZoom(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<ZoomAuthenticationOptions> configuration)
{
return builder.AddZoom(ZoomAuthenticationDefaults.AuthenticationScheme, configuration);
}

/// <summary>
/// Adds <see cref="ZoomAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Zoom authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Zoom options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddZoom(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[NotNull] Action<ZoomAuthenticationOptions> configuration)
{
return builder.AddZoom(scheme, ZoomAuthenticationDefaults.DisplayName, configuration);
}

/// <summary>
/// Adds <see cref="ZoomAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Zoom authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="caption">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Zoom options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddZoom(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[CanBeNull] string caption,
[NotNull] Action<ZoomAuthenticationOptions> configuration)
{
return builder.AddOAuth<ZoomAuthenticationOptions, ZoomAuthenticationHandler>(scheme, caption, configuration);
}
}
71 changes: 71 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

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

namespace AspNet.Security.OAuth.Zoom;

public partial class ZoomAuthenticationHandler : OAuthHandler<ZoomAuthenticationOptions>
{
public ZoomAuthenticationHandler(
[NotNull] IOptionsMonitor<ZoomAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder)
: base(options, logger, encoder)
{
}

protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
var requestUri = Options.UserInformationEndpoint;

using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

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

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
}

private static partial class Log
{
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
{
UserProfileError(
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);
}
}
38 changes: 38 additions & 0 deletions src/AspNet.Security.OAuth.Zoom/ZoomAuthenticationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using System.Security.Claims;
using static AspNet.Security.OAuth.Zoom.ZoomAuthenticationConstants;

namespace AspNet.Security.OAuth.Zoom;

/// <summary>
/// Defines a set of options used by <see cref="ZoomAuthenticationHandler"/>.
/// </summary>
public class ZoomAuthenticationOptions : OAuthOptions
{
public ZoomAuthenticationOptions()
{
ClaimsIssuer = ZoomAuthenticationDefaults.Issuer;
CallbackPath = ZoomAuthenticationDefaults.CallbackPath;

AuthorizationEndpoint = ZoomAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = ZoomAuthenticationDefaults.TokenEndpoint;
UserInformationEndpoint = ZoomAuthenticationDefaults.UserInformationEndpoint;
UsePkce = true;

ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, ProfileFields.Id);
ClaimActions.MapJsonKey(ClaimTypes.Name, ProfileFields.Name);
ClaimActions.MapJsonKey(ClaimTypes.Email, ProfileFields.Email);
ClaimActions.MapJsonKey(ClaimTypes.GivenName, ProfileFields.GivenName);
ClaimActions.MapJsonKey(ClaimTypes.Surname, ProfileFields.FamilyName);
ClaimActions.MapJsonKey(Claims.Picture, ProfileFields.PictureUrl);
ClaimActions.MapJsonKey(ClaimTypes.MobilePhone, ProfileFields.PhoneNumber);
ClaimActions.MapJsonKey(Claims.Status, ProfileFields.Status);
ClaimActions.MapJsonKey(Claims.Verified, ProfileFields.Verified);
ClaimActions.MapJsonKey(Claims.PersonalMeetingUrl, ProfileFields.PersonalMeetingUrl);
}
}
Loading

0 comments on commit 7397cb2

Please sign in to comment.