source: josm/trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java@ 12743

Last change on this file since 12743 was 12743, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - deprecate gui.JosmUserIdentityManager - replaced by data.UserIdentityManager

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
4import java.awt.Component;
5import java.net.Authenticator.RequestorType;
6import java.net.PasswordAuthentication;
7import java.util.Objects;
8
9import org.openstreetmap.josm.data.UserIdentityManager;
10import org.openstreetmap.josm.data.oauth.OAuthToken;
11import org.openstreetmap.josm.io.OsmApi;
12import org.openstreetmap.josm.tools.CheckParameterUtil;
13import 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 */
21public 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 @FunctionalInterface
46 public interface CredentialsAgentFactory {
47 CredentialsAgent getCredentialsAgent();
48 }
49
50 /**
51 * Plugins can register a CredentialsAgentFactory, thereby overriding
52 * JOSM's default credentials agent.
53 * @param agentFactory The Factory that provides the custom CredentialsAgent.
54 * Can be null to clear the factory and switch back to default behavior.
55 */
56 public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
57 CredentialsManager.agentFactory = agentFactory;
58 CredentialsManager.instance = null;
59 }
60
61 /* non-static fields and methods */
62
63 /**
64 * The credentials agent doing the real stuff
65 */
66 private final CredentialsAgent delegate;
67
68 /**
69 * Constructs a new {@code CredentialsManager}.
70 * @param delegate The credentials agent backing this credential manager. Must not be {@code null}
71 */
72 public CredentialsManager(CredentialsAgent delegate) {
73 CheckParameterUtil.ensureParameterNotNull(delegate, "delegate");
74 this.delegate = delegate;
75 }
76
77 /**
78 * Returns type of credentials agent backing this credentials manager.
79 * @return The type of credentials agent
80 */
81 public final Class<? extends CredentialsAgent> getCredentialsAgentClass() {
82 return delegate.getClass();
83 }
84
85 /**
86 * Returns the username for OSM API
87 * @return the username for OSM API
88 */
89 public String getUsername() {
90 return getUsername(OsmApi.getOsmApi().getHost());
91 }
92
93 /**
94 * Returns the username for a given host
95 * @param host The host for which username is wanted
96 * @return The username for {@code host}
97 */
98 public String getUsername(String host) {
99 String username = null;
100 try {
101 PasswordAuthentication auth = lookup(RequestorType.SERVER, host);
102 if (auth != null) {
103 username = auth.getUserName();
104 }
105 } catch (CredentialsAgentException ex) {
106 Logging.debug(ex);
107 return null;
108 }
109 if (username == null) return null;
110 username = username.trim();
111 return username.isEmpty() ? null : username;
112 }
113
114 @Override
115 public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException {
116 return delegate.lookup(requestorType, host);
117 }
118
119 @Override
120 public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
121 if (requestorType == RequestorType.SERVER && Objects.equals(OsmApi.getOsmApi().getHost(), host)) {
122 String username = credentials.getUserName();
123 if (username != null && !username.trim().isEmpty()) {
124 UserIdentityManager.getInstance().setPartiallyIdentified(username);
125 }
126 }
127 delegate.store(requestorType, host, credentials);
128 }
129
130 @Override
131 public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse)
132 throws CredentialsAgentException {
133 return delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
134 }
135
136 @Override
137 public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException {
138 return delegate.lookupOAuthAccessToken();
139 }
140
141 @Override
142 public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException {
143 delegate.storeOAuthAccessToken(accessToken);
144 }
145
146 @Override
147 public Component getPreferencesDecorationPanel() {
148 return delegate.getPreferencesDecorationPanel();
149 }
150}
Note: See TracBrowser for help on using the repository browser.