Skip to content

Commit

Permalink
Merge pull request #12 from aguacongas/master
Browse files Browse the repository at this point in the history
Redis store and concurrency check
  • Loading branch information
aguacongas authored Dec 21, 2018
2 parents 9851589 + 0a150ef commit 6c1e968
Show file tree
Hide file tree
Showing 33 changed files with 873 additions and 894 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,6 @@ ASALocalRun/
.mfractor/
/ReportGenerator
/coverage/docs
coverage.*
coverage.*

*.xml
15 changes: 15 additions & 0 deletions DymamicAuthProviders.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.npmrc = .npmrc
.releaserc.json = .releaserc.json
appveyor.yml = appveyor.yml
appveyorinit.ps1 = appveyorinit.ps1
build.cmd = build.cmd
build.ps1 = build.ps1
dotnet-install.ps1 = dotnet-install.ps1
Expand All @@ -38,6 +39,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.AspNetCore.Authe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.AspNetCore.Authentication.TestBase", "src\Aguacongas.AspNetCore.Authentication.TestBase\Aguacongas.AspNetCore.Authentication.TestBase.csproj", "{B3DEE02F-9CD3-4009-AFEE-00F0B09811EC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.AspNetCore.Authentication.Redis", "src\Aguacongas.AspNetCore.Authentication.Redis\Aguacongas.AspNetCore.Authentication.Redis.csproj", "{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.AspNetCore.Authentication.Redis.Test", "test\Aguacongas.AspNetCore.Authentication.Redis.Test\Aguacongas.AspNetCore.Authentication.Redis.Test.csproj", "{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +73,14 @@ Global
{B3DEE02F-9CD3-4009-AFEE-00F0B09811EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3DEE02F-9CD3-4009-AFEE-00F0B09811EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3DEE02F-9CD3-4009-AFEE-00F0B09811EC}.Release|Any CPU.Build.0 = Release|Any CPU
{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA}.Release|Any CPU.Build.0 = Release|Any CPU
{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -79,6 +92,8 @@ Global
{8CC87457-C579-4E39-A1C4-BAADADF500DE} = {245AA4AB-ECF4-4C69-B63D-C59D4B3F918C}
{01E5055D-BF6A-435C-8858-F63A8FAC6D86} = {8DE53FA4-6C58-4FE4-840C-8CD5D5370324}
{B3DEE02F-9CD3-4009-AFEE-00F0B09811EC} = {245AA4AB-ECF4-4C69-B63D-C59D4B3F918C}
{CE9FE264-6139-47BF-970C-D1BBFCD7EFEA} = {245AA4AB-ECF4-4C69-B63D-C59D4B3F918C}
{87CFA3EF-2A15-4861-A6DC-C74E9CA3EBCE} = {8DE53FA4-6C58-4FE4-840C-8CD5D5370324}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {823D9407-93C0-443E-89EC-D9DBB006A195}
Expand Down
27 changes: 3 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Nuget packages
--------------
|Aguacongas.AspNetCore.Authentication|Aguacongas.AspNetCore.Authentication.EntityFramework|Aguacongas.AspNetCore.Authentication.TestBase|
|:------:|:------:|:------:|
|[![][Aguacongas.AspNetCore.Authentication-badge]][Aguacongas.AspNetCore.Authentication-nuget]|[![][Aguacongas.AspNetCore.Authentication.EntityFramework-badge]][Aguacongas.AspNetCore.Authentication.EntityFramework-nuget]|
|[![][Aguacongas.AspNetCore.Authentication.TestBase-downloadbadge]][Aguacongas.AspNetCore.Authentication.TestBase-nuget]|
|[![][Aguacongas.AspNetCore.Authentication-badge]][Aguacongas.AspNetCore.Authentication-nuget]|[![][Aguacongas.AspNetCore.Authentication.EntityFramework-badge]][Aguacongas.AspNetCore.Authentication.EntityFramework-nuget]|[![][Aguacongas.AspNetCore.Authentication.TestBase-badge]][Aguacongas.AspNetCore.Authentication.TestBase-nuget]|
|[![][Aguacongas.AspNetCore.Authentication-downloadbadge]][Aguacongas.AspNetCore.Authentication-nuget]|[![][Aguacongas.AspNetCore.Authentication.EntityFramework-downloadbadge]][Aguacongas.AspNetCore.Authentication.EntityFramework-nuget]|[![][Aguacongas.AspNetCore.Authentication.TestBase-downloadbadge]][Aguacongas.AspNetCore.Authentication.TestBase-nuget]|

[Aguacongas.AspNetCore.Authentication-badge]: https://img.shields.io/nuget/v/Aguacongas.AspNetCore.Authentication.svg
[Aguacongas.AspNetCore.Authentication-downloadbadge]: https://img.shields.io/nuget/dt/Aguacongas.AspNetCore.Authentication.svg
Expand Down Expand Up @@ -114,25 +114,4 @@ And in the `Configure` method load the configuration with `LoadDynamicAuthentica

```

## Extends

You can implement your own store by implementing `IDynamicProviderStore<TSchemeDefinition>` interface.
To verify your implementation you can override `DynamicManagerTestBase<TSchemeDefinition>` of `Aguacongas.AspNetCore.Authentication.TestBase`.

``` csharp
public class DynamicManagerTest: DynamicManagerTestBase<SchemeDefinition>
{
public DynamicManagerTest(ITestOutputHelper output): base(output)
{
}

protected override DynamicAuthenticationBuilder AddStore(DynamicAuthenticationBuilder builder)
{
builder.Services.AddDbContext<SchemeDbContext>(options =>
{
options.UseInMemoryDatabase(Guid.NewGuid().ToString());
});
return builder.AddEntityFrameworkStore<SchemeDbContext>();
}
}
```
Read the [wiki](../../wiki) for more information.
17 changes: 10 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: 1.0.{build}
skip_tags: true
stack: node 9
stack: node 9, redis
skip_commits:
message: /\[chore\]/
message: /^chore(release)/
branches:
only:
- master
Expand All @@ -26,6 +26,9 @@ install:
- sh: nuget install GitVersion.CommandLine -ExcludeVersion
- sh: mono GitVersion.CommandLine/tools/GitVersion.exe /l console /output buildserver
- ps: if ($isWindows) { .\dotnet-install.ps1 -Version 2.2.100 }
- cmd: nuget install redis-64 -excludeversion
- cmd: redis-64\tools\redis-server.exe --service-install
- cmd: redis-64\tools\redis-server.exe --service-start
- cmd: gitversion /l console /output buildserver
- cmd: nuget install ReportGenerator -ExcludeVersion
- ps: ./appveyorinit.ps1
Expand All @@ -49,7 +52,7 @@ deploy:
auth_token: $(GH_TOKEN)
draft: true
prerelease: true
release: $(APPVEYOR_BUILD_VERSION)
release: $(Version)
on:
branch:
- /preview*/
Expand Down Expand Up @@ -83,10 +86,10 @@ for:
- cmd: git config --global user.name "Aguacongas"
- cmd: git checkout gh-pages
- cmd: git stash
- cmd: mkdir %APPVEYOR_BUILD_VERSION%
- cmd: move coverage\docs %APPVEYOR_BUILD_VERSION%
- cmd: git add %APPVEYOR_BUILD_VERSION%
- cmd: git commit %APPVEYOR_BUILD_VERSION% -m "Appveyor build succed %APPVEYOR_BUILD_NUMBER%"
- cmd: mkdir %Version%
- cmd: move coverage\docs %Version%
- cmd: git add %Version%
- cmd: git commit %Version% -m "Appveyor build succed %APPVEYOR_BUILD_NUMBER%"
- cmd: git push
-
branches:
Expand Down
6 changes: 3 additions & 3 deletions appveyorinit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ else
}

appveyor SetVariable -Name SemVer -Value $nextversion
appveyor AddMessage "SemVer = $nextversion"

if (![string]::IsNullOrEmpty($env:GitVersion_PreReleaseLabel))
{
$nextversion = "$nextversion-$env:GitVersion_PreReleaseLabel$env:GitVersion_CommitsSinceVersionSourcePadded"
}

appveyor SetVariable -Name Version -Value $nextversion
appveyor UpdateBuild -Version $nextversion
$builnumbersuffix = Get-Date -Format "mmddyyyy-HHmm"
$builnumber = "$builnumber-$builnumbersuffix"
appveyor AddMessage "Version = $nextversion"

dotnet restore
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\..\src\Aguacongas.AspNetCore.Authentication.EntityFramework\Aguacongas.AspNetCore.Authentication.EntityFramework.csproj" />
<PackageReference Include="Aguacongas.AspNetCore.Authentication.EntityFramework" Version="1.0.0-preview*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public IActionResult Index()
return View(_manager.ManagedHandlerType.Select(t => t.Name));
}

// Returns an empty details view to create a scheme for a type of handler
[Route("Create/{type}")]
public IActionResult Create(string type)
{
Expand All @@ -39,6 +40,7 @@ public IActionResult Create(string type)
});
}

