@Test public void crestActionBlowupIsAllowed() throws SSOException, DelegationException { // Given... final Set<String> actions = new HashSet<>(Arrays.asList("MODIFY")); final DelegationPermission permission = new DelegationPermission( "/abc", "rest", "1.0", "policies", "destroy", actions, EXTENSIONS, DUMB_FUNC); given(factory.newInstance("/abc", "rest", "1.0", "policies", "destroy", actions, EXTENSIONS)) .willReturn(permission); given(subjectContext.getCallerSSOToken()).willReturn(token); given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(true); JsonValue jsonValue = json(object(field("someKey", "someValue"))); Promise<ActionResponse, ResourceException> promise = Promises.newResultPromise(Responses.newActionResponse(jsonValue)); given(provider.actionCollection(isA(Context.class), isA(ActionRequest.class))) .willReturn(promise); // When... final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module); final Router router = new Router(); router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain); final RealmContext context = new RealmContext(subjectContext); context.setSubRealm("abc", "abc"); final ActionRequest request = Requests.newActionRequest("/policies", "blowup"); Promise<ActionResponse, ResourceException> result = router.handleAction(context, request); // Then... assertThat(result).succeeded().withContent().stringAt("someKey").isEqualTo("someValue"); }
private ResourceResponse getResourceResponse( Context context, String clientId, Iterable<JsonValue> tokens) throws NotFoundException, InvalidClientException, ServerException, InternalServerErrorException { String realm = getAttributeValue(tokens.iterator().next(), REALM.getOAuthField()); OAuth2ProviderSettings oAuth2ProviderSettings = oAuth2ProviderSettingsFactory.get(context); ClientRegistration clientRegistration = clientRegistrationStore.get(clientId, realm, context); Map<String, String> scopeDescriptions = clientRegistration.getScopeDescriptions(getLocale(context)); Map<String, String> scopes = new HashMap<>(); for (JsonValue token : tokens) { for (String scope : token.get(SCOPE.getOAuthField()).asSet(String.class)) { if (scopeDescriptions.containsKey(scope)) { scopes.put(scope, scopeDescriptions.get(scope)); } else { scopes.put(scope, scope); } } } String displayName = clientRegistration.getDisplayName(getLocale(context)); String expiryDateTime = calculateExpiryDateTime(tokens, oAuth2ProviderSettings); JsonValue content = json( object( field("_id", clientId), field("name", displayName), field("scopes", scopes), field("expiryDateTime", expiryDateTime))); return Responses.newResourceResponse( clientId, String.valueOf(content.getObject().hashCode()), content); }
/** * Allows users to query OAuth2 applications that they have given their consent access to and that * have active access and/or refresh tokens. * * <p>Applications consist of an id, a name (the client id), a set of scopes and an expiry time. * The scopes field is the union of the scopes of the individual access/refresh tokens. The expiry * time is the time when the last access/refresh token will expire, or null if the server is * configured to allow tokens to be refreshed indefinitely. * * @param context The request context. * @param queryHandler The query handler. * @param request Unused but necessary for used of the {@link @Query} annotation. * @return A promise of a query response. */ @Query public Promise<QueryResponse, ResourceException> query( Context context, QueryResourceHandler queryHandler, QueryRequest request) { String userId = contextHelper.getUserId(context); String realm = contextHelper.getRealm(context); try { QueryFilter<CoreTokenField> queryFilter = getQueryFilter(userId, realm); JsonValue tokens = tokenStore.query(queryFilter); Map<String, Set<JsonValue>> applicationTokensMap = new HashMap<>(); for (JsonValue token : tokens) { String clientId = getAttributeValue(token, CLIENT_ID.getOAuthField()); Set<JsonValue> applicationTokens = applicationTokensMap.get(clientId); if (applicationTokens == null) { applicationTokens = new HashSet<>(); applicationTokensMap.put(clientId, applicationTokens); } applicationTokens.add(token); } for (Map.Entry<String, Set<JsonValue>> applicationTokens : applicationTokensMap.entrySet()) { ResourceResponse resource = getResourceResponse(context, applicationTokens.getKey(), applicationTokens.getValue()); queryHandler.handleResource(resource); } return Promises.newResultPromise(Responses.newQueryResponse()); } catch (CoreTokenException | ServerException | InvalidClientException | NotFoundException e) { debug.message("Failed to query OAuth2 clients for user {}", userId, e); return new InternalServerErrorException(e).asPromise(); } catch (InternalServerErrorException e) { debug.message("Failed to query OAuth2 clients for user {}", userId, e); return e.asPromise(); } }
@Test public void crestQueryIsAllowed() throws SSOException, DelegationException, ResourceException { // Given... final Set<String> actions = new HashSet<>(Arrays.asList("READ")); final DelegationPermission permission = new DelegationPermission( "/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS, DUMB_FUNC); given(factory.newInstance("/abc", "rest", "1.0", "policies", "read", actions, EXTENSIONS)) .willReturn(permission); given(subjectContext.getCallerSSOToken()).willReturn(token); given(evaluator.isAllowed(eq(token), eq(permission), eq(ENVIRONMENT))).willReturn(true); QueryResourceHandler handler = mock(QueryResourceHandler.class); Promise<QueryResponse, ResourceException> promise = Promises.newResultPromise(Responses.newQueryResponse("abc-def")); given( provider.queryCollection( isA(Context.class), isA(QueryRequest.class), isA(QueryResourceHandler.class))) .willReturn(promise); // When... final FilterChain chain = AuthorizationFilters.createAuthorizationFilter(provider, module); final Router router = new Router(); router.addRoute(RoutingMode.STARTS_WITH, Router.uriTemplate("/policies"), chain); final RealmContext context = new RealmContext(subjectContext); context.setSubRealm("abc", "abc"); final QueryRequest request = Requests.newQueryRequest("/policies"); Promise<QueryResponse, ResourceException> result = router.handleQuery(context, request, handler); // Then... QueryResponse response = result.getOrThrowUninterruptibly(); assertThat(response.getPagedResultsCookie()).isEqualTo("abc-def"); }