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 |
|
---|
8 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
9 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
10 | import org.openstreetmap.josm.io.OsmApi;
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * CredentialManager is a factory for the single credential agent used.
|
---|
15 | *
|
---|
16 | * Currently, it defaults to replying an instance of {@see JosmPreferencesCredentialAgent}.
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class CredentialsManager implements CredentialsAgent {
|
---|
20 |
|
---|
21 | private static CredentialsManager instance;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Replies the single credential agent used in JOSM
|
---|
25 | *
|
---|
26 | * @return the single credential agent used in JOSM
|
---|
27 | */
|
---|
28 | static public CredentialsManager getInstance() {
|
---|
29 | if (instance == null) {
|
---|
30 | CredentialsAgent delegate;
|
---|
31 | if (agentFactory == null) {
|
---|
32 | delegate = new JosmPreferencesCredentialAgent();
|
---|
33 | } else {
|
---|
34 | delegate = agentFactory.getCredentialsAgent();
|
---|
35 | }
|
---|
36 | instance = new CredentialsManager(delegate);
|
---|
37 | }
|
---|
38 | return instance;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static CredentialsAgentFactory agentFactory;
|
---|
42 |
|
---|
43 | public interface CredentialsAgentFactory {
|
---|
44 | CredentialsAgent getCredentialsAgent();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Plugins can register a CredentialsAgentFactory, thereby overriding
|
---|
49 | * JOSM's default credentials agent.
|
---|
50 | * @param agentFactory The Factory that provides the custom CredentialsAgent.
|
---|
51 | * Can be null to clear the factory and switch back to default behavior.
|
---|
52 | */
|
---|
53 | public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
|
---|
54 | CredentialsManager.agentFactory = agentFactory;
|
---|
55 | CredentialsManager.instance = null;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*****
|
---|
59 | * non-static fields and methods
|
---|
60 | */
|
---|
61 |
|
---|
62 | private CredentialsAgent delegate;
|
---|
63 |
|
---|
64 | public CredentialsManager(CredentialsAgent delegate) {
|
---|
65 | this.delegate = delegate;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getUsername() {
|
---|
69 | return getUsername(OsmApi.getOsmApi().getHost());
|
---|
70 | }
|
---|
71 |
|
---|
72 | public String getUsername(String host) {
|
---|
73 | String username = null;
|
---|
74 | try {
|
---|
75 | PasswordAuthentication auth = lookup(RequestorType.SERVER, host);
|
---|
76 | if (auth != null) {
|
---|
77 | username = auth.getUserName();
|
---|
78 | }
|
---|
79 | } catch (CredentialsAgentException ex) {
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 | if (username == null) return null;
|
---|
83 | username = username.trim();
|
---|
84 | return Utils.equal(username, "") ? null : username;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException {
|
---|
89 | return delegate.lookup(requestorType, host);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
|
---|
94 | if (requestorType == RequestorType.SERVER && OsmApi.getOsmApi().getHost().equals(host)) {
|
---|
95 | String username = credentials.getUserName();
|
---|
96 | if(username != null && !username.trim().isEmpty()) {
|
---|
97 | JosmUserIdentityManager.getInstance().setPartiallyIdentified(username);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | delegate.store(requestorType, host, credentials);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException {
|
---|
105 | return delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException {
|
---|
110 | return delegate.lookupOAuthAccessToken();
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException {
|
---|
115 | delegate.storeOAuthAccessToken(accessToken);
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public Component getPreferencesDecorationPanel() {
|
---|
120 | return delegate.getPreferencesDecorationPanel();
|
---|
121 | }
|
---|
122 | }
|
---|