1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.auth;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.net.Authenticator.RequestorType;
|
---|
6 | import java.net.PasswordAuthentication;
|
---|
7 | import java.util.Objects;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.UserIdentityManager;
|
---|
10 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
11 | import org.openstreetmap.josm.io.OsmApi;
|
---|
12 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
13 | import org.openstreetmap.josm.tools.Logging;
|
---|
14 | import org.openstreetmap.josm.tools.Utils;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * CredentialManager is a factory for the single credential agent used.
|
---|
18 | *
|
---|
19 | * Currently, it defaults to replying an instance of {@link JosmPreferencesCredentialAgent}.
|
---|
20 | * @since 2641
|
---|
21 | */
|
---|
22 | public class CredentialsManager implements CredentialsAgent {
|
---|
23 |
|
---|
24 | private static volatile CredentialsManager instance;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Replies the single credential agent used in JOSM
|
---|
28 | *
|
---|
29 | * @return the single credential agent used in JOSM
|
---|
30 | */
|
---|
31 | public static CredentialsManager getInstance() {
|
---|
32 | if (instance == null) {
|
---|
33 | CredentialsAgent delegate;
|
---|
34 | if (agentFactory == null) {
|
---|
35 | delegate = new JosmPreferencesCredentialAgent();
|
---|
36 | } else {
|
---|
37 | delegate = agentFactory.getCredentialsAgent();
|
---|
38 | }
|
---|
39 | instance = new CredentialsManager(delegate);
|
---|
40 | }
|
---|
41 | return instance;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static CredentialsAgentFactory agentFactory;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Credentials agent factory.
|
---|
48 | */
|
---|
49 | @FunctionalInterface
|
---|
50 | public interface CredentialsAgentFactory {
|
---|
51 | /**
|
---|
52 | * Returns the credentials agent instance.
|
---|
53 | * @return the credentials agent instance
|
---|
54 | */
|
---|
55 | CredentialsAgent getCredentialsAgent();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Plugins can register a CredentialsAgentFactory, thereby overriding
|
---|
60 | * JOSM's default credentials agent.
|
---|
61 | * @param agentFactory The Factory that provides the custom CredentialsAgent.
|
---|
62 | * Can be null to clear the factory and switch back to default behavior.
|
---|
63 | */
|
---|
64 | public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
|
---|
65 | CredentialsManager.agentFactory = agentFactory;
|
---|
66 | CredentialsManager.instance = null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* non-static fields and methods */
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * The credentials agent doing the real stuff
|
---|
73 | */
|
---|
74 | private final CredentialsAgent delegate;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Constructs a new {@code CredentialsManager}.
|
---|
78 | * @param delegate The credentials agent backing this credential manager. Must not be {@code null}
|
---|
79 | */
|
---|
80 | public CredentialsManager(CredentialsAgent delegate) {
|
---|
81 | CheckParameterUtil.ensureParameterNotNull(delegate, "delegate");
|
---|
82 | this.delegate = delegate;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Returns type of credentials agent backing this credentials manager.
|
---|
87 | * @return The type of credentials agent
|
---|
88 | */
|
---|
89 | public final Class<? extends CredentialsAgent> getCredentialsAgentClass() {
|
---|
90 | return delegate.getClass();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Returns the username for OSM API
|
---|
95 | * @return the username for OSM API
|
---|
96 | */
|
---|
97 | public String getUsername() {
|
---|
98 | return getUsername(OsmApi.getOsmApi().getHost());
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns the username for a given host
|
---|
103 | * @param host The host for which username is wanted
|
---|
104 | * @return The username for {@code host}
|
---|
105 | */
|
---|
106 | public String getUsername(String host) {
|
---|
107 | String username = null;
|
---|
108 | try {
|
---|
109 | PasswordAuthentication auth = lookup(RequestorType.SERVER, host);
|
---|
110 | if (auth != null) {
|
---|
111 | username = auth.getUserName();
|
---|
112 | }
|
---|
113 | } catch (CredentialsAgentException ex) {
|
---|
114 | Logging.debug(ex);
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 | if (username == null) return null;
|
---|
118 | username = username.trim();
|
---|
119 | return username.isEmpty() ? null : username;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException {
|
---|
124 | return delegate.lookup(requestorType, host);
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
|
---|
129 | if (requestorType == RequestorType.SERVER && Objects.equals(OsmApi.getOsmApi().getHost(), host)) {
|
---|
130 | String username = credentials.getUserName();
|
---|
131 | if (!Utils.isBlank(username)) {
|
---|
132 | UserIdentityManager.getInstance().setPartiallyIdentified(username);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | // see #11914: clear cache before we store new value
|
---|
136 | purgeCredentialsCache(requestorType);
|
---|
137 | delegate.store(requestorType, host, credentials);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse)
|
---|
142 | throws CredentialsAgentException {
|
---|
143 | CredentialsAgentResponse credentials = delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
|
---|
144 | if (requestorType == RequestorType.SERVER) {
|
---|
145 | // see #11914 : Keep UserIdentityManager up to date
|
---|
146 | String userName = credentials.getUsername();
|
---|
147 | userName = userName == null ? "" : userName.trim();
|
---|
148 | if (!Objects.equals(UserIdentityManager.getInstance().getUserName(), userName)) {
|
---|
149 | if (userName.isEmpty())
|
---|
150 | UserIdentityManager.getInstance().setAnonymous();
|
---|
151 | else
|
---|
152 | UserIdentityManager.getInstance().setPartiallyIdentified(userName);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return credentials;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException {
|
---|
160 | return delegate.lookupOAuthAccessToken();
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException {
|
---|
165 | delegate.storeOAuthAccessToken(accessToken);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public Component getPreferencesDecorationPanel() {
|
---|
170 | return delegate.getPreferencesDecorationPanel();
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public void purgeCredentialsCache(RequestorType requestorType) {
|
---|
175 | delegate.purgeCredentialsCache(requestorType);
|
---|
176 | }
|
---|
177 | }
|
---|