diff --git a/TestAppRuna.TokenGenerator/Program.cs b/TestAppRuna.TokenGenerator/Program.cs new file mode 100644 index 0000000..63cf461 --- /dev/null +++ b/TestAppRuna.TokenGenerator/Program.cs @@ -0,0 +1,34 @@ +using IdentityModel.Client; +using System; +using System.Net.Http; +using System.Threading.Tasks; + +namespace TestAppRuna.TokenGenerator +{ + class Program + { + static void Main(string[] args) + { + Startup().Wait(); + } + static async Task Startup() + { + var clientID = "testappruna"; + var clientSecret = "1071ccf6-1ea7-4d75-b6e7-28846e386eb5"; + + var client = new HttpClient(); + var disco = await client.GetDiscoveryDocumentAsync("https://accounts.nanao.moe/auth/realms/staging"); + + var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest + { + Address = disco.TokenEndpoint, + + ClientId = clientID, + ClientSecret = clientSecret, + Scope = "customer-api" + }); + + Console.WriteLine(response.AccessToken); + } + } +} diff --git a/TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj b/TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj new file mode 100644 index 0000000..9f0437c --- /dev/null +++ b/TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/TestAppRuna.sln b/TestAppRuna.sln index 756519b..709b786 100644 --- a/TestAppRuna.sln +++ b/TestAppRuna.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31112.23 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna", "TestAppRuna\TestAppRuna.csproj", "{07486869-70E3-49C6-8C73-782BFCBD4284}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAppRuna", "TestAppRuna\TestAppRuna.csproj", "{07486869-70E3-49C6-8C73-782BFCBD4284}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna.Entities", "TestAppRuna.Entities\TestAppRuna.Entities.csproj", "{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAppRuna.Entities", "TestAppRuna.Entities\TestAppRuna.Entities.csproj", "{15F529A1-7CA7-4379-886B-47D1DBDA0AB4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAppRuna.TokenGenerator", "TestAppRuna.TokenGenerator\TestAppRuna.TokenGenerator.csproj", "{31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Debug|Any CPU.Build.0 = Debug|Any CPU {15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Release|Any CPU.ActiveCfg = Release|Any CPU {15F529A1-7CA7-4379-886B-47D1DBDA0AB4}.Release|Any CPU.Build.0 = Release|Any CPU + {31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31FC0DAE-E09B-4F14-9C1F-2F5A79D76353}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TestAppRuna/API/CustomerController.cs b/TestAppRuna/API/CustomerController.cs index 9437ea9..7e2303d 100644 --- a/TestAppRuna/API/CustomerController.cs +++ b/TestAppRuna/API/CustomerController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -13,6 +14,7 @@ namespace TestAppRuna.API { [Route("api/[controller]")] [ApiController] + [Authorize(AuthenticationSchemes = "nanaoaccounts")] public class CustomerController : ControllerBase { public CustomerController(ShopDbContext shopDbContext) diff --git a/TestAppRuna/Shop.slite.db b/TestAppRuna/Shop.slite.db new file mode 100644 index 0000000..033b63d Binary files /dev/null and b/TestAppRuna/Shop.slite.db differ diff --git a/TestAppRuna/Startup.cs b/TestAppRuna/Startup.cs index e85f3ed..04ba6a7 100644 --- a/TestAppRuna/Startup.cs +++ b/TestAppRuna/Startup.cs @@ -44,7 +44,13 @@ namespace TestAppRuna options.UseSqlite(Configuration.GetConnectionString("ShopDB")); }); - services.AddSwaggerDocument(); + services.AddAuthentication() + .AddJwtBearer("nanaoaccounts", options => + { + options.Authority = "https://accounts.nanao.moe/auth/realms/staging"; + options.Audience = "testappruna"; + } + ); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -76,13 +82,15 @@ namespace TestAppRuna app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); }); - } } } diff --git a/TestAppRuna/TestAppRuna.csproj b/TestAppRuna/TestAppRuna.csproj index bf6273d..c877435 100644 --- a/TestAppRuna/TestAppRuna.csproj +++ b/TestAppRuna/TestAppRuna.csproj @@ -5,6 +5,7 @@ +