View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.security.credential.criteria;
18  
19  import java.security.PublicKey;
20  
21  import org.opensaml.xml.security.credential.Credential;
22  import org.opensaml.xml.security.criteria.PublicKeyCriteria;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Instance of evaluable credential criteria for evaluating whether a credential contains a particular
28   * public key.
29   */
30  public class EvaluablePublicKeyCredentialCriteria implements EvaluableCredentialCriteria {
31      
32      /** Logger. */
33      private final Logger log = LoggerFactory.getLogger(EvaluablePublicKeyCredentialCriteria.class);
34      
35      /** Base criteria. */
36      private PublicKey publicKey;
37      
38      /**
39       * Constructor.
40       *
41       * @param criteria the criteria which is the basis for evaluation
42       */
43      public EvaluablePublicKeyCredentialCriteria(PublicKeyCriteria criteria) {
44          if (criteria == null) {
45              throw new NullPointerException("Criteria instance may not be null");
46          }
47          publicKey = criteria.getPublicKey();
48      }
49      
50      /**
51       * Constructor.
52       *
53       * @param newPublicKey the criteria value which is the basis for evaluation
54       */
55      public EvaluablePublicKeyCredentialCriteria(PublicKey newPublicKey) {
56          if (newPublicKey == null) {
57              throw new IllegalArgumentException("Public key may not be null");
58          }
59          publicKey = newPublicKey;
60      }
61  
62      /** {@inheritDoc} */
63      public Boolean evaluate(Credential target) {
64          if (target == null) {
65              log.error("Credential target was null");
66              return null;
67          }
68          PublicKey key = target.getPublicKey();
69          if (key == null) {
70              log.info("Credential contained no public key, does not satisfy public key criteria");
71              return Boolean.FALSE;
72          }
73          
74          Boolean result = publicKey.equals(key);
75          return result;
76      }
77  
78  }