Search in sources :

Example 1 with UriRouterContext

use of org.forgerock.http.routing.UriRouterContext in project OpenAM by OpenRock.

the class RealmContextFilterTest method verifyUriRouterContext.

private void verifyUriRouterContext(Context context, String matchedUri) {
    UriRouterContext routerContext = context.asContext(UriRouterContext.class);
    if (matchedUri.isEmpty()) {
        assertThat(routerContext.getBaseUri()).isEqualTo(JSON_PATH_ELEMENT);
    } else {
        assertThat(routerContext.getBaseUri()).isEqualTo(JSON_PATH_ELEMENT + "/" + matchedUri);
    }
    assertThat(routerContext.getMatchedUri()).isEqualTo(matchedUri);
    assertThat(routerContext.getRemainingUri()).isEqualTo(ENDPOINT_PATH_ELEMENT);
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext)

Example 2 with UriRouterContext

use of org.forgerock.http.routing.UriRouterContext in project OpenAM by OpenRock.

the class UmaResourceSetRegistrationHook method createAdminContext.

/**
     * Used to create a context for deleting policies. If this is being called, we know that the user has the right
     * to delete the policies.
     * @param realm The realm to delete the policies in.
     * @param resourceOwnerId The owner of the ResourceSet that the policies are for.
     * @return The generated context.
     */
private Context createAdminContext(String realm, String resourceOwnerId) {
    RealmContext realmContext = new RealmContext(new RootContext());
    realmContext.setSubRealm(realm, realm);
    SubjectContext subjectContext = new AdminSubjectContext(logger, sessionCache, realmContext);
    Map<String, String> templateVariables = new HashMap<>();
    templateVariables.put("user", resourceOwnerId);
    UriRouterContext routerContext = new UriRouterContext(subjectContext, "", "", templateVariables);
    return routerContext;
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) HashMap(java.util.HashMap) AdminSubjectContext(org.forgerock.openam.rest.resource.AdminSubjectContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext)

Example 3 with UriRouterContext

use of org.forgerock.http.routing.UriRouterContext in project OpenAM by OpenRock.

the class PrivilegeAuthzModule method evaluate.

/**
     * Given the calling context and the privilege definition attempts to authorise the calling subject.
     *
     * @param context
     *         the server context
     * @param definition
     *         the privilege definition
     *
     * @return the authorisation result
     */
protected Promise<AuthorizationResult, ResourceException> evaluate(final Context context, final PrivilegeDefinition definition) {
    // If no realm is specified default to the root realm.
    final String realm = (context.containsContext(RealmContext.class)) ? context.asContext(RealmContext.class).getResolvedRealm() : "/";
    final SubjectContext subjectContext = context.asContext(SubjectContext.class);
    final UriRouterContext routerContext = context.asContext(UriRouterContext.class);
    // Map the set of actions to a set of action strings.
    final Set<String> actions = transformSet(definition.getActions(), ACTION_TO_STRING_MAPPER);
    try {
        Session callerSession = subjectContext.getCallerSession();
        if (callerSession == null) {
            // you don't have a session so return access denied
            return Promises.newResultPromise(AuthorizationResult.accessDenied("No session for request."));
        }
        final String loggedInRealm = coreWrapper.convertOrgNameToRealmName(callerSession.getClientDomain());
        final DelegationPermission permissionRequest = permissionFactory.newInstance(loggedInRealm, REST, VERSION, routerContext.getMatchedUri(), definition.getCommonVerb(), actions, Collections.<String, String>emptyMap());
        if (evaluator.isAllowed(subjectContext.getCallerSSOToken(), permissionRequest, Collections.<String, Set<String>>emptyMap()) && loggedIntoValidRealm(realm, loggedInRealm)) {
            // Authorisation has been approved.
            return Promises.newResultPromise(AuthorizationResult.accessPermitted());
        }
    } catch (DelegationException dE) {
        return new InternalServerErrorException("Attempt to authorise the user has failed", dE).asPromise();
    } catch (SSOException e) {
        //you don't have a user so return access denied
        return Promises.newResultPromise(AuthorizationResult.accessDenied("No user supplied in request."));
    }
    return Promises.newResultPromise(AuthorizationResult.accessDenied("The user has insufficient privileges"));
}
Also used : Set(java.util.Set) CollectionUtils.transformSet(org.forgerock.openam.utils.CollectionUtils.transformSet) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) UriRouterContext(org.forgerock.http.routing.UriRouterContext) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) DelegationException(com.sun.identity.delegation.DelegationException) SSOException(com.iplanet.sso.SSOException) DelegationPermission(com.sun.identity.delegation.DelegationPermission) Session(com.iplanet.dpro.session.Session)

