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;
18  
19  import java.security.PrivateKey;
20  import java.security.PublicKey;
21  
22  import javax.crypto.SecretKey;
23  
24  import org.opensaml.xml.util.DatatypeHelper;
25  import org.opensaml.xml.util.LazySet;
26  
27  /**
28   * A basic implementation of {@link Credential}.
29   */
30  public class BasicCredential extends AbstractCredential {
31  
32      /** Constructor. */
33      public BasicCredential() {
34          super();
35          keyNames = new LazySet<String>();
36          usageType = UsageType.UNSPECIFIED;
37      }
38      
39      /** {@inheritDoc} */
40      public Class<? extends Credential> getCredentialType() {
41          return Credential.class;
42      }
43  
44      /**
45       * Sets the ID of the entity this credential is for.
46       * 
47       * @param id ID of the entity this credential is for
48       */
49      public void setEntityId(String id) {
50          entityID = DatatypeHelper.safeTrimOrNullString(id);
51      }
52  
53      /**
54       * Sets the usage type for this credential.
55       * 
56       * @param usage usage type for this credential
57       */
58      public void setUsageType(UsageType usage) {
59          if (usage != null) {
60              usageType = usage;
61          } else {
62              usageType = UsageType.UNSPECIFIED;
63          }
64      }
65  
66      /**
67       * Sets the public key for this credential.
68       * 
69       * @param key public key for this credential
70       */
71      public void setPublicKey(PublicKey key) {
72          publicKey = key;
73          if (key != null) {
74              setSecretKey(null);
75          }
76      }
77  
78      /**
79       * Sets the secret key for this credential.
80       * 
81       * @param key secret key for this credential
82       */
83      public void setSecretKey(SecretKey key) {
84          secretKey = key;
85          if (key != null) {
86              setPublicKey(null);
87              setPrivateKey(null);
88          }
89      }
90  
91      /**
92       * Sets the private key for this credential.
93       * 
94       * @param key private key for this credential
95       */
96      public void setPrivateKey(PrivateKey key) {
97          privateKey = key;
98          if (key != null) {
99              setSecretKey(null);
100         }
101     }
102 }