1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package edu.internet2.middleware.shibboleth.wayf.plugins.provider;
18
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.opensaml.saml2.common.Extensions;
23 import org.opensaml.saml2.metadata.EntitiesDescriptor;
24 import org.opensaml.saml2.metadata.EntityDescriptor;
25 import org.opensaml.saml2.metadata.RoleDescriptor;
26 import org.opensaml.saml2.metadata.SPSSODescriptor;
27 import org.opensaml.saml2.metadata.provider.FilterException;
28 import org.opensaml.saml2.metadata.provider.MetadataFilter;
29 import org.opensaml.xml.XMLObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import edu.internet2.middleware.shibboleth.wayf.DiscoveryResponseImpl;
34 import edu.internet2.middleware.shibboleth.wayf.HandlerConfig;
35
36
37
38
39
40
41
42
43 public class BindingFilter implements MetadataFilter {
44
45
46
47
48 private static final Logger LOG = LoggerFactory.getLogger(BindingFilter.class.getName());
49
50
51
52
53 private final boolean warnOnFailure;
54
55
56
57
58 private BindingFilter() {
59 this.warnOnFailure = false;
60 }
61
62
63
64
65
66 public BindingFilter(boolean warn) {
67 this.warnOnFailure = warn;
68 }
69
70
71
72
73
74
75
76 public void doFilter(XMLObject metadata) throws FilterException {
77
78 if (metadata instanceof EntitiesDescriptor) {
79
80 checkEntities((EntitiesDescriptor) metadata);
81
82 } else if (metadata instanceof EntityDescriptor) {
83 EntityDescriptor entity = (EntityDescriptor) metadata;
84
85 if (!checkEntity(entity)) {
86 if (warnOnFailure) {
87 LOG.warn("Badly formatted binding for " + entity.getEntityID());
88 } else {
89 LOG.error("Badly formatted binding for top level entity " + entity.getEntityID());
90 }
91 }
92 }
93 }
94
95
96
97
98
99
100
101
102 private static boolean checkEntity(EntityDescriptor entity) {
103 List<RoleDescriptor> roles = entity.getRoleDescriptors();
104
105 for (RoleDescriptor role:roles) {
106
107
108
109
110 if (role instanceof SPSSODescriptor) {
111
112
113
114
115
116 Extensions exts = role.getExtensions();
117 if (exts != null) {
118
119
120
121 List<XMLObject> children = exts.getOrderedChildren();
122
123 for (XMLObject obj : children) {
124 if (obj instanceof DiscoveryResponseImpl) {
125
126
127
128 DiscoveryResponseImpl ds = (DiscoveryResponseImpl) obj;
129 String binding = ds.getBinding();
130
131 if (!DiscoveryResponseImpl.METADATA_NS.equals(binding)) {
132 return false;
133 }
134 }
135 }
136 }
137 }
138 }
139 return true;
140 }
141
142
143
144
145
146
147
148 private void checkEntities(EntitiesDescriptor entities) {
149 List<EntitiesDescriptor> childEntities = entities.getEntitiesDescriptors();
150 List<EntityDescriptor> children = entities.getEntityDescriptors();
151
152 if (children != null) {
153 Iterator<EntityDescriptor> itr;
154 EntityDescriptor entity;
155 itr = children.iterator();
156
157 while (itr.hasNext()) {
158 entity = itr.next();
159 if (!checkEntity(entity)) {
160 if (warnOnFailure) {
161 LOG.warn("Badly formatted binding for " + entity.getEntityID());
162 } else {
163 LOG.error("Badly formatted binding for " + entity.getEntityID() + ". Entity has been removed");
164 itr.remove();
165 }
166 }
167 }
168 }
169
170 if (childEntities != null) {
171 for (EntitiesDescriptor descriptor : childEntities) {
172 checkEntities(descriptor);
173 }
174 }
175 }
176 }