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