77
88namespace NATS . Jwt . Tests ;
99
10- public class DecodeClaimsTests ( ITestOutputHelper output )
10+ public class ValidationTests ( ITestOutputHelper output )
1111{
1212 [ Fact ]
1313 public void Invalid_jwt_when_its_too_short ( )
@@ -95,6 +95,43 @@ public void Verify_version_1()
9595 Assert . Equal ( 1 , claims . AuthorizationRequest . Version ) ;
9696 }
9797
98+ [ Fact ]
99+ public void Verify_version_1_from_type ( )
100+ {
101+ var kp = KeyPair . CreatePair ( PrefixByte . Server ) ;
102+ var part1 = EncodingUtils . ToBase64UrlEncoded ( Encoding . ASCII . GetBytes ( """{"typ":"JWT","alg":"ed25519-nkey"}""" ) ) ;
103+ var part2 = EncodingUtils . ToBase64UrlEncoded ( Encoding . ASCII . GetBytes ( $$$ """ {"type":"authorization_request","iss":"{{{ kp . GetPublicKey ( ) }}} ","nats":{}}""" ) ) ;
104+ var sig = new byte [ 64 ] ;
105+ kp . Sign ( Encoding . ASCII . GetBytes ( part2 ) , sig ) ;
106+ var part3 = EncodingUtils . ToBase64UrlEncoded ( sig ) ;
107+ var token = $ "{ part1 } .{ part2 } .{ part3 } ";
108+ var jwt = new NatsJwt ( ) ;
109+ var claims = jwt . DecodeClaims < NatsAuthorizationRequestClaims > ( token ) ;
110+ output . WriteLine ( $ "claims:{ claims } ") ;
111+ Assert . Equal ( kp . GetPublicKey ( ) , claims . Issuer ) ;
112+ }
113+
114+ [ Theory ]
115+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","X":{}}""" , "Failed to get nats element" ) ]
116+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","nats":{"X":2}}""" , "Failed to get nats.version element" ) ]
117+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","nats":{"version":2.2}}""" , "Failed to get nats.version as integer" ) ]
118+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","nats":{"version":2, "X":"X"}}""" , "Failed to get nats.type element" ) ]
119+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","nats":{"version":2, "type":""}}""" , "Failed to get nats.type element as non-empty string" ) ]
120+ [ InlineData ( """{"type":"","iss":"@@PublicKey@@","nats":{"version":2, "type":" "}}""" , "Failed to get nats.type element as non-empty string" ) ]
121+ public void Verify_version_and_type_check ( string json , string error )
122+ {
123+ var kp = KeyPair . CreatePair ( PrefixByte . Server ) ;
124+ var part1 = EncodingUtils . ToBase64UrlEncoded ( Encoding . ASCII . GetBytes ( """{"typ":"JWT","alg":"ed25519-nkey"}""" ) ) ;
125+ var part2 = EncodingUtils . ToBase64UrlEncoded ( Encoding . ASCII . GetBytes ( json . Replace ( "@@PublicKey@@" , kp . GetPublicKey ( ) ) ) ) ;
126+ var sig = new byte [ 64 ] ;
127+ kp . Sign ( Encoding . ASCII . GetBytes ( part2 ) , sig ) ;
128+ var part3 = EncodingUtils . ToBase64UrlEncoded ( sig ) ;
129+ var token = $ "{ part1 } .{ part2 } .{ part3 } ";
130+ var jwt = new NatsJwt ( ) ;
131+ var exception = Assert . Throws < NatsJwtException > ( ( ) => jwt . DecodeClaims < NatsAuthorizationRequestClaims > ( token ) ) ;
132+ Assert . Equal ( error , exception . Message ) ;
133+ }
134+
98135 [ Fact ]
99136 public void Verify_decode_encode_subject_is_not_set ( )
100137 {
@@ -134,4 +171,61 @@ public void Verify_decode_encode_invalid_signing_key()
134171 output . WriteLine ( $ "Error: '{ exception . Message } '") ;
135172 Assert . Equal ( "Invalid signing key of 'Account': expected one of 'Server'" , exception . Message ) ;
136173 }
174+
175+ [ Theory ]
176+ [ InlineData ( "X" , "" , "Invalid JWT header: not supported type X" ) ]
177+ [ InlineData ( "JWT" , "X" , "Invalid JWT header: unexpected X algorithm" ) ]
178+ public void Header_validation ( string type , string algo , string error )
179+ {
180+ var header = new JwtHeader { Type = type , Algorithm = algo } ;
181+ var exception = Assert . Throws < NatsJwtException > ( ( ) => header . Validate ( ) ) ;
182+ Assert . Equal ( error , exception . Message ) ;
183+ }
184+
185+ [ Fact ]
186+ public void Prefix_validation ( )
187+ {
188+ /*func (a *AccountClaims) ExpectedPrefixes() []nkeys.PrefixByte {
189+ return []nkeys.PrefixByte{nkeys.PrefixByteAccount, nkeys.PrefixByteOperator}
190+ }*/
191+ Assert . Equal ( [ PrefixByte . Account , PrefixByte . Operator ] , new NatsAccountClaims ( ) . ExpectedPrefixes ( ) ) ;
192+
193+ /*func (a *ActivationClaims) ExpectedPrefixes() []nkeys.PrefixByte {
194+ return []nkeys.PrefixByte{nkeys.PrefixByteAccount, nkeys.PrefixByteOperator}
195+ }*/
196+ Assert . Equal ( [ PrefixByte . Account , PrefixByte . Operator ] , new NatsActivationClaims ( ) . ExpectedPrefixes ( ) ) ;
197+
198+ /*func (gc *GenericClaims) ExpectedPrefixes() []nkeys.PrefixByte {
199+ return nil
200+ }*/
201+ Assert . Equal ( [ ] , new NatsGenericClaims ( ) . ExpectedPrefixes ( ) ) ;
202+
203+ /*func (oc *OperatorClaims) ExpectedPrefixes() []nkeys.PrefixByte {
204+ return []nkeys.PrefixByte{nkeys.PrefixByteOperator}
205+ }*/
206+ Assert . Equal ( [ PrefixByte . Operator ] , new NatsOperatorClaims ( ) . ExpectedPrefixes ( ) ) ;
207+
208+ /*func (u *UserClaims) ExpectedPrefixes() []nkeys.PrefixByte {
209+ return []nkeys.PrefixByte{nkeys.PrefixByteAccount}
210+ }*/
211+ Assert . Equal ( [ PrefixByte . Account ] , new NatsUserClaims ( ) . ExpectedPrefixes ( ) ) ;
212+
213+ /*func (ac *AuthorizationRequestClaims) ExpectedPrefixes() []nkeys.PrefixByte {
214+ return []nkeys.PrefixByte{nkeys.PrefixByteServer}
215+ }*/
216+ Assert . Equal ( [ PrefixByte . Server ] , new NatsAuthorizationRequestClaims ( ) . ExpectedPrefixes ( ) ) ;
217+
218+ /*func (ar *AuthorizationResponseClaims) ExpectedPrefixes() []nkeys.PrefixByte {
219+ return []nkeys.PrefixByte{nkeys.PrefixByteAccount}
220+ }*/
221+ Assert . Equal ( [ PrefixByte . Account ] , new NatsAuthorizationResponseClaims ( ) . ExpectedPrefixes ( ) ) ;
222+ }
223+
224+ [ Fact ]
225+ public void Prefix_validation_non_existent ( )
226+ {
227+ var claims = new JwtClaimsData ( ) ;
228+ var exception = Assert . Throws < NatsJwtException > ( ( ) => claims . ExpectedPrefixes ( ) ) ;
229+ Assert . Equal ( "Can't find prefixes for JwtClaimsData" , exception . Message ) ;
230+ }
137231}
0 commit comments