Skip to content

Commit 4d5a766

Browse files
hotchkjkevinchalet
authored andcommitted
Add an Autodesk provider
1 parent 9445803 commit 4d5a766

12 files changed

+418
-3
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.Untap
8585
EndProject
8686
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.MailChimp", "src\AspNet.Security.OAuth.MailChimp\AspNet.Security.OAuth.MailChimp.xproj", "{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}"
8787
EndProject
88+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.Autodesk", "src\AspNet.Security.OAuth.Autodesk\AspNet.Security.OAuth.Autodesk.xproj", "{92406BD8-0270-4BF9-B860-D1AE6508DF8C}"
89+
EndProject
8890
Global
8991
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9092
Debug|Any CPU = Debug|Any CPU
@@ -247,6 +249,10 @@ Global
247249
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
248250
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
249251
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Release|Any CPU.Build.0 = Release|Any CPU
252+
{92406BD8-0270-4BF9-B860-D1AE6508DF8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
253+
{92406BD8-0270-4BF9-B860-D1AE6508DF8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
254+
{92406BD8-0270-4BF9-B860-D1AE6508DF8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
255+
{92406BD8-0270-4BF9-B860-D1AE6508DF8C}.Release|Any CPU.Build.0 = Release|Any CPU
250256
EndGlobalSection
251257
GlobalSection(SolutionProperties) = preSolution
252258
HideSolutionNode = FALSE
@@ -291,5 +297,6 @@ Global
291297
{F276EB53-7758-4CEE-8013-61BA537B94C6} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
292298
{C17F7C35-4CAD-408F-9031-44EF1A3CC379} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
293299
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
300+
{92406BD8-0270-4BF9-B860-D1AE6508DF8C} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
294301
EndGlobalSection
295302
EndGlobal

samples/Mvc.Client/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"AspNet.Security.OAuth.ArcGIS": { "target": "project" },
1919
"AspNet.Security.OAuth.Asana": { "target": "project" },
20+
"AspNet.Security.OAuth.Autodesk": { "target": "project" },
2021
"AspNet.Security.OAuth.Automatic": { "target": "project" },
2122
"AspNet.Security.OAuth.Beam": { "target": "project" },
2223
"AspNet.Security.OAuth.BattleNet": { "target": "project" },
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>92406bd8-0270-4bf9-b860-d1ae6508df8c</ProjectGuid>
10+
<RootNamespace>AspNet.Security.OAuth.Autodesk</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
</PropertyGroup>
17+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
18+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
namespace AspNet.Security.OAuth.Autodesk
8+
{
9+
/// <summary>
10+
/// Constants used by the Autodesk authentication middleware.
11+
/// </summary>
12+
public static class AutodeskAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
/// <summary>
17+
/// Name of the claim containing a boolean value indicating
18+
/// whether the user's email has been verified.
19+
/// </summary>
20+
public const string EmailVerified = "urn:autodesk:emailverified";
21+
22+
/// <summary>
23+
/// Name of the claim containing a boolean value indicating
24+
/// whether the user has enabled two factor authentication.
25+
/// </summary>
26+
public const string TwoFactorEnabled = "urn:autodesk:twofactorenabled";
27+
}
28+
29+
public static class Scopes
30+
{
31+
/// <summary>
32+
/// Name of the OAuth2 scope used for reading Autodesk user profile.
33+
/// </summary>
34+
public const string UserProfileRead = "user-profile:read";
35+
}
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using Microsoft.AspNetCore.Builder;
8+
9+
namespace AspNet.Security.OAuth.Autodesk
10+
{
11+
/// <summary>
12+
/// Default values used by the Autodesk authentication middleware.
13+
/// </summary>
14+
public static class AutodeskAuthenticationDefaults
15+
{
16+
/// <summary>
17+
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>.
18+
/// </summary>
19+
public const string AuthenticationScheme = "Autodesk";
20+
21+
/// <summary>
22+
/// Default value for <see cref="RemoteAuthenticationOptions.DisplayName"/>.
23+
/// </summary>
24+
public const string DisplayName = "Autodesk";
25+
26+
/// <summary>
27+
/// Default value for <see cref="AuthenticationOptions.ClaimsIssuer"/>.
28+
/// </summary>
29+
public const string Issuer = "Autodesk";
30+
31+
/// <summary>
32+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
33+
/// </summary>
34+
public const string CallbackPath = "/signin-autodesk";
35+
36+
/// <summary>
37+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
38+
/// </summary>
39+
public const string AuthorizationEndpoint = "https://developer.api.autodesk.com/authentication/v1/authorize";
40+
41+
/// <summary>
42+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
43+
/// </summary>
44+
public const string TokenEndpoint = "https://developer.api.autodesk.com/authentication/v1/gettoken";
45+
46+
/// <summary>
47+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
48+
/// </summary>
49+
public const string UserInformationEndpoint = "https://developer.api.autodesk.com/userprofile/v1/users/@me";
50+
}
51+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System;
8+
using AspNet.Security.OAuth.Autodesk;
9+
using JetBrains.Annotations;
10+
using Microsoft.Extensions.Options;
11+
12+
namespace Microsoft.AspNetCore.Builder
13+
{
14+
/// <summary>
15+
/// Extension methods to add Autodesk authentication capabilities to an HTTP application pipeline.
16+
/// </summary>
17+
public static class AutodeskAuthenticationExtensions
18+
{
19+
/// <summary>
20+
/// Adds the <see cref="AutodeskAuthenticationMiddleware"/> middleware to the specified
21+
/// <see cref="IApplicationBuilder"/>, which enables Autodesk authentication capabilities.
22+
/// </summary>
23+
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
24+
/// <param name="options">A <see cref="AutodeskAuthenticationOptions"/> that specifies options for the middleware.</param>
25+
/// <returns>A reference to this instance after the operation has completed.</returns>
26+
public static IApplicationBuilder UseAutodeskAuthentication(
27+
[NotNull] this IApplicationBuilder app,
28+
[NotNull] AutodeskAuthenticationOptions options)
29+
{
30+
if (app == null)
31+
{
32+
throw new ArgumentNullException(nameof(app));
33+
}
34+
35+
if (options == null)
36+
{
37+
throw new ArgumentNullException(nameof(options));
38+
}
39+
40+
return app.UseMiddleware<AutodeskAuthenticationMiddleware>(Options.Create(options));
41+
}
42+
43+
/// <summary>
44+
/// Adds the <see cref="AutodeskAuthenticationMiddleware"/> middleware to the specified
45+
/// <see cref="IApplicationBuilder"/>, which enables Autodesk authentication capabilities.
46+
/// </summary>
47+
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
48+
/// <param name="configuration">An action delegate to configure the provided <see cref="AutodeskAuthenticationOptions"/>.</param>
49+
/// <returns>A reference to this instance after the operation has completed.</returns>
50+
public static IApplicationBuilder UseAutodeskAuthentication(
51+
[NotNull] this IApplicationBuilder app,
52+
[NotNull] Action<AutodeskAuthenticationOptions> configuration)
53+
{
54+
if (app == null)
55+
{
56+
throw new ArgumentNullException(nameof(app));
57+
}
58+
59+
if (configuration == null)
60+
{
61+
throw new ArgumentNullException(nameof(configuration));
62+
}
63+
64+
var options = new AutodeskAuthenticationOptions();
65+
configuration(options);
66+
67+
return app.UseMiddleware<AutodeskAuthenticationMiddleware>(Options.Create(options));
68+
}
69+
}
70+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Net.Http;
8+
using System.Net.Http.Headers;
9+
using System.Security.Claims;
10+
using System.Threading.Tasks;
11+
using AspNet.Security.OAuth.Extensions;
12+
using JetBrains.Annotations;
13+
using Microsoft.AspNetCore.Authentication;
14+
using Microsoft.AspNetCore.Authentication.OAuth;
15+
using Microsoft.AspNetCore.Http.Authentication;
16+
using Newtonsoft.Json.Linq;
17+
18+
namespace AspNet.Security.OAuth.Autodesk
19+
{
20+
public class AutodeskAuthenticationHandler : OAuthHandler<AutodeskAuthenticationOptions>
21+
{
22+
public AutodeskAuthenticationHandler([NotNull] HttpClient client)
23+
: base(client)
24+
{
25+
}
26+
27+
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
28+
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
29+
{
30+
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
31+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
32+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
33+
34+
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
35+
response.EnsureSuccessStatusCode();
36+
37+
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
38+
39+
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, AutodeskAuthenticationHelper.GetUserId(payload), Options.ClaimsIssuer)
40+
.AddOptionalClaim(ClaimTypes.Name, AutodeskAuthenticationHelper.GetUserName(payload), Options.ClaimsIssuer)
41+
.AddOptionalClaim(ClaimTypes.GivenName, AutodeskAuthenticationHelper.GetFirstName(payload), Options.ClaimsIssuer)
42+
.AddOptionalClaim(ClaimTypes.Surname, AutodeskAuthenticationHelper.GetLastName(payload), Options.ClaimsIssuer)
43+
.AddOptionalClaim(ClaimTypes.Email, AutodeskAuthenticationHelper.GetEmailAddress(payload), ClaimValueTypes.Email, Options.ClaimsIssuer)
44+
.AddOptionalClaim(AutodeskAuthenticationConstants.Claims.EmailVerified, AutodeskAuthenticationHelper.GetEmailVerified(payload), ClaimValueTypes.Boolean, Options.ClaimsIssuer)
45+
.AddOptionalClaim(AutodeskAuthenticationConstants.Claims.TwoFactorEnabled, AutodeskAuthenticationHelper.GetTwoFactorEnabled(payload), ClaimValueTypes.Boolean, Options.ClaimsIssuer);
46+
47+
var principal = new ClaimsPrincipal(identity);
48+
var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme);
49+
50+
var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, payload);
51+
await Options.Events.CreatingTicket(context);
52+
53+
return context.Ticket;
54+
}
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using Newtonsoft.Json.Linq;
8+
9+
namespace AspNet.Security.OAuth.Autodesk
10+
{
11+
/// <summary>
12+
/// Contains static methods that allow to extract user's information from a <see cref="JObject"/>
13+
/// instance retrieved from Autodesk after a successful authentication process.
14+
/// </summary>
15+
public static class AutodeskAuthenticationHelper
16+
{
17+
/// <summary>
18+
/// Gets the unique user identifier corresponding to the authenticated user.
19+
/// </summary>
20+
public static string GetUserId(JObject user) => user.Value<string>("userId");
21+
22+
/// <summary>
23+
/// Gets the username corresponding to the authenticated user (e.g. John Doe or john.doe)
24+
/// </summary>
25+
public static string GetUserName(JObject user) => user.Value<string>("userName");
26+
27+
/// <summary>
28+
/// Gets the email address associated with the authenticated user (e.g. [email protected])
29+
/// </summary>
30+
public static string GetEmailAddress(JObject user) => user.Value<string>("emailId");
31+
32+
/// <summary>
33+
/// Gets the first name of the authenticated user (e.g. John)
34+
/// </summary>
35+
public static string GetFirstName(JObject user) => user.Value<string>("firstName");
36+
37+
/// <summary>
38+
/// Gets the last name of the authenticated user (e.g. Doe)
39+
/// </summary>
40+
public static string GetLastName(JObject user) => user.Value<string>("lastName");
41+
42+
/// <summary>
43+
/// Gets a boolean value indicating whether or not the user's email address has been verified.
44+
/// </summary>
45+
public static string GetEmailVerified(JObject user) => user.Value<string>("emailVerified");
46+
47+
/// <summary>
48+
/// Gets a boolean value indicating whether or not the user has enabled two-factor authentication.
49+
/// </summary>
50+
public static string GetTwoFactorEnabled(JObject user) => user.Value<string>("2FaEnabled");
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Text.Encodings.Web;
8+
using JetBrains.Annotations;
9+
using Microsoft.AspNetCore.Authentication;
10+
using Microsoft.AspNetCore.Authentication.OAuth;
11+
using Microsoft.AspNetCore.DataProtection;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Options;
15+
16+
namespace AspNet.Security.OAuth.Autodesk
17+
{
18+
public class AutodeskAuthenticationMiddleware : OAuthMiddleware<AutodeskAuthenticationOptions>
19+
{
20+
public AutodeskAuthenticationMiddleware(
21+
[NotNull] RequestDelegate next,
22+
[NotNull] IDataProtectionProvider dataProtectionProvider,
23+
[NotNull] ILoggerFactory loggerFactory,
24+
[NotNull] UrlEncoder encoder,
25+
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
26+
[NotNull] IOptions<AutodeskAuthenticationOptions> options)
27+
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
28+
{
29+
}
30+
31+
protected override AuthenticationHandler<AutodeskAuthenticationOptions> CreateHandler()
32+
{
33+
return new AutodeskAuthenticationHandler(Backchannel);
34+
}
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Http;
9+
10+
namespace AspNet.Security.OAuth.Autodesk
11+
{
12+
/// <summary>
13+
/// Defines a set of options used by <see cref="AutodeskAuthenticationHandler"/>.
14+
/// </summary>
15+
public class AutodeskAuthenticationOptions : OAuthOptions
16+
{
17+
public AutodeskAuthenticationOptions()
18+
{
19+
AuthenticationScheme = AutodeskAuthenticationDefaults.AuthenticationScheme;
20+
DisplayName = AutodeskAuthenticationDefaults.DisplayName;
21+
ClaimsIssuer = AutodeskAuthenticationDefaults.Issuer;
22+
23+
CallbackPath = new PathString(AutodeskAuthenticationDefaults.CallbackPath);
24+
25+
AuthorizationEndpoint = AutodeskAuthenticationDefaults.AuthorizationEndpoint;
26+
TokenEndpoint = AutodeskAuthenticationDefaults.TokenEndpoint;
27+
UserInformationEndpoint = AutodeskAuthenticationDefaults.UserInformationEndpoint;
28+
29+
Scope.Add(AutodeskAuthenticationConstants.Scopes.UserProfileRead);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)