Skip to content

Commit d231af8

Browse files
committed
Tactical fix to update JWT validation parameters after specialization
1 parent 905e90a commit d231af8

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed
Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Security.Claims;
56
using System.Text;
7+
using System.Threading;
68
using System.Threading.Tasks;
79
using Microsoft.AspNetCore.Authentication;
810
using Microsoft.AspNetCore.Authentication.JwtBearer;
911
using Microsoft.Azure.Web.DataProtection;
1012
using Microsoft.Azure.WebJobs.Extensions.Http;
1113
using Microsoft.Azure.WebJobs.Script.Config;
14+
using Microsoft.Azure.WebJobs.Script.WebHost;
1215
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authentication;
1316
using Microsoft.IdentityModel.Tokens;
1417
using static Microsoft.Azure.WebJobs.Script.EnvironmentSettingNames;
@@ -18,11 +21,23 @@ namespace Microsoft.Extensions.DependencyInjection
1821
{
1922
public static class ScriptJwtBearerExtensions
2023
{
24+
private static double _specialized = 0;
25+
2126
public static AuthenticationBuilder AddScriptJwtBearer(this AuthenticationBuilder builder)
2227
=> builder.AddJwtBearer(o =>
2328
{
2429
o.Events = new JwtBearerEvents()
2530
{
31+
OnMessageReceived = c =>
32+
{
33+
// Temporary: Tactical fix to address specialization issues. This should likely be moved to a token validator
34+
if (_specialized == 0 && !WebScriptHostManager.InStandbyMode && Interlocked.CompareExchange(ref _specialized, 1, 0) == 0)
35+
{
36+
o.TokenValidationParameters = CreateTokenValidationParameters();
37+
}
38+
39+
return Task.CompletedTask;
40+
},
2641
OnTokenValidated = c =>
2742
{
2843
c.Principal.AddIdentity(new ClaimsIdentity(new Claim[]
@@ -35,19 +50,32 @@ public static AuthenticationBuilder AddScriptJwtBearer(this AuthenticationBuilde
3550
return Task.CompletedTask;
3651
}
3752
};
38-
string defaultKey = Util.GetDefaultKeyValue();
39-
if (defaultKey != null)
53+
54+
o.TokenValidationParameters = CreateTokenValidationParameters();
55+
56+
if (!WebScriptHostManager.InStandbyMode)
4057
{
41-
// TODO: Once ScriptSettingsManager is gone, Audience and Issuer shouold be pulled from configuration.
42-
o.TokenValidationParameters = new TokenValidationParameters()
43-
{
44-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(defaultKey)),
45-
ValidateAudience = true,
46-
ValidateIssuer = true,
47-
ValidAudience = string.Format(AdminJwtValidAudienceFormat, ScriptSettingsManager.Instance.GetSetting(AzureWebsiteName)),
48-
ValidIssuer = string.Format(AdminJwtValidIssuerFormat, ScriptSettingsManager.Instance.GetSetting(AzureWebsiteName))
49-
};
58+
// We're not in standby mode, so flag as specialized
59+
_specialized = 1;
5060
}
5161
});
62+
63+
private static TokenValidationParameters CreateTokenValidationParameters()
64+
{
65+
string defaultKey = Util.GetDefaultKeyValue();
66+
67+
var result = new TokenValidationParameters();
68+
if (defaultKey != null)
69+
{
70+
// TODO: Once ScriptSettingsManager is gone, Audience and Issuer shouold be pulled from configuration.
71+
result.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(defaultKey));
72+
result.ValidateAudience = true;
73+
result.ValidateIssuer = true;
74+
result.ValidAudience = string.Format(AdminJwtValidAudienceFormat, ScriptSettingsManager.Instance.GetSetting(AzureWebsiteName));
75+
result.ValidIssuer = string.Format(AdminJwtValidIssuerFormat, ScriptSettingsManager.Instance.GetSetting(AzureWebsiteName));
76+
}
77+
78+
return result;
79+
}
5280
}
5381
}

0 commit comments

Comments
 (0)