Skip to content

Commit 0c6cd1e

Browse files
Merge pull request #5 from andresmoschini/migrate-to-core-v1
Migrate to ASP.NET Core v1
2 parents a12da2a + a3273a6 commit 0c6cd1e

13 files changed

+189
-125
lines changed

MakingSense.AspNet.Authentication.SimpleToken.sln renamed to MakingSense.AspNetCore.Authentication.SimpleToken.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.23107.0
4+
VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1A4906E1-3779-42B3-BE5D-2079B4D0C0C9}"
77
EndProject
@@ -17,7 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1717
README.md = README.md
1818
EndProjectSection
1919
EndProject
20-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MakingSense.AspNet.Authentication.SimpleToken", "src\MakingSense.AspNet.Authentication.SimpleToken\MakingSense.AspNet.Authentication.SimpleToken.xproj", "{209CD8C9-0545-4A2E-826D-5B83044261AF}"
20+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MakingSense.AspNetCore.Authentication.SimpleToken", "src\MakingSense.AspNetCore.Authentication.SimpleToken\MakingSense.AspNetCore.Authentication.SimpleToken.xproj", "{209CD8C9-0545-4A2E-826D-5B83044261AF}"
2121
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution

appveyor.yml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
nuget:
2-
account_feed: true
3-
4-
version: 0.1.0-rc1-final-{build}
5-
6-
# init:
7-
# - git config --global core.autocrlf true
1+
os: Visual Studio 2015
82

9-
#install:
10-
# - cmd: nuget sources add -Name api.nuget.org -Source https://api.nuget.org/v3/index.json
11-
# - cmd: nuget sources add -Name aspnetrelease -Source https://www.myget.org/F/aspnetrelease/api/v2
3+
version: 1.0.2-alpha-{build}
124

13-
#environment:
14-
# DNX_BUILD_VERSION: rc1-final
15-
# DNX_ASSEMBLY_FILE_VERSION: 2.0.0
5+
nuget:
6+
account_feed: true
7+
project_feed: true
168

179
before_build:
18-
- ps: dnvm install 1.0.0-rc1-final
19-
- ps: dnvm upgrade
20-
- ps: dnvm update-self
21-
- ps: dnvm list
22-
- ps: dnvm use 1.0.0-rc1-final
23-
- ps: dnu restore
24-
- cmd: set DNX_BUILD_VERSION=%APPVEYOR_BUILD_NUMBER%
10+
- cmd: set DOTNET_ASSEMBLY_FILE_VERSION=%APPVEYOR_BUILD_NUMBER%
11+
- cmd: set DOTNET_BUILD_VERSION=%APPVEYOR_BUILD_NUMBER%
12+
- ps: dotnet restore
2513

2614
artifacts:
2715
- path: '**\*.nupkg'
2816

17+
build_script:
18+
- cmd: dotnet build src/MakingSense.AspNetCore.Authentication.SimpleToken
19+
20+
after_build:
21+
- cmd: dotnet pack src/MakingSense.AspNetCore.Authentication.SimpleToken
22+
2923
notifications:
30-
- provider: Slack
31-
auth_token:
32-
secure: fOssPHTsqm6seWcnywN2TJE9gLtRdd1iXLRWWWWAhx4atR0u0ECGb/9Ij/gI/zafJI+FQvevEemi9epjeRhV/g==
33-
channel: rest-api
34-
template: "<{{buildUrl}}|Build {{projectName}} {{buildVersion}} {{status}}> Commit <{{commitUrl}}|{{commitId}}> by {{commitAuthor}} on {{commitDate}}: _{{commitMessage}}_"
24+
- provider: Slack
25+
auth_token:
26+
secure: fOssPHTsqm6seWcnywN2TJE9gLtRdd1iXLRWWWWAhx4atR0u0ECGb/9Ij/gI/zafJI+FQvevEemi9epjeRhV/g==
27+
channel: rest-api
28+
template: "<{{buildUrl}}|Build {{projectName}} {{buildVersion}} {{status}}> Commit <{{commitUrl}}|{{commitId}}> by {{commitAuthor}} on {{commitDate}}: _{{commitMessage}}_"

global.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"projects": [ "src", "test" ],
33
"sdk": {
4-
"version": "1.0.0-rc1-update1"
4+
"version": "1.0.0-preview2-003131",
5+
"runtime": "clr",
6+
"architecture": "x86"
57
}
68
}

