1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.text.MessageFormat;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
11 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
12 | import org.openstreetmap.josm.data.Preferences.StringSetting;
|
---|
13 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
14 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
15 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.io.OsmServerUserInfoReader;
|
---|
17 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
18 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * JosmUserStateManager is a global object which keeps track of what JOSM knows about
|
---|
23 | * the identity of the current user.
|
---|
24 | *
|
---|
25 | * JOSM can be operated anonymously provided the current user never invokes an operation
|
---|
26 | * on the OSM server which required authentication. In this case JOSM neither knows
|
---|
27 | * the user name of the OSM account of the current user nor its unique id. Perhaps the
|
---|
28 | * user doesn't have one.
|
---|
29 | *
|
---|
30 | * If the current user supplies a user name and a password in the JOSM preferences JOSM
|
---|
31 | * can partially identify the user.
|
---|
32 | *
|
---|
33 | * The current user is fully identified if JOSM knows both the user name and the unique
|
---|
34 | * id of the users OSM account. The latter is retrieved from the OSM server with a
|
---|
35 | * <tt>GET /api/0.6/user/details</tt> request, submitted with the user name and password
|
---|
36 | * of the current user.
|
---|
37 | *
|
---|
38 | * The global JosmUserStateManager listens to {@link PreferenceChangeEvent}s and keeps track
|
---|
39 | * of what the current JOSM instance knows about the current user. Other subsystems can
|
---|
40 | * let the global JosmUserStateManager know in case they fully identify the current user, see
|
---|
41 | * {@link #setFullyIdentified}.
|
---|
42 | *
|
---|
43 | * The information kept by the JosmUserStateManager can be used to
|
---|
44 | * <ul>
|
---|
45 | * <li>safely query changesets owned by the current user based on its user id, not on its user name</li>
|
---|
46 | * <li>safely search for objects last touched by the current user based on its user id, not on its user name</li>
|
---|
47 | * </ul>
|
---|
48 | *
|
---|
49 | */
|
---|
50 | public class JosmUserIdentityManager implements PreferenceChangedListener{
|
---|
51 |
|
---|
52 | static private JosmUserIdentityManager instance;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Replies the unique instance of the JOSM user identity manager
|
---|
56 | *
|
---|
57 | * @return the unique instance of the JOSM user identity manager
|
---|
58 | */
|
---|
59 | static public JosmUserIdentityManager getInstance() {
|
---|
60 | if (instance == null) {
|
---|
61 | instance = new JosmUserIdentityManager();
|
---|
62 | if (Main.pref.get("osm-server.auth-method").equals("oauth") && OAuthAccessTokenHolder.getInstance().containsAccessToken()) {
|
---|
63 | try {
|
---|
64 | instance.initFromOAuth(Main.parent);
|
---|
65 | } catch (Throwable t) {
|
---|
66 | t.printStackTrace();
|
---|
67 | // Fall back to preferences if OAuth identification fails for any reason
|
---|
68 | instance.initFromPreferences();
|
---|
69 | }
|
---|
70 | } else {
|
---|
71 | instance.initFromPreferences();
|
---|
72 | }
|
---|
73 | Main.pref.addPreferenceChangeListener(instance);
|
---|
74 | }
|
---|
75 | return instance;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private String userName;
|
---|
79 | private UserInfo userInfo;
|
---|
80 | private boolean accessTokenKeyChanged;
|
---|
81 | private boolean accessTokenSecretChanged;
|
---|
82 |
|
---|
83 | private JosmUserIdentityManager() {
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Remembers the fact that the current JOSM user is anonymous.
|
---|
88 | */
|
---|
89 | public void setAnonymous() {
|
---|
90 | userName = null;
|
---|
91 | userInfo = null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Remebers the fact that the current JOSM user is partially identified
|
---|
96 | * by the user name of its OSM account.
|
---|
97 | *
|
---|
98 | * @param userName the user name. Must not be null. Must not be empty (whitespace only).
|
---|
99 | * @throws IllegalArgumentException thrown if userName is null
|
---|
100 | * @throws IllegalArgumentException thrown if userName is empty
|
---|
101 | */
|
---|
102 | public void setPartiallyIdentified(String userName) throws IllegalArgumentException {
|
---|
103 | CheckParameterUtil.ensureParameterNotNull(userName, "userName");
|
---|
104 | if (userName.trim().isEmpty())
|
---|
105 | throw new IllegalArgumentException(MessageFormat.format("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
|
---|
106 | this.userName = userName;
|
---|
107 | userInfo = null;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Remembers the fact that the current JOSM user is fully identified with a
|
---|
112 | * verified pair of user name and user id.
|
---|
113 | *
|
---|
114 | * @param username the user name. Must not be null. Must not be empty.
|
---|
115 | * @param userinfo additional information about the user, retrieved from the OSM server and including the user id
|
---|
116 | * @throws IllegalArgumentException thrown if userName is null
|
---|
117 | * @throws IllegalArgumentException thrown if userName is empty
|
---|
118 | * @throws IllegalArgumentException thrown if userinfo is null
|
---|
119 | */
|
---|
120 | public void setFullyIdentified(String username, UserInfo userinfo) throws IllegalArgumentException {
|
---|
121 | CheckParameterUtil.ensureParameterNotNull(username, "username");
|
---|
122 | if (username.trim().isEmpty())
|
---|
123 | throw new IllegalArgumentException(tr("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
|
---|
124 | CheckParameterUtil.ensureParameterNotNull(userinfo, "userinfo");
|
---|
125 | this.userName = username;
|
---|
126 | this.userInfo = userinfo;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Replies true if the current JOSM user is anonymous.
|
---|
131 | *
|
---|
132 | * @return {@code true} if the current user is anonymous.
|
---|
133 | */
|
---|
134 | public boolean isAnonymous() {
|
---|
135 | return userName == null && userInfo == null;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Replies true if the current JOSM user is partially identified.
|
---|
140 | *
|
---|
141 | * @return true if the current JOSM user is partially identified.
|
---|
142 | */
|
---|
143 | public boolean isPartiallyIdentified() {
|
---|
144 | return userName != null && userInfo == null;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Replies true if the current JOSM user is fully identified.
|
---|
149 | *
|
---|
150 | * @return true if the current JOSM user is fully identified.
|
---|
151 | */
|
---|
152 | public boolean isFullyIdentified() {
|
---|
153 | return userName != null && userInfo != null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Replies the user name of the current JOSM user. null, if {@link #isAnonymous()} is true.
|
---|
158 | *
|
---|
159 | * @return the user name of the current JOSM user
|
---|
160 | */
|
---|
161 | public String getUserName() {
|
---|
162 | return userName;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Replies the user id of the current JOSM user. 0, if {@link #isAnonymous()} or
|
---|
167 | * {@link #isPartiallyIdentified()} is true.
|
---|
168 | *
|
---|
169 | * @return the user id of the current JOSM user
|
---|
170 | */
|
---|
171 | public int getUserId() {
|
---|
172 | if (userInfo == null) return 0;
|
---|
173 | return userInfo.getId();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Replies verified additional information about the current user if the user is
|
---|
178 | * {@link #isFullyIdentified()}.
|
---|
179 | *
|
---|
180 | * @return verified additional information about the current user
|
---|
181 | */
|
---|
182 | public UserInfo getUserInfo() {
|
---|
183 | return userInfo;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Initializes the user identity manager from Basic Authentication values in the {@link org.openstreetmap.josm.data.Preferences}
|
---|
188 | * This method should be called if {@code osm-server.auth-method} is set to {@code basic}.
|
---|
189 | * @see #initFromOAuth
|
---|
190 | */
|
---|
191 | public void initFromPreferences() {
|
---|
192 | String userName = CredentialsManager.getInstance().getUsername();
|
---|
193 | if (isAnonymous()) {
|
---|
194 | if (userName != null && !userName.trim().isEmpty()) {
|
---|
195 | setPartiallyIdentified(userName);
|
---|
196 | }
|
---|
197 | } else {
|
---|
198 | if (userName != null && !userName.equals(this.userName)) {
|
---|
199 | setPartiallyIdentified(userName);
|
---|
200 | } else {
|
---|
201 | // same name in the preferences as JOSM already knows about;
|
---|
202 | // keep the state, be it partially or fully identified
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Initializes the user identity manager from OAuth request of user details.
|
---|
209 | * This method should be called if {@code osm-server.auth-method} is set to {@code oauth}.
|
---|
210 | * @param parent component relative to which the {@link PleaseWaitDialog} is displayed.
|
---|
211 | * @see #initFromPreferences
|
---|
212 | * @since 5434
|
---|
213 | */
|
---|
214 | public void initFromOAuth(Component parent) {
|
---|
215 | try {
|
---|
216 | UserInfo info = new OsmServerUserInfoReader().fetchUserInfo(NullProgressMonitor.INSTANCE);
|
---|
217 | setFullyIdentified(info.getDisplayName(), info);
|
---|
218 | } catch (IllegalArgumentException e) {
|
---|
219 | e.printStackTrace();
|
---|
220 | } catch (OsmTransferException e) {
|
---|
221 | e.printStackTrace();
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Replies true if the user with name <code>username</code> is the current
|
---|
227 | * user
|
---|
228 | *
|
---|
229 | * @param username the user name
|
---|
230 | * @return true if the user with name <code>username</code> is the current
|
---|
231 | * user
|
---|
232 | */
|
---|
233 | public boolean isCurrentUser(String username) {
|
---|
234 | if (username == null || this.userName == null) return false;
|
---|
235 | return this.userName.equals(username);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* ------------------------------------------------------------------- */
|
---|
239 | /* interface PreferenceChangeListener */
|
---|
240 | /* ------------------------------------------------------------------- */
|
---|
241 | @Override
|
---|
242 | public void preferenceChanged(PreferenceChangeEvent evt) {
|
---|
243 | if (evt.getKey().equals("osm-server.username")) {
|
---|
244 | if (!(evt.getNewValue() instanceof StringSetting)) return;
|
---|
245 | String newValue = ((StringSetting) evt.getNewValue()).getValue();
|
---|
246 | if (newValue == null || newValue.trim().length() == 0) {
|
---|
247 | setAnonymous();
|
---|
248 | } else {
|
---|
249 | if (! newValue.equals(userName)) {
|
---|
250 | setPartiallyIdentified(newValue);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return;
|
---|
254 |
|
---|
255 | } else if (evt.getKey().equals("osm-server.url")) {
|
---|
256 | if (!(evt.getNewValue() instanceof StringSetting)) return;
|
---|
257 | String newValue = ((StringSetting) evt.getNewValue()).getValue();
|
---|
258 | if (newValue == null || newValue.trim().isEmpty()) {
|
---|
259 | setAnonymous();
|
---|
260 | } else if (isFullyIdentified()) {
|
---|
261 | setPartiallyIdentified(getUserName());
|
---|
262 | }
|
---|
263 |
|
---|
264 | } else if (evt.getKey().equals("oauth.access-token.key")) {
|
---|
265 | accessTokenKeyChanged = true;
|
---|
266 |
|
---|
267 | } else if (evt.getKey().equals("oauth.access-token.secret")) {
|
---|
268 | accessTokenSecretChanged = true;
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (accessTokenKeyChanged && accessTokenSecretChanged) {
|
---|
272 | accessTokenKeyChanged = false;
|
---|
273 | accessTokenSecretChanged = false;
|
---|
274 | if (Main.pref.get("osm-server.auth-method").equals("oauth")) {
|
---|
275 | try {
|
---|
276 | instance.initFromOAuth(Main.parent);
|
---|
277 | } catch (Throwable t) {
|
---|
278 | t.printStackTrace();
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|