James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
245836 1 /*
JM 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.gitblit.transport.ssh;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
241f57 23 import com.google.inject.Inject;
JM 24
245836 25 /**
JM 26  * Memory public key manager.
27  *
28  * @author James Moger
29  *
30  */
31 public class MemoryKeyManager extends IPublicKeyManager {
32
521cb6 33     final Map<String, List<SshKey>> keys;
245836 34
241f57 35     @Inject
245836 36     public MemoryKeyManager() {
bcc8a0 37         keys = new HashMap<String, List<SshKey>>();
245836 38     }
JM 39
40     @Override
41     public String toString() {
42         return getClass().getSimpleName();
43     }
44
45     @Override
46     public MemoryKeyManager start() {
47         log.info(toString());
48         return this;
49     }
50
51     @Override
52     public boolean isReady() {
53         return true;
54     }
55
56     @Override
57     public MemoryKeyManager stop() {
58         return this;
59     }
60
61     @Override
62     protected boolean isStale(String username) {
521cb6 63         // always return true so we gets keys from our hashmap
JM 64         return true;
245836 65     }
JM 66
67     @Override
bcc8a0 68     protected List<SshKey> getKeysImpl(String username) {
245836 69         String id = username.toLowerCase();
JM 70         if (keys.containsKey(id)) {
71             return keys.get(id);
72         }
73         return null;
74     }
75
76     @Override
bcc8a0 77     public boolean addKey(String username, SshKey key) {
JM 78         String id = username.toLowerCase();
79         if (!keys.containsKey(id)) {
80             keys.put(id, new ArrayList<SshKey>());
81         }
521cb6 82         log.info("added {} key {}", username, key.getFingerprint());
bcc8a0 83         return keys.get(id).add(key);
245836 84     }
JM 85
86     @Override
bcc8a0 87     public boolean removeKey(String username, SshKey key) {
JM 88         String id = username.toLowerCase();
89         if (!keys.containsKey(id)) {
521cb6 90             log.info("can't remove keys for {}", username);
bcc8a0 91             return false;
JM 92         }
521cb6 93         List<SshKey> list = keys.get(id);
JM 94         boolean success = list.remove(key);
95         if (success) {
96             log.info("removed {} key {}", username, key.getFingerprint());
97         }
98
99         if (list.isEmpty()) {
100             keys.remove(id);
101             log.info("no {} keys left, removed {}", username, username);
102         }
103         return success;
245836 104     }
JM 105
106     @Override
107     public boolean removeAllKeys(String username) {
108         String id = username.toLowerCase();
109         keys.remove(id.toLowerCase());
521cb6 110         log.info("removed all keys for {}", username);
245836 111         return true;
JM 112     }
113 }