use of org.restlet.data.ChallengeResponse in project qi4j-sdk by Qi4j.
the class ContextResourceClient method invokeQuery.
private HandlerCommand invokeQuery(Reference ref, Object queryRequest, ResponseHandler resourceHandler, ResponseHandler processingErrorHandler) {
Request request = new Request(Method.GET, ref);
if (queryRequest != null) {
contextResourceFactory.writeRequest(request, queryRequest);
}
contextResourceFactory.updateQueryRequest(request);
User user = request.getClientInfo().getUser();
if (user != null)
request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, user.getName(), user.getSecret()));
Response response = new Response(request);
contextResourceFactory.getClient().handle(request, response);
if (response.getStatus().isSuccess()) {
contextResourceFactory.updateCache(response);
return resourceHandler.handleResponse(response, this);
} else if (response.getStatus().isRedirection()) {
Reference redirectedTo = response.getLocationRef();
return invokeQuery(redirectedTo, queryRequest, resourceHandler, processingErrorHandler);
} else {
if (response.getStatus().equals(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY) && processingErrorHandler != null) {
return processingErrorHandler.handleResponse(response, this);
} else {
// TODO This needs to be expanded to allow custom handling of all the various cases
return errorHandler.handleResponse(response, this);
}
}
}
use of org.restlet.data.ChallengeResponse in project qi4j-sdk by Qi4j.
the class RootResource method administration.
@SubResource
public void administration() {
ChallengeResponse challenge = Request.getCurrent().getChallengeResponse();
if (challenge == null) {
Response.getCurrent().setChallengeRequests(Collections.singletonList(new ChallengeRequest(ChallengeScheme.HTTP_BASIC, "Forum")));
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED);
}
User user = module.currentUnitOfWork().newQuery(module.newQueryBuilder(User.class).where(QueryExpressions.eq(QueryExpressions.templateFor(User.class).name(), challenge.getIdentifier()))).find();
if (user == null || !user.isCorrectPassword(new String(challenge.getSecret()))) {
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED);
}
current().select(user);
subResource(AdministrationResource.class);
}
use of org.restlet.data.ChallengeResponse in project OpenAM by OpenRock.
the class AccessTokenProtectionFilterTest method testBeforeHandleWithServerException.
@Test
public void testBeforeHandleWithServerException() throws Exception {
//Given
Request req = mock(Request.class);
Response resp = mock(Response.class);
OAuth2Request oAuth2Request = mock(OAuth2Request.class);
when(requestFactory.create(req)).thenReturn(oAuth2Request);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC);
challengeResponse.setRawValue("tokenId");
when(req.getChallengeResponse()).thenReturn(challengeResponse);
when(tokenStore.readAccessToken(oAuth2Request, "tokenId")).thenThrow(ServerException.class);
//When
int result = filter.beforeHandle(req, resp);
//Then
assertThat(result).isEqualTo(Filter.STOP);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(resp).setStatus(statusCaptor.capture());
Status status = statusCaptor.getValue();
assertThat(status.getThrowable()).isInstanceOf(ServerException.class);
}
use of org.restlet.data.ChallengeResponse in project OpenAM by OpenRock.
the class AccessTokenProtectionFilterTest method testBeforeHandle.
@Test
public void testBeforeHandle() throws Exception {
//Given
Request req = mock(Request.class);
OAuth2Request oAuth2Request = mock(OAuth2Request.class);
when(requestFactory.create(req)).thenReturn(oAuth2Request);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC);
challengeResponse.setRawValue("tokenId");
when(req.getChallengeResponse()).thenReturn(challengeResponse);
AccessToken accessToken = new AccessToken(json(object(field("id", "tokenId"), field("tokenName", "access_token"), field("scope", asSet("a", REQUIRED_SCOPE)), field("expireTime", System.currentTimeMillis() + 5000))));
when(tokenStore.readAccessToken(oAuth2Request, "tokenId")).thenReturn(accessToken);
//When
int result = filter.beforeHandle(req, null);
//Then
assertThat(result).isEqualTo(Filter.CONTINUE);
}
use of org.restlet.data.ChallengeResponse in project OpenAM by OpenRock.
the class AccessTokenProtectionFilterTest method testBeforeHandleWithoutScope.
@Test
public void testBeforeHandleWithoutScope() throws Exception {
//Given
Request req = mock(Request.class);
Response resp = mock(Response.class);
OAuth2Request oAuth2Request = mock(OAuth2Request.class);
when(requestFactory.create(req)).thenReturn(oAuth2Request);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC);
challengeResponse.setRawValue("tokenId");
when(req.getChallengeResponse()).thenReturn(challengeResponse);
AccessToken accessToken = new AccessToken(json(object(field("id", "tokenId"), field("tokenName", "access_token"), field("scope", asSet("a")), field("expireTime", System.currentTimeMillis() + 5000))));
when(tokenStore.readAccessToken(oAuth2Request, "tokenId")).thenReturn(accessToken);
//When
int result = filter.beforeHandle(req, resp);
//Then
assertThat(result).isEqualTo(Filter.STOP);
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(resp).setStatus(statusCaptor.capture());
Status status = statusCaptor.getValue();
assertThat(status.getThrowable()).isInstanceOf(InsufficientScopeException.class);
}
Aggregations