svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.swing / org.gvsig.downloader.swing.scribejava / src / main / java / org / gvsig / downloader / swing / scribejava / keycloak / DownloaderAuthenticationKeycloakConfig.java @ 47841
History | View | Annotate | Download (10.4 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.downloader.swing.scribejava.keycloak; |
7 |
|
8 |
import java.awt.event.ActionEvent; |
9 |
import javax.json.JsonObject; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
12 |
import org.gvsig.downloader.DownloaderCredentials; |
13 |
import org.gvsig.downloader.DownloaderManager; |
14 |
import org.gvsig.downloader.spi.AbstractDownloaderAuthenticationConfig; |
15 |
import static org.gvsig.downloader.swing.scribejava.keycloak.DownloaderAuthenticationKeycloakFactory.AUTH_MODE_KEYCLOAK; |
16 |
import org.gvsig.json.Json; |
17 |
import org.gvsig.json.JsonObjectBuilder; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.dynobject.DynStruct; |
20 |
import org.gvsig.tools.persistence.PersistenceManager; |
21 |
import org.gvsig.tools.persistence.Persistent; |
22 |
import org.gvsig.tools.persistence.PersistentState; |
23 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
24 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
25 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
26 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
27 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
28 |
import org.gvsig.tools.util.LabeledValue; |
29 |
|
30 |
/**
|
31 |
*
|
32 |
* @author jjdelcerro
|
33 |
*/
|
34 |
public class DownloaderAuthenticationKeycloakConfig |
35 |
extends AbstractDownloaderAuthenticationConfig
|
36 |
implements DownloaderAuthenticationConfig, LabeledValue<DownloaderAuthenticationConfig>
|
37 |
{ |
38 |
|
39 |
private static final String DEFAULT_CLIENTID = "gvsigdesktop"; |
40 |
private static final String DEFAULT_SCOPE = "openid"; |
41 |
private static final int DEFAULT_LOCALPORT = 8888; |
42 |
|
43 |
private static final String PERSISTENT_DEFINITION_NAME = "UserIdentificationKeycloakConfig"; |
44 |
|
45 |
private String clientid; |
46 |
private String realm; |
47 |
private String keycloakBaseurl; |
48 |
private int localPort; |
49 |
private String scope; |
50 |
|
51 |
public DownloaderAuthenticationKeycloakConfig(DownloaderAuthenticationKeycloakFactory factory, String serviceUrl) { |
52 |
super(AUTH_MODE_KEYCLOAK, factory,serviceUrl);
|
53 |
this.clientid = DEFAULT_CLIENTID;
|
54 |
this.scope = DEFAULT_SCOPE;
|
55 |
this.localPort = DEFAULT_LOCALPORT;
|
56 |
this.realm = null; |
57 |
this.keycloakBaseurl = null; |
58 |
} |
59 |
|
60 |
public DownloaderAuthenticationKeycloakConfig() {
|
61 |
this((DownloaderAuthenticationKeycloakFactory)null, null); |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public DownloaderAuthenticationKeycloakFactory getFactory() {
|
66 |
return (DownloaderAuthenticationKeycloakFactory) super.getFactory(); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public String getKey() { |
71 |
StringBuilder builder = new StringBuilder(); |
72 |
builder.append("ServiceUrl=").append(this.getServiceUrl()).append(";"); |
73 |
builder.append("localport=").append(this.localPort).append(";"); |
74 |
builder.append("clientid=").append(this.clientid).append(";"); |
75 |
builder.append("scope=").append(this.scope).append(";"); |
76 |
builder.append("realm=").append(this.realm).append(";"); |
77 |
builder.append("baseurl=").append(this.keycloakBaseurl).append(";"); |
78 |
return builder.toString();
|
79 |
} |
80 |
|
81 |
public int getLocalPort() { |
82 |
return localPort;
|
83 |
} |
84 |
|
85 |
public String getKeycloakBaseurl() { |
86 |
return keycloakBaseurl;
|
87 |
} |
88 |
|
89 |
public String getRealm() { |
90 |
return realm;
|
91 |
} |
92 |
|
93 |
public String getClientid() { |
94 |
return clientid;
|
95 |
} |
96 |
|
97 |
public String getScope() { |
98 |
return scope;
|
99 |
} |
100 |
|
101 |
public void setKeycloakBaseurl(String keycloakBaseurl) { |
102 |
this.keycloakBaseurl = keycloakBaseurl;
|
103 |
} |
104 |
|
105 |
public void setLocalPort(int localPort) { |
106 |
this.localPort = localPort;
|
107 |
} |
108 |
|
109 |
public void setRealm(String realm) { |
110 |
this.realm = realm;
|
111 |
} |
112 |
|
113 |
public void setClientid(String clientid) { |
114 |
this.clientid = clientid;
|
115 |
} |
116 |
|
117 |
public void setScope(String scope) { |
118 |
this.scope = scope;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public DownloaderAuthenticationKeycloakRequester create() {
|
123 |
if( StringUtils.isBlank(clientid) ) {
|
124 |
return null; |
125 |
} |
126 |
if( StringUtils.isBlank(realm) ) {
|
127 |
return null; |
128 |
} |
129 |
if( StringUtils.isBlank(keycloakBaseurl) ) {
|
130 |
return null; |
131 |
} |
132 |
if( localPort<1024 ) { |
133 |
return null; |
134 |
} |
135 |
return new DownloaderAuthenticationKeycloakRequester(this); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public boolean isConfigurable() { |
140 |
return true; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public boolean requestAuthenticationConfig() { |
145 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
146 |
DownloaderAuthenticationKeycloakConfigPanel panel = new DownloaderAuthenticationKeycloakConfigPanel();
|
147 |
panel.put(this);
|
148 |
Dialog dialog = winmgr.createDialog(
|
149 |
panel, |
150 |
"Configuracion de Keycloak",
|
151 |
null,
|
152 |
WindowManager_v2.BUTTONS_OK_CANCEL |
153 |
); |
154 |
dialog.addActionListener((ActionEvent e) -> {
|
155 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
156 |
panel.fetch(this);
|
157 |
} |
158 |
}); |
159 |
dialog.show(WindowManager.MODE.DIALOG); |
160 |
return dialog.getAction()==WindowManager_v2.BUTTON_OK;
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void fromJson(JsonObject json) { |
165 |
// DownloaderManager manager = DownloaderLocator.getDownloaderManager();
|
166 |
String type = json.getString("type", null); |
167 |
if( type!=null ) { |
168 |
if( !StringUtils.equalsIgnoreCase(type, "OAuth") ) { |
169 |
throw new IllegalArgumentException("Invalid value of type '"+type+"' for '"+AUTH_MODE_KEYCLOAK+"'."); |
170 |
} |
171 |
// this.factory = manager.getDownloaderAuthenticationFactory(AUTH_MODE_KEYCLOAK);
|
172 |
this.realm = json.getString("realm", null); |
173 |
this.keycloakBaseurl = json.getString("url", null); |
174 |
if( StringUtils.isNotBlank(this.keycloakBaseurl) ) { |
175 |
if( this.keycloakBaseurl.endsWith("/") ) { |
176 |
this.keycloakBaseurl = this.keycloakBaseurl+"realms"; |
177 |
} else {
|
178 |
this.keycloakBaseurl = this.keycloakBaseurl+"/realms"; |
179 |
} |
180 |
} |
181 |
} else {
|
182 |
super.fromJson(json);
|
183 |
this.clientid = json.getString("clientid", null); |
184 |
this.scope = json.getString("scope", null); |
185 |
this.realm = json.getString("realm", null); |
186 |
this.keycloakBaseurl = json.getString("baseurl", null); |
187 |
this.localPort = json.getInt("localPort", 0); |
188 |
} |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public JsonObjectBuilder toJsonBuilder() {
|
193 |
JsonObjectBuilder builder = super.toJsonBuilder();
|
194 |
builder.add_class(this);
|
195 |
builder.add("clientid", this.clientid); |
196 |
builder.add("scope", this.scope); |
197 |
builder.add("realm", this.realm); |
198 |
builder.add("baseurl", this.keycloakBaseurl); |
199 |
builder.add("localPort", this.localPort); |
200 |
return builder;
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void saveToState(PersistentState state) throws PersistenceException { |
205 |
super.saveToState(state);
|
206 |
state.set("clientid",this.clientid); |
207 |
state.set("scope",this.scope); |
208 |
state.set("baseurl",this.keycloakBaseurl); |
209 |
state.set("realm",this.realm); |
210 |
state.set("localPort",this.localPort); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void loadFromState(PersistentState state) throws PersistenceException { |
215 |
super.loadFromState(state);
|
216 |
this.clientid = state.getString("clientid"); |
217 |
this.scope = state.getString("scope"); |
218 |
this.keycloakBaseurl = state.getString("baseurl"); |
219 |
this.realm = state.getString("realm"); |
220 |
this.localPort = state.getInt("localPort"); |
221 |
} |
222 |
|
223 |
public static void selfRegister() { |
224 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
225 |
if (manager.getDefinition(PERSISTENT_DEFINITION_NAME) == null) { |
226 |
DynStruct definition = manager.addDefinition(DownloaderAuthenticationKeycloakConfig.class, |
227 |
PERSISTENT_DEFINITION_NAME, |
228 |
"DownloaderAuthenticationKeycloakConfig persistence definition", null, null |
229 |
); |
230 |
definition.extend( |
231 |
PersistenceManager.PERSISTENCE_NAMESPACE, |
232 |
DownloaderAuthenticationConfig.PERSISTENT_DEFINITION_NAME |
233 |
); |
234 |
definition.addDynFieldString("clientid");
|
235 |
definition.addDynFieldString("scope");
|
236 |
definition.addDynFieldString("baseurl");
|
237 |
definition.addDynFieldString("realm");
|
238 |
definition.addDynFieldInt("localPort");
|
239 |
} |
240 |
Json.registerSerializer(DownloaderAuthenticationKeycloakConfig.class); |
241 |
} |
242 |
|
243 |
@Override
|
244 |
public String getLabel() { |
245 |
return String.format("%s - %s", this.keycloakBaseurl, this.realm); |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public DownloaderAuthenticationKeycloakConfig getValue() {
|
250 |
return this; |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public String toString() { |
255 |
return String.format(// "Factory: %s\ngvSIG online URL: %s\nKeycloak URL:%s\nRealm: %s\nLocal port:%d", |
256 |
"Factory: %s\nKeycloak URL:%s\nRealm: %s\nLocal port:%d",
|
257 |
this.getFactory().getProviderName(),
|
258 |
// this.getUrlOnline(),
|
259 |
this.getKeycloakBaseurl(),
|
260 |
this.getRealm(),
|
261 |
this.getLocalPort()
|
262 |
); |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public DownloaderCredentials getCredentials(DownloaderCredentials credentials) {
|
267 |
if(!(credentials instanceof DownloaderKeycloakCredentials)){ |
268 |
return null; |
269 |
} |
270 |
DownloaderKeycloakCredentials theCredentials = (DownloaderKeycloakCredentials)credentials; |
271 |
DownloaderAuthenticationKeycloakConfig otherConfig = theCredentials.getConfig(); |
272 |
|
273 |
if (!DownloaderManager.areSameURLs(this.getKeycloakBaseurl(), otherConfig.getKeycloakBaseurl())) { |
274 |
return null; |
275 |
} |
276 |
if (!StringUtils.equals(this.getRealm(), otherConfig.getRealm())) { |
277 |
return null; |
278 |
} |
279 |
if (!StringUtils.equals(this.getClientid(), otherConfig.getClientid())) { |
280 |
return null; |
281 |
} |
282 |
if (!StringUtils.equals(this.getScope(), otherConfig.getScope())) { |
283 |
return null; |
284 |
} |
285 |
|
286 |
return new DownloaderKeycloakCredentials( |
287 |
this,
|
288 |
theCredentials.getToken(), |
289 |
theCredentials.getUserid(), |
290 |
theCredentials.getTime() |
291 |
); |
292 |
} |
293 |
|
294 |
|
295 |
} |