Skip to content

Commit

Permalink
Added new packages (#2)
Browse files Browse the repository at this point in the history
Added new packages
- EMG.Extensions.AspNetCore
- EMG.Extensions.AspNetCore.Authentication.JWT
  • Loading branch information
Kralizek authored Apr 4, 2019
1 parent b40ca1c commit 2f26909
Show file tree
Hide file tree
Showing 30 changed files with 1,406 additions and 1 deletion.
84 changes: 84 additions & 0 deletions AspNetCoreExtensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Error", "tests\Filter
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Error.Web", "tests\Filters\Error\Tests.Error.Web\Tests.Error.Web.csproj", "{6A0FD3D1-2F9F-47E8-8481-67CCF435B081}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authentication", "Authentication", "{DDFC72B2-92CB-41FB-85E5-B560D7D004D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT", "src\Authentication\JWT\JWT.csproj", "{32F69F18-F877-41CF-8C4A-45A465A2632E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authentication", "Authentication", "{A42F5C4C-C045-4A5A-9BF3-7A91AD3F4BFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT", "samples\Authentication\JWT\JWT.csproj", "{2FAA4523-845A-4AC8-96A7-3FEF6C419238}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extensions", "src\Extensions\Extensions.csproj", "{B9AFB4F4-2084-43D9-8535-692AE2D567DC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Authentication", "Authentication", "{01867768-930E-4D35-B3E4-35CBB5E25619}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Jwt", "tests\Authentication\Tests.Jwt\Tests.Jwt.csproj", "{95D7C991-6F3B-464B-8016-EC3B2508465B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Jwt.Web", "tests\Authentication\Tests.Jwt.Web\Tests.Jwt.Web.csproj", "{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +102,66 @@ Global
{6A0FD3D1-2F9F-47E8-8481-67CCF435B081}.Release|x64.Build.0 = Release|Any CPU
{6A0FD3D1-2F9F-47E8-8481-67CCF435B081}.Release|x86.ActiveCfg = Release|Any CPU
{6A0FD3D1-2F9F-47E8-8481-67CCF435B081}.Release|x86.Build.0 = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|x64.ActiveCfg = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|x64.Build.0 = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|x86.ActiveCfg = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Debug|x86.Build.0 = Debug|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|Any CPU.Build.0 = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|x64.ActiveCfg = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|x64.Build.0 = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|x86.ActiveCfg = Release|Any CPU
{32F69F18-F877-41CF-8C4A-45A465A2632E}.Release|x86.Build.0 = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|x64.ActiveCfg = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|x64.Build.0 = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|x86.ActiveCfg = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Debug|x86.Build.0 = Debug|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|Any CPU.Build.0 = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|x64.ActiveCfg = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|x64.Build.0 = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|x86.ActiveCfg = Release|Any CPU
{2FAA4523-845A-4AC8-96A7-3FEF6C419238}.Release|x86.Build.0 = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|x64.ActiveCfg = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|x64.Build.0 = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|x86.ActiveCfg = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Debug|x86.Build.0 = Debug|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|Any CPU.Build.0 = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|x64.ActiveCfg = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|x64.Build.0 = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|x86.ActiveCfg = Release|Any CPU
{B9AFB4F4-2084-43D9-8535-692AE2D567DC}.Release|x86.Build.0 = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|x64.ActiveCfg = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|x64.Build.0 = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|x86.ActiveCfg = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Debug|x86.Build.0 = Debug|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|Any CPU.Build.0 = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|x64.ActiveCfg = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|x64.Build.0 = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|x86.ActiveCfg = Release|Any CPU
{95D7C991-6F3B-464B-8016-EC3B2508465B}.Release|x86.Build.0 = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|x64.ActiveCfg = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|x64.Build.0 = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|x86.ActiveCfg = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Debug|x86.Build.0 = Debug|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|Any CPU.Build.0 = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|x64.ActiveCfg = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|x64.Build.0 = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|x86.ActiveCfg = Release|Any CPU
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5E6F85BC-5225-41C2-B297-5D98906818AE} = {BD9BDC22-5E7C-4585-808F-443B62010518}
Expand All @@ -96,5 +172,13 @@ Global
{D22E93E1-1D31-4DD9-823E-586DA20F9142} = {29802F64-6109-4841-89DE-B4BD6F0AA45E}
{F31259DC-7E97-4050-8FC8-2D19DD01AF94} = {D22E93E1-1D31-4DD9-823E-586DA20F9142}
{6A0FD3D1-2F9F-47E8-8481-67CCF435B081} = {D22E93E1-1D31-4DD9-823E-586DA20F9142}
{DDFC72B2-92CB-41FB-85E5-B560D7D004D5} = {82EC7D42-2E39-46F6-9601-FA33913A4EB5}
{32F69F18-F877-41CF-8C4A-45A465A2632E} = {DDFC72B2-92CB-41FB-85E5-B560D7D004D5}
{A42F5C4C-C045-4A5A-9BF3-7A91AD3F4BFE} = {BD9BDC22-5E7C-4585-808F-443B62010518}
{2FAA4523-845A-4AC8-96A7-3FEF6C419238} = {A42F5C4C-C045-4A5A-9BF3-7A91AD3F4BFE}
{B9AFB4F4-2084-43D9-8535-692AE2D567DC} = {82EC7D42-2E39-46F6-9601-FA33913A4EB5}
{01867768-930E-4D35-B3E4-35CBB5E25619} = {F1D5D932-EFCA-43F0-87B6-81ADC0940D60}
{95D7C991-6F3B-464B-8016-EC3B2508465B} = {01867768-930E-4D35-B3E4-35CBB5E25619}
{7D8D9BFB-6D3E-433A-A688-FD38B9B3C314} = {01867768-930E-4D35-B3E4-35CBB5E25619}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Task("RunTests")
var projectFile = MakeAbsolute(file).ToString();

var dotCoverSettings = new DotCoverCoverSettings()
.WithFilter("+:EMG*")
.WithFilter("+:EMG.Extensions*")
.WithFilter("-:Tests*")
.WithFilter("-:TestUtils");

Expand Down
45 changes: 45 additions & 0 deletions samples/Authentication/JWT/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace JWT.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
26 changes: 26 additions & 0 deletions samples/Authentication/JWT/JWT.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Authentication\JWT\JWT.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions samples/Authentication/JWT/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace JWT
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
.UseStartup<Startup>();
}
}
48 changes: 48 additions & 0 deletions samples/Authentication/JWT/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using EMG.Extensions.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace JWT
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddJwtAuthentication(Configuration)
.RequireAuthentication()
.AddBasicUserAuthenticator()
.AddFormUserExtractor();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}


app.UseJwtAuthentication();


app.UseHttpsRedirection();
app.UseMvc();
}
}
}
9 changes: 9 additions & 0 deletions samples/Authentication/JWT/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
19 changes: 19 additions & 0 deletions samples/Authentication/JWT/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"JWT": {
"SecretKey": "test-key-test-key-test-key",
"IssuerOptions": {
"Issuer": "Test API",
"Audience": "https://localhost:5001"
},
"Client": {
"Username": "Test",
"Password": "testing"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Security.Claims;
using System.Threading.Tasks;

namespace EMG.Extensions.AspNetCore.Authenticators
{
public class AlwaysTrueUserAuthenticator : IUserAuthenticator
{
public Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity)
{
var claims = new[] { new Claim(ClaimTypes.Name, user.UserName) };

identity = new ClaimsIdentity(claims);

return Task.FromResult(true);
}
}
}
42 changes: 42 additions & 0 deletions src/Authentication/JWT/Authenticators/BasicUserAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using System.Threading.Tasks;

namespace EMG.Extensions.AspNetCore.Authenticators
{
public class BasicUserAuthenticator : IUserAuthenticator
{
private readonly BasicCredentials _pair;

public BasicUserAuthenticator(BasicCredentials pair)
{
_pair = pair ?? throw new ArgumentNullException(nameof(pair));
}

public Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity)
{
identity = null;

if (user.UserName == _pair.Username && user.Password == _pair.Password)
{
var claims = new[] { new Claim(ClaimTypes.Name, user.UserName) };

identity = new ClaimsIdentity(claims);

return Task.FromResult(true);
}

return Task.FromResult(false);
}
}

public class BasicCredentials
{
[Required]
public string Username { get; set; }

[Required]
public string Password { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/Authentication/JWT/Authenticators/IUserAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Security.Claims;
using System.Threading.Tasks;

namespace EMG.Extensions.AspNetCore.Authenticators
{
public interface IUserAuthenticator
{
Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity);
}
}
Loading

0 comments on commit 2f26909

Please sign in to comment.