@@ -26,52 +26,70 @@ public static class ScriptJwtBearerExtensions
2626
2727 public static AuthenticationBuilder AddScriptJwtBearer ( this AuthenticationBuilder builder )
2828 => builder . AddJwtBearer ( o =>
29+ {
30+ o . Events = new JwtBearerEvents ( )
31+ {
32+ OnMessageReceived = c =>
33+ {
34+ // By default, tokens are passed via the standard Authorization Bearer header. However we also support
35+ // passing tokens via the x-ms-site-token header.
36+ if ( c . Request . Headers . TryGetValue ( ScriptConstants . SiteTokenHeaderName , out StringValues values ) )
37+ {
38+ // the token we set here will be the one used - Authorization header won't be checked.
39+ c . Token = values . FirstOrDefault ( ) ;
40+ }
41+
42+ // Temporary: Tactical fix to address specialization issues. This should likely be moved to a token validator
43+ // TODO: DI (FACAVAL) This will be fixed once the permanent fix is in place
44+ if ( _specialized == 0 && ! SystemEnvironment . Instance . IsPlaceholderModeEnabled ( ) && Interlocked . CompareExchange ( ref _specialized , 1 , 0 ) == 0 )
45+ {
46+ o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
47+ }
48+
49+ return Task . CompletedTask ;
50+ } ,
51+ OnTokenValidated = c =>
52+ {
53+ c . Principal . AddIdentity ( new ClaimsIdentity ( new Claim [ ]
2954 {
30- o . Events = new JwtBearerEvents ( )
31- {
32- OnMessageReceived = c =>
33- {
34- // By default, tokens are passed via the standard Authorization Bearer header. However we also support
35- // passing tokens via the x-ms-site-token header.
36- if ( c . Request . Headers . TryGetValue ( ScriptConstants . SiteTokenHeaderName , out StringValues values ) )
37- {
38- // the token we set here will be the one used - Authorization header won't be checked.
39- c . Token = values . FirstOrDefault ( ) ;
40- }
55+ new Claim ( SecurityConstants . AuthLevelClaimType , AuthorizationLevel . Admin . ToString ( ) )
56+ } ) ) ;
4157
42- // Temporary: Tactical fix to address specialization issues. This should likely be moved to a token validator
43- // TODO: DI (FACAVAL) This will be fixed once the permanent fix is in place
44- if ( _specialized == 0 && ! SystemEnvironment . Instance . IsPlaceholderModeEnabled ( ) && Interlocked . CompareExchange ( ref _specialized , 1 , 0 ) == 0 )
45- {
46- o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
47- }
58+ c . Success ( ) ;
4859
49- return Task . CompletedTask ;
50- } ,
51- OnTokenValidated = c =>
52- {
53- c . Principal . AddIdentity ( new ClaimsIdentity ( new Claim [ ]
54- {
55- new Claim ( SecurityConstants . AuthLevelClaimType , AuthorizationLevel . Admin . ToString ( ) )
56- } ) ) ;
60+ return Task . CompletedTask ;
61+ }
62+ } ;
5763
58- c . Success ( ) ;
64+ o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
5965
60- return Task . CompletedTask ;
61- }
62- } ;
66+ // TODO: DI (FACAVAL) Remove this once the work above is completed.
67+ if ( ! SystemEnvironment . Instance . IsPlaceholderModeEnabled ( ) )
68+ {
69+ // We're not in standby mode, so flag as specialized
70+ _specialized = 1 ;
71+ }
72+ } ) ;
6373
64- o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
74+ private static string [ ] GetValidAudiences ( )
75+ {
76+ if ( SystemEnvironment . Instance . IsPlaceholderModeEnabled ( ) &&
77+ SystemEnvironment . Instance . IsLinuxConsumptionOnLegion ( ) )
78+ {
79+ return new string [ ]
80+ {
81+ ScriptSettingsManager . Instance . GetSetting ( WebsitePodName )
82+ } ;
83+ }
6584
66- // TODO: DI (FACAVAL) Remove this once the work above is completed.
67- if ( ! SystemEnvironment . Instance . IsPlaceholderModeEnabled ( ) )
68- {
69- // We're not in standby mode, so flag as specialized
70- _specialized = 1 ;
71- }
72- } ) ;
85+ return new string [ ]
86+ {
87+ string . Format ( SiteAzureFunctionsUriFormat , ScriptSettingsManager . Instance . GetSetting ( AzureWebsiteName ) ) ,
88+ string . Format ( SiteUriFormat , ScriptSettingsManager . Instance . GetSetting ( AzureWebsiteName ) )
89+ } ;
90+ }
7391
74- private static TokenValidationParameters CreateTokenValidationParameters ( )
92+ public static TokenValidationParameters CreateTokenValidationParameters ( )
7593 {
7694 var signingKeys = SecretsUtility . GetTokenIssuerSigningKeys ( ) ;
7795 var result = new TokenValidationParameters ( ) ;
@@ -80,11 +98,7 @@ private static TokenValidationParameters CreateTokenValidationParameters()
8098 result . IssuerSigningKeys = signingKeys ;
8199 result . ValidateAudience = true ;
82100 result . ValidateIssuer = true ;
83- result . ValidAudiences = new string [ ]
84- {
85- string . Format ( SiteAzureFunctionsUriFormat , ScriptSettingsManager . Instance . GetSetting ( AzureWebsiteName ) ) ,
86- string . Format ( SiteUriFormat , ScriptSettingsManager . Instance . GetSetting ( AzureWebsiteName ) )
87- } ;
101+ result . ValidAudiences = GetValidAudiences ( ) ;
88102 result . ValidIssuers = new string [ ]
89103 {
90104 AppServiceCoreUri ,
0 commit comments