diff --git a/src/Yavsc.Server/Config.cs b/src/Yavsc.Server/Config.cs index 0c378a1d..88a43540 100644 --- a/src/Yavsc.Server/Config.cs +++ b/src/Yavsc.Server/Config.cs @@ -31,38 +31,34 @@ public static class Config /// /// Lists Available user profile classes, - /// populated at startup, using reflexion. + /// populated at startup, using reflection. /// public static List ProfileTypes = new List(); public static IEnumerable IdentityResources => - new IdentityResource[] - { + [ new IdentityResources.OpenId(), new IdentityResources.Profile(), new IdentityResources.Email() - }; + ]; - public static IEnumerable ApiScopes => - new ApiScope[] - { + public static IEnumerable TestingApiScopes => + [ new ApiScope("scope1",new string[] {"scope1"}), new ApiScope("scope2",new string[] {"scope2"}), - }; + ]; - public static IEnumerable Clients => - new Client[] - { + public static IEnumerable TestingClients => + [ // m2m client credentials flow client new Client { ClientId = "m2m.client", ClientName = "Client Credentials Client", - - AllowedGrantTypes = GrantTypes.ClientCredentials, ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) }, + AllowedGrantTypes = GrantTypes.ClientCredentials, AllowedScopes = { "scope1" } }, @@ -87,7 +83,7 @@ public static class Config IdentityServerConstants.StandardScopes.OfflineAccess, "scope2" }, }, - }; + ]; public static PayPalSettings? PayPalSettings { get; set; } } diff --git a/src/Yavsc.Server/Models/ApplicationDbContext.cs b/src/Yavsc.Server/Models/ApplicationDbContext.cs index dd316b11..8a057638 100644 --- a/src/Yavsc.Server/Models/ApplicationDbContext.cs +++ b/src/Yavsc.Server/Models/ApplicationDbContext.cs @@ -1,17 +1,22 @@  -using Yavsc.Models.Haircut; -using Yavsc.Models.IT.Evolution; -using Yavsc.Models.IT.Fixing; +using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Yavsc.Abstract.Models.Messaging; using Yavsc.Server.Models.EMailing; using Yavsc.Server.Models.IT.SourceCode; using Yavsc.Server.Models.IT; -using Yavsc.Models.Streaming; - +using Yavsc.Abstract.Identity; +using Yavsc.Server.Models.Calendar; + namespace Yavsc.Models { + using Haircut; + using IT.Evolution; + using IT.Fixing; + using Streaming; using Relationship; using Forms; - using Yavsc; using Auth; using Billing; using Musical; @@ -28,18 +33,13 @@ namespace Yavsc.Models using Bank; using Payment; using Blog; - using Yavsc.Abstract.Identity; - using Microsoft.EntityFrameworkCore; - using Microsoft.AspNetCore.Identity.EntityFrameworkCore; - using Yavsc.Server.Models.Calendar; - - using Microsoft.EntityFrameworkCore.ChangeTracking; - using Yavsc.Abstract.Models.Messaging; - using Microsoft.Extensions.Logging; - using System.Configuration; public class ApplicationDbContext : IdentityDbContext - { + { + public ApplicationDbContext(DbContextOptions options) : base(options) + { + } + protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); @@ -272,8 +272,8 @@ namespace Yavsc.Models public DbSet InstrumentRating { get; set; } public DbSet Scopes { get; set; } - - public DbSet blogSpotPublications{ get; set; } - // public DbSet> AspNetUserLogins { get; set; } + + public DbSet blogSpotPublications { get; set; } + public DbSet Client { get; set; } } } diff --git a/src/Yavsc/Extensions/HostingExtensions.cs b/src/Yavsc/Extensions/HostingExtensions.cs index 02b8187a..6b5623ed 100644 --- a/src/Yavsc/Extensions/HostingExtensions.cs +++ b/src/Yavsc/Extensions/HostingExtensions.cs @@ -1,10 +1,7 @@ using System.Diagnostics; using System.Globalization; -using System.Security.Cryptography.X509Certificates; using Google.Apis.Util.Store; using IdentityServer8; -using IdentityServer8.Services; -using IdentityServerHost.Quickstart.UI; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; @@ -18,14 +15,9 @@ using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; using Newtonsoft.Json; -using Yavsc.Abstract.Workflow; -using Yavsc.Billing; using Yavsc.Helpers; using Yavsc.Interface; using Yavsc.Models; -using Yavsc.Models.Billing; -using Yavsc.Models.Haircut; -using Yavsc.Models.Workflow; using Yavsc.Services; using Yavsc.Settings; using Yavsc.ViewModels.Auth; @@ -34,8 +26,6 @@ using System.Security.Cryptography; using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Protocols.Configuration; using IdentityModel; -using System.Security.Claims; -using IdentityServer8.Security; using Yavsc.Interfaces; namespace Yavsc.Extensions; @@ -238,8 +228,9 @@ public static class HostingExtensions options.EmitStaticAudienceClaim = true; }) .AddInMemoryIdentityResources(Config.IdentityResources) - .AddInMemoryClients(Config.Clients) - .AddInMemoryApiScopes(Config.ApiScopes) + .AddInMemoryClients(Config.TestingClients) + .AddClientStore() + .AddInMemoryApiScopes(Config.TestingApiScopes) .AddAspNetIdentity() ; if (builder.Environment.IsDevelopment()) diff --git a/src/Yavsc/Services/ClientStore.cs b/src/Yavsc/Services/ClientStore.cs new file mode 100644 index 00000000..f54d07d5 --- /dev/null +++ b/src/Yavsc/Services/ClientStore.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Yavsc.Models; +using IdentityServer8.Stores; +using IdentityServer8.Models; + +namespace Yavsc.Services; + +public class ClientStore : IClientStore +{ + public ClientStore(ApplicationDbContext applicationDbContext) + { + ApplicationDbContext = applicationDbContext; + } + + public ApplicationDbContext ApplicationDbContext { get; } + + public async Task FindClientByIdAsync(string clientId) + { + var clientFromDb = await ApplicationDbContext.Client.FirstAsync(c => c.Id == clientId); + + return new Client + { + ClientId = clientFromDb.Id, + ClientName = clientFromDb.DisplayName, + ClientSecrets = { new Secret(clientFromDb.Secret.Sha256()) }, + AllowedGrantTypes =[ GrantType.ClientCredentials, GrantType.DeviceFlow], + AllowedScopes = ["openid", "profile", "scope1"] + }; + } +} diff --git a/test/yavscTests/Mandatory/Remoting.cs b/test/yavscTests/Mandatory/Remoting.cs index 8cf25613..1d87879c 100644 --- a/test/yavscTests/Mandatory/Remoting.cs +++ b/test/yavscTests/Mandatory/Remoting.cs @@ -24,22 +24,20 @@ namespace yavscTests [MemberData(nameof(GetLoginIntentData), parameters: 1)] public async Task TestUserMayLogin ( - string clientId, - string clientSecret, - string scope, - string authorizeUrl, - string redirectUrl, - string accessTokenUrl + string userName, + string password ) { try { + String auth = _serverFixture.SiteSettings.Authority; + var oauthor = new OAuthenticator(clientId, clientSecret, scope, new Uri(authorizeUrl), new Uri(redirectUrl), new Uri(accessTokenUrl)); var query = new Dictionary { - ["Username"] = _serverFixture.TestingSetup.ValidCreds.UserName, - ["Password"] = _serverFixture.TestingSetup.ValidCreds.Password, + ["Username"] = userName, + ["Password"] = password, ["GrantType"] = "Password" }; @@ -56,7 +54,7 @@ namespace yavscTests var webex = ex as WebException; if (webex != null && webex.Status == (WebExceptionStatus)400) { - if (_serverFixture.TestingSetup.ValidCreds.UserName == "lame-user") + if (_serverFixture?.TestingSetup?.ValidCreds.UserName == "lame-user") { Console.WriteLine("Bad pass joe!"); return; @@ -66,9 +64,17 @@ namespace yavscTests } } - public static IEnumerable GetLoginIntentData(int count) + public static IEnumerable GetLoginIntentData(int countFakes = 0) { - return new object[][] {new object[]{ "", "", "", "", "", "" } }; + if (countFakes == 0) + return new object[][] { new object[] { "testuser", "test" } }; + + var fakUsers = new List(); + for (int i = 0; i < countFakes; i++) + { + fakUsers.Add(new String[] { "fakeTester" + i, "pass" + i }); + } + return fakUsers; } } diff --git a/test/yavscTests/WebServerFixture.cs b/test/yavscTests/WebServerFixture.cs index 533bb9fb..f1dd87e9 100644 --- a/test/yavscTests/WebServerFixture.cs +++ b/test/yavscTests/WebServerFixture.cs @@ -121,7 +121,7 @@ namespace isnd.tests EmailConfirmed = true }; - var result = userManager.CreateAsync(TestingUser).Result; + var result = userManager.CreateAsync(TestingUser,"test").Result; Assert.True(result.Succeeded);