From 29644d15c0357970bb70d3d17154ce5924ad5031 Mon Sep 17 00:00:00 2001 From: Damillora Date: Thu, 25 Mar 2021 11:21:55 +0700 Subject: [PATCH] Authentication --- TestAppRuna.TokenGenerator/Program.cs | 34 ++++++++++++++++++ .../TestAppRuna.TokenGenerator.csproj | 12 +++++++ TestAppRuna.sln | 10 ++++-- TestAppRuna/API/CustomerController.cs | 4 ++- TestAppRuna/Shop.slite.db | Bin 0 -> 36864 bytes TestAppRuna/Startup.cs | 12 +++++-- TestAppRuna/TestAppRuna.csproj | 1 + 7 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 TestAppRuna.TokenGenerator/Program.cs create mode 100644 TestAppRuna.TokenGenerator/TestAppRuna.TokenGenerator.csproj create mode 100644 TestAppRuna/Shop.slite.db 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 0000000000000000000000000000000000000000..033b63d10444d0af9a802d4bc18d8cbd0908e197 GIT binary patch literal 36864 zcmeI)+fUn890zbaTmrP%K#r=Ukd!Tu?ZT*bp%~=zqQ=@^m~7u2MuT3b-A_AM zx}}4gVpBbgy-++}ToiY9__IOq^s*hjxMZIYno;}3u-$Db7%0$yC|xZqR8z zVl|6DxC?nv6!{<9Nt;Pr$*;wU`lEnXR|q?j(~4JMOm(_PYovNqC)LKm0dbG2C-s`m z@=4)%|C+c!j%$@eyLLwQ=^3#b^`lCawSGve^+#EIBcuYU(^J-1+F(}T#`(Rkk9X^* z-8M71Xq1YF>>t%=Wv`lyWxJ4-VXqWPjrz1kt1dlZ5vC)G;#tm4l2UB`J$gXdB5~~# z*Y;>S#`lgP9rxHWn;yYUWRnBPYEG@Pc==6szq(SqlIQZ`<|hA2OJ*^vnmx}?&SjR( zT2A@XoS0`qe|kD^YL3llHbqsorn35MDP-Bl{jfF4PiK`4`HT5Q(cR>^a*+Y;L05WR#=)n=rGie~mLM$R>aGKxrokFUJ5_ zkVe)Mm>2mk^QfB*y_009U<00Izz z00bcL9|dasd}GIzHMK-bz9Z?D?@PL4mn26w%aWrirm1+kYnSQSFnk`6!*CD{J7Kro z$r3N){3k)H)xHd)!EUS9*~`s0KGRB$X_=;ONRH#kl5QHdXr}6yEc0yG z51;o)HRuMtmq~h}Ar{O2j`BYS_wJZhSX(*aS5A1te&K)s1Rwwb2tWV=5P$##AOHaf zK;T0V*q&?fvs^&PeLmM%p5+3;DvOF2UUS0hwLh;PA3~TI0|X!d0SG_<0uX=z1Rwwb z2tZ(Vfwvp$zGiujr<5e!W_$O#?6AFiS=L!f(>&YsJXLk0K|kzX$h$IY`In7#)A8++ z!M6KdMP>W*mS2`E+qERsbPbQ%hHHAtc$M!q)@9Rj6<<{)Pbs^Su6Ukg>y{?@u5Kur zT((tJ9j~(YShNh=^Bu*MypnFRDw-@gCB>2CvdQi_uw~cu{lnnA$8Ih7GQ1eJo`s~| z529zp4Z2E+4VJPS6dsFZ*|Qv__*DDHX5#&SUU(Dl z|LfO}*?mzG0uX=z1Rwwb2tWV=5P$##AaKV8ewyXNLi~k!-_LS6A^ujq+AJ3l;(yyO z&T{D>+5eyA;y}Fr&%X2z2LvDh0SG_<0uX=z1Rwwb2tWV=cUSeDiGiQkNtlv2M9m_0uX=z1Rwwb2tWV=5P-nl7QpBKyFI^%4gwH> V00bZa0SG_<0uX=z1RxL#{0(BrLiPXv literal 0 HcmV?d00001 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 @@ +