-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23b52e6
commit 060ef4a
Showing
10 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/AspNet.Security.OAuth.HealthGraph/AspNet.Security.OAuth.HealthGraph.xproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>c20be880-52cb-491c-977c-f08702376766</ProjectGuid> | ||
<RootNamespace>AspNet.Security.OAuth.HealthGraph</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
50 changes: 50 additions & 0 deletions
50
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using Microsoft.AspNet.Authentication; | ||
using Microsoft.AspNet.Authentication.OAuth; | ||
|
||
namespace AspNet.Security.OAuth.HealthGraph { | ||
/// <summary> | ||
/// Default values used by the HealthGraph authentication middleware. | ||
/// </summary> | ||
public static class HealthGraphAuthenticationDefaults { | ||
/// <summary> | ||
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>. | ||
/// </summary> | ||
public const string AuthenticationScheme = "HealthGraph"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.DisplayName"/>. | ||
/// </summary> | ||
public const string DisplayName = "HealthGraph"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.ClaimsIssuer"/>. | ||
/// </summary> | ||
public const string Issuer = "HealthGraph"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.CallbackPath"/>. | ||
/// </summary> | ||
public const string CallbackPath = "/signin-healthgraph"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
/// </summary> | ||
public const string AuthorizationEndpoint = "https://runkeeper.com/apps/authorize"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
/// </summary> | ||
public const string TokenEndpoint = "https://runkeeper.com/apps/token"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
/// </summary> | ||
public const string UserInformationEndpoint = "https://api.runkeeper.com/user"; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using System; | ||
using AspNet.Security.OAuth.HealthGraph; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNet.Builder { | ||
public static class HealthGraphAuthenticationExtensions { | ||
public static IApplicationBuilder UseHealthGraphAuthentication( | ||
[NotNull] this IApplicationBuilder app, | ||
[NotNull] HealthGraphAuthenticationOptions options) { | ||
return app.UseMiddleware<HealthGraphAuthenticationMiddleware>(options); | ||
} | ||
|
||
public static IApplicationBuilder UseHealthGraphAuthentication( | ||
[NotNull] this IApplicationBuilder app, | ||
[NotNull] Action<HealthGraphAuthenticationOptions> configuration) { | ||
var options = new HealthGraphAuthenticationOptions(); | ||
configuration(options); | ||
|
||
return app.UseMiddleware<HealthGraphAuthenticationMiddleware>(options); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using AspNet.Security.OAuth.Extensions; | ||
using Microsoft.AspNet.Authentication; | ||
using Microsoft.AspNet.Authentication.OAuth; | ||
using Microsoft.AspNet.Http.Authentication; | ||
using Microsoft.Extensions.Internal; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace AspNet.Security.OAuth.HealthGraph { | ||
public class HealthGraphAuthenticationHandler : OAuthHandler<HealthGraphAuthenticationOptions> { | ||
public HealthGraphAuthenticationHandler([NotNull] HttpClient client) | ||
: base(client) { | ||
} | ||
|
||
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity, | ||
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens) { | ||
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint); | ||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.com.runkeeper.User+json")); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); | ||
|
||
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); | ||
|
||
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, HealthGraphAuthenticationHelper.GetIdentifier(payload), Options.ClaimsIssuer); | ||
|
||
var context = new OAuthCreatingTicketContext(Context, Options, Backchannel, tokens, payload) { | ||
Principal = new ClaimsPrincipal(identity), | ||
Properties = properties | ||
}; | ||
|
||
await Options.Events.CreatingTicket(context); | ||
|
||
if (context.Principal?.Identity == null) { | ||
return null; | ||
} | ||
|
||
return new AuthenticationTicket(context.Principal, context.Properties, context.Options.AuthenticationScheme); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using Microsoft.Extensions.Internal; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace AspNet.Security.OAuth.HealthGraph { | ||
/// <summary> | ||
/// Contains static methods that allow to extract user's information from a <see cref="JObject"/> | ||
/// instance retrieved from HealthGraph after a successful authentication process. | ||
/// </summary> | ||
public static class HealthGraphAuthenticationHelper { | ||
/// <summary> | ||
/// Gets the identifier corresponding to the authenticated user. | ||
/// </summary> | ||
public static string GetIdentifier([NotNull] JObject user) => user.Value<string>("userID"); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using Microsoft.AspNet.Authentication; | ||
using Microsoft.AspNet.Authentication.OAuth; | ||
using Microsoft.AspNet.Builder; | ||
using Microsoft.AspNet.DataProtection; | ||
using Microsoft.Extensions.Internal; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.OptionsModel; | ||
using Microsoft.Extensions.WebEncoders; | ||
|
||
namespace AspNet.Security.OAuth.HealthGraph { | ||
public class HealthGraphAuthenticationMiddleware : OAuthMiddleware<HealthGraphAuthenticationOptions> { | ||
public HealthGraphAuthenticationMiddleware( | ||
[NotNull] RequestDelegate next, | ||
[NotNull] HealthGraphAuthenticationOptions options, | ||
[NotNull] IDataProtectionProvider dataProtectionProvider, | ||
[NotNull] ILoggerFactory loggerFactory, | ||
[NotNull] IUrlEncoder encoder, | ||
[NotNull] IOptions<SharedAuthenticationOptions> externalOptions) | ||
: base(next, dataProtectionProvider, loggerFactory, encoder, externalOptions, options) { | ||
} | ||
|
||
protected override AuthenticationHandler<HealthGraphAuthenticationOptions> CreateHandler() { | ||
return new HealthGraphAuthenticationHandler(Backchannel); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/AspNet.Security.OAuth.HealthGraph/HealthGraphAuthenticationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using Microsoft.AspNet.Authentication.OAuth; | ||
using Microsoft.AspNet.Http; | ||
|
||
namespace AspNet.Security.OAuth.HealthGraph { | ||
/// <summary> | ||
/// Defines a set of options used by <see cref="HealthGraphAuthenticationHandler"/>. | ||
/// </summary> | ||
public class HealthGraphAuthenticationOptions : OAuthOptions { | ||
public HealthGraphAuthenticationOptions() { | ||
AuthenticationScheme = HealthGraphAuthenticationDefaults.AuthenticationScheme; | ||
DisplayName = HealthGraphAuthenticationDefaults.DisplayName; | ||
ClaimsIssuer = HealthGraphAuthenticationDefaults.Issuer; | ||
|
||
CallbackPath = new PathString(HealthGraphAuthenticationDefaults.CallbackPath); | ||
|
||
AuthorizationEndpoint = HealthGraphAuthenticationDefaults.AuthorizationEndpoint; | ||
TokenEndpoint = HealthGraphAuthenticationDefaults.TokenEndpoint; | ||
UserInformationEndpoint = HealthGraphAuthenticationDefaults.UserInformationEndpoint; | ||
|
||
SaveTokensAsClaims = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"description": "ASP.NET 5 security middleware enabling HealthGraph authentication.", | ||
"authors": [ "Adam Reisinger" ], | ||
"owners": [ "Kévin Chalet", "Jerrie Pelser" ], | ||
|
||
"projectUrl": "https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers", | ||
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html", | ||
|
||
"dependencies": { | ||
"AspNet.Security.OAuth.Extensions": { | ||
"type": "build", | ||
"version": "1.0.0-*" | ||
}, | ||
|
||
"Microsoft.AspNet.Authentication.OAuth": "1.0.0-*", | ||
|
||
"Microsoft.Extensions.NotNullAttribute.Sources": { | ||
"type": "build", | ||
"version": "1.0.0-*" | ||
} | ||
}, | ||
|
||
"frameworks": { | ||
"dnx451": { }, | ||
|
||
"dnxcore50": { | ||
"dependencies": { | ||
"System.Security.Claims": "4.0.1-*" | ||
} | ||
} | ||
} | ||
} |