1 package org.apache.archiva.redback.keys.memory; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.archiva.redback.keys.AbstractKeyManager; 23 import org.apache.archiva.redback.keys.AuthenticationKey; 24 import org.apache.archiva.redback.keys.KeyManagerException; 25 import org.apache.archiva.redback.keys.KeyNotFoundException; 26 import org.apache.commons.lang.StringUtils; 27 import org.springframework.stereotype.Service; 28 29 import java.util.ArrayList; 30 import java.util.Calendar; 31 import java.util.Date; 32 import java.util.HashMap; 33 import java.util.List; 34 import java.util.Map; 35 36 /** 37 * KeyManager backed by an in-memory only store. 38 * 39 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 40 * 41 */ 42 @Service("keyManager#memory") 43 public class MemoryKeyManager 44 extends AbstractKeyManager 45 { 46 private Map<String, AuthenticationKey> keys = new HashMap<String, AuthenticationKey>(); 47 48 public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes ) 49 throws KeyManagerException 50 { 51 AuthenticationKey key = new MemoryAuthenticationKey(); 52 key.setKey( super.generateUUID() ); 53 key.setForPrincipal( principal ); 54 key.setPurpose( purpose ); 55 key.setDateCreated( new Date() ); 56 57 if ( expirationMinutes >= 0 ) 58 { 59 Calendar expiration = Calendar.getInstance(); 60 expiration.add( Calendar.MINUTE, expirationMinutes ); 61 key.setDateExpires( expiration.getTime() ); 62 } 63 64 keys.put( key.getKey(), key ); 65 66 return key; 67 } 68 69 public AuthenticationKey findKey( String key ) 70 throws KeyNotFoundException, KeyManagerException 71 { 72 if ( StringUtils.isEmpty( key ) ) 73 { 74 throw new KeyNotFoundException( "Empty key not found." ); 75 } 76 77 AuthenticationKey authkey = keys.get( key ); 78 79 if ( authkey == null ) 80 { 81 throw new KeyNotFoundException( "Key [" + key + "] not found." ); 82 } 83 84 assertNotExpired( authkey ); 85 86 return authkey; 87 } 88 89 public void deleteKey( AuthenticationKey authkey ) 90 throws KeyManagerException 91 { 92 keys.remove( authkey ); 93 } 94 95 public void deleteKey( String key ) 96 throws KeyManagerException 97 { 98 AuthenticationKey authkey = keys.get( key ); 99 if ( authkey != null ) 100 { 101 keys.remove( authkey ); 102 } 103 } 104 105 public List<AuthenticationKey> getAllKeys() 106 { 107 return new ArrayList<AuthenticationKey>( keys.values() ); 108 } 109 110 public AuthenticationKey addKey( AuthenticationKey key ) 111 { 112 keys.put( key.getKey(), key ); 113 return key; 114 } 115 116 public void eraseDatabase() 117 { 118 keys.clear(); 119 } 120 121 public String getId() 122 { 123 return "Memory Key Manager"; 124 } 125 }