Skip to content

Commit

Permalink
Add a new provider for HealthGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Res42 authored and kevinchalet committed Oct 23, 2015
1 parent 23b52e6 commit 060ef4a
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AspNet.Security.OAuth.Providers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.Asana
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.BattleNet", "src\AspNet.Security.OAuth.BattleNet\AspNet.Security.OAuth.BattleNet.xproj", "{6299EB3E-5E8C-4D54-AAEB-7B825C96411E}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.HealthGraph", "src\AspNet.Security.OAuth.HealthGraph\AspNet.Security.OAuth.HealthGraph.xproj", "{C20BE880-52CB-491C-977C-F08702376766}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -115,6 +117,10 @@ Global
{6299EB3E-5E8C-4D54-AAEB-7B825C96411E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6299EB3E-5E8C-4D54-AAEB-7B825C96411E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6299EB3E-5E8C-4D54-AAEB-7B825C96411E}.Release|Any CPU.Build.0 = Release|Any CPU
{C20BE880-52CB-491C-977C-F08702376766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C20BE880-52CB-491C-977C-F08702376766}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C20BE880-52CB-491C-977C-F08702376766}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C20BE880-52CB-491C-977C-F08702376766}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -137,5 +143,6 @@ Global
{A0F9AC43-6E42-4E92-92A3-85C2E5CC0986} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{DC58F830-EEE1-45BE-B5F4-AB2222E744A5} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{6299EB3E-5E8C-4D54-AAEB-7B825C96411E} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
{C20BE880-52CB-491C-977C-F08702376766} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions samples/Mvc.Client/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"AspNet.Security.OAuth.Dropbox": "1.0.0-*",
"AspNet.Security.OAuth.Foursquare": "1.0.0-*",
"AspNet.Security.OAuth.GitHub": "1.0.0-*",
"AspNet.Security.OAuth.HealthGraph": "1.0.0-*",
"AspNet.Security.OAuth.Imgur": "1.0.0-*",
"AspNet.Security.OAuth.LinkedIn": "1.0.0-*",
"AspNet.Security.OAuth.Onshape": "1.0.0-*",
Expand Down
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>
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";
}
}
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);
}
}
}
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);
}
}
}
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");
}
}
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);
}
}
}
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;
}
}
}
33 changes: 33 additions & 0 deletions src/AspNet.Security.OAuth.HealthGraph/project.json
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-*"
}
}
}
}

0 comments on commit 060ef4a

Please sign in to comment.