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.changepw.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 both getPrincipal and changePassword, as required by the Set/Change Password
39   * service.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev$, $Date$
43   */
44  public class MapPrincipalStoreImpl implements PrincipalStore
45  {
46      private static Map<KerberosPrincipal, PrincipalStoreEntry> store = new HashMap<KerberosPrincipal, PrincipalStoreEntry>();
47  
48      static
49      {
50          String principalName = "hnelson@EXAMPLE.COM";
51          String passPhrase = "secret";
52  
53          PrincipalStoreEntry entry = getEntry( principalName, passPhrase );
54  
55          store.put( entry.getPrincipal(), entry );
56  
57          principalName = "kadmin/changepw@EXAMPLE.COM";
58          passPhrase = "secret";
59  
60          entry = getEntry( principalName, passPhrase );
61  
62          store.put( entry.getPrincipal(), entry );
63      }
64  
65  
66      public PrincipalStoreEntry getPrincipal( KerberosPrincipal principal ) throws Exception
67      {
68          PrincipalStoreEntry entry = store.get( principal );
69  
70          return entry;
71      }
72  
73  
74      public String changePassword( KerberosPrincipal principal, String newPassword ) throws Exception
75      {
76          return principal.getName();
77      }
78  
79  
80      public String addPrincipal( PrincipalStoreEntry entry ) throws Exception
81      {
82          return null;
83      }
84  
85  
86      public String deletePrincipal( KerberosPrincipal principal ) throws Exception
87      {
88          return null;
89      }
90  
91  
92      public PrincipalStoreEntry[] getAllPrincipals( String realm ) throws Exception
93      {
94          return null;
95      }
96  
97  
98      private static PrincipalStoreEntry getEntry( String principalName, String passPhrase )
99      {
100         KerberosPrincipal clientPrincipal = new KerberosPrincipal( principalName );
101 
102         PrincipalStoreEntryModifier modifier = new PrincipalStoreEntryModifier();
103         modifier.setPrincipal( clientPrincipal );
104 
105         Map<EncryptionType, EncryptionKey> keyMap = KerberosKeyFactory.getKerberosKeys( principalName, passPhrase );
106 
107         modifier.setKeyMap( keyMap );
108 
109         return modifier.getEntry();
110     }
111 }