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

Last change on this file since 11043 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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