@@ -26,52 +26,70 @@ public static class ScriptJwtBearerExtensions
26
26
27
27
public static AuthenticationBuilder AddScriptJwtBearer ( this AuthenticationBuilder builder )
28
28
=> 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 [ ]
29
54
{
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
+ } ) ) ;
41
57
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 ( ) ;
48
59
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
+ } ;
57
63
58
- c . Success ( ) ;
64
+ o . TokenValidationParameters = CreateTokenValidationParameters ( ) ;
59
65
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
+ } ) ;
63
73
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
+ }
65
84
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
+ }
73
91
74
- private static TokenValidationParameters CreateTokenValidationParameters ( )
92
+ public static TokenValidationParameters CreateTokenValidationParameters ( )
75
93
{
76
94
var signingKeys = SecretsUtility . GetTokenIssuerSigningKeys ( ) ;
77
95
var result = new TokenValidationParameters ( ) ;
@@ -80,11 +98,7 @@ private static TokenValidationParameters CreateTokenValidationParameters()
80
98
result . IssuerSigningKeys = signingKeys ;
81
99
result . ValidateAudience = true ;
82
100
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 ( ) ;
88
102
result . ValidIssuers = new string [ ]
89
103
{
90
104
AppServiceCoreUri ,
0 commit comments