10 changed files with 6394 additions and 112 deletions
File diff suppressed because it is too large
@ -0,0 +1,237 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace LY.MicroService.Applications.Single.EntityFrameworkCore.Migrations |
|||
{ |
|||
/// <inheritdoc />
|
|||
public partial class AddModuleOpenIddict : Migration |
|||
{ |
|||
/// <inheritdoc />
|
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.CreateTable( |
|||
name: "OpenIddictApplications", |
|||
columns: table => new |
|||
{ |
|||
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"), |
|||
ClientId = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ClientSecret = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ConsentType = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
DisplayName = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
DisplayNames = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Permissions = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
PostLogoutRedirectUris = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Properties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
RedirectUris = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Requirements = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Type = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ClientUri = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
LogoUri = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ExtraProperties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ConcurrencyStamp = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false), |
|||
CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false, defaultValue: false), |
|||
DeleterId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
DeletionTime = table.Column<DateTime>(type: "datetime(6)", nullable: true) |
|||
}, |
|||
constraints: table => |
|||
{ |
|||
table.PrimaryKey("PK_OpenIddictApplications", x => x.Id); |
|||
}) |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
|
|||
migrationBuilder.CreateTable( |
|||
name: "OpenIddictScopes", |
|||
columns: table => new |
|||
{ |
|||
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"), |
|||
Description = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Descriptions = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
DisplayName = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
DisplayNames = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Name = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Properties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Resources = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ExtraProperties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ConcurrencyStamp = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false), |
|||
CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false, defaultValue: false), |
|||
DeleterId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
DeletionTime = table.Column<DateTime>(type: "datetime(6)", nullable: true) |
|||
}, |
|||
constraints: table => |
|||
{ |
|||
table.PrimaryKey("PK_OpenIddictScopes", x => x.Id); |
|||
}) |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
|
|||
migrationBuilder.CreateTable( |
|||
name: "OpenIddictAuthorizations", |
|||
columns: table => new |
|||
{ |
|||
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"), |
|||
ApplicationId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
CreationDate = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
Properties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Scopes = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Status = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Subject = table.Column<string>(type: "varchar(400)", maxLength: 400, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Type = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ExtraProperties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ConcurrencyStamp = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false), |
|||
CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false, defaultValue: false), |
|||
DeleterId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
DeletionTime = table.Column<DateTime>(type: "datetime(6)", nullable: true) |
|||
}, |
|||
constraints: table => |
|||
{ |
|||
table.PrimaryKey("PK_OpenIddictAuthorizations", x => x.Id); |
|||
table.ForeignKey( |
|||
name: "FK_OpenIddictAuthorizations_OpenIddictApplications_ApplicationId", |
|||
column: x => x.ApplicationId, |
|||
principalTable: "OpenIddictApplications", |
|||
principalColumn: "Id"); |
|||
}) |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
|
|||
migrationBuilder.CreateTable( |
|||
name: "OpenIddictTokens", |
|||
columns: table => new |
|||
{ |
|||
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"), |
|||
ApplicationId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
AuthorizationId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
CreationDate = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
ExpirationDate = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
Payload = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Properties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
RedemptionDate = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
ReferenceId = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Status = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Subject = table.Column<string>(type: "varchar(400)", maxLength: 400, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
Type = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ExtraProperties = table.Column<string>(type: "longtext", nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
ConcurrencyStamp = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: true) |
|||
.Annotation("MySql:CharSet", "utf8mb4"), |
|||
CreationTime = table.Column<DateTime>(type: "datetime(6)", nullable: false), |
|||
CreatorId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
LastModificationTime = table.Column<DateTime>(type: "datetime(6)", nullable: true), |
|||
LastModifierId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false, defaultValue: false), |
|||
DeleterId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"), |
|||
DeletionTime = table.Column<DateTime>(type: "datetime(6)", nullable: true) |
|||
}, |
|||
constraints: table => |
|||
{ |
|||
table.PrimaryKey("PK_OpenIddictTokens", x => x.Id); |
|||
table.ForeignKey( |
|||
name: "FK_OpenIddictTokens_OpenIddictApplications_ApplicationId", |
|||
column: x => x.ApplicationId, |
|||
principalTable: "OpenIddictApplications", |
|||
principalColumn: "Id"); |
|||
table.ForeignKey( |
|||
name: "FK_OpenIddictTokens_OpenIddictAuthorizations_AuthorizationId", |
|||
column: x => x.AuthorizationId, |
|||
principalTable: "OpenIddictAuthorizations", |
|||
principalColumn: "Id"); |
|||
}) |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictApplications_ClientId", |
|||
table: "OpenIddictApplications", |
|||
column: "ClientId"); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictAuthorizations_ApplicationId_Status_Subject_Type", |
|||
table: "OpenIddictAuthorizations", |
|||
columns: new[] { "ApplicationId", "Status", "Subject", "Type" }); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictScopes_Name", |
|||
table: "OpenIddictScopes", |
|||
column: "Name"); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictTokens_ApplicationId_Status_Subject_Type", |
|||
table: "OpenIddictTokens", |
|||
columns: new[] { "ApplicationId", "Status", "Subject", "Type" }); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictTokens_AuthorizationId", |
|||
table: "OpenIddictTokens", |
|||
column: "AuthorizationId"); |
|||
|
|||
migrationBuilder.CreateIndex( |
|||
name: "IX_OpenIddictTokens_ReferenceId", |
|||
table: "OpenIddictTokens", |
|||
column: "ReferenceId"); |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.DropTable( |
|||
name: "OpenIddictScopes"); |
|||
|
|||
migrationBuilder.DropTable( |
|||
name: "OpenIddictTokens"); |
|||
|
|||
migrationBuilder.DropTable( |
|||
name: "OpenIddictAuthorizations"); |
|||
|
|||
migrationBuilder.DropTable( |
|||
name: "OpenIddictApplications"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,461 @@ |
|||
using LINGYUN.Abp.IdentityServer.IdentityResources; |
|||
using OpenIddict.Abstractions; |
|||
using System.Globalization; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.IdentityServer.ApiResources; |
|||
using Volo.Abp.IdentityServer.ApiScopes; |
|||
using Volo.Abp.IdentityServer.Clients; |
|||
using Volo.Abp.IdentityServer.IdentityResources; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.PermissionManagement; |
|||
|
|||
namespace LY.MicroService.Applications.Single.DataSeeder; |
|||
|
|||
public class ClientDataSeederContributor : IDataSeedContributor, ITransientDependency |
|||
{ |
|||
private readonly IOpenIddictApplicationManager _applicationManager; |
|||
private readonly IOpenIddictScopeManager _scopeManager; |
|||
|
|||
private readonly IClientRepository _clientRepository; |
|||
private readonly IApiResourceRepository _apiResourceRepository; |
|||
private readonly IApiScopeRepository _apiScopeRepository; |
|||
private readonly ICustomIdentityResourceDataSeeder _customIdentityResourceDataSeeder; |
|||
private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder; |
|||
|
|||
private readonly IGuidGenerator _guidGenerator; |
|||
private readonly IPermissionDataSeeder _permissionDataSeeder; |
|||
private readonly IConfiguration _configuration; |
|||
private readonly ICurrentTenant _currentTenant; |
|||
|
|||
public ClientDataSeederContributor( |
|||
IOpenIddictApplicationManager applicationManager, |
|||
IOpenIddictScopeManager scopeManager, |
|||
IClientRepository clientRepository, |
|||
IApiResourceRepository apiResourceRepository, |
|||
IApiScopeRepository apiScopeRepository, |
|||
ICustomIdentityResourceDataSeeder customIdentityResourceDataSeeder, |
|||
IIdentityResourceDataSeeder identityResourceDataSeeder, |
|||
IGuidGenerator guidGenerator, |
|||
IPermissionDataSeeder permissionDataSeeder, |
|||
IConfiguration configuration, |
|||
ICurrentTenant currentTenant) |
|||
{ |
|||
_applicationManager = applicationManager; |
|||
_scopeManager = scopeManager; |
|||
_clientRepository = clientRepository; |
|||
_apiResourceRepository = apiResourceRepository; |
|||
_apiScopeRepository = apiScopeRepository; |
|||
_customIdentityResourceDataSeeder = customIdentityResourceDataSeeder; |
|||
_identityResourceDataSeeder = identityResourceDataSeeder; |
|||
_guidGenerator = guidGenerator; |
|||
_permissionDataSeeder = permissionDataSeeder; |
|||
_configuration = configuration; |
|||
_currentTenant = currentTenant; |
|||
} |
|||
|
|||
public async virtual Task SeedAsync(DataSeedContext context) |
|||
{ |
|||
using (_currentTenant.Change(context.TenantId)) |
|||
{ |
|||
if (_configuration.GetValue<bool>("AuthServer:UseOpenIddict")) |
|||
{ |
|||
await SeedOpenIddictAsync(); |
|||
return; |
|||
} |
|||
|
|||
await SeedIdentityServerAsync(); |
|||
} |
|||
} |
|||
|
|||
#region OpenIddict
|
|||
|
|||
private async Task SeedOpenIddictAsync() |
|||
{ |
|||
await CreateScopeAsync("lingyun-abp-application"); |
|||
await CreateApplicationAsync("lingyun-abp-application"); |
|||
} |
|||
|
|||
private async Task CreateScopeAsync(string scope) |
|||
{ |
|||
if (await _scopeManager.FindByNameAsync(scope) == null) |
|||
{ |
|||
await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor() |
|||
{ |
|||
Name = scope, |
|||
DisplayName = scope + " access", |
|||
DisplayNames = |
|||
{ |
|||
[CultureInfo.GetCultureInfo("zh-Hans")] = "Abp API 应用程序访问", |
|||
[CultureInfo.GetCultureInfo("en")] = "Abp API Application Access" |
|||
}, |
|||
Resources = |
|||
{ |
|||
scope |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
private async Task CreateApplicationAsync(string scope) |
|||
{ |
|||
var configurationSection = _configuration.GetSection("OpenIddict:Applications"); |
|||
|
|||
var vueClientId = configurationSection["VueAdmin:ClientId"]; |
|||
if (!vueClientId.IsNullOrWhiteSpace()) |
|||
{ |
|||
var vueClientRootUrl = configurationSection["VueAdmin:RootUrl"].EnsureEndsWith('/'); |
|||
|
|||
if (await _applicationManager.FindByClientIdAsync(vueClientId) == null) |
|||
{ |
|||
await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor |
|||
{ |
|||
ClientId = vueClientId, |
|||
ClientSecret = "1q2w3e*", |
|||
ConsentType = OpenIddictConstants.ConsentTypes.Explicit, |
|||
DisplayName = "Abp Vue Admin Client", |
|||
PostLogoutRedirectUris = |
|||
{ |
|||
new Uri(vueClientRootUrl + "signout-callback-oidc"), |
|||
new Uri(vueClientRootUrl) |
|||
}, |
|||
RedirectUris = |
|||
{ |
|||
new Uri(vueClientRootUrl + "/signin-oidc"), |
|||
new Uri(vueClientRootUrl) |
|||
}, |
|||
Permissions = |
|||
{ |
|||
OpenIddictConstants.Permissions.Endpoints.Authorization, |
|||
OpenIddictConstants.Permissions.Endpoints.Token, |
|||
OpenIddictConstants.Permissions.Endpoints.Device, |
|||
OpenIddictConstants.Permissions.Endpoints.Introspection, |
|||
OpenIddictConstants.Permissions.Endpoints.Revocation, |
|||
OpenIddictConstants.Permissions.Endpoints.Logout, |
|||
|
|||
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode, |
|||
OpenIddictConstants.Permissions.GrantTypes.Implicit, |
|||
OpenIddictConstants.Permissions.GrantTypes.Password, |
|||
OpenIddictConstants.Permissions.GrantTypes.RefreshToken, |
|||
OpenIddictConstants.Permissions.GrantTypes.DeviceCode, |
|||
OpenIddictConstants.Permissions.GrantTypes.ClientCredentials, |
|||
|
|||
OpenIddictConstants.Permissions.ResponseTypes.Code, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeIdToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeIdTokenToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.IdToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.IdTokenToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.None, |
|||
OpenIddictConstants.Permissions.ResponseTypes.Token, |
|||
|
|||
OpenIddictConstants.Permissions.Scopes.Roles, |
|||
OpenIddictConstants.Permissions.Scopes.Profile, |
|||
OpenIddictConstants.Permissions.Scopes.Email, |
|||
OpenIddictConstants.Permissions.Scopes.Address, |
|||
OpenIddictConstants.Permissions.Scopes.Phone, |
|||
OpenIddictConstants.Permissions.Prefixes.Scope + scope |
|||
} |
|||
}); |
|||
|
|||
var vueClientPermissions = new string[1] |
|||
{ |
|||
"AbpIdentity.UserLookup" |
|||
}; |
|||
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, vueClientId, vueClientPermissions); |
|||
} |
|||
} |
|||
|
|||
var internalServiceClientId = configurationSection["InternalService:ClientId"]; |
|||
if (!internalServiceClientId.IsNullOrWhiteSpace()) |
|||
{ |
|||
if (await _applicationManager.FindByClientIdAsync(internalServiceClientId) == null) |
|||
{ |
|||
await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor |
|||
{ |
|||
ClientId = internalServiceClientId, |
|||
ClientSecret = "1q2w3e*", |
|||
ConsentType = OpenIddictConstants.ConsentTypes.Explicit, |
|||
DisplayName = "Abp Vue Admin Client", |
|||
Permissions = |
|||
{ |
|||
OpenIddictConstants.Permissions.Endpoints.Authorization, |
|||
OpenIddictConstants.Permissions.Endpoints.Token, |
|||
OpenIddictConstants.Permissions.Endpoints.Device, |
|||
OpenIddictConstants.Permissions.Endpoints.Introspection, |
|||
OpenIddictConstants.Permissions.Endpoints.Revocation, |
|||
OpenIddictConstants.Permissions.Endpoints.Logout, |
|||
|
|||
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode, |
|||
OpenIddictConstants.Permissions.GrantTypes.Implicit, |
|||
OpenIddictConstants.Permissions.GrantTypes.Password, |
|||
OpenIddictConstants.Permissions.GrantTypes.RefreshToken, |
|||
OpenIddictConstants.Permissions.GrantTypes.DeviceCode, |
|||
OpenIddictConstants.Permissions.GrantTypes.ClientCredentials, |
|||
|
|||
OpenIddictConstants.Permissions.ResponseTypes.Code, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeIdToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeIdTokenToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.CodeToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.IdToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.IdTokenToken, |
|||
OpenIddictConstants.Permissions.ResponseTypes.None, |
|||
OpenIddictConstants.Permissions.ResponseTypes.Token, |
|||
|
|||
OpenIddictConstants.Permissions.Scopes.Roles, |
|||
OpenIddictConstants.Permissions.Scopes.Profile, |
|||
OpenIddictConstants.Permissions.Scopes.Email, |
|||
OpenIddictConstants.Permissions.Scopes.Address, |
|||
OpenIddictConstants.Permissions.Scopes.Phone, |
|||
OpenIddictConstants.Permissions.Prefixes.Scope + scope |
|||
} |
|||
}); |
|||
|
|||
var internalServicePermissions = new string[2] |
|||
{ |
|||
"AbpIdentity.UserLookup","AbpIdentity.Users" |
|||
}; |
|||
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, internalServiceClientId, internalServicePermissions); |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region IdentityServer
|
|||
|
|||
private async Task SeedIdentityServerAsync() |
|||
{ |
|||
await _identityResourceDataSeeder.CreateStandardResourcesAsync(); |
|||
await _customIdentityResourceDataSeeder.CreateCustomResourcesAsync(); |
|||
await CreateApiResourcesAsync(); |
|||
await CreateApiScopesAsync(); |
|||
await CreateClientsAsync(); |
|||
} |
|||
|
|||
private async Task CreateApiScopesAsync() |
|||
{ |
|||
await CreateApiScopeAsync("lingyun-abp-application"); |
|||
} |
|||
|
|||
private async Task CreateApiResourcesAsync() |
|||
{ |
|||
var commonApiUserClaims = new[] |
|||
{ |
|||
"email", |
|||
"email_verified", |
|||
"name", |
|||
"phone_number", |
|||
"phone_number_verified", |
|||
"role" |
|||
}; |
|||
|
|||
await CreateApiResourceAsync("lingyun-abp-application", commonApiUserClaims); |
|||
} |
|||
|
|||
private async Task<ApiResource> CreateApiResourceAsync(string name, IEnumerable<string> claims, IEnumerable<string> secrets = null) |
|||
{ |
|||
var apiResource = await _apiResourceRepository.FindByNameAsync(name); |
|||
if (apiResource == null) |
|||
{ |
|||
apiResource = await _apiResourceRepository.InsertAsync( |
|||
new ApiResource( |
|||
_guidGenerator.Create(), |
|||
name, |
|||
name + " API" |
|||
), |
|||
autoSave: true |
|||
); |
|||
} |
|||
|
|||
foreach (var claim in claims) |
|||
{ |
|||
if (apiResource.FindClaim(claim) == null) |
|||
{ |
|||
apiResource.AddUserClaim(claim); |
|||
} |
|||
} |
|||
if (secrets != null) |
|||
{ |
|||
foreach (var secret in secrets) |
|||
{ |
|||
if (apiResource.FindSecret(secret) == null) |
|||
{ |
|||
apiResource.AddSecret(secret); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return await _apiResourceRepository.UpdateAsync(apiResource); |
|||
} |
|||
|
|||
private async Task<ApiScope> CreateApiScopeAsync(string name) |
|||
{ |
|||
var apiScope = await _apiScopeRepository.FindByNameAsync(name); |
|||
if (apiScope == null) |
|||
{ |
|||
apiScope = await _apiScopeRepository.InsertAsync( |
|||
new ApiScope( |
|||
_guidGenerator.Create(), |
|||
name, |
|||
name + " API" |
|||
), |
|||
autoSave: true |
|||
); |
|||
} |
|||
|
|||
return apiScope; |
|||
} |
|||
|
|||
private async Task CreateClientsAsync() |
|||
{ |
|||
|
|||
string commonSecret = IdentityServer4.Models.HashExtensions.Sha256("1q2w3e*"); |
|||
|
|||
var commonScopes = new[] |
|||
{ |
|||
"email", |
|||
"openid", |
|||
"profile", |
|||
"role", |
|||
"phone", |
|||
"address", |
|||
"offline_access" // 加上刷新,
|
|||
|
|||
}; |
|||
|
|||
var configurationSection = _configuration.GetSection("IdentityServer:Clients"); |
|||
|
|||
var vueClientId = configurationSection["VueAdmin:ClientId"]; |
|||
if (!vueClientId.IsNullOrWhiteSpace()) |
|||
{ |
|||
var vueClientPermissions = new string[1] |
|||
{ |
|||
"AbpIdentity.UserLookup" |
|||
}; |
|||
var vueClientRootUrl = configurationSection["VueAdmin:RootUrl"].EnsureEndsWith('/'); |
|||
await CreateClientAsync( |
|||
vueClientId, |
|||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
|||
new[] { "password", "client_credentials", "implicit", "phone_verify", "wx-mp" }, |
|||
commonSecret, |
|||
redirectUri: $"{vueClientRootUrl}signin-oidc", |
|||
postLogoutRedirectUri: $"{vueClientRootUrl}signout-callback-oidc", |
|||
corsOrigins: configurationSection["CorsOrigins"], |
|||
permissions: vueClientPermissions |
|||
); |
|||
} |
|||
|
|||
// InternalService 内部服务间通讯客户端,必要的话需要在前端指定它拥有所有权限,当前项目仅预置用户查询权限
|
|||
var internalServiceClientId = configurationSection["InternalService:ClientId"]; |
|||
if (!internalServiceClientId.IsNullOrWhiteSpace()) |
|||
{ |
|||
var internalServicePermissions = new string[2] |
|||
{ |
|||
"AbpIdentity.UserLookup","AbpIdentity.Users" |
|||
}; |
|||
await CreateClientAsync( |
|||
internalServiceClientId, |
|||
commonScopes.Union(new[] { "lingyun-abp-application" }), |
|||
new[] { "client_credentials" }, |
|||
commonSecret, |
|||
permissions: internalServicePermissions |
|||
); |
|||
} |
|||
} |
|||
|
|||
private async Task<Client> CreateClientAsync( |
|||
string name, |
|||
IEnumerable<string> scopes, |
|||
IEnumerable<string> grantTypes, |
|||
string secret, |
|||
string redirectUri = null, |
|||
string postLogoutRedirectUri = null, |
|||
IEnumerable<string> permissions = null, |
|||
string corsOrigins = null) |
|||
{ |
|||
var client = await _clientRepository.FindByClientIdAsync(name); |
|||
if (client == null) |
|||
{ |
|||
client = await _clientRepository.InsertAsync( |
|||
new Client( |
|||
_guidGenerator.Create(), |
|||
name |
|||
) |
|||
{ |
|||
ClientName = name, |
|||
ProtocolType = "oidc", |
|||
Description = name, |
|||
AlwaysIncludeUserClaimsInIdToken = true, |
|||
AllowOfflineAccess = true, |
|||
AbsoluteRefreshTokenLifetime = 10800, //3 hours
|
|||
AccessTokenLifetime = 7200, //2 hours
|
|||
AuthorizationCodeLifetime = 300, |
|||
IdentityTokenLifetime = 300, |
|||
RequireConsent = false |
|||
}, |
|||
autoSave: true |
|||
); |
|||
} |
|||
|
|||
foreach (var scope in scopes) |
|||
{ |
|||
if (client.FindScope(scope) == null) |
|||
{ |
|||
client.AddScope(scope); |
|||
} |
|||
} |
|||
|
|||
foreach (var grantType in grantTypes) |
|||
{ |
|||
if (client.FindGrantType(grantType) == null) |
|||
{ |
|||
client.AddGrantType(grantType); |
|||
} |
|||
} |
|||
|
|||
if (client.FindSecret(secret) == null) |
|||
{ |
|||
client.AddSecret(secret); |
|||
} |
|||
|
|||
if (redirectUri != null) |
|||
{ |
|||
if (client.FindRedirectUri(redirectUri) == null) |
|||
{ |
|||
client.AddRedirectUri(redirectUri); |
|||
} |
|||
} |
|||
|
|||
if (postLogoutRedirectUri != null) |
|||
{ |
|||
if (client.FindPostLogoutRedirectUri(postLogoutRedirectUri) == null) |
|||
{ |
|||
client.AddPostLogoutRedirectUri(postLogoutRedirectUri); |
|||
} |
|||
} |
|||
|
|||
if (corsOrigins != null) |
|||
{ |
|||
var corsOriginsSplit = corsOrigins.Split(";"); |
|||
foreach (var corsOrigin in corsOriginsSplit) |
|||
{ |
|||
if (client.FindCorsOrigin(corsOrigin) == null) |
|||
{ |
|||
client.AddCorsOrigin(corsOrigin); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (permissions != null) |
|||
{ |
|||
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, name, permissions); |
|||
} |
|||
|
|||
return await _clientRepository.UpdateAsync(client); |
|||
} |
|||
|
|||
#endregion
|
|||
} |
|||
@ -1,50 +1,83 @@ |
|||
using LY.MicroService.Applications.Single; |
|||
using Microsoft.AspNetCore.Cors; |
|||
using Serilog; |
|||
using Volo.Abp.IO; |
|||
using Volo.Abp.Modularity.PlugIns; |
|||
|
|||
namespace LY.MicroService.Applications.Single; |
|||
|
|||
public class Program |
|||
var builder = WebApplication.CreateBuilder(args); |
|||
builder.Services.AddCors(options => |
|||
{ |
|||
public async static Task<int> Main(string[] args) |
|||
options.AddDefaultPolicy(policy => |
|||
{ |
|||
policy |
|||
.WithOrigins( |
|||
builder.Configuration["App:CorsOrigins"] |
|||
.Split(",", StringSplitOptions.RemoveEmptyEntries) |
|||
.Select(o => o.RemovePostFix("/")) |
|||
.ToArray() |
|||
) |
|||
.WithAbpExposedHeaders() |
|||
.WithAbpWrapExposedHeaders() |
|||
.SetIsOriginAllowedToAllowWildcardSubdomains() |
|||
.AllowAnyHeader() |
|||
.AllowAnyMethod() |
|||
.AllowCredentials(); |
|||
}); |
|||
}); |
|||
builder.Host.AddAppSettingsSecretsJson() |
|||
.UseAutofac() |
|||
.UseSerilog((context, provider, config) => |
|||
{ |
|||
try |
|||
{ |
|||
Log.Information("Starting MicroService Applications Single Host."); |
|||
|
|||
var builder = WebApplication.CreateBuilder(args); |
|||
builder.Host.AddAppSettingsSecretsJson() |
|||
.UseAutofac() |
|||
.UseSerilog((context, provider, config) => |
|||
{ |
|||
config.ReadFrom.Configuration(context.Configuration); |
|||
}); |
|||
await builder.AddApplicationAsync<MicroServiceApplicationsSingleModule>(options => |
|||
{ |
|||
// 搜索 Modules 目录下所有文件作为插件
|
|||
// 取消显示引用所有其他项目的模块,改为通过插件的形式引用
|
|||
var pluginFolder = Path.Combine( |
|||
Directory.GetCurrentDirectory(), "Modules"); |
|||
DirectoryHelper.CreateIfNotExists(pluginFolder); |
|||
options.PlugInSources.AddFolder( |
|||
pluginFolder, |
|||
SearchOption.AllDirectories); |
|||
}); |
|||
var app = builder.Build(); |
|||
await app.InitializeApplicationAsync(); |
|||
await app.RunAsync(); |
|||
return 0; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Log.Fatal(ex, "Host terminated unexpectedly!"); |
|||
Console.WriteLine("Host terminated unexpectedly!"); |
|||
Console.WriteLine(ex.ToString()); |
|||
return 1; |
|||
} |
|||
finally |
|||
{ |
|||
Log.CloseAndFlush(); |
|||
} |
|||
} |
|||
config.ReadFrom.Configuration(context.Configuration); |
|||
}); |
|||
|
|||
await builder.AddApplicationAsync<MicroServiceApplicationsSingleModule>(options => |
|||
{ |
|||
// 搜索 Modules 目录下所有文件作为插件
|
|||
// 取消显示引用所有其他项目的模块,改为通过插件的形式引用
|
|||
var pluginFolder = Path.Combine( |
|||
Directory.GetCurrentDirectory(), "Modules"); |
|||
DirectoryHelper.CreateIfNotExists(pluginFolder); |
|||
options.PlugInSources.AddFolder( |
|||
pluginFolder, |
|||
SearchOption.AllDirectories); |
|||
}); |
|||
|
|||
var app = builder.Build(); |
|||
|
|||
await app.InitializeApplicationAsync(); |
|||
|
|||
if (app.Environment.IsDevelopment()) |
|||
{ |
|||
app.UseDeveloperExceptionPage(); |
|||
} |
|||
|
|||
app.UseCookiePolicy(); |
|||
app.UseMapRequestLocalization(); |
|||
app.UseCorrelationId(); |
|||
app.UseStaticFiles(); |
|||
app.UseRouting(); |
|||
app.UseCors(); |
|||
app.UseAuthentication(); |
|||
|
|||
if (builder.Configuration.GetValue<bool>("AuthServer:UseOpenIddict")) |
|||
{ |
|||
app.UseAbpOpenIddictValidation(); |
|||
} |
|||
else |
|||
{ |
|||
app.UseJwtTokenMiddleware(); |
|||
app.UseIdentityServer(); |
|||
} |
|||
app.UseMultiTenancy(); |
|||
app.UseAuthorization(); |
|||
app.UseSwagger(); |
|||
app.UseSwaggerUI(options => |
|||
{ |
|||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support App API"); |
|||
}); |
|||
app.UseAuditing(); |
|||
app.UseAbpSerilogEnrichers(); |
|||
app.UseConfiguredEndpoints(); |
|||
|
|||
await app.RunAsync(); |
|||
|
|||
Loading…
Reference in new issue