Example #1
0
 public int compareTo(final RegisteredService other) {
   final int result = this.evaluationOrder - other.getEvaluationOrder();
   if (result == 0) {
     return (int) (this.id - other.getId());
   }
   return result;
 }
 /**
  * {@inheritDoc} Compares this instance with the {@code other} registered service based on
  * evaluation order, name. The name comparison is case insensitive.
  *
  * @see #getEvaluationOrder()
  */
 @Override
 public int compareTo(final RegisteredService other) {
   return new CompareToBuilder()
       .append(this.getEvaluationOrder(), other.getEvaluationOrder())
       .append(this.getName().toLowerCase(), other.getName().toLowerCase())
       .append(this.getServiceId(), other.getServiceId())
       .toComparison();
 }
  public RegisteredService findServiceById(final long id) {
    for (final RegisteredService r : this.registeredServices) {
      if (r.getId() == id) {
        return r;
      }
    }

    return null;
  }
  @Test
  public void verifySaveWithReturnedPersistedInstance() {
    final RegisteredServiceImpl r = new RegisteredServiceImpl();
    r.setId(1000L);
    r.setName("test");
    r.setServiceId("test");

    final RegisteredService persistedRs = this.defaultServicesManagerImpl.save(r);
    assertNotNull(persistedRs);
    assertEquals(1000L, persistedRs.getId());
  }
  /**
   * This isn't super-fast but I don't expect thousands of services.
   *
   * @return
   */
  private long findHighestId() {
    long id = 0;

    for (final RegisteredService r : this.registeredServices) {
      if (r.getId() > id) {
        id = r.getId();
      }
    }

    return id;
  }
  @Test
  public void checkSaveMethodWithNonExistentServiceAndNoAttributes() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setName("testSaveMethodWithNonExistentServiceAndNoAttributes");
    r.setServiceId("testId");
    r.setTheme("theme");
    r.setDescription("description");

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());

    assertEquals(r, r2);
    assertEquals(r2, r3);
  }
