Search in sources :

Example 6 with DelegationPermission

use of com.sun.identity.delegation.DelegationPermission in project OpenAM by OpenRock.

the class SSOTokenAuthZ method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    int statusCode = HttpServletResponse.SC_OK;
    String statusMessage = null;
    Principal clientPrincipal = ((HttpServletRequest) request).getUserPrincipal();
    if (clientPrincipal instanceof ISubjectable) {
        try {
            Subject clientSubject = ((ISubjectable) clientPrincipal).createSubject();
            DelegationEvaluator eval = new DelegationEvaluatorImpl();
            SSOToken token = SubjectUtils.getSSOToken(clientSubject);
            String action = mapMethodToAction.get(((HttpServletRequest) request).getMethod());
            if (action == null) {
                statusCode = HttpServletResponse.SC_UNAUTHORIZED;
                statusMessage = "Unable to get HTTP method for request.";
            } else {
                Set<String> setAction = new HashSet<String>();
                setAction.add(action);
                DelegationPermission permission = new DelegationPermission("/", "sunEntitlementService", "1.0", "application", getURI(request), setAction, null);
                if (!eval.isAllowed(token, permission, Collections.EMPTY_MAP)) {
                    statusCode = HttpServletResponse.SC_UNAUTHORIZED;
                    statusMessage = "Unauthorized.";
                }
            }
        } catch (Exception e) {
            statusCode = HttpServletResponse.SC_UNAUTHORIZED;
            statusMessage = e.getMessage();
        }
    } else {
        statusCode = HttpServletResponse.SC_UNAUTHORIZED;
        statusMessage = "Unable to obtain subject.";
    }
    if (statusCode == HttpServletResponse.SC_OK) {
        statusCode = validateTokenId((HttpServletRequest) request);
        if (statusCode == HttpServletResponse.SC_OK) {
            chain.doFilter(request, response);
        } else {
            statusMessage = "SSO token is invalid or has expired.";
        }
    }
    if (statusCode != HttpServletResponse.SC_OK) {
        ((HttpServletResponse) response).sendError(statusCode, statusMessage);
        return;
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) DelegationEvaluator(com.sun.identity.delegation.DelegationEvaluator) HttpServletResponse(javax.servlet.http.HttpServletResponse) Subject(javax.security.auth.Subject) DelegationPermission(com.sun.identity.delegation.DelegationPermission) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RestException(com.sun.identity.rest.RestException) SSOException(com.iplanet.sso.SSOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) DelegationEvaluatorImpl(com.sun.identity.delegation.DelegationEvaluatorImpl) Principal(java.security.Principal) ISubjectable(com.sun.identity.rest.ISubjectable) HashSet(java.util.HashSet)

Example 7 with DelegationPermission

use of com.sun.identity.delegation.DelegationPermission in project OpenAM by OpenRock.

the class XacmlServiceTest method testPermissionsCheckSuccess.

