1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.security.credential;
18
19 import java.util.List;
20
21 import org.opensaml.xml.Configuration;
22 import org.opensaml.xml.XMLObject;
23 import org.opensaml.xml.security.SecurityException;
24 import org.opensaml.xml.security.keyinfo.KeyInfoGenerator;
25 import org.opensaml.xml.security.keyinfo.KeyInfoGeneratorFactory;
26 import org.opensaml.xml.security.keyinfo.KeyInfoHelper;
27 import org.opensaml.xml.signature.KeyInfo;
28 import org.opensaml.xml.signature.impl.KeyInfoBuilder;
29 import org.opensaml.xml.util.DatatypeHelper;
30
31
32
33
34
35
36
37
38 public class BasicKeyInfoGeneratorFactory implements KeyInfoGeneratorFactory {
39
40
41 private BasicOptions options;
42
43
44
45
46
47
48 public BasicKeyInfoGeneratorFactory() {
49 options = newOptions();
50 }
51
52
53 public Class<? extends Credential> getCredentialType() {
54 return Credential.class;
55 }
56
57
58 public boolean handles(Credential credential) {
59
60 return true;
61 }
62
63
64 public KeyInfoGenerator newInstance() {
65
66 BasicOptions newOptions = options.clone();
67 return new BasicKeyInfoGenerator(newOptions);
68 }
69
70
71
72
73
74
75 public boolean emitEntityIDAsKeyName() {
76 return options.emitEntityIDAsKeyName;
77 }
78
79
80
81
82
83
84 public void setEmitEntityIDAsKeyName(boolean newValue) {
85 options.emitEntityIDAsKeyName = newValue;
86 }
87
88
89
90
91
92
93 public boolean emitKeyNames() {
94 return options.emitKeyNames;
95 }
96
97
98
99
100
101
102 public void setEmitKeyNames(boolean newValue) {
103 options.emitKeyNames = newValue;
104 }
105
106
107
108
109
110
111 public boolean emitPublicKeyValue() {
112 return options.emitPublicKeyValue;
113 }
114
115
116
117
118
119
120 public void setEmitPublicKeyValue(boolean newValue) {
121 options.emitPublicKeyValue = newValue;
122
123 }
124
125
126
127
128
129
130
131
132 protected BasicOptions newOptions() {
133 return new BasicOptions();
134 }
135
136
137
138
139
140
141
142 protected BasicOptions getOptions() {
143 return options;
144 }
145
146
147
148
149
150 public class BasicKeyInfoGenerator implements KeyInfoGenerator {
151
152
153 private BasicOptions options;
154
155
156 private KeyInfoBuilder keyInfoBuilder;
157
158
159
160
161
162
163 protected BasicKeyInfoGenerator(BasicOptions newOptions) {
164 options = newOptions;
165 keyInfoBuilder =
166 (KeyInfoBuilder) Configuration.getBuilderFactory().getBuilder(KeyInfo.DEFAULT_ELEMENT_NAME);
167 }
168
169
170 public KeyInfo generate(Credential credential) throws SecurityException {
171 KeyInfo keyInfo = keyInfoBuilder.buildObject();
172
173 processKeyNames(keyInfo, credential);
174 processEntityID(keyInfo, credential);
175 processPublicKey(keyInfo, credential);
176
177 List<XMLObject> children = keyInfo.getOrderedChildren();
178 if (children != null && children.size() > 0) {
179 return keyInfo;
180 } else {
181 return null;
182 }
183 }
184
185
186
187
188
189
190 protected void processKeyNames(KeyInfo keyInfo, Credential credential) {
191 if (options.emitKeyNames) {
192 for (String keyNameValue : credential.getKeyNames()) {
193 if ( ! DatatypeHelper.isEmpty(keyNameValue)) {
194 KeyInfoHelper.addKeyName(keyInfo, keyNameValue);
195 }
196 }
197 }
198 }
199
200
201
202
203
204
205 protected void processEntityID(KeyInfo keyInfo, Credential credential) {
206 if (options.emitEntityIDAsKeyName) {
207 String keyNameValue = credential.getEntityId();
208 if ( ! DatatypeHelper.isEmpty(keyNameValue)) {
209 KeyInfoHelper.addKeyName(keyInfo, keyNameValue);
210 }
211 }
212 }
213
214
215
216
217
218
219 protected void processPublicKey(KeyInfo keyInfo, Credential credential) {
220 if (options.emitPublicKeyValue) {
221 if (credential.getPublicKey() != null) {
222 KeyInfoHelper.addPublicKey(keyInfo, credential.getPublicKey());
223 }
224 }
225 }
226 }
227
228
229
230
231 protected class BasicOptions implements Cloneable {
232
233
234 private boolean emitKeyNames;
235
236
237 private boolean emitEntityIDAsKeyName;
238
239
240 private boolean emitPublicKeyValue;
241
242
243 protected BasicOptions clone() {
244 try {
245 return (BasicOptions) super.clone();
246 } catch (CloneNotSupportedException e) {
247
248 return null;
249 }
250 }
251
252 }
253
254 }