forked from dsuryd/dotNetify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed basic JWT authentication middleware and Authorize filter.
dicky
authored and
dicky
committed
Jul 29, 2017
1 parent
bc72916
commit 5a5c41c
Showing
17 changed files
with
290 additions
and
154 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
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
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,47 @@ | ||
/* | ||
Copyright 2017 Dicky Suryadi | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using DotNetify; | ||
using DotNetify.Security; | ||
using System; | ||
using System.Security.Claims; | ||
|
||
namespace DotNetify.Security | ||
{ | ||
/// <summary> | ||
/// View model filter to perform authorization check. | ||
/// </summary> | ||
public class AuthorizeFilter : IVMFilter<AuthorizeAttribute> | ||
{ | ||
public void Invoke(AuthorizeAttribute auth, VMContext context) | ||
{ | ||
var principal = context.HubContext.Principal; | ||
|
||
bool authd = principal?.Identity?.IsAuthenticated == true; | ||
if (authd) | ||
{ | ||
if (!string.IsNullOrEmpty(auth.Role)) | ||
authd &= principal.IsInRole(auth.Role); | ||
|
||
if (!string.IsNullOrEmpty(auth.ClaimType)) | ||
authd &= principal is ClaimsPrincipal ? (principal as ClaimsPrincipal).HasClaim(auth.ClaimType, auth.ClaimValue) : false; | ||
} | ||
|
||
if (!authd) | ||
throw new UnauthorizedAccessException(); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
DotNetifyLib.SignalR/Security/JwtBearerAuthenticationMiddleware.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,72 @@ | ||
/* | ||
Copyright 2017 Dicky Suryadi | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace DotNetify.Security | ||
{ | ||
/// <summary> | ||
/// Middleware to handle JWT bearer token authentication. | ||
/// </summary> | ||
public class JwtBearerAuthenticationMiddleware : IMiddleware | ||
{ | ||
private class HeaderData | ||
{ | ||
public string Authorization { get; set; } | ||
} | ||
|
||
private readonly TokenValidationParameters _tokenValidationParameters; | ||
|
||
public JwtBearerAuthenticationMiddleware(TokenValidationParameters tokenValidationParameters) | ||
{ | ||
_tokenValidationParameters = tokenValidationParameters; | ||
} | ||
|
||
public void Invoke(DotNetifyHubContext hubContext) | ||
{ | ||
try | ||
{ | ||
var headers = ParseHeaders<HeaderData>(hubContext); | ||
if (headers?.Authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase) == true) | ||
{ | ||
var token = headers.Authorization.Substring("Bearer ".Length).Trim(); | ||
hubContext.Principal = new JwtSecurityTokenHandler().ValidateToken(token, _tokenValidationParameters, out SecurityToken validatedToken); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Trace.WriteLine(ex.Message); | ||
} | ||
} | ||
|
||
private T ParseHeaders<T>(DotNetifyHubContext context) => context.Headers is JObject ? (context.Headers as JObject).ToObject<T>() : default(T); | ||
} | ||
|
||
/// <summary> | ||
/// Method extension to specify parameter type for the middleware. | ||
/// </summary> | ||
public static class JwtBearerAuthenticationMiddlewareExtensions | ||
{ | ||
public static void UseJwtBearerAuthentication(this IDotNetifyConfiguration config, TokenValidationParameters tokenValidationParameters) | ||
{ | ||
config.UseMiddleware<JwtBearerAuthenticationMiddleware>(tokenValidationParameters); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using DotNetify; | ||
using Newtonsoft.Json; | ||
using System.Diagnostics; | ||
|
||
namespace WebApplication.Core.React | ||
{ | ||
public class LogRequestMiddleware : IMiddleware | ||
{ | ||
public void Invoke(DotNetifyHubContext hubContext) | ||
{ | ||
Trace.WriteLine($"{hubContext.CallType} {hubContext.VMId}, {JsonConvert.SerializeObject(hubContext.Data)}"); | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.