77using System . Net ;
88using System . Net . Http ;
99using System . Net . Http . Headers ;
10+ using System . Text ;
1011using System . Threading ;
1112using System . Threading . Tasks ;
1213using System . Web . Http . Controllers ;
1516using Microsoft . Azure . WebJobs . Extensions . Http ;
1617using Microsoft . Azure . WebJobs . Script . WebHost ;
1718using Microsoft . Azure . WebJobs . Script . WebHost . Filters ;
19+ using Microsoft . Azure . WebJobs . Script . WebHost . Security ;
1820using Microsoft . IdentityModel . Tokens ;
1921using Xunit ;
2022using static Microsoft . Azure . WebJobs . Script . Config . ScriptSettingsManager ;
@@ -26,6 +28,7 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Filters
2628 public class JwtAuthenticationAttributeTests : IDisposable
2729 {
2830 private const string TestKeyValue = "0F75CA46E7EBDD39E4CA6B074D1F9A5972B849A55F91A248" ;
31+ private const string PlatformDefaultKeyValue = "B77F872A341F8970D50F093E1FA924777A5A61CCABC63C2A" ;
2932 private const string TestAppName = "testsite" ;
3033 private TestScopedEnvironmentVariable _testEnv ;
3134
@@ -34,6 +37,7 @@ public JwtAuthenticationAttributeTests()
3437 var values = new Dictionary < string , string >
3538 {
3639 { "AzureWebEncryptionKey" , TestKeyValue } ,
40+ { EnvironmentSettingNames . WebsiteAuthEncryptionKey , PlatformDefaultKeyValue } ,
3741 { AzureWebsiteName , TestAppName }
3842 } ;
3943 _testEnv = new TestScopedEnvironmentVariable ( values ) ;
@@ -56,12 +60,27 @@ public async Task AuthenticateAsync_WithValidToken_SetsAdminAuthorizationLevel(s
5660 {
5761 issuer = issuer ?? string . Format ( ScmSiteUriFormat , Instance . GetSetting ( AzureWebsiteName ) ) ;
5862 audience = audience ?? string . Format ( SiteAzureFunctionsUriFormat , Instance . GetSetting ( AzureWebsiteName ) ) ;
59-
60- string token = JwtGenerator . GenerateToken ( issuer , audience , expires : DateTime . UtcNow . AddMinutes ( 10 ) ) ;
63+ string token = JwtTokenHelper . CreateToken ( DateTime . UtcNow . AddMinutes ( 10 ) , audience , issuer ) ;
6164
6265 await AuthenticateAsync ( token , headerName , AuthorizationLevel . Admin ) ;
6366 }
6467
68+ [ Theory ]
69+ [ InlineData ( "AzureWebEncryptionKey" , true ) ]
70+ [ InlineData ( "AzureWebEncryptionKey" , false ) ]
71+ [ InlineData ( EnvironmentSettingNames . WebsiteAuthEncryptionKey , true ) ]
72+ public async Task AuthenticateAsync_WithValidToken_WithSupportedKeyConfigurations_SetsAdminAuthorizationLevel ( string keyName , bool hexEncoding )
73+ {
74+ string issuer = "https://testsite.azurewebsites.net" ;
75+ string audience = "https://testsite.azurewebsites.net" ;
76+
77+ string keyValue = Environment . GetEnvironmentVariable ( keyName ) ;
78+ byte [ ] key = hexEncoding ? keyValue . ToKeyBytes ( ) : Encoding . UTF8 . GetBytes ( keyValue ) ;
79+ string token = JwtTokenHelper . CreateToken ( DateTime . UtcNow . AddMinutes ( 10 ) , audience , issuer , key ) ;
80+
81+ await AuthenticateAsync ( token , ScriptConstants . SiteTokenHeaderName , AuthorizationLevel . Admin ) ;
82+ }
83+
6584 [ Theory ]
6685 [ InlineData ( - 10 , null , null ) ] // Our default clock skew setting is 5 minutes
6786 [ InlineData ( 10 , "invalid" , null ) ]
0 commit comments