Search in sources :

Example 1 with Policy

use of org.opencastproject.urlsigning.common.Policy in project opencast by opencast.

the class PolicyUtilsTest method testToBase64EncodedPolicy.

@Test
public void testToBase64EncodedPolicy() throws UnsupportedEncodingException {
    String resource = "http://mh-allinone/";
    DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC);
    Policy policy = Policy.mkSimplePolicy("http://mh-allinone/", before);
    Policy result = PolicyUtils.fromBase64EncodedPolicy(PolicyUtils.toBase64EncodedPolicy(policy));
    assertEquals(resource, result.getBaseUrl());
    assertEquals(before, result.getValidUntil());
}
Also used : Policy(org.opencastproject.urlsigning.common.Policy) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with Policy

use of org.opencastproject.urlsigning.common.Policy in project opencast by opencast.

the class PolicyUtilsTest method testFromJson.

@Test
public void testFromJson() throws UnsupportedEncodingException {
    String policyJson = "{\"Statement\": {\"Resource\":\"http://mh-allinone/engage/url/to/resource.mp4\",\"Condition\":{\"DateLessThan\":1425170777000,\"DateGreaterThan\":1425084379000,\"IpAddress\": \"10.0.0.1\"}}}";
    Policy policy = PolicyUtils.fromJson(policyJson);
    assertEquals("http://mh-allinone/engage/url/to/resource.mp4", policy.getBaseUrl());
    assertEquals(EXAMPLE_IP, policy.getClientIpAddress().get().getHostAddress());
    DateTime after = new DateTime(2015, 02, 28, 00, 46, 19, 0, DateTimeZone.UTC);
    after = after.withSecondOfMinute(19);
    assertEquals(after, policy.getValidFrom().get());
    DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC);
    assertEquals(before, policy.getValidUntil());
}
Also used : Policy(org.opencastproject.urlsigning.common.Policy) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with Policy

use of org.opencastproject.urlsigning.common.Policy in project opencast by opencast.

the class PolicyUtilsTest method testFromBase64EncodedPolicy.

@Test
public void testFromBase64EncodedPolicy() throws UnsupportedEncodingException {
    String examplePolicy = "{\"Statement\": {\"Resource\":\"http://mh-allinone/engage/url/to/resource.mp4\",\"Condition\":{\"DateLessThan\":1425170777000,\"DateGreaterThan\":1425084379000,\"IpAddress\": \"10.0.0.1\"}}}";
    Policy policy = PolicyUtils.fromBase64EncodedPolicy(PolicyUtils.base64Encode(examplePolicy));
    assertEquals("http://mh-allinone/engage/url/to/resource.mp4", policy.getBaseUrl());
    assertEquals(EXAMPLE_IP, policy.getClientIpAddress().get().getHostAddress());
    DateTime after = new DateTime(2015, 02, 28, 00, 46, 19, 0, DateTimeZone.UTC);
    after = after.withSecondOfMinute(19);
    assertEquals(after, policy.getValidFrom().get());
    DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC);
    assertEquals(before, policy.getValidUntil());
}
Also used : Policy(org.opencastproject.urlsigning.common.Policy) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with Policy

use of org.opencastproject.urlsigning.common.Policy in project opencast by opencast.

the class PolicyUtilsTest method testToJson.

@Test
public void testToJson() {
    DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC);
    Policy policy = Policy.mkSimplePolicy("http://mh-allinone/", before);
    assertEquals("{\"Statement\":{\"Condition\":{\"DateLessThan\":" + before.getMillis() + "},\"Resource\":\"http:\\/\\/mh-allinone\\/\"}}", PolicyUtils.toJson(policy).toJSONString());
    // With optional parameters
    policy = Policy.mkPolicyValidFromWithIP("http://mh-allinone/", before, new DateTime(2015, 02, 28, 00, 46, 19, 0, DateTimeZone.UTC), EXAMPLE_IP);
    assertEquals("{\"Statement\":{\"Condition\":{\"DateGreaterThan\":1425084379000,\"DateLessThan\":1425170777000,\"IpAddress\":\"10.0.0.1\"},\"Resource\":\"http:\\/\\/mh-allinone\\/\"}}", PolicyUtils.toJson(policy).toJSONString());
}
Also used : Policy(org.opencastproject.urlsigning.common.Policy) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with Policy

use of org.opencastproject.urlsigning.common.Policy in project opencast by opencast.

the class GenericUrlSigningProviderTest method testMultitenantSign.

