1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
+ using System ;
4
5
using System . Security . Claims ;
5
6
using System . Text ;
7
+ using System . Threading ;
6
8
using System . Threading . Tasks ;
7
9
using Microsoft . AspNetCore . Authentication ;
8
10
using Microsoft . AspNetCore . Authentication . JwtBearer ;
9
11
using Microsoft . Azure . Web . DataProtection ;
10
12
using Microsoft . Azure . WebJobs . Extensions . Http ;
11
13
using Microsoft . Azure . WebJobs . Script . Config ;
14
+ using Microsoft . Azure . WebJobs . Script . WebHost ;
12
15
using Microsoft . Azure . WebJobs . Script . WebHost . Security . Authentication ;
13
16
using Microsoft . IdentityModel . Tokens ;
14
17
using static Microsoft . Azure . WebJobs . Script . EnvironmentSettingNames ;
@@ -18,11 +21,23 @@ namespace Microsoft.Extensions.DependencyInjection
18
21
{
19
22
public static class ScriptJwtBearerExtensions
20
23
{
24
+ private static double _specialized = 0 ;
25
+
21
26
public static AuthenticationBuilder AddScriptJwtBearer ( this AuthenticationBuilder builder )
22
27
=> builder . AddJwtBearer ( o =>
23
28
{
24
29
o . Events = new JwtBearerEvents ( )
25
30
{
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
+ } ,
26
41
OnTokenValidated = c =>
27
42
{
28
43
c . Principal . AddIdentity ( new ClaimsIdentity ( new Claim [ ]
@@ -35,19 +50,32 @@ public static AuthenticationBuilder AddScriptJwtBearer(this AuthenticationBuilde
35
50
return Task . CompletedTask ;
36
51
}
37
52
} ;
38
- string defaultKey = Util . GetDefaultKeyValue ( ) ;
39
- if ( defaultKey != null )
53
+
54
+ o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
55
+
56
+ if ( ! WebScriptHostManager . InStandbyMode )
40
57
{
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 ;
50
60
}
51
61
} ) ;
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
+ }
52
80
}
53
81
}
0 commit comments