001package org.apache.archiva.redback.keys.memory; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.redback.keys.AbstractKeyManager; 023import org.apache.archiva.redback.keys.AuthenticationKey; 024import org.apache.archiva.redback.keys.KeyManagerException; 025import org.apache.archiva.redback.keys.KeyNotFoundException; 026import org.apache.commons.lang3.StringUtils; 027import org.springframework.stereotype.Service; 028 029import java.util.ArrayList; 030import java.util.Calendar; 031import java.util.Date; 032import java.util.HashMap; 033import java.util.List; 034import java.util.Map; 035 036/** 037 * KeyManager backed by an in-memory only store. 038 * 039 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 040 * 041 */ 042@Service("keyManager#memory") 043public class MemoryKeyManager 044 extends AbstractKeyManager 045{ 046 private Map<String, AuthenticationKey> keys = new HashMap<String, AuthenticationKey>(); 047 048 public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes ) 049 throws KeyManagerException 050 { 051 AuthenticationKey key = new MemoryAuthenticationKey(); 052 key.setKey( super.generateUUID() ); 053 key.setForPrincipal( principal ); 054 key.setPurpose( purpose ); 055 key.setDateCreated( new Date() ); 056 057 if ( expirationMinutes >= 0 ) 058 { 059 Calendar expiration = Calendar.getInstance(); 060 expiration.add( Calendar.MINUTE, expirationMinutes ); 061 key.setDateExpires( expiration.getTime() ); 062 } 063 064 keys.put( key.getKey(), key ); 065 066 return key; 067 } 068 069 public AuthenticationKey findKey( String key ) 070 throws KeyNotFoundException, KeyManagerException 071 { 072 if ( StringUtils.isEmpty( key ) ) 073 { 074 throw new KeyNotFoundException( "Empty key not found." ); 075 } 076 077 AuthenticationKey authkey = keys.get( key ); 078 079 if ( authkey == null ) 080 { 081 throw new KeyNotFoundException( "Key [" + key + "] not found." ); 082 } 083 084 assertNotExpired( authkey ); 085 086 return authkey; 087 } 088 089 public void deleteKey( AuthenticationKey authkey ) 090 throws KeyManagerException 091 { 092 keys.remove( authkey ); 093 } 094 095 public void deleteKey( String key ) 096 throws KeyManagerException 097 { 098 AuthenticationKey authkey = keys.get( key ); 099 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}