22
22
import java .util .HashMap ;
23
23
import java .util .Map ;
24
24
import java .util .StringTokenizer ;
25
+ import java .util .stream .Collectors ;
25
26
26
27
import org .codehaus .plexus .components .cipher .PlexusCipher ;
27
28
import org .codehaus .plexus .components .cipher .PlexusCipherException ;
37
38
@ Singleton
38
39
@ Named
39
40
public 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 = "]" ;
43
43
44
44
protected final PlexusCipher cipher ;
45
45
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 ;
48
48
49
49
@ Inject
50
50
public DefaultSecDispatcher (
51
51
PlexusCipher cipher ,
52
52
Map <String , MasterPasswordSource > masterPasswordSources ,
53
- Map <String , PasswordDecryptor > decryptors ,
53
+ Map <String , Dispatcher > dispatchers ,
54
54
@ 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 ) ;
59
59
}
60
60
61
61
// ---------------------------------------------------------------
62
62
63
63
@ 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 ;
68
66
69
67
try {
70
- bare = cipher .unDecorate (str );
71
-
72
- Map <String , String > attr = stripAttributes (bare );
73
-
74
68
String res ;
75
-
76
69
SettingsSecurity sec = getSec ();
77
-
78
- if (attr == null || attr .get ("type" ) == null ) {
70
+ if (attr == null || attr .get (TYPE_ATTR ) == null ) {
79
71
String master = getMaster (sec );
80
-
81
- res = cipher .decrypt (bare , master );
72
+ res = cipher .encrypt (str , master );
82
73
} else {
83
74
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
-
89
75
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
+ }
90
90
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 );
95
106
String pass = strip (bare );
96
-
97
107
return dispatcher .decrypt (pass , attr , conf );
98
108
}
99
-
100
- return res ;
101
109
} catch (PlexusCipherException e ) {
102
110
throw new SecDispatcherException (e .getMessage (), e );
103
111
}
104
112
}
105
113
106
114
private String strip (String str ) {
107
115
int pos = str .indexOf (ATTR_STOP );
108
-
109
116
if (pos != -1 ) return str .substring (pos + 1 );
110
-
111
117
return str ;
112
118
}
113
119
@@ -151,7 +157,6 @@ private Map<String, String> stripAttributes(String str) {
151
157
152
158
private boolean isEncryptedString (String str ) {
153
159
if (str == null ) return false ;
154
-
155
160
return cipher .isEncryptedString (str );
156
161
}
157
162
@@ -171,8 +176,6 @@ private SettingsSecurity getSec() throws SecDispatcherException {
171
176
return sec ;
172
177
}
173
178
174
- // ----------------------------------------------------------------------------
175
-
176
179
private String getMaster (SettingsSecurity sec ) throws SecDispatcherException {
177
180
String masterSource = requireNonNull (sec .getMasterSource (), "masterSource is null" );
178
181
try {
@@ -186,36 +189,8 @@ private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
186
189
}
187
190
throw new SecDispatcherException ("master password could not be fetched" );
188
191
}
189
- // ---------------------------------------------------------------
192
+
190
193
public String getConfigurationFile () {
191
194
return configurationFile ;
192
195
}
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
- }
221
196
}
0 commit comments