-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(pr: issue-346): add client for smalltalk app (reactjs) in auth #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/Services/auth/O2NextGen.Auth.Web/Extensions/IdentityExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Identity.UI.Services; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using O2NextGen.Auth.Web.Data; | ||
| using O2NextGen.Auth.Web.Helpers; | ||
| using O2NextGen.Auth.Web.Utilities; | ||
|
|
||
| namespace O2NextGen.Auth.Web.Extensions | ||
| { | ||
| public static class IdentityExtensions | ||
| { | ||
| public static IServiceCollection AddConfiguredIdentity(this IServiceCollection services, | ||
| IConfiguration configuration) | ||
| { | ||
| services.AddDbContext<AuthDbContext>(options => | ||
| options.UseSqlServer(configuration["ConnectionString"])); | ||
|
|
||
| services | ||
| .AddIdentity<O2User, IdentityRole>(options => | ||
| { | ||
| options.Password.RequireDigit = false; | ||
| //TODO: uncomment after some tests | ||
| //options.Password.RequiredLength = 12; | ||
| options.Password.RequireLowercase = false; | ||
| options.Password.RequireUppercase = false; | ||
| options.Password.RequireNonAlphanumeric = false; | ||
| }) | ||
| .AddEntityFrameworkStores<AuthDbContext>() | ||
| .AddDefaultTokenProviders(); | ||
|
|
||
| services.AddSingleton<IEmailSender, DummyEmailSender>(); | ||
| services.AddSingleton<IBase64QrCodeGenerator, Base64QrCodeGenerator>(); | ||
|
|
||
|
|
||
| return services; | ||
| } | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/Services/auth/O2NextGen.Auth.Web/Extensions/IdentityServerExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using IdentityServer4; | ||
| using IdentityServer4.Models; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using O2NextGen.Auth.Web.Data; | ||
|
|
||
| namespace O2NextGen.Auth.Web.Extensions | ||
| { | ||
| public static class IdentityServerExtensions | ||
| { | ||
| public static IServiceCollection AddConfiguredIdentityServer(this IServiceCollection services, | ||
| IHostingEnvironment environment, IConfiguration configuration) | ||
| { | ||
| var builder = services.AddIdentityServer(options => | ||
| { | ||
| options.Events.RaiseErrorEvents = true; | ||
| options.Events.RaiseInformationEvents = true; | ||
| options.Events.RaiseFailureEvents = true; | ||
| options.Events.RaiseSuccessEvents = true; | ||
| }) | ||
| // using in memory, but we could also get it, for example, from the database | ||
|
|
||
| // access to data regarding the user's identity | ||
| .AddInMemoryIdentityResources(GetIdentityResources()) | ||
| // APIs that may be accessed | ||
| .AddInMemoryApiResources(GetApis()) | ||
| // client applications that may access users data and APIs on the user's behalf | ||
| .AddInMemoryClients(GetClients()) | ||
| // configures IdentityServer integration with ASP.NET Core Identity | ||
| .AddAspNetIdentity<O2User>() | ||
|
|
||
| // to avoid bombarding the db with checks, make use of cache | ||
| .AddInMemoryCaching(); | ||
| // more about EF integration: | ||
| // - http://docs.identityserver.io/en/latest/quickstarts/7_entity_framework.html | ||
| // - http://docs.identityserver.io/en/latest/reference/ef.html?highlight=dbcontext | ||
|
|
||
| return services; | ||
| } | ||
|
|
||
| private static IEnumerable<IdentityResource> GetIdentityResources() | ||
| { | ||
| var profile = new IdentityResources.Profile(); | ||
| profile.Required = true; | ||
| return new IdentityResource[] | ||
| { | ||
| new IdentityResources.OpenId(), | ||
| profile | ||
| }; | ||
| } | ||
|
|
||
| private static IEnumerable<ApiResource> GetApis() | ||
| { | ||
| var apiResource = new ApiResource("GroupManagement", "Group Management"); | ||
| apiResource.Scopes.First().Required = true; | ||
| return new[] | ||
| { | ||
| apiResource | ||
| }; | ||
| } | ||
|
|
||
| private static IEnumerable<Client> GetClients() | ||
| { | ||
| return new[] | ||
| { | ||
| new Client | ||
| { | ||
| ClientId = "WebFrontend", | ||
| AllowedGrantTypes = GrantTypes.Code, | ||
| ClientSecrets = {new Secret("secret".Sha256())}, | ||
| RedirectUris = new[] {"https://localhost:1001/signin-oidc"}, | ||
| RefreshTokenUsage = TokenUsage.OneTimeOnly, | ||
| AllowedScopes = | ||
| { | ||
| IdentityServerConstants.StandardScopes.OpenId, | ||
| IdentityServerConstants.StandardScopes.Profile, | ||
| "GroupManagement", | ||
| IdentityServerConstants.StandardScopes.OfflineAccess | ||
| }, | ||
| AllowOfflineAccess = true, | ||
| AccessTokenLifetime = 60, | ||
| RefreshTokenExpiration = TokenExpiration.Sliding, | ||
| //RequireConsent = false | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Services/auth/O2NextGen.Auth.Web/StartupHelpers/DatabaseExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using O2NextGen.Auth.Web.Data; | ||
|
|
||
| namespace O2NextGen.Auth.Web.StartupHelpers | ||
| { | ||
| internal static class DatabaseExtensions | ||
| { | ||
| internal static async Task EnsureDbUpToDateAsync(this IWebHost host) | ||
| { | ||
| using (var scope = host.Services.CreateScope()) | ||
| { | ||
| var hostingEnvironment = scope.ServiceProvider.GetRequiredService<IHostingEnvironment>(); | ||
| var authDbContext = scope.ServiceProvider.GetRequiredService<AuthDbContext>(); | ||
| await authDbContext.Database.MigrateAsync(); | ||
|
|
||
| // var grantDbContext = scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>(); | ||
| // await grantDbContext.Database.MigrateAsync(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
src/Services/auth/O2NextGen.Auth.Web/Utilities/Base64QrCodeGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System; | ||
| using SkiaSharp; | ||
| using SkiaSharp.QrCode; | ||
|
|
||
| namespace O2NextGen.Auth.Web.Utilities | ||
| { | ||
| public class Base64QrCodeGenerator : IBase64QrCodeGenerator | ||
| { | ||
| public string Generate(Uri target) | ||
| { | ||
| using (var generator = new QRCodeGenerator()) | ||
| { | ||
| var code = generator.CreateQrCode(target.OriginalString, ECCLevel.Q); | ||
|
|
||
| var info = new SKImageInfo(256, 256); | ||
| using (var surface = SKSurface.Create(info)) | ||
| { | ||
| var canvas = surface.Canvas; | ||
| canvas.Render(code, info.Width, info.Height); | ||
|
|
||
| using (var image = surface.Snapshot()) | ||
| using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) | ||
| { | ||
| return Convert.ToBase64String(data.ToArray()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Services/auth/O2NextGen.Auth.Web/Utilities/IBase64QrCodeGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System; | ||
|
|
||
| namespace O2NextGen.Auth.Web.Utilities | ||
| { | ||
| public interface IBase64QrCodeGenerator | ||
| { | ||
| string Generate(Uri target); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.