-
-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathJwtAuthMessageHandler.cs
More file actions
80 lines (68 loc) · 3.26 KB
/
Copy pathJwtAuthMessageHandler.cs
File metadata and controls
80 lines (68 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace Dnn.AuthServices.Jwt.Auth
{
using System;
using System.Net.Http;
using System.Security.Principal;
using System.Threading;
using Dnn.AuthServices.Jwt.Components.Common.Controllers;
using DotNetNuke.Instrumentation;
using DotNetNuke.Web.Api.Auth;
using Microsoft.Extensions.Logging;
/// <summary>
/// This class implements Json Web Token (JWT) authentication scheme.
/// For detailed description of JWT refer to:
/// <para>- JTW standard https://tools.ietf.org/html/rfc7519. </para>
/// <para>- Introduction to JSON Web Tokens http://jwt.io/introduction/. </para>
/// </summary>
public class JwtAuthMessageHandler : AuthMessageHandlerBase
{
private readonly ILogger<JwtAuthMessageHandler> logger;
private readonly IJwtController jwtController = JwtController.Instance;
/// <summary>Initializes a new instance of the <see cref="JwtAuthMessageHandler"/> class.</summary>
/// <param name="includeByDefault">A value indicating whether this handler should be included by default on all API endpoints.</param>
/// <param name="forceSsl">A value indicating whether this handler should enforce SSL usage.</param>
public JwtAuthMessageHandler(bool includeByDefault, bool forceSsl)
: base(includeByDefault, forceSsl)
{
// Once an instance is enabled and gets registered in
// ServicesRoutingManager.RegisterAuthenticationHandlers()
// this scheme gets marked as enabled.
IsEnabled = true;
this.logger = DnnLoggingController.GetLogger<JwtAuthMessageHandler>();
}
/// <inheritdoc />
public override string AuthScheme => this.jwtController.SchemeType;
/// <inheritdoc />
public override bool BypassAntiForgeryToken => true;
/// <summary>Gets or sets a value indicating whether this handler is enabled.</summary>
internal static bool IsEnabled { get; set; }
/// <inheritdoc />
public override HttpResponseMessage OnInboundRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (this.NeedsAuthentication(request))
{
this.TryToAuthenticate(request);
}
return base.OnInboundRequest(request, cancellationToken);
}
private void TryToAuthenticate(HttpRequestMessage request)
{
try
{
var username = this.jwtController.ValidateToken(request);
if (!string.IsNullOrEmpty(username))
{
this.logger.LogTrace("Authenticated user '{userName}'", username);
SetCurrentPrincipal(new GenericPrincipal(new GenericIdentity(username, this.AuthScheme), null), request);
}
}
catch (Exception ex)
{
this.logger.LogError(ex, "Unexpected error in authenticating the user.");
}
}
}
}