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

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

fix some Sonar issues

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