Search in sources :

Example 1 with PermissionRequestFilter

use of org.forgerock.openam.uma.extensions.PermissionRequestFilter in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method setup.

@BeforeMethod
@SuppressWarnings("unchecked")
public void setup() throws ServerException, InvalidGrantException, NotFoundException {
    resourceSetStore = mock(ResourceSetStore.class);
    OAuth2RequestFactory<?, Request> requestFactory = mock(OAuth2RequestFactory.class);
    umaTokenStore = mock(UmaTokenStore.class);
    OAuth2ProviderSettingsFactory providerSettingFactory = mock(OAuth2ProviderSettingsFactory.class);
    OAuth2ProviderSettings providerSettings = mock(OAuth2ProviderSettings.class);
    given(providerSettingFactory.get(Matchers.<OAuth2Request>anyObject())).willReturn(providerSettings);
    given(providerSettings.getResourceSetStore()).willReturn(resourceSetStore);
    UmaProviderSettingsFactory umaProviderSettingsFactory = mock(UmaProviderSettingsFactory.class);
    UmaProviderSettings umaProviderSettings = mock(UmaProviderSettings.class);
    given(umaProviderSettingsFactory.get(any(Request.class))).willReturn(umaProviderSettings);
    given(umaProviderSettings.getUmaTokenStore()).willReturn(umaTokenStore);
    ExtensionFilterManager extensionFilterManager = mock(ExtensionFilterManager.class);
    permissionRequestFilter = mock(PermissionRequestFilter.class);
    given(extensionFilterManager.getFilters(PermissionRequestFilter.class)).willReturn(Collections.singleton(permissionRequestFilter));
    UmaExceptionHandler exceptionHandler = mock(UmaExceptionHandler.class);
    endpoint = spy(new PermissionRequestEndpoint(providerSettingFactory, requestFactory, umaProviderSettingsFactory, extensionFilterManager, exceptionHandler, jacksonRepresentationFactory));
    response = mock(Response.class);
    endpoint.setResponse(response);
    Request request = mock(Request.class);
    given(endpoint.getRequest()).willReturn(request);
    AccessToken accessToken = mock(AccessToken.class);
    given(accessToken.getClientId()).willReturn("CLIENT_ID");
    given(accessToken.getResourceOwnerId()).willReturn("RESOURCE_OWNER_ID");
    OAuth2Request oAuth2Request = mock(OAuth2Request.class);
    given(requestFactory.create(request)).willReturn(oAuth2Request);
    given(oAuth2Request.getToken(AccessToken.class)).willReturn(accessToken);
}
Also used : PermissionRequestFilter(org.forgerock.openam.uma.extensions.PermissionRequestFilter) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Response(org.restlet.Response) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) OAuth2ProviderSettingsFactory(org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) AccessToken(org.forgerock.oauth2.core.AccessToken) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) ExtensionFilterManager(org.forgerock.openam.oauth2.extensions.ExtensionFilterManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with PermissionRequestFilter

use of org.forgerock.openam.uma.extensions.PermissionRequestFilter in project OpenAM by OpenRock.

the class PermissionRequestEndpoint method registerPermissionRequest.

/**
     * Registers the permission that the client requires for it to be able to access a protected resource.
     *
     * @param entity The permission request JSON body.
     * @return A JSON object containing the permission ticket.
     * @throws UmaException If the JSON request body is invalid or the requested resource set does not exist.
     */
@Post
public Representation registerPermissionRequest(JsonRepresentation entity) throws UmaException, NotFoundException, ServerException {
    JsonValue permissionRequest = json(toMap(entity));
    String resourceSetId = getResourceSetId(permissionRequest);
    OAuth2Request oAuth2Request = requestFactory.create(getRequest());
    String clientId = getClientId(oAuth2Request);
    OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(oAuth2Request);
    String resourceOwnerId = getResourceOwnerId(oAuth2Request);
    ResourceSetDescription resourceSetDescription = getResourceSet(resourceSetId, resourceOwnerId, providerSettings);
    Set<String> scopes = validateScopes(permissionRequest, resourceSetDescription);
    for (PermissionRequestFilter filter : extensionFilterManager.getFilters(PermissionRequestFilter.class)) {
        filter.onPermissionRequest(resourceSetDescription, scopes, clientId);
    }
    String ticket = umaProviderSettingsFactory.get(getRequest()).getUmaTokenStore().createPermissionTicket(resourceSetId, scopes, clientId).getId();
    return setResponse(201, Collections.<String, Object>singletonMap("ticket", ticket));
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) PermissionRequestFilter(org.forgerock.openam.uma.extensions.PermissionRequestFilter) JsonValue(org.forgerock.json.JsonValue) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Post(org.restlet.resource.Post)

Aggregations

OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)2 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)2 PermissionRequestFilter (org.forgerock.openam.uma.extensions.PermissionRequestFilter)2 JsonValue (org.forgerock.json.JsonValue)1 AccessToken (org.forgerock.oauth2.core.AccessToken)1 OAuth2ProviderSettingsFactory (org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory)1 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)1 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)1 ExtensionFilterManager (org.forgerock.openam.oauth2.extensions.ExtensionFilterManager)1 Request (org.restlet.Request)1 Response (org.restlet.Response)1 Post (org.restlet.resource.Post)1 BeforeMethod (org.testng.annotations.BeforeMethod)1