Skip to content

Commit

Permalink
Add default OpenId scopes (#16661)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Sep 3, 2024
1 parent 4a89f4d commit fc4c439
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OpenIddict.Abstractions;
using OrchardCore.Data.Migration;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.OpenId.Abstractions.Descriptors;
using OrchardCore.OpenId.Abstractions.Managers;

namespace OrchardCore.OpenId.Migrations;

public sealed class DefaultScopesMigration : DataMigration
{
#pragma warning disable CA1822 // Mark members as static
public int Create()
#pragma warning restore CA1822 // Mark members as static
{
ShellScope.AddDeferredTask(async shellScope =>
{
var S = shellScope.ServiceProvider.GetService<IStringLocalizer<DefaultScopesMigration>>();
var scopeManager = shellScope.ServiceProvider.GetRequiredService<IOpenIdScopeManager>();
if (await scopeManager.FindByNameAsync(OpenIddictConstants.Scopes.Profile) == null)
{
var descriptor = new OpenIdScopeDescriptor
{
DisplayName = S["Profile"],
Name = OpenIddictConstants.Scopes.Profile,
Description = S["Requests access to the user's default profile information."]
};
await scopeManager.CreateAsync(descriptor);
}
if (await scopeManager.FindByNameAsync(OpenIddictConstants.Scopes.Email) == null)
{
var descriptor = new OpenIdScopeDescriptor
{
DisplayName = S["Email"],
Name = OpenIddictConstants.Scopes.Email,
Description = S["Requests access to the user's email address. This scope provides the email and email_verified claims, which indicate the user's email address and whether it has been verified."]
};
await scopeManager.CreateAsync(descriptor);
}
if (await scopeManager.FindByNameAsync(OpenIddictConstants.Scopes.Phone) == null)
{
var descriptor = new OpenIdScopeDescriptor
{
DisplayName = S["Phone"],
Name = OpenIddictConstants.Scopes.Phone,
Description = S["Requests access to the user's phone number. This scope includes the phone_number and phone_number_verified claims, which provide the user's phone number and indicate whether it has been verified."]
};
await scopeManager.CreateAsync(descriptor);
}
});

return 1;
}
}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.OpenId/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using OpenIddict.Validation.AspNetCore;
using OpenIddict.Validation.DataProtection;
using OrchardCore.BackgroundTasks;
using OrchardCore.Data.Migration;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Environment.Shell.Builders;
Expand All @@ -22,6 +23,7 @@
using OrchardCore.OpenId.Configuration;
using OrchardCore.OpenId.Deployment;
using OrchardCore.OpenId.Drivers;
using OrchardCore.OpenId.Migrations;
using OrchardCore.OpenId.Recipes;
using OrchardCore.OpenId.Services;
using OrchardCore.OpenId.Services.Handlers;
Expand Down Expand Up @@ -99,6 +101,7 @@ public override void ConfigureServices(IServiceCollection services)

services.TryAddSingleton<IOpenIdServerService, OpenIdServerService>();

services.AddDataMigration<DefaultScopesMigration>();
// Note: the following services are registered using TryAddEnumerable to prevent duplicate registrations.
services.TryAddEnumerable(new[]
{
Expand Down

0 comments on commit fc4c439

Please sign in to comment.