// Creates a new scheme
[HttpPost]
[Route("Create/{type}")]
public async Task<IActionResult> Create(AuthenticationViewModel model)
Expand Down Expand Up @@ -72,25 +74,15 @@ await _manager.AddAsync(new SchemeDefinition
return View(model);
}

// Returns a scheme details view to update id
[Route("Update/{scheme}")]
public async Task<IActionResult> Update(string scheme)
{
AuthenticationViewModel model;
var definition = await _manager.FindBySchemeAsync(scheme);
if (definition == null)
{
var schemes = await _signInManager.GetExternalAuthenticationSchemesAsync();
var authenticationScheme = schemes.FirstOrDefault(s => s.Name == scheme);
if (authenticationScheme == null)
{
return NotFound();
}
model = new AuthenticationViewModel
{
Scheme = authenticationScheme.Name,
DisplayName = authenticationScheme.DisplayName,
HandlerType = authenticationScheme.HandlerType.Name
};
return NotFound();
}
else
{
Expand All @@ -101,20 +93,15 @@ public async Task<IActionResult> Update(string scheme)
HandlerType = definition.HandlerType.Name
};

if (definition.Options is OAuthOptions oAuthOptions) // GoogleOptions is OAuthOptions
{
model.ClientId = oAuthOptions.ClientId;
model.ClientSecret = oAuthOptions.ClientSecret;
}
else
{
return Error();
}
var oAuthOptions = definition.Options as OAuthOptions; // GoogleOptions is OAuthOptions
model.ClientId = oAuthOptions.ClientId;
model.ClientSecret = oAuthOptions.ClientSecret;
}

