1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.auth;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.net.PasswordAuthentication;
|
---|
8 | import java.net.Authenticator.RequestorType;
|
---|
9 |
|
---|
10 | import javax.swing.text.html.HTMLEditorKit;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
14 | import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
|
---|
15 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
16 | import org.openstreetmap.josm.io.OsmApi;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
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 | */
|
---|
24 | public 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 (Utils.equal(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 (Utils.equal(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 thrown 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 thrown 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(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
|
---|
134 | pnlMessage.setText(
|
---|
135 | tr(
|
---|
136 | "<html><body>"
|
---|
137 | + "<p class=\"warning-body\">"
|
---|
138 | + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
|
---|
139 | + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
|
---|
140 | + "<strong>Do not use a valuable password.</strong>"
|
---|
141 | + "</p>"
|
---|
142 | + "</body></html>"
|
---|
143 | )
|
---|
144 | );
|
---|
145 | return pnlMessage;
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public String getSaveUsernameAndPasswordCheckboxText() {
|
---|
150 | return tr("Save user and password (unencrypted)");
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|