AuthServerDataSeeder.cs.bak 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using IdentityServer4.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Volo.Abp.Authorization.Permissions;
  7. using Volo.Abp.Data;
  8. using Volo.Abp.DependencyInjection;
  9. using Volo.Abp.Guids;
  10. using Volo.Abp.Identity;
  11. using Volo.Abp.IdentityServer.ApiResources;
  12. using Volo.Abp.IdentityServer.ApiScopes;
  13. using Volo.Abp.IdentityServer.Clients;
  14. using Volo.Abp.IdentityServer.IdentityResources;
  15. using Volo.Abp.PermissionManagement;
  16. using Volo.Abp.Uow;
  17. using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
  18. using ApiScope = Volo.Abp.IdentityServer.ApiScopes.ApiScope;
  19. using Client = Volo.Abp.IdentityServer.Clients.Client;
  20. namespace AuthServer.Host
  21. {
  22. public class AuthServerDataSeeder : IDataSeedContributor, ITransientDependency
  23. {
  24. private readonly IApiResourceRepository _apiResourceRepository;
  25. private readonly IApiScopeRepository _apiScopeRepository;
  26. private readonly IClientRepository _clientRepository;
  27. private readonly IIdentityResourceDataSeeder _identityResourceDataSeeder;
  28. private readonly IGuidGenerator _guidGenerator;
  29. private readonly IPermissionDataSeeder _permissionDataSeeder;
  30. public AuthServerDataSeeder(
  31. IClientRepository clientRepository,
  32. IApiResourceRepository apiResourceRepository,
  33. IApiScopeRepository apiScopeRepository,
  34. IIdentityResourceDataSeeder identityResourceDataSeeder,
  35. IGuidGenerator guidGenerator,
  36. IPermissionDataSeeder permissionDataSeeder)
  37. {
  38. _clientRepository = clientRepository;
  39. _apiResourceRepository = apiResourceRepository;
  40. _apiScopeRepository = apiScopeRepository;
  41. _identityResourceDataSeeder = identityResourceDataSeeder;
  42. _guidGenerator = guidGenerator;
  43. _permissionDataSeeder = permissionDataSeeder;
  44. }
  45. [UnitOfWork]
  46. public virtual async Task SeedAsync(DataSeedContext context)
  47. {
  48. await _identityResourceDataSeeder.CreateStandardResourcesAsync();
  49. await CreateApiResourcesAsync();
  50. await CreateApiScopesAsync();
  51. await CreateClientsAsync();
  52. }
  53. private async Task CreateApiScopesAsync()
  54. {
  55. await CreateApiScopeAsync("BaseService");
  56. await CreateApiScopeAsync("InternalGateway");
  57. await CreateApiScopeAsync("WebAppGateway");
  58. await CreateApiScopeAsync("BusinessService");
  59. }
  60. private async Task CreateApiResourcesAsync()
  61. {
  62. var commonApiUserClaims = new[]
  63. {
  64. "email",
  65. "email_verified",
  66. "name",
  67. "phone_number",
  68. "phone_number_verified",
  69. "role"
  70. };
  71. await CreateApiResourceAsync("BaseService", commonApiUserClaims);
  72. await CreateApiResourceAsync("InternalGateway", commonApiUserClaims);
  73. await CreateApiResourceAsync("WebAppGateway", commonApiUserClaims);
  74. await CreateApiResourceAsync("BusinessService", commonApiUserClaims);
  75. }
  76. private async Task<ApiResource> CreateApiResourceAsync(string name, IEnumerable<string> claims)
  77. {
  78. var apiResource = await _apiResourceRepository.FindByNameAsync(name);
  79. if (apiResource == null)
  80. {
  81. apiResource = await _apiResourceRepository.InsertAsync(
  82. new ApiResource(
  83. _guidGenerator.Create(),
  84. name,
  85. name + " API"
  86. ),
  87. autoSave: true
  88. );
  89. }
  90. foreach (var claim in claims)
  91. {
  92. if (apiResource.FindClaim(claim) == null)
  93. {
  94. apiResource.AddUserClaim(claim);
  95. }
  96. }
  97. return await _apiResourceRepository.UpdateAsync(apiResource);
  98. }
  99. private async Task<ApiScope> CreateApiScopeAsync(string name)
  100. {
  101. var apiScope = await _apiScopeRepository.FindByNameAsync(name);
  102. if (apiScope == null)
  103. {
  104. apiScope = await _apiScopeRepository.InsertAsync(
  105. new ApiScope(
  106. _guidGenerator.Create(),
  107. name,
  108. name + " API"
  109. ),
  110. autoSave: true
  111. );
  112. }
  113. return apiScope;
  114. }
  115. private async Task CreateClientsAsync()
  116. {
  117. var commonScopes = new[]
  118. {
  119. "email",
  120. "openid",
  121. "profile",
  122. "role",
  123. "phone",
  124. "address"
  125. };
  126. await CreateClientAsync(
  127. name: "blazor-app",
  128. scopes: commonScopes.Append("BaseService").Append("WebAppGateway").Append("BusinessService"),
  129. grantTypes: new[] { "authorization_code" },
  130. secret: null,
  131. requireClientSecret: false,
  132. redirectUri: $"http://localhost:44307/authentication/login-callback",
  133. postLogoutRedirectUri: $"http://localhost:44307/authentication/logout-callback",
  134. corsOrigins: new[] { "http://localhost:44307" }
  135. );
  136. await CreateClientAsync(
  137. name: "basic-web",
  138. scopes: commonScopes.Append("BaseService").Append("WebAppGateway").Append("BusinessService"),
  139. grantTypes: new[] { "password" },
  140. secret: null,
  141. requireClientSecret: false
  142. );
  143. await CreateClientAsync(
  144. name: "business-app",
  145. scopes: new[] { "InternalGateway", "BaseService" },
  146. grantTypes: new[] { "client_credentials" },
  147. secret: "1q2w3E*".Sha256(),
  148. permissions: new[] { IdentityPermissions.Users.Default, IdentityPermissions.UserLookup.Default }
  149. );
  150. }
  151. private async Task<Client> CreateClientAsync(
  152. string name,
  153. IEnumerable<string> scopes,
  154. IEnumerable<string> grantTypes,
  155. string secret = null,
  156. string redirectUri = null,
  157. string postLogoutRedirectUri = null,
  158. string frontChannelLogoutUri = null,
  159. bool requireClientSecret = true,
  160. bool requirePkce = false,
  161. IEnumerable<string> permissions = null,
  162. IEnumerable<string> corsOrigins = null)
  163. {
  164. var client = await _clientRepository.FindByClientIdAsync(name);
  165. if (client == null)
  166. {
  167. client = await _clientRepository.InsertAsync(
  168. new Client(
  169. _guidGenerator.Create(),
  170. name
  171. )
  172. {
  173. ClientName = name,
  174. ProtocolType = "oidc",
  175. Description = name,
  176. AlwaysIncludeUserClaimsInIdToken = true,
  177. AllowOfflineAccess = true,
  178. AbsoluteRefreshTokenLifetime = 31536000, //365 days
  179. AccessTokenLifetime = 31536000, //365 days
  180. AuthorizationCodeLifetime = 300,
  181. IdentityTokenLifetime = 300,
  182. RequireConsent = false,
  183. FrontChannelLogoutUri = frontChannelLogoutUri,
  184. RequireClientSecret = requireClientSecret,
  185. RequirePkce = requirePkce
  186. },
  187. autoSave: true
  188. );
  189. }
  190. foreach (var scope in scopes)
  191. {
  192. if (client.FindScope(scope) == null)
  193. {
  194. client.AddScope(scope);
  195. }
  196. }
  197. foreach (var grantType in grantTypes)
  198. {
  199. if (client.FindGrantType(grantType) == null)
  200. {
  201. client.AddGrantType(grantType);
  202. }
  203. }
  204. if (!secret.IsNullOrEmpty())
  205. {
  206. if (client.FindSecret(secret) == null)
  207. {
  208. client.AddSecret(secret);
  209. }
  210. }
  211. if (redirectUri != null)
  212. {
  213. if (client.FindRedirectUri(redirectUri) == null)
  214. {
  215. client.AddRedirectUri(redirectUri);
  216. }
  217. }
  218. if (postLogoutRedirectUri != null)
  219. {
  220. if (client.FindPostLogoutRedirectUri(postLogoutRedirectUri) == null)
  221. {
  222. client.AddPostLogoutRedirectUri(postLogoutRedirectUri);
  223. }
  224. }
  225. if (permissions != null)
  226. {
  227. await _permissionDataSeeder.SeedAsync(
  228. ClientPermissionValueProvider.ProviderName,
  229. name,
  230. permissions,
  231. null
  232. );
  233. }
  234. if (corsOrigins != null)
  235. {
  236. foreach (var origin in corsOrigins)
  237. {
  238. if (!origin.IsNullOrWhiteSpace() && client.FindCorsOrigin(origin) == null)
  239. {
  240. client.AddCorsOrigin(origin);
  241. }
  242. }
  243. }
  244. return await _clientRepository.UpdateAsync(client);
  245. }
  246. }
  247. }