1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker.mvnenc;
20
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24
25 import org.apache.maven.api.di.Inject;
26 import org.apache.maven.api.di.Named;
27 import org.apache.maven.api.di.Singleton;
28 import org.apache.maven.api.services.Prompter;
29 import org.apache.maven.api.services.PrompterException;
30 import org.codehaus.plexus.components.secdispatcher.MasterSource;
31 import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
32 import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
33 import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
34
35
36
37
38 @Singleton
39 @Named(ConsolePasswordPrompt.NAME)
40 public class ConsolePasswordPrompt implements MasterSource, MasterSourceMeta {
41 public static final String NAME = "console-prompt";
42
43 private final Prompter prompter;
44
45 @Inject
46 public ConsolePasswordPrompt(Prompter prompter) {
47 this.prompter = prompter;
48 }
49
50 @Override
51 public String description() {
52 return "Secure console password prompt";
53 }
54
55 @Override
56 public Optional<String> configTemplate() {
57 return Optional.empty();
58 }
59
60 @Override
61 public String handle(String config) throws SecDispatcherException {
62 if (NAME.equals(config)) {
63 try {
64 return prompter.promptForPassword("Enter the master password: ");
65 } catch (PrompterException e) {
66 throw new SecDispatcherException("Could not collect the password", e);
67 }
68 }
69 return null;
70 }
71
72 @Override
73 public SecDispatcher.ValidationResponse validateConfiguration(String config) {
74 if (NAME.equals(config)) {
75 return new SecDispatcher.ValidationResponse(getClass().getSimpleName(), true, Map.of(), List.of());
76 }
77 return null;
78 }
79 }