001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2021, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.openid.connect.sdk.federation.trust.constraints; 019 020 021import java.util.HashSet; 022import java.util.List; 023import java.util.Objects; 024import java.util.Set; 025 026import net.jcip.annotations.Immutable; 027 028import com.nimbusds.oauth2.sdk.ParseException; 029import com.nimbusds.oauth2.sdk.id.Identifier; 030import com.nimbusds.oauth2.sdk.util.CollectionUtils; 031import com.nimbusds.openid.connect.sdk.federation.entities.EntityType; 032 033 034/** 035 * Leaf entity type constraint. 036 * 037 * <p>Related specifications: 038 * 039 * <ul> 040 * <li>OpenID Connect Federation 1.0, section 5.2.3. 041 * </ul> 042 */ 043@Immutable 044public final class LeafEntityTypeConstraint { 045 046 047 /** 048 * Any leaf entity types allowed constant. 049 */ 050 public static final LeafEntityTypeConstraint ANY = new LeafEntityTypeConstraint(null); 051 052 053 /** 054 * The allowed leaf entity types, {@code null} or empty for any. 055 */ 056 private final Set<EntityType> allowed; 057 058 059 /** 060 * Creates a new leaf entity type constraint. 061 * 062 * @param allowed The allowed leaf entity types, {@code null} or empty 063 * for any. 064 */ 065 public LeafEntityTypeConstraint(final Set<EntityType> allowed) { 066 this.allowed = CollectionUtils.isNotEmpty(allowed) ? allowed : null; 067 } 068 069 070 /** 071 * Returns {@code true} if any leaf entity types are allowed. 072 * 073 * @return {@code true} if any leaf entity types are allowed. 074 */ 075 public boolean allowsAny() { 076 return CollectionUtils.isEmpty(allowed); 077 } 078 079 080 /** 081 * Returns the allowed leaf entity types. 082 * 083 * @return The allowed leaf entity types, {@code null} for any. 084 */ 085 public Set<EntityType> getAllowed() { 086 return allowed; 087 } 088 089 090 /** 091 * Returns the allowed leaf entity types as a string list. 092 * 093 * @return The allowed leaf entity types as a string list, {@code null} 094 * for any. 095 */ 096 public List<String> getAllowedAsStringList() { 097 if (allowsAny()) { 098 return null; 099 } 100 return Identifier.toStringList(allowed); 101 } 102 103 104 /** 105 * Returns {@code true} if the specified entity type is allowed for a 106 * leaf entity. 107 * 108 * @param type The entity type. 109 * 110 * @return {@code true} if the entity type is allowed, else 111 * {@code false}. 112 */ 113 public boolean isAllowed(final EntityType type) { 114 return allowsAny() || allowed.contains(type); 115 } 116 117 118 @Override 119 public String toString() { 120 if (allowsAny()) { 121 return "null"; 122 } 123 return allowed.toString(); 124 } 125 126 127 @Override 128 public boolean equals(Object o) { 129 if (this == o) return true; 130 if (!(o instanceof LeafEntityTypeConstraint)) return false; 131 LeafEntityTypeConstraint that = (LeafEntityTypeConstraint) o; 132 return Objects.equals(getAllowed(), that.getAllowed()); 133 } 134 135 136 @Override 137 public int hashCode() { 138 return Objects.hash(getAllowed()); 139 } 140 141 142 /** 143 * Parses a leaf entity type constraint. 144 * 145 * @param values The string values, {@code null} if not specified. 146 * 147 * @return The parsed leaf entity type constraint. 148 * 149 * @throws ParseException If parsing failed. 150 */ 151 public static LeafEntityTypeConstraint parse(final List<String> values) 152 throws ParseException { 153 154 if (CollectionUtils.isEmpty(values)) { 155 return ANY; 156 } 157 158 Set<EntityType> types = new HashSet<>(); 159 160 for (String v: values) { 161 types.add(new EntityType(v)); 162 } 163 164 return new LeafEntityTypeConstraint(types); 165 } 166}