src/MakingSense.AspNet.Authentication.SimpleToken/SimpleTokenAppBuilderExtensions.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/MakingSense.AspNet.Authentication.SimpleToken/project.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/MakingSense.AspNet.Authentication.SimpleToken/MakingSense.AspNet.Authentication.SimpleToken.xproj renamed to src/MakingSense.AspNetCore.Authentication.SimpleToken/MakingSense.AspNetCore.Authentication.SimpleToken.xproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
55
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
66
</PropertyGroup>
7-
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
88
<PropertyGroup Label="Globals">
99
<ProjectGuid>209cd8c9-0545-4a2e-826d-5b83044261af</ProjectGuid>
10-
<RootNamespace>MakingSense.AspNet.Authentication.SimpleToken</RootNamespace>
11-
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12-
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
10+
<RootNamespace>MakingSense.AspNetCore.Authentication.SimpleToken</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj\</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
1616
<SchemaVersion>2.0</SchemaVersion>
1717
</PropertyGroup>
18-
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
18+
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
1919
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:15135/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
}
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using MakingSense.AspNetCore.Authentication.SimpleToken;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Framework.Internal;
5+
using Microsoft.IdentityModel.Tokens;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace Microsoft.AspNetCore.Builder
9+
{
10+
public static class SimpleTokenAppBuilderExtensions
11+
{
12+
public static IApplicationBuilder UseSimpleTokenAuthentication(this IApplicationBuilder app)
13+
{
14+
if (app == null)
15+
{
16+
throw new ArgumentNullException(nameof(app));
17+
}
18+
19+
return UseSimpleTokenAuthentication(app, new SimpleTokenAuthenticationOptions());
20+
}
21+
22+
public static IApplicationBuilder UseSimpleTokenAuthentication(this IApplicationBuilder app, SimpleTokenAuthenticationOptions options)
23+
{
24+
if (app == null)
25+
{
26+
throw new ArgumentNullException(nameof(app));
27+
}
28+
29+
if (options == null)
30+
{
31+
throw new ArgumentNullException(nameof(options));
32+
}
33+
34+
if (string.IsNullOrEmpty(options.AuthenticationScheme))
35+
{
36+
options.AuthenticationScheme = "Bearer";
37+
}
38+
39+
if (options.SecurityTokenValidatorsFactory == null)
40+
{
41+
options.SecurityTokenValidatorsFactory = () => app.ApplicationServices.GetServices<ISecurityTokenValidator>();
42+
}
43+
44+
return app.UseMiddleware<SimpleTokenAuthenticationMiddleware>(Options.Create(options));
45+
}
46+
}
47+
}

src/MakingSense.AspNet.Authentication.SimpleToken/SimpleTokenAuthenticationHandler.cs renamed to src/MakingSense.AspNetCore.Authentication.SimpleToken/SimpleTokenAuthenticationHandler.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using System;
2-
using System.IdentityModel.Tokens;
32
using System.Text;
43
using System.Threading.Tasks;
5-
using MakingSense.AspNet.Authentication.Abstractions;
6-
using Microsoft.AspNet.Authentication;
7-
using Microsoft.AspNet.Http;
8-
using Microsoft.AspNet.Http.Authentication;
4+
using MakingSense.AspNetCore.Authentication.Abstractions;
5+
using Microsoft.AspNetCore.Authentication;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Http.Authentication;
9+
using Microsoft.IdentityModel.Tokens;
910
using Microsoft.Net.Http.Headers;
1011

11-
namespace MakingSense.AspNet.Authentication.SimpleToken
12+
namespace MakingSense.AspNetCore.Authentication.SimpleToken
1213
{
1314
public class SimpleTokenAuthenticationHandler : AuthenticationHandler<SimpleTokenAuthenticationOptions>
1415
{
@@ -20,8 +21,11 @@ public class SimpleTokenAuthenticationHandler : AuthenticationHandler<SimpleToke
2021
/// It does not search in Form-Encoded Body Parameter (http://tools.ietf.org/html/rfc6750#section-2.2).
2122
/// </remarks>
2223
/// <returns>
23-
/// Returns Token if found, null otherwise
24+
/// Returns Token if found, null otherwise.
2425
/// </returns>
26+
/// <exception>
27+
/// Throws AuthenticationException when Authentication header is found, but id does not contains valid data format.
28+
/// </exception>
2529
public static string ExtractToken(HttpRequest request)
2630
{
2731
var authorizationHeader = (string)request.Headers[HeaderNames.Authorization];
@@ -54,7 +58,6 @@ public static string ExtractToken(HttpRequest request)
5458
throw new AuthenticationException("Authorization header exists but does not contains valid information.");
5559
}
5660

57-
// Search in URI Query Parameter (http://tools.ietf.org/html/rfc6750#section-2.3)
5861
var tokenFromQuery = (string)request.Query["access_token"] ?? request.Query["api_key"];
5962
if (tokenFromQuery != null)
6063
{
@@ -72,16 +75,12 @@ public static string ExtractToken(HttpRequest request)
7275
/// <returns></returns>
7376
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7477
{
75-
// Ugly patch to make this method should to be async in order to allow result caching by caller
76-
await DoneTask;
77-
78-
string token = ExtractToken(Request);
78+
var token = ExtractToken(Request);
7979

8080
// If no token found, no further work possible
8181
if (string.IsNullOrEmpty(token))
8282
{
83-
// Not so nice, but AuthenticateResult.Fail has the same behavior
84-
return null;
83+
return AuthenticateResult.Skip();
8584
}
8685

8786
var validationParameters = Options.TokenValidationParameters.Clone();
@@ -98,6 +97,9 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
9897
}
9998
}
10099

100+
// Ugly patch to make this method should to be async in order to allow result caching by caller
101+
await DoneTask;
102+
101103
// Not so nice, but AuthenticateResult.Fail does not allow us to show the error
102104
throw new AuthenticationException("Authorization token has been detected but it cannot be read.");
103105
}

0 commit comments

Comments
 (0)