1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.protocol;
21  
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.security.auth.kerberos.KerberosPrincipal;
27  
28  import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
29  import org.apache.directory.server.kerberos.shared.crypto.encryption.KerberosKeyFactory;
30  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
31  import org.apache.directory.server.kerberos.shared.store.PrincipalStore;
32  import org.apache.directory.server.kerberos.shared.store.PrincipalStoreEntry;
33  import org.apache.directory.server.kerberos.shared.store.PrincipalStoreEntryModifier;
34  
35  
36  /**
37   * An implementation of {@link PrincipalStore} that is backed by a {@link Map}.  This
38   * store implements only getPrincipal, as required by the Kerberos service.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev$, $Date$
42   */
43  public class MapPrincipalStoreImpl implements PrincipalStore
44  {
45      private static Map<KerberosPrincipal, PrincipalStoreEntry> store = new HashMap<KerberosPrincipal, PrincipalStoreEntry>();
46  
47      static
48      {
49          String principalName = "hnelson@EXAMPLE.COM";
50          String passPhrase = "secret";
51  
52          PrincipalStoreEntry entry = getEntry( principalName, passPhrase );
53          store.put( entry.getPrincipal(), entry );
54  
55          principalName = "tquist@EXAMPLE.COM";
56          passPhrase = "secret";
57  
58          entry = getNullKeyEntry( principalName );
59          store.put( entry.getPrincipal(), entry );
60  
61          principalName = "krbtgt/EXAMPLE.COM@EXAMPLE.COM";
62          passPhrase = "randomKey";
63  
64          entry = getEntry( principalName, passPhrase );
65          store.put( entry.getPrincipal(), entry );
66  
67          principalName = "ldap/ldap.example.com@EXAMPLE.COM";
68          passPhrase = "randomKey";
69  
70          entry = getEntry( principalName, passPhrase );
71          store.put( entry.getPrincipal(), entry );
72      }
73  
74  
75      public PrincipalStoreEntry getPrincipal( KerberosPrincipal principal ) throws Exception
76      {
77          PrincipalStoreEntry entry = store.get( principal );
78  
79          return entry;
80      }
81  
82  
83      public String addPrincipal( PrincipalStoreEntry entry ) throws Exception
84      {
85          return null;
86      }
87  
88  
89      public String changePassword( KerberosPrincipal principal, String newPassword ) throws Exception
90      {
91          return null;
92      }
93  
94  
95      public String deletePrincipal( KerberosPrincipal principal ) throws Exception
96      {
97          return null;
98      }
99  
100 
101     public PrincipalStoreEntry[] getAllPrincipals( String realm ) throws Exception
102     {
103         return null;
104     }
105 
106 
107     private static PrincipalStoreEntry getEntry( String principalName, String passPhrase )
108     {
109         KerberosPrincipal clientPrincipal = new KerberosPrincipal( principalName );
110 
111         PrincipalStoreEntryModifier modifier = new PrincipalStoreEntryModifier();
112         modifier.setPrincipal( clientPrincipal );
113 
114         Map<EncryptionType, EncryptionKey> keyMap = KerberosKeyFactory.getKerberosKeys( principalName, passPhrase );
115 
116         modifier.setKeyMap( keyMap );
117 
118         return modifier.getEntry();
119     }
120 
121 
122     private static PrincipalStoreEntry getNullKeyEntry( String principalName )
123     {
124         KerberosPrincipal clientPrincipal = new KerberosPrincipal( principalName );
125 
126         PrincipalStoreEntryModifier modifier = new PrincipalStoreEntryModifier();
127         modifier.setPrincipal( clientPrincipal );
128 
129         return modifier.getEntry();
130     }
131 }