source: josm/trunk/src/org/openstreetmap/josm/io/auth/JosmPreferencesCredentialManager.java@ 3681

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

fixed #5624 - npe in authentication code

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
4import java.net.PasswordAuthentication;
5import java.net.Authenticator.RequestorType;
6import java.util.HashMap;
7import java.util.Map;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.oauth.OAuthToken;
11import org.openstreetmap.josm.gui.io.CredentialDialog;
12import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
13
14/**
15 * This is the default credential manager in JOSM. It keeps username and password for both
16 * the OSM API and an optional HTTP proxy in the JOSM preferences file.
17 *
18 */
19public class JosmPreferencesCredentialManager implements CredentialsManager {
20
21 Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new HashMap<RequestorType, PasswordAuthentication>();
22 /**
23 * @see CredentialsManager#lookup(RequestorType)
24 */
25 public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException{
26 if (requestorType == null)
27 return null;
28 String user;
29 String password;
30 switch(requestorType) {
31 case SERVER:
32 user = Main.pref.get("osm-server.username", null);
33 password = Main.pref.get("osm-server.password", null);
34 if (user == null)
35 return null;
36 return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
37 case PROXY:
38 user = Main.pref.get(ProxyPreferencesPanel.PROXY_USER, null);
39 password = Main.pref.get(ProxyPreferencesPanel.PROXY_PASS, null);
40 if (user == null)
41 return null;
42 return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
43 }
44 return null;
45 }
46
47 /**
48 * @see CredentialsManager#store(RequestorType, PasswordAuthentication)
49 */
50 public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException {
51 if (requestorType == null)
52 return;
53 switch(requestorType) {
54 case SERVER:
55 Main.pref.put("osm-server.username", credentials.getUserName());
56 if (credentials.getPassword() == null) {
57 Main.pref.put("osm-server.password", null);
58 } else {
59 Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
60 }
61 break;
62 case PROXY:
63 Main.pref.put(ProxyPreferencesPanel.PROXY_USER, credentials.getUserName());
64 if (credentials.getPassword() == null) {
65 Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, null);
66 } else {
67 Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, String.valueOf(credentials.getPassword()));
68 }
69 break;
70 }
71 }
72
73 /**
74 * @see CredentialsManager#getCredentials(RequestorType, boolean)
75 */
76 public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException{
77 if (requestorType == null)
78 return null;
79 PasswordAuthentication credentials = lookup(requestorType);
80 String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
81 String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
82
83 CredentialsManagerResponse response = new CredentialsManagerResponse();
84
85 /*
86 * Last request was successful and there was no credentials stored
87 * in file (or only the username is stored).
88 * -> Try to recall credentials that have been entered
89 * manually in this session.
90 */
91 if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) &&
92 (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) {
93 PasswordAuthentication pa = memoryCredentialsCache.get(requestorType);
94 response.setUsername(pa.getUserName());
95 response.setPassword(pa.getPassword());
96 response.setCanceled(false);
97 /*
98 * Prompt the user for credentials. This happens the first time each
99 * josm start if the user does not save the credentials to preference
100 * file (username=="") and each time after authentication failed
101 * (noSuccessWithLastResponse == true).
102 */
103 } else if (noSuccessWithLastResponse || username.equals("") || password.equals("")) {
104 CredentialDialog dialog = null;
105 switch(requestorType) {
106 case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
107 case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
108 }
109 dialog.setVisible(true);
110 response.setCanceled(dialog.isCanceled());
111 if (dialog.isCanceled())
112 return response;
113 response.setUsername(dialog.getUsername());
114 response.setPassword(dialog.getPassword());
115 if (dialog.isSaveCredentials()) {
116 store(requestorType, new PasswordAuthentication(
117 response.getUsername(),
118 response.getPassword()
119 ));
120 /*
121 * User decides not to save credentials to file. Keep it
122 * in memory so we don't have to ask over and over again.
123 */
124 } else {
125 PasswordAuthentication pa = new PasswordAuthentication(dialog.getUsername(), dialog.getPassword());
126 memoryCredentialsCache.put(requestorType, pa);
127 }
128 /*
129 * We got it from file.
130 */
131 } else {
132 response.setUsername(username);
133 response.setPassword(password.toCharArray());
134 response.setCanceled(false);
135 }
136 return response;
137 }
138
139 /**
140 * Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
141 * Access Token is currently managed by this CredentialManager.
142 *
143 * @return the current OAuth Access Token to access the OSM server.
144 * @throws CredentialsManagerException thrown if something goes wrong
145 */
146 public OAuthToken lookupOAuthAccessToken() throws CredentialsManagerException {
147 String accessTokenKey = Main.pref.get("oauth.access-token.key", null);
148 String accessTokenSecret = Main.pref.get("oauth.access-token.secret", null);
149 if (accessTokenKey == null && accessTokenSecret == null)
150 return null;
151 return new OAuthToken(accessTokenKey, accessTokenSecret);
152 }
153
154 /**
155 * Stores the OAuth Access Token <code>accessToken</code>.
156 *
157 * @param accessToken the access Token. null, to remove the Access Token.
158 * @throws CredentialsManagerException thrown if something goes wrong
159 */
160 public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsManagerException {
161 if (accessToken == null) {
162 Main.pref.put("oauth.access-token.key", null);
163 Main.pref.put("oauth.access-token.secret", null);
164 } else {
165 Main.pref.put("oauth.access-token.key", accessToken.getKey());
166 Main.pref.put("oauth.access-token.secret", accessToken.getSecret());
167 }
168 }
169}
Note: See TracBrowser for help on using the repository browser.