return View(model);
}

// Updates a scheme
[HttpPost]
[Route("Update/{scheme}")]
public async Task<IActionResult> Update(AuthenticationViewModel model)
Expand All @@ -124,15 +111,13 @@ public async Task<IActionResult> Update(AuthenticationViewModel model)
var definition = await _manager.FindBySchemeAsync(model.Scheme);
if (definition == null)
{
await Create(model);

return View(model);
return NotFound();
}

if (definition.Options is OAuthOptions oAuthOptions) // GoogleOptions is OAuthOptions
{
oAuthOptions.ClientId = model.ClientId;
oAuthOptions.ClientSecret = model.ClientSecret;
oAuthOptions.ClientSecret = model.ClientSecret;
}

definition.DisplayName = model.DisplayName;
Expand All @@ -143,6 +128,7 @@ public async Task<IActionResult> Update(AuthenticationViewModel model)
return View(model);
}

// Lists all schemes we can manage
[Route("List")]
public async Task<IActionResult> List()
{
Expand All @@ -160,9 +146,16 @@ public async Task<IActionResult> List()
}));
}

// Deletes a scheme
[Route("Delete/{scheme}")]
public async Task<IActionResult> Delete(string scheme)
{
var definition = await _manager.FindBySchemeAsync(scheme);
if (definition == null)
{
return NotFound();
}

await _manager.RemoveAsync(scheme);
return RedirectToAction("List");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Threading.Tasks;

namespace Aguacongas.AspNetCore.Authentication.Sample
{
/// <summary>
/// Subsribe to scheme change notifications
/// </summary>
public class SchemeChangeSubscriber
{
private readonly NoPersistentDynamicManager<SchemeDefinition> _manager;
private readonly IDynamicProviderStore<SchemeDefinition> _store;

public SchemeChangeSubscriber(NoPersistentDynamicManager<SchemeDefinition> manager, IDynamicProviderStore<SchemeDefinition> store)
{
_manager = manager;
_store = store;
}

/// <summary>
/// Called when scheme change.
/// </summary>
/// <param name="change">The change.</param>
/// <returns></returns>
public async Task OnSchemeChange(SchemeChange change)
{
var definition = await _store.FindBySchemeAsync(change.Scheme);
switch(change.Kind)
{
case SchemeChangeKind.Added:
await _manager.AddAsync(definition);
break;
case SchemeChangeKind.Updated:
await _manager.UpdateAsync(definition);
break;
case SchemeChangeKind.Deleted:
await _manager.RemoveAsync(change.Scheme);
break;
}
}
}

public enum SchemeChangeKind
{
Added,
Updated,
Deleted
}

public class SchemeChange
{
public string Scheme { get; set; }
public SchemeChangeKind Kind { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<DocumentationFile>C:\Projects\Perso\DymamicAuthProviders\src\Aguacongas.AspNetCore.Authentication.EntityFramework\Aguacongas.AspNetCore.Authentication.EntityFramework.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Remove="Aguacongas.AspNetCore.Authentication.EntityFramework.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 6c1e968

Please sign in to comment.