Example #7
0
 /**
  * Creates a new instance with required parameters.
  *
  * @param service Service principal.
  * @param registeredService Registered service corresponding to given service.
  */
 public ServiceContext(
     @NotNull final Service service, @NotNull final RegisteredService registeredService) {
   this.service = service;
   this.registeredService = registeredService;
   if (!registeredService.matches(service)) {
     throw new IllegalArgumentException("Registered service does not match given service.");
   }
 }
  @Test
  public void verifyAccessStrategyWithEndpoint() throws Exception {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setServiceId("^https://.+");
    r.setName("verifyAccessStrategyWithEndpoint");
    r.setId(62);

    final RemoteEndpointServiceAccessStrategy authz = new RemoteEndpointServiceAccessStrategy();

    authz.setEndpointUrl("http://www.google.com?this=that");
    authz.setAcceptableResponseCodes("200,405,403");
    authz.setUnauthorizedRedirectUrl(new URI("https://www.github.com"));
    r.setAccessStrategy(authz);

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());
    assertEquals(r2, r3);
  }
  public RegisteredService save(final RegisteredService registeredService) {
    if (registeredService.getId() == -1) {
      ((AbstractRegisteredService) registeredService).setId(findHighestId() + 1);
    }

    this.registeredServices.remove(registeredService);
    this.registeredServices.add(registeredService);

    return registeredService;
  }
  @Test
  public void verifyAccessStrategyWithStarEndDate() throws Exception {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setServiceId("^https://.+");
    r.setName("verifyAAccessStrategyWithStarEndDate");
    r.setId(62);

    final TimeBasedRegisteredServiceAccessStrategy authz =
        new TimeBasedRegisteredServiceAccessStrategy(true, false);

    authz.setStartingDateTime(ZonedDateTime.now(ZoneOffset.UTC).plusDays(1).toString());
    authz.setEndingDateTime(ZonedDateTime.now(ZoneOffset.UTC).plusDays(10).toString());

    authz.setUnauthorizedRedirectUrl(new URI("https://www.github.com"));
    r.setAccessStrategy(authz);

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());
    assertEquals(r2, r3);
  }
 @Test
 public void verifySaveMethodWithDefaultAnonymousAttribute() {
   final RegexRegisteredService r = new RegexRegisteredService();
   r.setName("testSaveMethodWithDefaultAnonymousAttribute");
   r.setServiceId("testId");
   r.setTheme("theme");
   r.setDescription("description");
   r.setUsernameAttributeProvider(
       new AnonymousRegisteredServiceUsernameAttributeProvider(
           new ShibbolethCompatiblePersistentIdGenerator("helloworld")));
   final RegisteredService r2 = this.dao.save(r);
   this.dao.load();
   final RegisteredService r3 = this.dao.findServiceById(r2.getId());
   final AnonymousRegisteredServiceUsernameAttributeProvider anon =
       (AnonymousRegisteredServiceUsernameAttributeProvider) r3.getUsernameAttributeProvider();
   final ShibbolethCompatiblePersistentIdGenerator ss =
       (ShibbolethCompatiblePersistentIdGenerator) anon.getPersistentIdGenerator();
   assertEquals(new String(ss.getSalt()), "helloworld");
   assertEquals(r2, r3);
 }
  @Test
  public void checkForAuthorizationStrategy() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setServiceId("^https://.+");
    r.setName("checkForAuthorizationStrategy");
    r.setId(42);

    final DefaultRegisteredServiceAccessStrategy authz =
        new DefaultRegisteredServiceAccessStrategy(false, false);

    final Map<String, Set<String>> attrs = new HashMap<>();
    attrs.put("cn", Sets.newHashSet("v1, v2, v3"));
    attrs.put("memberOf", Sets.newHashSet(Lists.newArrayList("v4, v5, v6")));
    authz.setRequiredAttributes(attrs);
    r.setAccessStrategy(authz);

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());
    assertEquals(r2, r3);
  }
  @Test
  public void verifySaveAttributeReleasePolicyAllowedAttrRulesAndFilter() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setName("testSaveAttributeReleasePolicyAllowedAttrRulesAndFilter");
    r.setServiceId("testId");
    r.setTheme("testtheme");
    r.setEvaluationOrder(1000);
    r.setAccessStrategy(new DefaultRegisteredServiceAccessStrategy(true, false));
    r.setProxyPolicy(new RegexMatchingRegisteredServiceProxyPolicy("https://.+"));
    r.setRequiredHandlers(new HashSet<>(Lists.newArrayList("h1", "h2")));

    final ReturnAllowedAttributeReleasePolicy policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAllowedAttributes(Lists.newArrayList("1", "2", "3"));
    r.setAttributeReleasePolicy(policy);
    r.getAttributeReleasePolicy()
        .setAttributeFilter(new RegisteredServiceRegexAttributeFilter("\\w+"));

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());

    assertEquals(r, r2);
    assertEquals(r2, r3);
    assertNotNull(r3.getAttributeReleasePolicy());
    assertEquals(r2.getAttributeReleasePolicy(), r3.getAttributeReleasePolicy());
  }
  @Test
  public void verifySaveAttributeReleasePolicy() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setName("testSaveAttributeReleasePolicy");
    r.setServiceId("testId");
    r.setTheme("theme");
    r.setDescription("description");
    r.setAttributeReleasePolicy(new ReturnAllAttributeReleasePolicy());

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());

    assertEquals(r, r2);
    assertEquals(r2, r3);
    assertNotNull(r3.getAttributeReleasePolicy());
    assertEquals(r2.getAttributeReleasePolicy(), r3.getAttributeReleasePolicy());
  }
  @Test
  public void verifySaveAttributeReleasePolicyAllowedAttrRules() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setName("testSaveAttributeReleasePolicyAllowedAttrRules");
    r.setServiceId("testId");

    final ReturnAllowedAttributeReleasePolicy policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAllowedAttributes(Lists.newArrayList("1", "2", "3"));
    r.setAttributeReleasePolicy(policy);

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());

    assertEquals(r, r2);
    assertEquals(r2, r3);
    assertNotNull(r3.getAttributeReleasePolicy());
    assertEquals(r2.getAttributeReleasePolicy(), r3.getAttributeReleasePolicy());
  }
  @Test
  public void verifySaveAttributeReleasePolicyMappingRules() {
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setName("testSaveAttributeReleasePolicyMappingRules");
    r.setServiceId("testId");

    final Map<String, String> map = new HashMap<>();
    map.put("attr1", "newattr1");
    map.put("attr2", "newattr2");
    map.put("attr2", "newattr3");

    final ReturnMappedAttributeReleasePolicy policy = new ReturnMappedAttributeReleasePolicy();
    policy.setAllowedAttributes(map);
    r.setAttributeReleasePolicy(policy);

    final RegisteredService r2 = this.dao.save(r);
    final RegisteredService r3 = this.dao.findServiceById(r2.getId());

    assertEquals(r, r2);
    assertEquals(r2, r3);
    assertNotNull(r3.getAttributeReleasePolicy());
    assertEquals(r2.getAttributeReleasePolicy(), r3.getAttributeReleasePolicy());
  }
 /**
  * Copies the properties of the source service into this instance.
  *
  * @param source Source service from which to copy properties.
  */
 public void copyFrom(final RegisteredService source) {
   this.setId(source.getId());
   this.setAllowedAttributes(new ArrayList<String>(source.getAllowedAttributes()));
   this.setAllowedToProxy(source.isAllowedToProxy());
   this.setDescription(source.getDescription());
   this.setEnabled(source.isEnabled());
   this.setName(source.getName());
   this.setServiceId(source.getServiceId());
   this.setSsoEnabled(source.isSsoEnabled());
   this.setTheme(source.getTheme());
   this.setAnonymousAccess(source.isAnonymousAccess());
   this.setIgnoreAttributes(source.isIgnoreAttributes());
   this.setEvaluationOrder(source.getEvaluationOrder());
   this.setUsernameAttribute(source.getUsernameAttribute());
   this.setLogoutType(source.getLogoutType());
 }
 /**
  * Copies the properties of the source service into this instance.
  *
  * @param source Source service from which to copy properties.
  */
 public void copyFrom(final RegisteredService source) {
   this.setId(source.getId());
   this.setProxyPolicy(source.getProxyPolicy());
   this.setDescription(source.getDescription());
   this.setName(source.getName());
   this.setServiceId(source.getServiceId());
   this.setTheme(source.getTheme());
   this.setEvaluationOrder(source.getEvaluationOrder());
   this.setUsernameAttributeProvider(source.getUsernameAttributeProvider());
   this.setLogoutType(source.getLogoutType());
   this.setAttributeReleasePolicy(source.getAttributeReleasePolicy());
   this.setAccessStrategy(source.getAccessStrategy());
   this.setLogo(source.getLogo());
   this.setLogoutUrl(source.getLogoutUrl());
   this.setPublicKey(source.getPublicKey());
   this.setRequiredHandlers(source.getRequiredHandlers());
   this.setProperties(source.getProperties());
 }