All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.security.ams.capsupport.AmsRuntimeConfiguration Maven / Gradle / Ivy

Go to download

Client Library for integrating SAP CAP applications with SAP Authorization Management Service (AMS)

The newest version!
/************************************************************************
 * © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cloud.security.ams.capsupport;

import com.sap.cds.services.runtime.CdsRuntimeConfiguration;
import com.sap.cds.services.runtime.CdsRuntimeConfigurer;
import com.sap.cloud.security.ams.dcl.client.factory.DclFactoryBase;
import com.sap.cloud.security.ams.dcl.client.pdp.PolicyDecisionPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * AMS runtime configuration adding the AMS {@link AmsUserInfoProvider} and the {@link
 * AmsAuthorizationHandler}.
 */
public class AmsRuntimeConfiguration implements CdsRuntimeConfiguration {

  private static final Logger logger = LoggerFactory.getLogger(AmsRuntimeConfiguration.class);

  /** Creates a new AmsRuntimeConfiguration with a default {@link PolicyDecisionPoint}. */
  public AmsRuntimeConfiguration() {
    this(PolicyDecisionPoint.create(DclFactoryBase.DEFAULT_KIND));
  }

  /**
   * Creates a new AmsRuntimeConfiguration with a custom {@link PolicyDecisionPoint}.
   *
   * @param pdp the PolicyDecisionPoint to use
   */
  public AmsRuntimeConfiguration(PolicyDecisionPoint pdp) {
    this.pdp = pdp;
  }

  private final PolicyDecisionPoint pdp;

  @Override
  public void providers(CdsRuntimeConfigurer configurer) {
    var cdsRuntime = configurer.getCdsRuntime();
    var cdsEnv = cdsRuntime.getEnvironment();
    var maxSize =
        cdsEnv.getProperty(
            AmsUserInfoProvider.PROPERTY_ROLES_CACHE_MAX_SIZE,
            Integer.class,
            AmsUserInfoProvider.PROPERTY_ROLES_CACHE_MAX_SIZE_DEFAULT);
    var expirationTimeSeconds =
        cdsEnv.getProperty(
            AmsUserInfoProvider.PROPERTY_ROLES_CACHE_EXPIRATION_TIME,
            Integer.class,
            AmsUserInfoProvider.PROPERTY_ROLES_CACHE_EXPIRATION_TIME_DEFAULT);
    configurer.provider(new AmsUserInfoProvider(pdp, maxSize, expirationTimeSeconds));
    logger.info("Configured AmsUserInfoProvider");
  }

  /**
   * Returning an order ensures the AMS configuration is done before others so that the handlers are
   * called correctly.
   *
   * @return the order of this configuration
   */
  @Override
  public int order() {
    return 25;
  }

  @Override
  public void eventHandlers(CdsRuntimeConfigurer configurer) {
    configurer.eventHandler(new AmsAuthorizationHandler(pdp));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy