30
30
import org .codehaus .plexus .components .secdispatcher .MasterSourceMeta ;
31
31
import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
32
32
import org .codehaus .plexus .components .secdispatcher .SecDispatcherException ;
33
+ import org .codehaus .plexus .components .secdispatcher .internal .SecUtil ;
33
34
34
35
/**
35
36
* This dispatcher is logically equivalent (but much more secure) that Maven3 "master password" encryption.
39
40
public class MasterDispatcher implements Dispatcher , DispatcherMeta {
40
41
public static final String NAME = "master" ;
41
42
42
- private static final String MASTER_CIPHER = "cipher" ;
43
- private static final String MASTER_SOURCE = "source" ;
43
+ private static final String MASTER_CIPHER_ATTR = "c" ;
44
+ private static final String CONF_MASTER_CIPHER = "cipher" ;
45
+ private static final String CONF_MASTER_SOURCE = "source" ;
44
46
45
47
private final PlexusCipher cipher ;
46
48
protected final Map <String , MasterSource > masterSources ;
@@ -64,7 +66,7 @@ public String displayName() {
64
66
@ Override
65
67
public Collection <Field > fields () {
66
68
return List .of (
67
- Field .builder (MASTER_SOURCE )
69
+ Field .builder (CONF_MASTER_SOURCE )
68
70
.optional (false )
69
71
.description ("Source of the master password" )
70
72
.options (masterSources .entrySet ().stream ()
@@ -87,7 +89,7 @@ public Collection<Field> fields() {
87
89
})
88
90
.toList ())
89
91
.build (),
90
- Field .builder (MASTER_CIPHER )
92
+ Field .builder (CONF_MASTER_CIPHER )
91
93
.optional (false )
92
94
.description ("Cipher to use with master password" )
93
95
.options (cipher .availableCiphers ().stream ()
@@ -104,7 +106,8 @@ public EncryptPayload encrypt(String str, Map<String, String> attributes, Map<St
104
106
String encrypted = cipher .encrypt (masterCipher , str , getMasterPassword (config ));
105
107
HashMap <String , String > attr = new HashMap <>(attributes );
106
108
attr .put (SecDispatcher .DISPATCHER_NAME_ATTR , NAME );
107
- attr .put (MASTER_CIPHER , masterCipher );
109
+ attr .put (SecDispatcher .DISPATCHER_VERSION_ATTR , SecUtil .specVersion ());
110
+ attr .put (MASTER_CIPHER_ATTR , masterCipher );
108
111
return new EncryptPayload (attr , encrypted );
109
112
} catch (PlexusCipherException e ) {
110
113
throw new SecDispatcherException ("Encrypt failed" , e );
@@ -123,9 +126,9 @@ public String decrypt(String str, Map<String, String> attributes, Map<String, St
123
126
}
124
127
125
128
private String getMasterPassword (Map <String , String > config ) throws SecDispatcherException {
126
- String masterSource = config .get (MASTER_SOURCE );
129
+ String masterSource = config .get (CONF_MASTER_SOURCE );
127
130
if (masterSource == null ) {
128
- throw new SecDispatcherException ("Invalid configuration: Missing configuration " + MASTER_SOURCE );
131
+ throw new SecDispatcherException ("Invalid configuration: Missing configuration " + CONF_MASTER_SOURCE );
129
132
}
130
133
for (MasterSource masterPasswordSource : masterSources .values ()) {
131
134
String masterPassword = masterPasswordSource .handle (masterSource );
@@ -135,14 +138,18 @@ private String getMasterPassword(Map<String, String> config) throws SecDispatche
135
138
}
136
139
137
140
private String getMasterCipher (Map <String , String > source , boolean config ) throws SecDispatcherException {
138
- String masterCipher = source .get (MASTER_CIPHER );
139
- if (masterCipher == null ) {
140
- if (config ) {
141
- throw new SecDispatcherException ("Invalid configuration: Missing configuration " + MASTER_CIPHER );
142
- } else {
143
- throw new SecDispatcherException ("Malformed attributes: Missing attribute " + MASTER_CIPHER );
141
+ if (config ) {
142
+ String masterCipher = source .get (CONF_MASTER_CIPHER );
143
+ if (masterCipher == null ) {
144
+ throw new SecDispatcherException ("Invalid configuration: Missing configuration " + CONF_MASTER_CIPHER );
144
145
}
146
+ return masterCipher ;
147
+ } else {
148
+ String masterCipher = source .get (MASTER_CIPHER_ATTR );
149
+ if (masterCipher == null ) {
150
+ throw new SecDispatcherException ("Malformed attributes: Missing attribute " + MASTER_CIPHER_ATTR );
151
+ }
152
+ return masterCipher ;
145
153
}
146
- return masterCipher ;
147
154
}
148
155
}
0 commit comments