Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Auth 2.0 using OptionsFactory (named options) #1144

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ad42516
#969 Make social auth claims mapping more configurable
Tratcher Feb 15, 2017
5039f74
Change korebuild branch and fix argument forwarding in bootstrapper
Mar 2, 2017
4da4402
Update AppVeyor and Travis settings
Mar 2, 2017
80ebf99
Update .travis.yml (#1138)
davidfowl Mar 9, 2017
0fd6f15
Fix wrong comment (GitHub -> Google)
Senorsen Mar 12, 2017
9de5519
Update of wrong variable name
JeanCollas Mar 8, 2017
32dd435
Add an opt-out DisableTelemetry option in the OpenID Connect middlewa…
Mar 13, 2017
35986c7
Back up to .NET 4.5.2
dougbu Mar 11, 2017
7f0b14a
React to aspnet/DotNetTools#272
Mar 14, 2017
8c70684
Update appveyor and travis settings
Mar 14, 2017
fc57e0a
React to aspnet/DataProtection#203
ajaybhargavb Mar 15, 2017
cdd1301
First cut of Authentication.Abstractions
HaoK Feb 3, 2017
8794a17
Add impl package
HaoK Feb 3, 2017
61875bb
Cleanup
HaoK Feb 3, 2017
13f79f7
Cleanup scheme to be data only
HaoK Feb 3, 2017
f54423e
Switch cookies over to new stack
HaoK Feb 9, 2017
8448bf0
Migrate twitter to new stack
HaoK Feb 9, 2017
e578250
Port jwtbearer
HaoK Feb 9, 2017
aaf1868
Port OIDC
HaoK Feb 10, 2017
0f6baee
Port OAuth
HaoK Feb 10, 2017
ed38a08
Add facebook
HaoK Feb 10, 2017
49bedc0
Fix test
HaoK Feb 10, 2017
2d8f752
Add google
HaoK Feb 13, 2017
deab966
Add feature to preserve old map resolve url behavior
HaoK Feb 13, 2017
4a4d110
PR feedback
HaoK Feb 13, 2017
533f733
PR tweaks
HaoK Feb 13, 2017
cb4e86e
Switch to ReadOnlyDictionary
HaoK Feb 13, 2017
17c66ef
Cleanup cookies finish response
HaoK Feb 14, 2017
8dccadf
Add fb test
HaoK Feb 14, 2017
b531da2
Add test for fb AppId/Secret options
HaoK Feb 14, 2017
00a101c
Fix issue with cookies renew + signout/signin
HaoK Feb 14, 2017
a032236
Add microsoft, cleanup test dirs
HaoK Feb 14, 2017
8922178
Reenable two skipped tests
HaoK Feb 15, 2017
780e3ce
Remove using
HaoK Feb 15, 2017
f9074f9
Support dynamic add/remove of schemes
HaoK Feb 22, 2017
33f7c91
Update samples to work with new stack
HaoK Feb 24, 2017
689ce6f
Update cookie interop tests
HaoK Feb 24, 2017
3e30b4d
Fix cookiepolicy test
HaoK Feb 24, 2017
860234d
Opt in for schemes to handle requests
HaoK Feb 24, 2017
10eb1f8
Opt out of request handling for JwtBearer
HaoK Feb 24, 2017
e84f79e
HandleRequest back to bool
HaoK Feb 27, 2017
5442175
Schemes must register callbackPaths
HaoK Feb 27, 2017
81b6c6c
Add GetAllSchemes
HaoK Feb 27, 2017
049623f
Rename Skip => Stop/None
HaoK Mar 6, 2017
2bc00f7
Cleanup
HaoK Mar 6, 2017
c40fb78
Bring back Skip
HaoK Mar 6, 2017
9257f78
Named options fork
HaoK Mar 9, 2017
26e91be
Options Factory fork
HaoK Mar 11, 2017
012b63d
Push dataprotection up to Remote
HaoK Mar 13, 2017
5a5c8d8
Remove AddRemoteScheme
HaoK Mar 13, 2017
e72e30e
Fix null ref
HaoK Mar 13, 2017
d4761bf
Add options factory tests
HaoK Mar 13, 2017
5de19cf
Add default behavior
HaoK Mar 15, 2017
31aa503
Update sln
HaoK Mar 15, 2017
05942d8
Fix merge issues
HaoK Mar 15, 2017
5660279
Drop Default prefix
HaoK Mar 15, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ env:
global:
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
- DOTNET_CLI_TELEMETRY_OPTOUT: 1
mono:
- 4.0.5
mono: none
os:
- linux
- osx
osx_image: xcode7.3
branches:
only:
- master
Expand All @@ -29,4 +27,4 @@ branches:
before_install:
- if test "$TRAVIS_OS_NAME" == "osx"; then brew update; brew install openssl; ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/; ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/; fi
script:
- ./build.sh --quiet verify
- ./build.sh
164 changes: 148 additions & 16 deletions Security.sln

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ branches:
- dev
- /^(.*\/)?ci-.*$/
build_script:
- build.cmd verify
- ps: .\build.ps1
clone_depth: 1
test: off
deploy: off
# Required for dotnet-test to work
os: Visual Studio 2015
os: Visual Studio 2017
16 changes: 8 additions & 8 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ErrorActionPreference = "Stop"

function DownloadWithRetry([string] $url, [string] $downloadLocation, [int] $retries)
function DownloadWithRetry([string] $url, [string] $downloadLocation, [int] $retries)
{
while($true)
{
Expand All @@ -19,7 +19,7 @@ function DownloadWithRetry([string] $url, [string] $downloadLocation, [int] $ret
Start-Sleep -Seconds 10

}
else
else
{
$exception = $_.Exception
throw $exception
Expand All @@ -33,7 +33,7 @@ cd $PSScriptRoot
$repoFolder = $PSScriptRoot
$env:REPO_FOLDER = $repoFolder

$koreBuildZip="https://github.com/aspnet/KoreBuild/archive/feature/msbuild.zip"
$koreBuildZip="https://github.com/aspnet/KoreBuild/archive/dev.zip"
if ($env:KOREBUILD_ZIP)
{
$koreBuildZip=$env:KOREBUILD_ZIP
Expand All @@ -43,18 +43,18 @@ $buildFolder = ".build"
$buildFile="$buildFolder\KoreBuild.ps1"

if (!(Test-Path $buildFolder)) {
Write-Host "Downloading KoreBuild from $koreBuildZip"
Write-Host "Downloading KoreBuild from $koreBuildZip"

$tempFolder=$env:TEMP + "\KoreBuild-" + [guid]::NewGuid()
New-Item -Path "$tempFolder" -Type directory | Out-Null

$localZipFile="$tempFolder\korebuild.zip"

DownloadWithRetry -url $koreBuildZip -downloadLocation $localZipFile -retries 6

Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($localZipFile, $tempFolder)

New-Item -Path "$buildFolder" -Type directory | Out-Null
copy-item "$tempFolder\**\build\*" $buildFolder -Recurse

Expand All @@ -64,4 +64,4 @@ if (!(Test-Path $buildFolder)) {
}
}

&"$buildFile" $args
&"$buildFile" @args
22 changes: 11 additions & 11 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
repoFolder="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $repoFolder

koreBuildZip="https://github.com/aspnet/KoreBuild/archive/feature/msbuild.zip"
koreBuildZip="https://github.com/aspnet/KoreBuild/archive/dev.zip"
if [ ! -z $KOREBUILD_ZIP ]; then
koreBuildZip=$KOREBUILD_ZIP
fi
Expand All @@ -12,12 +12,12 @@ buildFile="$buildFolder/KoreBuild.sh"

if test ! -d $buildFolder; then
echo "Downloading KoreBuild from $koreBuildZip"
tempFolder="/tmp/KoreBuild-$(uuidgen)"

tempFolder="/tmp/KoreBuild-$(uuidgen)"
mkdir $tempFolder

localZipFile="$tempFolder/korebuild.zip"

retries=6
until (wget -O $localZipFile $koreBuildZip 2>/dev/null || curl -o $localZipFile --location $koreBuildZip 2>/dev/null)
do
Expand All @@ -29,18 +29,18 @@ if test ! -d $buildFolder; then
echo "Waiting 10 seconds before retrying. Retries left: $retries"
sleep 10s
done

unzip -q -d $tempFolder $localZipFile

mkdir $buildFolder
cp -r $tempFolder/**/build/** $buildFolder

chmod +x $buildFile

# Cleanup
if test -d $tempFolder; then
rm -rf $tempFolder
rm -rf $tempFolder
fi
fi

$buildFile -r $repoFolder "$@"
$buildFile -r $repoFolder "$@"
10 changes: 4 additions & 6 deletions samples/CookieSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -13,24 +14,21 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.AddCookieAuthentication();
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{
loggerfactory.AddConsole(LogLevel.Information);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true
});
app.UseAuthentication();

app.Run(async context =>
{
if (!context.User.Identities.Any(identity => identity.IsAuthenticated))
{
var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }, CookieAuthenticationDefaults.AuthenticationScheme));
await context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);
await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);

context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Hello First timer");
Expand Down
11 changes: 4 additions & 7 deletions samples/CookieSessionSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -14,18 +15,14 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.AddCookieAuthentication(o => o.SessionStore = new MemoryCacheTicketStore());
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{
loggerfactory.AddConsole(LogLevel.Information);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
SessionStore = new MemoryCacheTicketStore()
});
app.UseAuthentication();

app.Run(async context =>
{
Expand All @@ -39,7 +36,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
claims.Add(new Claim(ClaimTypes.Role, "SomeRandomGroup" + i, ClaimValueTypes.String, "IssuedByBob", "OriginalIssuerJoe"));
}

await context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme)));

context.Response.ContentType = "text/plain";
Expand Down
54 changes: 27 additions & 27 deletions samples/JwtBearerSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -42,7 +43,28 @@ public Startup(IHostingEnvironment env)
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.AddJwtBearerAuthentication(o =>
{
// You also need to update /wwwroot/app/scripts/app.js
o.Authority = Configuration["jwt:authority"];
o.Audience = Configuration["jwt:audience"];
o.Events = new JwtBearerEvents()
{
OnAuthenticationFailed = c =>
{
c.HandleResponse();

c.Response.StatusCode = 500;
c.Response.ContentType = "text/plain";
if (Environment.IsDevelopment())
{
// Debug only, in production do not share exceptions with the remote host.
return c.Response.WriteAsync(c.Exception.ToString());
}
return c.Response.WriteAsync("An error occurred processing your authentication.");
}
};
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -69,44 +91,22 @@ public void Configure(IApplicationBuilder app)
app.UseDefaultFiles();
app.UseStaticFiles();

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
// You also need to update /wwwroot/app/scripts/app.js
Authority = Configuration["jwt:authority"],
Audience = Configuration["jwt:audience"],
Events = new JwtBearerEvents()
{
OnAuthenticationFailed = c =>
{
c.HandleResponse();

c.Response.StatusCode = 500;
c.Response.ContentType = "text/plain";
if (Environment.IsDevelopment())
{
// Debug only, in production do not share exceptions with the remote host.
return c.Response.WriteAsync(c.Exception.ToString());
}
return c.Response.WriteAsync("An error occurred processing your authentication.");
}
}
});
app.UseAuthentication();

// [Authorize] would usually handle this
app.Use(async (context, next) =>
{
// Use this if options.AutomaticAuthenticate = false
// Use this if there are multiple authentication schemes
// var user = await context.Authentication.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);

var user = context.User; // We can do this because of options.AutomaticAuthenticate = true;
var user = context.User; // We can do this because of there's only a single authentication scheme
if (user?.Identity?.IsAuthenticated ?? false)
{
await next();
}
else
{
// We can do this because of options.AutomaticChallenge = true;
await context.Authentication.ChallengeAsync();
await context.ChallengeAsync(JwtBearerDefaults.AuthenticationScheme);
}
});

Expand Down
12 changes: 5 additions & 7 deletions samples/OpenIdConnect.AzureAdSample/AuthPropertiesTokenCache.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace OpenIdConnect.AzureAdSample
Expand Down Expand Up @@ -58,10 +57,9 @@ private void BeforeAccessNotificationWithProperties(TokenCacheNotificationArgs a
private void BeforeAccessNotificationWithContext(TokenCacheNotificationArgs args)
{
// Retrieve the auth session with the cached tokens
var authenticateContext = new AuthenticateContext(_signInScheme);
_httpContext.Authentication.AuthenticateAsync(authenticateContext).Wait();
_authProperties = new AuthenticationProperties(authenticateContext.Properties);
_principal = authenticateContext.Principal;
var result = _httpContext.AuthenticateAsync(_signInScheme).Result;
_authProperties = result.Ticket.Properties;
_principal = result.Ticket.Principal;

BeforeAccessNotificationWithProperties(args);
}
Expand All @@ -87,7 +85,7 @@ private void AfterAccessNotificationWithContext(TokenCacheNotificationArgs args)
var cachedTokens = Serialize();
var cachedTokensText = Convert.ToBase64String(cachedTokens);
_authProperties.Items[TokenCacheKey] = cachedTokensText;
_httpContext.Authentication.SignInAsync(_signInScheme, _principal, _authProperties).Wait();
_httpContext.SignInAsync(_signInScheme, _principal, _authProperties).Wait();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.2.0-*" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.2.0-*" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.13.8" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild*" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.2.0-*" />
</ItemGroup>
</Project>
Loading