@Test
public void testPermissionsCheckSuccess() {
    RestLog restLog = PowerMockito.mock(RestLog.class);
    DelegationEvaluator evaluator = mock(DelegationEvaluator.class);
    XacmlService xacmlService = new XacmlService(importExport, adminTokenAction, this.debug, restLog, evaluator, jacksonRepresentationFactory);
    SSOToken adminToken = mock(SSOToken.class);
    DelegationPermission delegationPermission = mock(DelegationPermission.class);
    String urlLastSegment = "blah";
    try {
        // when
        when(evaluator.isAllowed(adminToken, delegationPermission, Collections.EMPTY_MAP)).thenReturn(true);
        boolean result = xacmlService.checkPermission(delegationPermission, adminToken, urlLastSegment);
        assertThat(result).isTrue();
        verify(restLog).auditAccessGranted(anyString(), anyString(), anyString(), any(SSOToken.class));
    } catch (DelegationException de) {
        // then
        fail("Did not expect DelegationException");
    } catch (SSOException ssoe) {
        //then
        fail("Did not expect SSOException");
    } catch (Exception e) {
        fail("Did not expect " + e.getClass().getName() + " with message " + e.getMessage());
    }
}
Also used : RestLog(org.forgerock.openam.forgerockrest.utils.RestLog) SSOToken(com.iplanet.sso.SSOToken) DelegationEvaluator(com.sun.identity.delegation.DelegationEvaluator) DelegationException(com.sun.identity.delegation.DelegationException) SSOException(com.iplanet.sso.SSOException) DelegationPermission(com.sun.identity.delegation.DelegationPermission) DelegationException(com.sun.identity.delegation.DelegationException) ResourceException(org.restlet.resource.ResourceException) SSOException(com.iplanet.sso.SSOException) EntitlementException(com.sun.identity.entitlement.EntitlementException) IOException(java.io.IOException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with DelegationPermission

use of com.sun.identity.delegation.DelegationPermission in project OpenAM by OpenRock.

the class DelegationIsAllowedSubResourceTest method test.

@Test
public void test() throws Exception {
    Set<String> actions = new HashSet<String>();
    actions.add("READ");
    SSOToken token = AuthUtils.authenticate("/", USER1, USER1);
    DelegationPermission dp = new DelegationPermission("/", "sunEntitlementService", "1.0", "application", "default/application/*", actions, null);
    DelegationEvaluator de = new DelegationEvaluatorImpl();
    if (!de.isAllowed(token, dp, Collections.EMPTY_MAP, true)) {
        throw new Exception("DelegationIsAllowedSubResourceTest.test: failed");
    }
}
Also used : DelegationEvaluatorImpl(com.sun.identity.delegation.DelegationEvaluatorImpl) SSOToken(com.iplanet.sso.SSOToken) DelegationEvaluator(com.sun.identity.delegation.DelegationEvaluator) DelegationPermission(com.sun.identity.delegation.DelegationPermission) SMSException(com.sun.identity.sm.SMSException) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 9 with DelegationPermission

use of com.sun.identity.delegation.DelegationPermission in project OpenAM by OpenRock.

the class OrgConfigViaAMSDK method checkRealmPermission.

// Check to see if the user has realm permissions
private boolean checkRealmPermission(SSOToken token, String realm, Set action) {
    boolean answer = false;
    if (token != null) {
        try {
            DelegationEvaluator de = new DelegationEvaluatorImpl();
            DelegationPermission dp = new DelegationPermission(realm, com.sun.identity.sm.SMSEntry.REALM_SERVICE, "1.0", "*", "*", action, Collections.EMPTY_MAP);
            answer = de.isAllowed(token, dp, null);
        } catch (DelegationException dex) {
            debug.error("OrgConfigViaAMSDK.checkRealmPermission: " + "Got Delegation Exception: ", dex);
        } catch (SSOException ssoe) {
            if (debug.messageEnabled()) {
                debug.message("OrgConfigViaAMSDK.checkRealmPermission: " + "Invalid SSOToken: ", ssoe);
            }
        }
    }
    return (answer);
}
Also used : DelegationEvaluatorImpl(com.sun.identity.delegation.DelegationEvaluatorImpl) DelegationEvaluator(com.sun.identity.delegation.DelegationEvaluator) DelegationException(com.sun.identity.delegation.DelegationException) SSOException(com.iplanet.sso.SSOException) DelegationPermission(com.sun.identity.delegation.DelegationPermission)

Example 10 with DelegationPermission

use of com.sun.identity.delegation.DelegationPermission in project OpenAM by OpenRock.

the class PrivilegeAuthzModuleTest method crestQueryIsAllowed.

@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");
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) RealmContext(org.forgerock.openam.rest.RealmContext) FilterChain(org.forgerock.json.resource.FilterChain) Router(org.forgerock.json.resource.Router) Matchers.anyString(org.mockito.Matchers.anyString) DelegationPermission(com.sun.identity.delegation.DelegationPermission) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

DelegationPermission (com.sun.identity.delegation.DelegationPermission)30 HashSet (java.util.HashSet)22 DelegationException (com.sun.identity.delegation.DelegationException)17 SSOException (com.iplanet.sso.SSOException)16 DelegationEvaluator (com.sun.identity.delegation.DelegationEvaluator)14 Set (java.util.Set)13 DelegationEvaluatorImpl (com.sun.identity.delegation.DelegationEvaluatorImpl)12 Test (org.testng.annotations.Test)12 FilterChain (org.forgerock.json.resource.FilterChain)9 ResourceException (org.forgerock.json.resource.ResourceException)9 Router (org.forgerock.json.resource.Router)9 RealmContext (org.forgerock.openam.rest.RealmContext)9 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Context (org.forgerock.services.context.Context)8 SSOToken (com.iplanet.sso.SSOToken)7 Iterator (java.util.Iterator)6 JsonValue (org.forgerock.json.JsonValue)6 ResourceResponse (org.forgerock.json.resource.ResourceResponse)6 IdRepoException (com.sun.identity.idm.IdRepoException)4