33
44using System ;
55using System . Collections . Generic ;
6+ using System . IdentityModel . Tokens . Jwt ;
7+ using System . Text ;
68using Microsoft . AspNetCore . Http ;
7- using Microsoft . Azure . WebJobs . Extensions . SignalRService ;
89using Microsoft . Extensions . Primitives ;
910using Microsoft . IdentityModel . Tokens ;
1011using Xunit ;
1112
12- namespace SignalRServiceExtension . Tests
13+ namespace Microsoft . Azure . WebJobs . Extensions . SignalRService . Tests
1314{
1415 public class DefaultSecurityTokenValidatorTests
1516 {
17+ private const string IssuerToken = "myfunctionauthtest" ;
1618 public static IEnumerable < object [ ] > TestData = new List < object [ ] >
1719 {
1820 new object [ ]
1921 {
20- "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWFhIiwiZXhwIjoxNjk5ODE5MDI1fQ.joh9CXSfRpgZhoraozdQ0Z1DxmUhlXF4ENt_1Ttz7x8" ,
22+ "Bearer " + GenerateJwtToken ( IssuerToken , "issuer" , "audience" , 10 ) ,
2123 SecurityTokenStatus . Valid
2224 } ,
2325 new object [ ]
@@ -32,18 +34,17 @@ public class DefaultSecurityTokenValidatorTests
3234 }
3335 } ;
3436
35- [ Theory ( Skip = "the valid case is failing on main" ) ]
37+ [ Theory ]
3638 [ MemberData ( nameof ( TestData ) ) ]
3739 public void ValidateSecurityTokenFacts ( string tokenString , SecurityTokenStatus expectedStatus )
3840 {
3941 var ctx = new DefaultHttpContext ( ) ;
4042 var req = ctx . Request ;
4143 req . Headers . Add ( "Authorization" , new StringValues ( tokenString ) ) ;
4244
43- var issuerToken = "bXlmdW5jdGlvbmF1dGh0ZXN0" ; // base64 encoded for "myfunctionauthtest";
4445 Action < TokenValidationParameters > configureTokenValidationParameters = parameters =>
4546 {
46- parameters . IssuerSigningKey = new SymmetricSecurityKey ( Convert . FromBase64String ( issuerToken ) ) ;
47+ parameters . IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( IssuerToken ) ) ;
4748 parameters . RequireSignedTokens = true ;
4849 parameters . ValidateAudience = false ;
4950 parameters . ValidateIssuer = false ;
@@ -56,5 +57,19 @@ public void ValidateSecurityTokenFacts(string tokenString, SecurityTokenStatus e
5657
5758 Assert . Equal ( expectedStatus , securityTokenResult . Status ) ;
5859 }
60+
61+ public static string GenerateJwtToken ( string secretKey , string issuer , string audience , int expireMinutes )
62+ {
63+ var securityKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( secretKey ) ) ;
64+ var credentials = new SigningCredentials ( securityKey , SecurityAlgorithms . HmacSha256 ) ;
65+ var token = new JwtSecurityToken (
66+ issuer : issuer ,
67+ audience : audience ,
68+ expires : DateTime . Now . AddMinutes ( expireMinutes ) ,
69+ signingCredentials : credentials
70+ ) ;
71+
72+ return new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ;
73+ }
5974 }
6075}
0 commit comments