@Test
public void testMultitenantSign() throws UrlSigningException, ConfigurationException {
    properties.put(GenericUrlSigningProvider.ID_PREFIX + ".1", KEY_ID);
    properties.put(GenericUrlSigningProvider.URL_PREFIX + ".1", MATCHING_URI);
    properties.put(GenericUrlSigningProvider.KEY_PREFIX + ".1", KEY);
    properties.put(GenericUrlSigningProvider.ID_PREFIX + ".2", ORGANIZATION_A_KEY_ID);
    properties.put(GenericUrlSigningProvider.URL_PREFIX + ".2", ORGANIZATION_A_MATCHING_URI);
    properties.put(GenericUrlSigningProvider.KEY_PREFIX + ".2", ORGANIZATION_A_KEY);
    properties.put(GenericUrlSigningProvider.ORGANIZATION_PREFIX + ".2", ORGANIZATION_A_ID);
    properties.put(GenericUrlSigningProvider.ID_PREFIX + ".3", ORGANIZATION_B_KEY_ID);
    properties.put(GenericUrlSigningProvider.URL_PREFIX + ".3", ORGANIZATION_B_MATCHING_URI);
    properties.put(GenericUrlSigningProvider.KEY_PREFIX + ".3", ORGANIZATION_B_KEY);
    properties.put(GenericUrlSigningProvider.ORGANIZATION_PREFIX + ".3", ORGANIZATION_B_ID);
    signerA.updated(properties);
    signerB.updated(properties);
    DateTime before = new DateTime(2020, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC);
    Policy policy;
    String result;
    boolean exceptionThrown;
    // Organization A can sign its URLs using its key
    policy = Policy.mkSimplePolicy(ORGANIZATION_A_RESOURCE_PATH, before);
    result = signerA.sign(policy);
    logger.info(result);
    assertEquals("http://organization-a.opencast.org/path/to/resource.mp4?policy=eyJTdGF0ZW1lbnQiOnsiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6MTU4MzAyMzU3NzAwMH0sIlJlc291cmNlIjoiaHR0cDpcL1wvb3JnYW5pemF0aW9uLWEub3BlbmNhc3Qub3JnXC9wYXRoXC90b1wvcmVzb3VyY2UubXA0In19&keyId=key-id-organization-a&signature=ea715d6ff561f49bb6e2cb8cc3e925029e139f14eeda62fc92573f362c694423", result);
    // Organization A can sign URLs not specific to any organization
    policy = Policy.mkSimplePolicy(RESOURCE_PATH, before);
    result = signerA.sign(policy);
    logger.info(result);
    assertEquals("http://www.opencast.org/path/to/resource.mp4?policy=eyJTdGF0ZW1lbnQiOnsiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6MTU4MzAyMzU3NzAwMH0sIlJlc291cmNlIjoiaHR0cDpcL1wvd3d3Lm9wZW5jYXN0Lm9yZ1wvcGF0aFwvdG9cL3Jlc291cmNlLm1wNCJ9fQ&keyId=theId&signature=5b45e678275e6bc7b06a579f7f42e9a7ea5c58f1da130701db532f121e363e98", result);
    // Organization A cannot sign URLs of organization B
    exceptionThrown = false;
    policy = Policy.mkSimplePolicy(ORGANIZATION_B_RESOURCE_PATH, before);
    try {
        result = signerA.sign(policy);
    } catch (UrlSigningException e) {
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    // Organization B can sign its URLs using its key
    policy = Policy.mkSimplePolicy(ORGANIZATION_B_RESOURCE_PATH, before);
    result = signerB.sign(policy);
    logger.info(result);
    assertEquals("http://organization-b.opencast.org/path/to/resource.mp4?policy=eyJTdGF0ZW1lbnQiOnsiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6MTU4MzAyMzU3NzAwMH0sIlJlc291cmNlIjoiaHR0cDpcL1wvb3JnYW5pemF0aW9uLWIub3BlbmNhc3Qub3JnXC9wYXRoXC90b1wvcmVzb3VyY2UubXA0In19&keyId=key-id-organizatino-b&signature=a6d803d4766f808bf2eaabdcf2e9114f85513d3d5596b4a01cb8d2488de816e8", result);
    // Organization B can sign URLs not specific to any organization
    policy = Policy.mkSimplePolicy(RESOURCE_PATH, before);
    result = signerB.sign(policy);
    logger.info(result);
    assertEquals("http://www.opencast.org/path/to/resource.mp4?policy=eyJTdGF0ZW1lbnQiOnsiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6MTU4MzAyMzU3NzAwMH0sIlJlc291cmNlIjoiaHR0cDpcL1wvd3d3Lm9wZW5jYXN0Lm9yZ1wvcGF0aFwvdG9cL3Jlc291cmNlLm1wNCJ9fQ&keyId=theId&signature=5b45e678275e6bc7b06a579f7f42e9a7ea5c58f1da130701db532f121e363e98", result);
    // Organization B cannot sign URLs of organization B
    exceptionThrown = false;
    policy = Policy.mkSimplePolicy(ORGANIZATION_A_RESOURCE_PATH, before);
    try {
        result = signerB.sign(policy);
    } catch (UrlSigningException e) {
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
}
Also used : Policy(org.opencastproject.urlsigning.common.Policy) UrlSigningException(org.opencastproject.security.urlsigning.exception.UrlSigningException) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

Policy (org.opencastproject.urlsigning.common.Policy)17 DateTime (org.joda.time.DateTime)16 Test (org.junit.Test)14 ResourceRequest (org.opencastproject.urlsigning.common.ResourceRequest)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 FilterChain (javax.servlet.FilterChain)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 UrlSigningVerifier (org.opencastproject.security.urlsigning.verifier.UrlSigningVerifier)5 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 UrlSigningException (org.opencastproject.security.urlsigning.exception.UrlSigningException)1 UrlSigningProvider (org.opencastproject.security.urlsigning.provider.UrlSigningProvider)1