001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.core.server.command; 018 019import com.echothree.control.user.core.common.form.DecryptForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.util.common.command.BaseResult; 022import com.echothree.util.common.exception.DecryptionBadPaddingException; 023import com.echothree.util.common.exception.UnknownEntityEncryptionKeyException; 024import com.echothree.util.common.message.ExecutionErrors; 025import com.echothree.util.common.validation.FieldDefinition; 026import com.echothree.util.common.validation.FieldType; 027import com.echothree.util.server.control.BaseSimpleCommand; 028import com.echothree.util.server.persistence.EncryptionUtils; 029import java.util.List; 030import javax.enterprise.context.Dependent; 031import javax.inject.Inject; 032 033@Dependent 034public class DecryptCommand 035 extends BaseSimpleCommand<DecryptForm> { 036 037 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 038 039 static { 040 FORM_FIELD_DEFINITIONS = List.of( 041 new FieldDefinition("EntityEncryptionKeyName", FieldType.ENTITY_NAME, true, null, null), 042 new FieldDefinition("InitializationVector", FieldType.INITIALIZATION_VECTOR, true, null, null), 043 new FieldDefinition("Value", FieldType.STRING, true, null, null) 044 ); 045 } 046 047 @Inject 048 EncryptionUtils encryptionUtils; 049 050 /** Creates a new instance of DecryptCommand */ 051 public DecryptCommand() { 052 super(null, FORM_FIELD_DEFINITIONS, false); 053 } 054 055 @Override 056 protected BaseResult execute() { 057 var result = CoreResultFactory.getDecryptResult(); 058 var entityEncryptionKeyName = form.getEntityEncryptionKeyName(); 059 var initializationVector = form.getInitializationVector(); 060 var value = form.getValue(); 061 062 try { 063 result.setValue(encryptionUtils.decrypt(entityEncryptionKeyName, initializationVector, true, value)); 064 } catch(UnknownEntityEncryptionKeyException e) { 065 addExecutionError(ExecutionErrors.UnknownEntityEncryptionKeyName.name(), entityEncryptionKeyName); 066 } catch(DecryptionBadPaddingException e) { 067 addExecutionError(ExecutionErrors.UnableToDecryptValue.name(), entityEncryptionKeyName); 068 } 069 070 return result; 071 } 072 073}