33using SmartHealthCard . Test . Serializers ;
44using SmartHealthCard . Test . Support ;
55using SmartHealthCard . Token ;
6+ using SmartHealthCard . Token . Exceptions ;
67using SmartHealthCard . Token . Model . Shc ;
78using SmartHealthCard . Token . Providers ;
89using System ;
910using System . Collections . Generic ;
1011using System . Security . Cryptography . X509Certificates ;
12+ using System . Threading . Tasks ;
1113using Xunit ;
1214
1315namespace SmartHealthCard . Test
@@ -26,7 +28,7 @@ public async void Decode_Token_Verify_with_JWKS()
2628
2729 //The base of the URL where a validator will retrieve the public keys from (e.g : [Issuer]/.well-known/jwks.json)
2830 Uri Issuer = new Uri ( "https://sonichealthcare.com/something" ) ;
29- string SmartHealthCardJwsToken = await SmartHealthCardJwsSupport . GetJWSCovidExampleOneAsync ( Certificate , Issuer ) ;
31+ string SmartHealthCardJwsToken = await SmartHealthCardJwsSupport . GetJWSCovidDetectedExampleOneAsync ( Certificate , Issuer ) ;
3032
3133 //This testing JwksSupport class provides us with a mocked IJwksProvider that will inject the JWKS file
3234 //rather than make the HTTP call to go get it from a public endpoint.
@@ -53,7 +55,7 @@ public async void Decode_Token_Verify_with_Certificate()
5355
5456 //The base of the URL where a validator will retrieve the public keys from (e.g : [Issuer]/.well-known/jwks.json)
5557 Uri Issuer = new Uri ( "https://sonichealthcare.com/something" ) ;
56- string SmartHealthCardJwsToken = await SmartHealthCardJwsSupport . GetJWSCovidExampleOneAsync ( Certificate , Issuer ) ;
58+ string SmartHealthCardJwsToken = await SmartHealthCardJwsSupport . GetJWSCovidDetectedExampleOneAsync ( Certificate , Issuer ) ;
5759
5860 //This testing JwksSupport class provides us with a mocked IJwksProvider that will inject the JWKS file
5961 //rather than make the HTTP call to go get it from a public endpoint.
@@ -82,7 +84,15 @@ public async void Decode_Token_Verify_InvalidTokenSignature_Certificate()
8284
8385 //The base of the URL where a validator will retrieve the public keys from (e.g : [Issuer]/.well-known/jwks.json)
8486 Uri Issuer = new Uri ( "https://sonichealthcare.com/something" ) ;
85- string SmartHealthCardJwsToken = await SmartHealthCardJwsSupport . GetJWSCovidExampleOneAsync ( Certificate , Issuer ) ;
87+
88+ string SmartHealthCardJwsTokenCovidDetected = await SmartHealthCardJwsSupport . GetJWSCovidDetectedExampleOneAsync ( Certificate , Issuer ) ;
89+ string SmartHealthCardJwsTokenCovidNotDetected = await SmartHealthCardJwsSupport . GetJWSCovidNotDetectedExampleOneAsync ( Certificate , Issuer ) ;
90+
91+ //Here we have taken the Payload of a NotDetected Covid test result and substituted it into
92+ //a token build with a Detected Covid Test result, so the signature is now be invalid for it's payload.
93+ string [ ] JWSSplitCovidNotDetected = SmartHealthCardJwsTokenCovidNotDetected . Split ( '.' ) ;
94+ string [ ] JWSSplitCovidDetected = SmartHealthCardJwsTokenCovidDetected . Split ( '.' ) ;
95+ string FraudulentSmartHealthCardJwsToken = string . Join ( '.' , JWSSplitCovidDetected [ 0 ] , JWSSplitCovidNotDetected [ 1 ] , JWSSplitCovidDetected [ 2 ] ) ;
8696
8797 //This testing JwksSupport class provides us with a mocked IJwksProvider that will inject the JWKS file
8898 //rather than make the HTTP call to go get it from a public endpoint.
@@ -94,12 +104,47 @@ public async void Decode_Token_Verify_InvalidTokenSignature_Certificate()
94104 //### Act #######################################################
95105
96106 //Verify and Decode
97- SmartHealthCardModel SmartHealthCardModel = await Decoder . DecodeAsync ( SmartHealthCardJwsToken , Verify : true ) ;
107+ SmartHealthCardSignatureInvalidException Exec = await Assert . ThrowsAsync < SmartHealthCardSignatureInvalidException > ( ( ) => Decoder . DecodeAsync ( FraudulentSmartHealthCardJwsToken , Verify : true ) ) ;
98108
99109 //### Assert #######################################################
110+ Assert . Equal ( "The JWS signing signature is invalid." , Exec . Message ) ;
111+
112+ }
113+
114+ [ Fact ]
115+ public async void Decode_Token_Verify_JWKS_Is_Inaccessible ( )
116+ {
117+ //### Prepare ######################################################
118+ //Get the ECC certificate from the Cert and Private key PEM files
119+ X509Certificate2 Certificate = CertificateSupport . GetCertificateFromPemFiles ( ) ;
120+
121+ //The base of the URL where a validator will retrieve the public keys from (e.g : [Issuer]/.well-known/jwks.json)
122+ Uri Issuer = new Uri ( "https://sonichealthcare.com/something" ) ;
123+
124+ string SmartHealthCardJwsTokenCovidDetected = await SmartHealthCardJwsSupport . GetJWSCovidDetectedExampleOneAsync ( Certificate , Issuer ) ;
125+ string SmartHealthCardJwsTokenCovidNotDetected = await SmartHealthCardJwsSupport . GetJWSCovidNotDetectedExampleOneAsync ( Certificate , Issuer ) ;
126+
127+ //Here we have taken the Payload of a NotDetected Covid test result and substituted it into
128+ //a token build with a Detected Covid Test result, so the signature is now be invalid for it's payload.
129+ string [ ] JWSSplitCovidNotDetected = SmartHealthCardJwsTokenCovidNotDetected . Split ( '.' ) ;
130+ string [ ] JWSSplitCovidDetected = SmartHealthCardJwsTokenCovidDetected . Split ( '.' ) ;
131+ string FraudulentSmartHealthCardJwsToken = string . Join ( '.' , JWSSplitCovidDetected [ 0 ] , JWSSplitCovidNotDetected [ 1 ] , JWSSplitCovidDetected [ 2 ] ) ;
132+
133+ //This testing JwksSupport class provides us with a mocked IJwksProvider that will inject the JWKS file
134+ //rather than make the HTTP call to go get it from a public endpoint.
135+ IJwksProvider MockedIJwksProvider = JwksSupport . GetMockedIJwksProvider ( Certificate , Issuer ) ;
136+
137+ //Instantiate the SmartHealthCard Decoder
138+ SmartHealthCardDecoder Decoder = new SmartHealthCardDecoder ( MockedIJwksProvider ) ;
139+
140+ //### Act #######################################################
141+
142+ //Verify and Decode
143+ SmartHealthCardSignatureInvalidException Exec = await Assert . ThrowsAsync < SmartHealthCardSignatureInvalidException > ( ( ) => Decoder . DecodeAsync ( FraudulentSmartHealthCardJwsToken , Verify : true ) ) ;
144+
145+ //### Assert #######################################################
146+ Assert . Equal ( "The JWS signing signature is invalid." , Exec . Message ) ;
100147
101- Assert . True ( ! string . IsNullOrWhiteSpace ( SmartHealthCardJwsToken ) ) ;
102- Assert . NotNull ( SmartHealthCardModel ) ;
103148 }
104149
105150
0 commit comments