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.data.oauth.OAuthToken;
|
---|
9 | import org.openstreetmap.josm.gui.io.CredentialDialog;
|
---|
10 | import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This is the default credential manager in JOSM. It keeps username and password for both
|
---|
14 | * the OSM API and an optional HTTP proxy in the JOSM preferences file.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class JosmPreferencesCredentialManager implements CredentialsManager {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @see CredentialsManager#lookup(RequestorType)
|
---|
21 | */
|
---|
22 | public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException{
|
---|
23 | if (requestorType == null)
|
---|
24 | return null;
|
---|
25 | String user;
|
---|
26 | String password;
|
---|
27 | switch(requestorType) {
|
---|
28 | case SERVER:
|
---|
29 | user = Main.pref.get("osm-server.username", null);
|
---|
30 | password = Main.pref.get("osm-server.password", null);
|
---|
31 | if (user == null)
|
---|
32 | return null;
|
---|
33 | return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
|
---|
34 | case PROXY:
|
---|
35 | user = Main.pref.get(ProxyPreferencesPanel.PROXY_USER, null);
|
---|
36 | password = Main.pref.get(ProxyPreferencesPanel.PROXY_PASS, null);
|
---|
37 | if (user == null)
|
---|
38 | return null;
|
---|
39 | return new PasswordAuthentication(user, password == null ? null : password.toCharArray());
|
---|
40 | }
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @see CredentialsManager#store(RequestorType, PasswordAuthentication)
|
---|
46 | */
|
---|
47 | public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException {
|
---|
48 | if (requestorType == null)
|
---|
49 | return;
|
---|
50 | switch(requestorType) {
|
---|
51 | case SERVER:
|
---|
52 | Main.pref.put("osm-server.username", credentials.getUserName());
|
---|
53 | if (credentials.getPassword() == null) {
|
---|
54 | Main.pref.put("osm-server.password", null);
|
---|
55 | } else {
|
---|
56 | Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
|
---|
57 | }
|
---|
58 | break;
|
---|
59 | case PROXY:
|
---|
60 | Main.pref.put(ProxyPreferencesPanel.PROXY_USER, credentials.getUserName());
|
---|
61 | if (credentials.getPassword() == null) {
|
---|
62 | Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, null);
|
---|
63 | } else {
|
---|
64 | Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, String.valueOf(credentials.getPassword()));
|
---|
65 | }
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * @see CredentialsManager#getCredentials(RequestorType, boolean)
|
---|
72 | */
|
---|
73 | public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException{
|
---|
74 | if (requestorType == null)
|
---|
75 | return null;
|
---|
76 | PasswordAuthentication credentials = lookup(requestorType);
|
---|
77 | String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
|
---|
78 | String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
|
---|
79 |
|
---|
80 | CredentialsManagerResponse response = new CredentialsManagerResponse();
|
---|
81 |
|
---|
82 | if (noSuccessWithLastResponse|| username.equals("") || password.equals("")) {
|
---|
83 | CredentialDialog dialog = null;
|
---|
84 | switch(requestorType) {
|
---|
85 | case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
|
---|
86 | case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
|
---|
87 | }
|
---|
88 | dialog.setVisible(true);
|
---|
89 | response.setCanceled(dialog.isCanceled());
|
---|
90 | if (dialog.isCanceled())
|
---|
91 | return response;
|
---|
92 | response.setUsername(dialog.getUsername());
|
---|
93 | response.setPassword(dialog.getPassword());
|
---|
94 | if (dialog.isSaveCredentials()) {
|
---|
95 | store(requestorType, new PasswordAuthentication(
|
---|
96 | response.getUsername(),
|
---|
97 | response.getPassword()
|
---|
98 | ));
|
---|
99 | }
|
---|
100 | } else {
|
---|
101 | response.setUsername(username);
|
---|
102 | response.setPassword(password.toCharArray());
|
---|
103 | response.setCanceled(false);
|
---|
104 | }
|
---|
105 | return response;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
|
---|
111 | * Access Token is currently managed by this CredentialManager.
|
---|
112 | *
|
---|
113 | * @return the current OAuth Access Token to access the OSM server.
|
---|
114 | * @throws CredentialsManagerException thrown if something goes wrong
|
---|
115 | */
|
---|
116 | public OAuthToken lookupOAuthAccessToken() throws CredentialsManagerException {
|
---|
117 | String accessTokenKey = Main.pref.get("oauth.access-token.key", null);
|
---|
118 | String accessTokenSecret = Main.pref.get("oauth.access-token.secret", null);
|
---|
119 | if (accessTokenKey == null && accessTokenSecret == null)
|
---|
120 | return null;
|
---|
121 | return new OAuthToken(accessTokenKey, accessTokenSecret);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Stores the OAuth Access Token <code>accessToken</code>.
|
---|
126 | *
|
---|
127 | * @param accessToken the access Token. null, to remove the Access Token.
|
---|
128 | * @throws CredentialsManagerException thrown if something goes wrong
|
---|
129 | */
|
---|
130 | public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsManagerException {
|
---|
131 | if (accessToken == null) {
|
---|
132 | Main.pref.put("oauth.access-token.key", null);
|
---|
133 | Main.pref.put("oauth.access-token.secret", null);
|
---|
134 | } else {
|
---|
135 | Main.pref.put("oauth.access-token.key", accessToken.getKey());
|
---|
136 | Main.pref.put("oauth.access-token.secret", accessToken.getSecret());
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|