2222import java .util .HashMap ;
2323import java .util .Map ;
2424import java .util .StringTokenizer ;
25+ import java .util .stream .Collectors ;
2526
2627import org .codehaus .plexus .components .cipher .PlexusCipher ;
2728import org .codehaus .plexus .components .cipher .PlexusCipherException ;
3738@ Singleton
3839@ Named
3940public class DefaultSecDispatcher implements SecDispatcher {
40- public static final String TYPE_ATTR = "type" ;
41- public static final char ATTR_START = '[' ;
42- public static final char ATTR_STOP = ']' ;
41+ public static final String ATTR_START = "[" ;
42+ public static final String ATTR_STOP = "]" ;
4343
4444 protected final PlexusCipher cipher ;
4545 protected final Map <String , MasterPasswordSource > masterPasswordSources ;
46- protected final Map <String , PasswordDecryptor > decryptors ;
47- protected String configurationFile ;
46+ protected final Map <String , Dispatcher > dispatchers ;
47+ protected final String configurationFile ;
4848
4949 @ Inject
5050 public DefaultSecDispatcher (
5151 PlexusCipher cipher ,
5252 Map <String , MasterPasswordSource > masterPasswordSources ,
53- Map <String , PasswordDecryptor > decryptors ,
53+ Map <String , Dispatcher > dispatchers ,
5454 @ Named ("${configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String configurationFile ) {
55- this .cipher = cipher ;
56- this .masterPasswordSources = masterPasswordSources ;
57- this .decryptors = decryptors ;
58- this .configurationFile = configurationFile ;
55+ this .cipher = requireNonNull ( cipher ) ;
56+ this .masterPasswordSources = requireNonNull ( masterPasswordSources ) ;
57+ this .dispatchers = requireNonNull ( dispatchers ) ;
58+ this .configurationFile = requireNonNull ( configurationFile ) ;
5959 }
6060
6161 // ---------------------------------------------------------------
6262
6363 @ Override
64- public String decrypt (String str ) throws SecDispatcherException {
65- if (!isEncryptedString (str )) return str ;
66-
67- String bare ;
64+ public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException {
65+ if (isEncryptedString (str )) return str ;
6866
6967 try {
70- bare = cipher .unDecorate (str );
71-
72- Map <String , String > attr = stripAttributes (bare );
73-
7468 String res ;
75-
7669 SettingsSecurity sec = getSec ();
77-
78- if (attr == null || attr .get ("type" ) == null ) {
70+ if (attr == null || attr .get (TYPE_ATTR ) == null ) {
7971 String master = getMaster (sec );
80-
81- res = cipher .decrypt (bare , master );
72+ res = cipher .encrypt (str , master );
8273 } else {
8374 String type = attr .get (TYPE_ATTR );
84-
85- if (decryptors == null )
86- throw new SecDispatcherException (
87- "plexus container did not supply any required dispatchers - cannot lookup " + type );
88-
8975 Map <String , String > conf = SecUtil .getConfig (sec , type );
76+ Dispatcher dispatcher = dispatchers .get (type );
77+ if (dispatcher == null ) throw new SecDispatcherException ("no dispatcher for type " + type );
78+ res = dispatcher .encrypt (str , attr , conf );
79+ res += ATTR_START
80+ + attr .entrySet ().stream ()
81+ .map (e -> e .getKey () + "=" + e .getValue ())
82+ .collect (Collectors .joining ("," ))
83+ + ATTR_STOP ;
84+ }
85+ return cipher .decorate (res );
86+ } catch (PlexusCipherException e ) {
87+ throw new SecDispatcherException (e .getMessage (), e );
88+ }
89+ }
9090
91- PasswordDecryptor dispatcher = decryptors .get (type );
92-
93- if (dispatcher == null ) throw new SecDispatcherException ("no dispatcher for hint " + type );
94-
91+ @ Override
92+ public String decrypt (String str ) throws SecDispatcherException {
93+ if (!isEncryptedString (str )) return str ;
94+ try {
95+ String bare = cipher .unDecorate (str );
96+ Map <String , String > attr = stripAttributes (bare );
97+ SettingsSecurity sec = getSec ();
98+ if (attr == null || attr .get (TYPE_ATTR ) == null ) {
99+ String master = getMaster (sec );
100+ return cipher .decrypt (bare , master );
101+ } else {
102+ String type = attr .get (TYPE_ATTR );
103+ Map <String , String > conf = SecUtil .getConfig (sec , type );
104+ Dispatcher dispatcher = dispatchers .get (type );
105+ if (dispatcher == null ) throw new SecDispatcherException ("no dispatcher for type " + type );
95106 String pass = strip (bare );
96-
97107 return dispatcher .decrypt (pass , attr , conf );
98108 }
99-
100- return res ;
101109 } catch (PlexusCipherException e ) {
102110 throw new SecDispatcherException (e .getMessage (), e );
103111 }
104112 }
105113
106114 private String strip (String str ) {
107115 int pos = str .indexOf (ATTR_STOP );
108-
109116 if (pos != -1 ) return str .substring (pos + 1 );
110-
111117 return str ;
112118 }
113119
@@ -151,7 +157,6 @@ private Map<String, String> stripAttributes(String str) {
151157
152158 private boolean isEncryptedString (String str ) {
153159 if (str == null ) return false ;
154-
155160 return cipher .isEncryptedString (str );
156161 }
157162
@@ -171,8 +176,6 @@ private SettingsSecurity getSec() throws SecDispatcherException {
171176 return sec ;
172177 }
173178
174- // ----------------------------------------------------------------------------
175-
176179 private String getMaster (SettingsSecurity sec ) throws SecDispatcherException {
177180 String masterSource = requireNonNull (sec .getMasterSource (), "masterSource is null" );
178181 try {
@@ -186,36 +189,8 @@ private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
186189 }
187190 throw new SecDispatcherException ("master password could not be fetched" );
188191 }
189- // ---------------------------------------------------------------
192+
190193 public String getConfigurationFile () {
191194 return configurationFile ;
192195 }
193-
194- public void setConfigurationFile (String file ) {
195- configurationFile = file ;
196- }
197-
198- // ---------------------------------------------------------------
199-
200- private static boolean propertyExists (String [] values , String [] av ) {
201- if (values != null ) {
202- for (String item : values ) {
203- String p = System .getProperty (item );
204-
205- if (p != null ) {
206- return true ;
207- }
208- }
209-
210- if (av != null )
211- for (String value : values )
212- for (String s : av ) {
213- if (("--" + value ).equals (s )) {
214- return true ;
215- }
216- }
217- }
218-
219- return false ;
220- }
221196}
0 commit comments