|
11 | 11 | import io.sloeber.core.tools.Messages;
|
12 | 12 |
|
13 | 13 | public class PasswordManager {
|
14 |
| - private String myPassword; |
15 |
| - private String myLogin = null; |
16 |
| - private String myhost = null; |
17 |
| - boolean ret = false; |
18 |
| - |
19 |
| - public PasswordManager() { |
20 |
| - // no constructor needed |
21 |
| - } |
22 |
| - |
23 |
| - public String getPassword() { |
24 |
| - return this.myPassword; |
25 |
| - } |
26 |
| - |
27 |
| - public String getLogin() { |
28 |
| - return this.myLogin; |
29 |
| - } |
30 |
| - |
31 |
| - public String getHost() { |
32 |
| - return this.myhost; |
33 |
| - } |
34 |
| - |
35 |
| - /** |
36 |
| - * Sets the host. If there is no Login,password for this host the method |
37 |
| - * returns false. If a login/password for the host is found the method |
38 |
| - * returns true |
39 |
| - * |
40 |
| - * @param host |
41 |
| - * @return |
42 |
| - */ |
43 |
| - public boolean setHost(String host) { |
44 |
| - this.myhost = host; |
45 |
| - this.myPassword = null; |
46 |
| - this.myLogin = null; |
47 |
| - |
48 |
| - String nodename = ConvertHostToNodeName(this.myhost); |
49 |
| - ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
50 |
| - ISecurePreferences node = root.node(nodename); |
51 |
| - |
52 |
| - try { |
53 |
| - if (root.nodeExists(nodename)) { |
54 |
| - this.myPassword = node.get(Messages.security_password, null); |
55 |
| - this.myLogin = node.get(Messages.security_login, null); |
56 |
| - return true; |
57 |
| - } |
58 |
| - |
59 |
| - // PasswordDialog dialog = new PasswordDialog( |
60 |
| - // PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
61 |
| - // if (this.myLogin != null) |
62 |
| - // dialog.setUser(this.myLogin); |
63 |
| - // dialog.sethost(host); |
64 |
| - // // get the new values from the dialog |
65 |
| - // if (dialog.open() == Window.OK) { |
66 |
| - // this.myLogin = dialog.getUser(); |
67 |
| - // this.myPassword = dialog.getPassword(); |
68 |
| - // node.put(Messages.security_login, this.myLogin, false); |
69 |
| - // node.put(Messages.security_password, this.myPassword, true); |
70 |
| - // } else { |
71 |
| - // return false; |
72 |
| - // } |
73 |
| - |
74 |
| - } catch (StorageException e) { |
75 |
| - Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$ |
| 14 | + private String myPassword; |
| 15 | + private String myLogin = null; |
| 16 | + private String myhost = null; |
| 17 | + boolean ret = false; |
| 18 | + |
| 19 | + public PasswordManager() { |
| 20 | + // no constructor needed |
| 21 | + } |
| 22 | + |
| 23 | + public String getPassword() { |
| 24 | + return this.myPassword; |
76 | 25 | }
|
77 | 26 |
|
78 |
| - return false; |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Sets the login and password for a host |
83 |
| - * |
84 |
| - * @param host |
85 |
| - * @param login |
86 |
| - * @param password |
87 |
| - * @throws Exception |
88 |
| - */ |
89 |
| - public void setLoginData(String host, String login, String password) throws Exception { |
90 |
| - this.myhost = host; |
91 |
| - this.myLogin = login; |
92 |
| - this.myPassword = password; |
93 |
| - String nodename = ConvertHostToNodeName(this.myhost); |
94 |
| - ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
95 |
| - ISecurePreferences node = root.node(nodename); |
96 |
| - |
97 |
| - node.put(Messages.security_login, this.myLogin, false); |
98 |
| - node.put(Messages.security_password, this.myPassword, true); |
99 |
| - } |
100 |
| - |
101 |
| - static public void setPwd(String host, String login, String pwd) { |
102 |
| - |
103 |
| - String nodename = ConvertHostToNodeName(host); |
104 |
| - ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
105 |
| - ISecurePreferences node = root.node(nodename); |
106 |
| - |
107 |
| - try { |
108 |
| - node.put(Messages.security_login, login, false); |
109 |
| - node.put(Messages.security_password, pwd, false); |
110 |
| - } catch (StorageException e) { |
111 |
| - |
112 |
| - Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$ |
| 27 | + public String getLogin() { |
| 28 | + return this.myLogin; |
113 | 29 | }
|
114 | 30 |
|
115 |
| - } |
| 31 | + public String getHost() { |
| 32 | + return this.myhost; |
| 33 | + } |
116 | 34 |
|
117 |
| - public static void ErasePassword(String host) { |
118 |
| - String nodename = ConvertHostToNodeName(host); |
119 |
| - ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
120 |
| - ISecurePreferences node = root.node(nodename); |
121 |
| - try { |
122 |
| - if (root.nodeExists(nodename)) { |
123 |
| - node.put(Messages.security_password, null, true); |
124 |
| - } |
| 35 | + /** |
| 36 | + * Sets the host. If there is no Login,password for this host the method |
| 37 | + * returns false. If a login/password for the host is found the method |
| 38 | + * returns true |
| 39 | + * |
| 40 | + * @param host |
| 41 | + * @return |
| 42 | + */ |
| 43 | + public boolean setHost(String host) { |
| 44 | + this.myhost = host; |
| 45 | + this.myPassword = null; |
| 46 | + this.myLogin = null; |
| 47 | + |
| 48 | + String nodename = ConvertHostToNodeName(this.myhost); |
| 49 | + ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
| 50 | + |
| 51 | + try { |
| 52 | + if (root.nodeExists(nodename)) { |
| 53 | + |
| 54 | + ISecurePreferences node = root.node(nodename); |
| 55 | + this.myPassword = node.get(Messages.security_password, null); |
| 56 | + this.myLogin = node.get(Messages.security_login, null); |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + } catch (StorageException e) { |
| 61 | + Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, |
| 62 | + "Set a password on the project properties->arduino page", e)); //$NON-NLS-1$ |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
125 | 67 |
|
126 |
| - } catch (StorageException e) { |
127 |
| - Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to erase login info", e)); //$NON-NLS-1$ |
| 68 | + /** |
| 69 | + * Sets the login and password for a host |
| 70 | + * |
| 71 | + * @param host |
| 72 | + * @param login |
| 73 | + * @param password |
| 74 | + * @throws Exception |
| 75 | + */ |
| 76 | + public void setLoginData(String host, String login, String password) throws Exception { |
| 77 | + this.myhost = host; |
| 78 | + this.myLogin = login; |
| 79 | + this.myPassword = password; |
| 80 | + String nodename = ConvertHostToNodeName(this.myhost); |
| 81 | + ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
| 82 | + ISecurePreferences node = root.node(nodename); |
| 83 | + |
| 84 | + node.put(Messages.security_login, this.myLogin, false); |
| 85 | + node.put(Messages.security_password, this.myPassword, true); |
128 | 86 | }
|
129 | 87 |
|
130 |
| - } |
| 88 | + static public void setPwd(String host, String login, String pwd) { |
| 89 | + |
| 90 | + String nodename = ConvertHostToNodeName(host); |
| 91 | + ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
| 92 | + ISecurePreferences node = root.node(nodename); |
131 | 93 |
|
132 |
| - private static String ConvertHostToNodeName(String host) { |
| 94 | + try { |
| 95 | + node.put(Messages.security_login, login, false); |
| 96 | + node.put(Messages.security_password, pwd, false); |
| 97 | + } catch (StorageException e) { |
133 | 98 |
|
134 |
| - return "ssh/" + host.replace(Const.DOT, Const.SLACH); //$NON-NLS-1$ |
135 |
| - } |
| 99 | + Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$ |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public static void ErasePassword(String host) { |
| 105 | + String nodename = ConvertHostToNodeName(host); |
| 106 | + ISecurePreferences root = SecurePreferencesFactory.getDefault(); |
| 107 | + |
| 108 | + if (root.nodeExists(nodename)) { |
| 109 | + ISecurePreferences node = root.node(nodename); |
| 110 | + node.removeNode();// (Messages.security_password, null, true); |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + private static String ConvertHostToNodeName(String host) { |
| 116 | + |
| 117 | + return "ssh/" + host.replace(Const.DOT, Const.SLACH); //$NON-NLS-1$ |
| 118 | + } |
136 | 119 |
|
137 | 120 | }
|
0 commit comments