source: josm/trunk/src/org/openstreetmap/josm/io/auth/JosmPreferencesCredentialAgent.java@ 4263

Last change on this file since 4263 was 4249, checked in by bastiK, 13 years ago

improve plugin hook for credentials handling

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.net.PasswordAuthentication;
8import java.net.Authenticator.RequestorType;
9
10import javax.swing.text.html.HTMLEditorKit;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.oauth.OAuthToken;
14import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
15import org.openstreetmap.josm.gui.widgets.HtmlPanel;
16
17/**
18 * This is the default credentials agent in JOSM. It keeps username and password for both
19 * the OSM API and an optional HTTP proxy in the JOSM preferences file.
20 *
21 */
22public class JosmPreferencesCredentialAgent extends AbstractCredentialsAgent {
23
24 /**
25 * @see CredentialsAgent#lookup(RequestorType)
26 */
27 @Override
28 public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException{
29 if (requestorType == null)
30 return null;
31 String user;
32 String password;
33 switch(requestorType) {
34 case SERVER:
35 user = Main.pref.get("osm-server.username", null);
36 password = Main.pref.get("osm-server.password", null);
37 if (user == null)
38 return null;
39 return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
40 case PROXY:
41 user = Main.pref.get(ProxyPreferencesPanel.PROXY_USER, null);
42 password = Main.pref.get(ProxyPreferencesPanel.PROXY_PASS, null);
43 if (user == null)
44 return null;
45 return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
46 }
47 return null;
48 }
49
50 /**
51 * @see CredentialsAgent#store(RequestorType, PasswordAuthentication)
52 */
53 @Override
54 public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException {
55 if (requestorType == null)
56 return;
57 switch(requestorType) {
58 case SERVER:
59 Main.pref.put("osm-server.username", credentials.getUserName());
60 if (credentials.getPassword() == null) {
61 Main.pref.put("osm-server.password", null);
62 } else {
63 Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
64 }
65 break;
66 case PROXY:
67 Main.pref.put(ProxyPreferencesPanel.PROXY_USER, credentials.getUserName());
68 if (credentials.getPassword() == null) {
69 Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, null);
70 } else {
71 Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, String.valueOf(credentials.getPassword()));
72 }
73 break;
74 }
75 }
76
77 /**
78 * Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
79 * Access Token is currently managed by this CredentialManager.
80 *
81 * @return the current OAuth Access Token to access the OSM server.
82 * @throws CredentialsAgentException thrown if something goes wrong
83 */
84 @Override
85 public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException {
86 String accessTokenKey = Main.pref.get("oauth.access-token.key", null);
87 String accessTokenSecret = Main.pref.get("oauth.access-token.secret", null);
88 if (accessTokenKey == null && accessTokenSecret == null)
89 return null;
90 return new OAuthToken(accessTokenKey, accessTokenSecret);
91 }
92
93 /**
94 * Stores the OAuth Access Token <code>accessToken</code>.
95 *
96 * @param accessToken the access Token. null, to remove the Access Token.
97 * @throws CredentialsAgentException thrown if something goes wrong
98 */
99 @Override
100 public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException {
101 if (accessToken == null) {
102 Main.pref.put("oauth.access-token.key", null);
103 Main.pref.put("oauth.access-token.secret", null);
104 } else {
105 Main.pref.put("oauth.access-token.key", accessToken.getKey());
106 Main.pref.put("oauth.access-token.secret", accessToken.getSecret());
107 }
108 }
109
110 @Override
111 public Component getPreferencesDecorationPanel() {
112 HtmlPanel pnlMessage = new HtmlPanel();
113 HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
114 kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
115 pnlMessage.setText(
116 tr(
117 "<html><body>"
118 + "<p class=\"warning-body\">"
119 + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
120 + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
121 + "<strong>Do not use a valuable password.</strong>"
122 + "</p>"
123 + "</body></html>"
124 )
125 );
126 return pnlMessage;
127 }
128
129 @Override
130 public String getSaveUsernameAndPasswordCheckboxText() {
131 return tr("Save user and password (unencrypted)");
132 }
133
134}
Note: See TracBrowser for help on using the repository browser.