1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.auth;
|
---|
3 |
|
---|
4 | import java.net.PasswordAuthentication;
|
---|
5 | import java.net.Authenticator.RequestorType;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.gui.io.CredentialDialog;
|
---|
9 | import org.openstreetmap.josm.gui.preferences.ProxyPreferences;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * This is the default credential manager in JOSM. It keeps username and password for both
|
---|
13 | * the OSM API and an optional HTTP proxy in the JOSM preferences file.
|
---|
14 | *
|
---|
15 | */
|
---|
16 | public class JosmPreferencesCredentialManager implements CredentialsManager {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @see CredentialsManager#lookup(RequestorType)
|
---|
20 | */
|
---|
21 | public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException{
|
---|
22 | if (requestorType == null)
|
---|
23 | return null;
|
---|
24 | String user;
|
---|
25 | String password;
|
---|
26 | switch(requestorType) {
|
---|
27 | case SERVER:
|
---|
28 | user = Main.pref.get("osm-server.username", null);
|
---|
29 | password = Main.pref.get("osm-server.password", null);
|
---|
30 | if (user == null)
|
---|
31 | return null;
|
---|
32 | return new PasswordAuthentication(user, password == null ? null : password.toCharArray());
|
---|
33 | case PROXY:
|
---|
34 | user = Main.pref.get(ProxyPreferences.PROXY_USER, null);
|
---|
35 | password = Main.pref.get(ProxyPreferences.PROXY_PASS, null);
|
---|
36 | if (user == null)
|
---|
37 | return null;
|
---|
38 | return new PasswordAuthentication(user, password == null ? null : password.toCharArray());
|
---|
39 | }
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @see CredentialsManager#store(RequestorType, PasswordAuthentication)
|
---|
45 | */
|
---|
46 | public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException {
|
---|
47 | if (requestorType == null)
|
---|
48 | return;
|
---|
49 | switch(requestorType) {
|
---|
50 | case SERVER:
|
---|
51 | Main.pref.put("osm-server.username", credentials.getUserName());
|
---|
52 | if (credentials.getPassword() == null) {
|
---|
53 | Main.pref.put("osm-server.password", null);
|
---|
54 | } else {
|
---|
55 | Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
|
---|
56 | }
|
---|
57 | break;
|
---|
58 | case PROXY:
|
---|
59 | Main.pref.put("proxy.username", credentials.getUserName());
|
---|
60 | if (credentials.getPassword() == null) {
|
---|
61 | Main.pref.put("proxy.password", null);
|
---|
62 | } else {
|
---|
63 | Main.pref.put("proxy.password", String.valueOf(credentials.getPassword()));
|
---|
64 | }
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @see CredentialsManager#getCredentials(RequestorType, boolean)
|
---|
71 | */
|
---|
72 | public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException{
|
---|
73 | if (requestorType == null)
|
---|
74 | return null;
|
---|
75 | PasswordAuthentication credentials = lookup(requestorType);
|
---|
76 | String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
|
---|
77 | String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
|
---|
78 |
|
---|
79 | CredentialsManagerResponse response = new CredentialsManagerResponse();
|
---|
80 |
|
---|
81 | if (noSuccessWithLastResponse|| username.equals("") || password.equals("")) {
|
---|
82 | CredentialDialog dialog = null;
|
---|
83 | switch(requestorType) {
|
---|
84 | case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
|
---|
85 | case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
|
---|
86 | }
|
---|
87 | dialog.setVisible(true);
|
---|
88 | response.setCanceled(dialog.isCanceled());
|
---|
89 | if (dialog.isCanceled())
|
---|
90 | return response;
|
---|
91 | response.setUsername(dialog.getUsername());
|
---|
92 | response.setPassword(dialog.getPassword());
|
---|
93 | if (dialog.isSaveCredentials()) {
|
---|
94 | store(requestorType, new PasswordAuthentication(
|
---|
95 | response.getUsername(),
|
---|
96 | response.getPassword()
|
---|
97 | ));
|
---|
98 | }
|
---|
99 | } else {
|
---|
100 | response.setUsername(username);
|
---|
101 | response.setPassword(password.toCharArray());
|
---|
102 | response.setCanceled(false);
|
---|
103 | }
|
---|
104 | return response;
|
---|
105 | }
|
---|
106 | }
|
---|