|
3 | 3 | import static org.junit.Assert.assertArrayEquals;
|
4 | 4 | import static org.junit.Assert.assertEquals;
|
5 | 5 | import static org.junit.Assert.assertNotNull;
|
| 6 | +import static org.junit.Assert.assertNull; |
6 | 7 | import static org.junit.Assert.assertTrue;
|
7 | 8 | import static org.junit.Assert.fail;
|
8 | 9 |
|
@@ -232,4 +233,89 @@ public void NullContentKeyIdentifierClaimShouldThrownJWT() throws Exception {
|
232 | 233 | assertTrue(e.getMessage().contains("keyIdForContentKeyIdentifierClaim"));
|
233 | 234 | }
|
234 | 235 | }
|
| 236 | + |
| 237 | + @Test |
| 238 | + public void OpenIdDocumentAsVerificationKeyRoundTrip() throws JAXBException, URISyntaxException |
| 239 | + { |
| 240 | + String openConnectId = "https://openconnectIddiscoveryUri"; |
| 241 | + String expectedElement = |
| 242 | + "<OpenIdDiscoveryUri>https://openconnectIddiscoveryUri</OpenIdDiscoveryUri>"; |
| 243 | + |
| 244 | + TokenRestrictionTemplate template = new TokenRestrictionTemplate(TokenType.JWT); |
| 245 | + template.setAudience(new URI(_sampleAudience)); |
| 246 | + template.setIssuer(new URI(_sampleIssuer)); |
| 247 | + OpenIdConnectDiscoveryDocument openId = new OpenIdConnectDiscoveryDocument(); |
| 248 | + openId.setOpenIdDiscoveryUri(openConnectId); |
| 249 | + template.setOpenIdConnectDiscoveryDocument(openId); |
| 250 | + String templateAsString = TokenRestrictionTemplateSerializer.serialize(template); |
| 251 | + assertTrue(templateAsString.contains("<PrimaryVerificationKey i:nil=\"true\"/>")); |
| 252 | + assertTrue(templateAsString.contains(expectedElement)); |
| 253 | + TokenRestrictionTemplate output = TokenRestrictionTemplateSerializer.deserialize(templateAsString); |
| 254 | + assertNotNull(output); |
| 255 | + assertNotNull(output.getOpenIdConnectDiscoveryDocument()); |
| 256 | + assertNull(output.getPrimaryVerificationKey()); |
| 257 | + assertTrue(output.getAlternateVerificationKeys().isEmpty()); |
| 258 | + assertEquals(output.getOpenIdConnectDiscoveryDocument().getOpenIdDiscoveryUri(), openConnectId); |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void TokenRestrictionTemplateSerializeNotPrimaryKeyAndNoOpenConnectIdDocument() throws URISyntaxException |
| 264 | + { |
| 265 | + TokenRestrictionTemplate template = new TokenRestrictionTemplate(TokenType.JWT); |
| 266 | + template.setAudience(new URI(_sampleAudience)); |
| 267 | + template.setIssuer(new URI(_sampleIssuer)); |
| 268 | + try { |
| 269 | + TokenRestrictionTemplateSerializer.serialize(template); |
| 270 | + fail(); |
| 271 | + } |
| 272 | + catch (Exception ex) { |
| 273 | + assertEquals("Both PrimaryVerificationKey and OpenIdConnectDiscoveryDocument are null.", ex.getMessage()); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void InputMissingPrimaryKeyShouldNotThrow() |
| 279 | + { |
| 280 | + String tokenTemplate = "<TokenRestrictionTemplate xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1\"><AlternateVerificationKeys><TokenVerificationKey i:type=\"SymmetricVerificationKey\"><KeyValue>GG07fDPZ+HMD2vcoknMqYjEJMb7LSq8zUmdCYMvRCevnQK//ilbhODO/FydMrHiwZGmI6XywvOOU7SSzRPlI3Q==</KeyValue></TokenVerificationKey></AlternateVerificationKeys><Audience>http://sampleaudience/</Audience><Issuer>http://sampleissuerurl/</Issuer><RequiredClaims><TokenClaim><ClaimType>urn:microsoft:azure:mediaservices:contentkeyidentifier</ClaimType><ClaimValue i:nil=\"true\" /></TokenClaim><TokenClaim><ClaimType>urn:myservice:claims:rental</ClaimType><ClaimValue>true</ClaimValue></TokenClaim></RequiredClaims></TokenRestrictionTemplate>"; |
| 281 | + try { |
| 282 | + TokenRestrictionTemplateSerializer.deserialize(tokenTemplate); |
| 283 | + fail(); |
| 284 | + } catch (Exception ex) { |
| 285 | + assertEquals("Both PrimaryVerificationKey and OpenIdConnectDiscoveryDocument are null.", ex.getMessage()); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + public void TokenRestrictionTemplateDeserializeNotAbsoluteDiscoveryUri() |
| 291 | + { |
| 292 | + String body = |
| 293 | + "<TokenRestrictionTemplate xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1\" ><AlternateVerificationKeys /><Audience>http://sampleissuerurl/</Audience><Issuer>http://sampleaudience/</Issuer><OpenIdConnectDiscoveryDocument ><OpenIdDiscoveryUri >RelativeUri</OpenIdDiscoveryUri></OpenIdConnectDiscoveryDocument></TokenRestrictionTemplate>"; |
| 294 | + |
| 295 | + try |
| 296 | + { |
| 297 | + TokenRestrictionTemplateSerializer.deserialize(body); |
| 298 | + fail(); |
| 299 | + } |
| 300 | + catch (Exception ex) |
| 301 | + { |
| 302 | + assertEquals("String representation of OpenIdConnectDiscoveryDocument.OpenIdDiscoveryUri is not valid absolute Uri.", ex.getMessage()); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + public void TokenRestrictionTemplateDeserializeNilOpenConnectIdDocumentUriNoPrimaryKey() |
| 308 | + { |
| 309 | + String body = |
| 310 | + "<TokenRestrictionTemplate xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1\" ><AlternateVerificationKeys /><Audience>http://sampleissuerurl/</Audience><Issuer>http://sampleaudience/</Issuer><OpenIdConnectDiscoveryDocument ><OpenIdDiscoveryUri i:nil=\"true\"></OpenIdDiscoveryUri></OpenIdConnectDiscoveryDocument></TokenRestrictionTemplate>"; |
| 311 | + try |
| 312 | + { |
| 313 | + TokenRestrictionTemplateSerializer.deserialize(body); |
| 314 | + fail(); |
| 315 | + } |
| 316 | + catch (Exception ex) |
| 317 | + { |
| 318 | + assertEquals("OpenIdConnectDiscoveryDocument.OpenIdDiscoveryUri string value is null or empty.", ex.getMessage()); |
| 319 | + } |
| 320 | + } |
235 | 321 | }
|
0 commit comments