Example 4 with UriRouterContext

use of org.forgerock.http.routing.UriRouterContext in project OpenAM by OpenRock.

the class ContextHelper method getUserId.

/**
     * Gets the username for the user of the accessed resource.
     *
     * @param context The context.
     * @return The resource users username.
     */
public String getUserId(Context context) {
    UriRouterContext routerContext = context.asContext(UriRouterContext.class);
    String userId = routerContext.getUriTemplateVariables().get("user");
    if (userId == null && !routerContext.isRootContext() && routerContext.getParent().containsContext(UriRouterContext.class)) {
        return getUserId(routerContext.getParent());
    }
    return userId;
}
Also used : UriRouterContext(org.forgerock.http.routing.UriRouterContext)

Example 5 with UriRouterContext

use of org.forgerock.http.routing.UriRouterContext in project OpenAM by OpenRock.

the class RealmContextFilter method evaluate.

private Context evaluate(Context context, String hostname, List<String> requestUri, List<String> overrideRealmParameter) throws ResourceException {
    if (!coreWrapper.isValidFQDN(hostname)) {
        throw new BadRequestException("FQDN \"" + hostname + "\" is not valid.");
    }
    SSOToken adminToken = coreWrapper.getAdminToken();
    String dnsAliasRealm = RealmUtils.cleanRealm(getRealmFromAlias(adminToken, hostname));
    StringBuilder matchedUriBuilder = new StringBuilder();
    String currentRealm = dnsAliasRealm;
    int consumedElementsCount = 0;
    for (String element : requestUri) {
        try {
            String subrealm = RealmUtils.cleanRealm(element);
            currentRealm = resolveRealm(adminToken, currentRealm, subrealm);
            matchedUriBuilder.append(subrealm);
            consumedElementsCount++;
        } catch (InternalServerErrorException ignored) {
            break;
        }
    }
    String overrideRealm = null;
    try {
        if (overrideRealmParameter != null && !overrideRealmParameter.isEmpty()) {
            overrideRealm = resolveRealm(adminToken, "/", RealmUtils.cleanRealm(overrideRealmParameter.get(0)));
        }
    } catch (InternalServerErrorException e) {
        throw new BadRequestException("Invalid realm, " + overrideRealmParameter.get(0), e);
    }
    List<String> remainingUri = requestUri.subList(consumedElementsCount, requestUri.size());
    String matchedUri = matchedUriBuilder.length() > 1 ? matchedUriBuilder.substring(1) : matchedUriBuilder.toString();
    RealmContext realmContext = new RealmContext(new UriRouterContext(context, matchedUri, Paths.joinPath(remainingUri), Collections.<String, String>emptyMap()));
    realmContext.setDnsAlias(hostname, dnsAliasRealm);
    realmContext.setSubRealm(matchedUri, RealmUtils.cleanRealm(currentRealm.substring(dnsAliasRealm.length())));
    realmContext.setOverrideRealm(overrideRealm);
    return realmContext;
}
Also used : SSOToken(com.iplanet.sso.SSOToken) UriRouterContext(org.forgerock.http.routing.UriRouterContext) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException)

Aggregations

UriRouterContext (org.forgerock.http.routing.UriRouterContext)8 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)2 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)2 Session (com.iplanet.dpro.session.Session)1 SSOException (com.iplanet.sso.SSOException)1 SSOToken (com.iplanet.sso.SSOToken)1 DelegationException (com.sun.identity.delegation.DelegationException)1 DelegationPermission (com.sun.identity.delegation.DelegationPermission)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 BadRequestException (org.forgerock.json.resource.BadRequestException)1 RealmContext (org.forgerock.openam.rest.RealmContext)1 AdminSubjectContext (org.forgerock.openam.rest.resource.AdminSubjectContext)1 CollectionUtils.transformSet (org.forgerock.openam.utils.CollectionUtils.transformSet)1 RootContext (org.forgerock.services.context.RootContext)1