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

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

see #8465 - replace Utils.equal by Objects.equals, new in Java 7

  • Property svn:eol-style set to native
File size: 4.9 KB
RevLine 
[2711]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.auth;
3
[4249]4import java.awt.Component;
[4246]5import java.net.Authenticator.RequestorType;
6import java.net.PasswordAuthentication;
[7083]7import java.util.Objects;
[4246]8
9import org.openstreetmap.josm.data.oauth.OAuthToken;
[4263]10import org.openstreetmap.josm.gui.JosmUserIdentityManager;
[4690]11import org.openstreetmap.josm.io.OsmApi;
[6349]12import org.openstreetmap.josm.tools.CheckParameterUtil;
[4246]13
[2711]14/**
[4245]15 * CredentialManager is a factory for the single credential agent used.
[2801]16 *
[5266]17 * Currently, it defaults to replying an instance of {@link JosmPreferencesCredentialAgent}.
[6349]18 * @since 2641
[2711]19 */
[4246]20public class CredentialsManager implements CredentialsAgent {
[6070]21
[4246]22 private static CredentialsManager instance;
[2711]23
24 /**
[4245]25 * Replies the single credential agent used in JOSM
[2801]26 *
[4245]27 * @return the single credential agent used in JOSM
[2711]28 */
[6883]29 public static CredentialsManager getInstance() {
[3344]30 if (instance == null) {
[4246]31 CredentialsAgent delegate;
32 if (agentFactory == null) {
33 delegate = new JosmPreferencesCredentialAgent();
34 } else {
35 delegate = agentFactory.getCredentialsAgent();
36 }
37 instance = new CredentialsManager(delegate);
[3344]38 }
[2711]39 return instance;
40 }
[6070]41
[4246]42 private static CredentialsAgentFactory agentFactory;
43
44 public interface CredentialsAgentFactory {
45 CredentialsAgent getCredentialsAgent();
46 }
[6070]47
[4246]48 /**
49 * Plugins can register a CredentialsAgentFactory, thereby overriding
50 * JOSM's default credentials agent.
[4249]51 * @param agentFactory The Factory that provides the custom CredentialsAgent.
52 * Can be null to clear the factory and switch back to default behavior.
[4246]53 */
54 public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
55 CredentialsManager.agentFactory = agentFactory;
56 CredentialsManager.instance = null;
57 }
58
[6349]59 /* non-static fields and methods */
60
61 /**
62 * The credentials agent doing the real stuff
[4246]63 */
64 private CredentialsAgent delegate;
65
[6349]66 /**
67 * Constructs a new {@code CredentialsManager}.
68 * @param delegate The credentials agent backing this credential manager. Must not be {@code null}
69 */
[4246]70 public CredentialsManager(CredentialsAgent delegate) {
[6349]71 CheckParameterUtil.ensureParameterNotNull(delegate, "delegate");
[4246]72 this.delegate = delegate;
73 }
[7083]74
[6349]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 }
[4246]82
[6349]83 /**
84 * Returns the username for OSM API
85 * @return the username for OSM API
86 */
[4263]87 public String getUsername() {
[4690]88 return getUsername(OsmApi.getOsmApi().getHost());
89 }
90
[6349]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 */
[4690]96 public String getUsername(String host) {
[4263]97 String username = null;
98 try {
[4690]99 PasswordAuthentication auth = lookup(RequestorType.SERVER, host);
[4263]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();
[7083]108 return username.isEmpty() ? null : username;
[4263]109 }
110
[4246]111 @Override
[4690]112 public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException {
113 return delegate.lookup(requestorType, host);
[4246]114 }
115
116 @Override
[4690]117 public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
[7083]118 if (requestorType == RequestorType.SERVER && Objects.equals(OsmApi.getOsmApi().getHost(), host)) {
[4690]119 String username = credentials.getUserName();
120 if(username != null && !username.trim().isEmpty()) {
121 JosmUserIdentityManager.getInstance().setPartiallyIdentified(username);
122 }
[4263]123 }
[4690]124 delegate.store(requestorType, host, credentials);
[4246]125 }
126
127 @Override
[4690]128 public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException {
129 return delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
[4246]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 }
[4249]141
142 @Override
143 public Component getPreferencesDecorationPanel() {
144 return delegate.getPreferencesDecorationPanel();
145 }
[2711]146}
Note: See TracBrowser for help on using the repository browser.