use of org.opencastproject.security.urlsigning.service.UrlSigningService in project opencast by opencast.
the class TrustedHttpClientImplTest method testAlreadySignedUrlIgnoredByUrlSigningService.
@Test
public void testAlreadySignedUrlIgnoredByUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://alreadysigned.com?signature=thesig&policy=thepolicy&keyId=thekey";
HttpHead headRequest = new HttpHead(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(headRequest);
assertTrue(request.hasCaptured());
assertEquals(acceptsUrl, request.getValue().getURI().toString());
}
use of org.opencastproject.security.urlsigning.service.UrlSigningService in project opencast by opencast.
the class TrustedHttpClientImplTest method testAcceptsUrlSigningService.
@Test
public void testAcceptsUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://accepts.com";
String signedUrl = "http://accepts.com?policy=testPolicy&signature=testSignature&keyId=testKeyId";
HttpGet get = new HttpGet(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.expect(urlSigningService.sign(EasyMock.anyString(), EasyMock.anyLong(), EasyMock.anyLong(), EasyMock.anyString())).andReturn(signedUrl);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(get);
assertTrue(request.hasCaptured());
assertEquals(signedUrl, request.getValue().getURI().toString());
}
use of org.opencastproject.security.urlsigning.service.UrlSigningService in project opencast by opencast.
the class TrustedHttpClientImplTest method testGetSignedUrl.
@Test
public void testGetSignedUrl() throws IOException, UrlSigningException {
HttpGet notAccepted = new HttpGet("http://notAccepted.com");
HttpGet alreadySigned = new HttpGet("http://alreadySigned.com?signature=thesignature&keyId=theKeyId&policy=thePolicy");
HttpPost notGetOrHead = new HttpPost("http://notGetOrHead.com");
HttpGet ok = new HttpGet("http://ok.com");
String signedOk = "http://ok.com?signature=thesignature&keyId=theKeyId&policy=thePolicy";
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(notAccepted.getURI().toString())).andReturn(false);
EasyMock.expect(urlSigningService.accepts(alreadySigned.getURI().toString())).andReturn(true);
EasyMock.expect(urlSigningService.accepts(notGetOrHead.getURI().toString())).andReturn(true);
EasyMock.expect(urlSigningService.accepts(ok.getURI().toString())).andReturn(true);
EasyMock.expect(urlSigningService.sign(ok.getURI().toString(), TrustedHttpClientImpl.DEFAULT_URL_SIGNING_EXPIRES_DURATION, null, null)).andReturn(signedOk);
EasyMock.replay(urlSigningService);
client = new TrustedHttpClientImpl("user", "pass");
client.setUrlSigningService(urlSigningService);
assertTrue(client.getSignedUrl(notAccepted).isNone());
assertTrue(client.getSignedUrl(notGetOrHead).isNone());
Opt<HttpUriRequest> result = client.getSignedUrl(ok);
assertTrue(result.isSome());
assertEquals(signedOk, result.get().getURI().toString());
}
use of org.opencastproject.security.urlsigning.service.UrlSigningService in project opencast by opencast.
the class TrustedHttpClientImplTest method testNotAcceptsUrlSigningService.
@Test
public void testNotAcceptsUrlSigningService() throws IOException {
String notAcceptsUrl = "http://notaccepts.com";
HttpGet get = new HttpGet(notAcceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(notAcceptsUrl)).andReturn(false);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(get);
assertTrue(request.hasCaptured());
assertEquals(get.getURI().toString(), request.getValue().getURI().toString());
}
Aggregations