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 @ 47828
History | View | Annotate | Download (9.15 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.DownloaderLocator; |
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.JsonObjectBuilder; |
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.dynobject.DynStruct; |
19 |
import org.gvsig.tools.persistence.PersistenceManager; |
20 |
import org.gvsig.tools.persistence.Persistent; |
21 |
import org.gvsig.tools.persistence.PersistentState; |
22 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
24 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
25 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
26 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
27 |
import org.gvsig.tools.util.LabeledValue; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
public class DownloaderAuthenticationKeycloakConfig |
34 |
extends AbstractDownloaderAuthenticationConfig
|
35 |
implements DownloaderAuthenticationConfig, Persistent, LabeledValue<DownloaderAuthenticationConfig>
|
36 |
{ |
37 |
|
38 |
private static final String DEFAULT_CLIENTID = "gvsigdesktop"; |
39 |
private static final String DEFAULT_SCOPE = "openid"; |
40 |
private static final int DEFAULT_LOCALPORT = 8888; |
41 |
|
42 |
private static final String PERSISTENT_DEFINITION_NAME = "UserIdentificationKeycloakConfig"; |
43 |
|
44 |
private String clientid; |
45 |
private String realm; |
46 |
private String baseurl; |
47 |
private int localPort; |
48 |
private String scope; |
49 |
|
50 |
public DownloaderAuthenticationKeycloakConfig(DownloaderAuthenticationKeycloakFactory factory, String urlOnline) { |
51 |
super(factory,urlOnline);
|
52 |
this.clientid = DEFAULT_CLIENTID;
|
53 |
this.scope = DEFAULT_SCOPE;
|
54 |
this.localPort = DEFAULT_LOCALPORT;
|
55 |
this.realm = null; |
56 |
this.baseurl = null; |
57 |
} |
58 |
|
59 |
public DownloaderAuthenticationKeycloakConfig() {
|
60 |
this((DownloaderAuthenticationKeycloakFactory)null, null); |
61 |
} |
62 |
|
63 |
@Override
|
64 |
public DownloaderAuthenticationKeycloakFactory getFactory() {
|
65 |
return (DownloaderAuthenticationKeycloakFactory) super.getFactory(); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public String getKey() { |
70 |
StringBuilder builder = new StringBuilder(); |
71 |
builder.append("localport=").append(this.localPort).append(";"); |
72 |
builder.append("clientid=").append(this.clientid).append(";"); |
73 |
builder.append("scope=").append(this.scope).append(";"); |
74 |
builder.append("realm=").append(this.realm).append(";"); |
75 |
builder.append("baseurl=").append(this.baseurl).append(";"); |
76 |
return builder.toString();
|
77 |
} |
78 |
|
79 |
public int getLocalPort() { |
80 |
return localPort;
|
81 |
} |
82 |
|
83 |
public String getBaseurl() { |
84 |
return baseurl;
|
85 |
} |
86 |
|
87 |
public String getRealm() { |
88 |
return realm;
|
89 |
} |
90 |
|
91 |
public String getClientid() { |
92 |
return clientid;
|
93 |
} |
94 |
|
95 |
public String getScope() { |
96 |
return scope;
|
97 |
} |
98 |
|
99 |
public void setBaseurl(String baseurl) { |
100 |
this.baseurl = baseurl;
|
101 |
} |
102 |
|
103 |
public void setLocalPort(int localPort) { |
104 |
this.localPort = localPort;
|
105 |
} |
106 |
|
107 |
public void setRealm(String realm) { |
108 |
this.realm = realm;
|
109 |
} |
110 |
|
111 |
public void setClientid(String clientid) { |
112 |
this.clientid = clientid;
|
113 |
} |
114 |
|
115 |
public void setScope(String scope) { |
116 |
this.scope = scope;
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public DownloaderAuthenticationKeycloakRequester create() {
|
121 |
if( StringUtils.isBlank(clientid) ) {
|
122 |
return null; |
123 |
} |
124 |
if( StringUtils.isBlank(realm) ) {
|
125 |
return null; |
126 |
} |
127 |
if( StringUtils.isBlank(baseurl) ) {
|
128 |
return null; |
129 |
} |
130 |
if( localPort<1024 ) { |
131 |
return null; |
132 |
} |
133 |
return new DownloaderAuthenticationKeycloakRequester(this); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public boolean isConfigurable() { |
138 |
return true; |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public boolean requestAuthenticationConfig() { |
143 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
144 |
DownloaderAuthenticationKeycloakConfigPanel panel = new DownloaderAuthenticationKeycloakConfigPanel();
|
145 |
panel.put(this);
|
146 |
Dialog dialog = winmgr.createDialog(
|
147 |
panel, |
148 |
"Configuracion de Keycloak",
|
149 |
null,
|
150 |
WindowManager_v2.BUTTONS_OK_CANCEL |
151 |
); |
152 |
dialog.addActionListener((ActionEvent e) -> {
|
153 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
154 |
panel.fetch(this);
|
155 |
} |
156 |
}); |
157 |
dialog.show(WindowManager.MODE.DIALOG); |
158 |
return dialog.getAction()==WindowManager_v2.BUTTON_OK;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public void fromJson(JsonObject json) { |
163 |
DownloaderManager manager = DownloaderLocator.getDownloaderManager(); |
164 |
String type = json.getString("type", null); |
165 |
if( type!=null ) { |
166 |
if( !StringUtils.equalsIgnoreCase(type, "OAuth") ) { |
167 |
throw new IllegalArgumentException("Invalid value of type '"+type+"' for '"+AUTH_MODE_KEYCLOAK+"'."); |
168 |
} |
169 |
this.factory = manager.getDownloaderAuthenticationFactory(AUTH_MODE_KEYCLOAK);
|
170 |
this.realm = json.getString("realm", null); |
171 |
this.baseurl = json.getString("url", null); |
172 |
if( StringUtils.isNotBlank(this.baseurl) ) { |
173 |
if( this.baseurl.endsWith("/") ) { |
174 |
this.baseurl = this.baseurl+"realms"; |
175 |
} else {
|
176 |
this.baseurl = this.baseurl+"/realms"; |
177 |
} |
178 |
} |
179 |
} else {
|
180 |
super.fromJson(json);
|
181 |
this.clientid = json.getString("clientid", null); |
182 |
this.scope = json.getString("scope", null); |
183 |
this.realm = json.getString("realm", null); |
184 |
this.baseurl = json.getString("baseurl", null); |
185 |
this.localPort = json.getInt("localPort", 0); |
186 |
} |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public JsonObjectBuilder toJsonBuilder() {
|
191 |
JsonObjectBuilder builder = super.toJsonBuilder();
|
192 |
builder.add("clientid", this.clientid); |
193 |
builder.add("scope", this.scope); |
194 |
builder.add("realm", this.realm); |
195 |
builder.add("baseurl", this.baseurl); |
196 |
builder.add("localPort", this.localPort); |
197 |
return builder;
|
198 |
} |
199 |
|
200 |
@Override
|
201 |
public void saveToState(PersistentState state) throws PersistenceException { |
202 |
state.set("factory",this.factory.getProviderName()); |
203 |
state.set("clientid",this.clientid); |
204 |
state.set("scope",this.scope); |
205 |
// state.set("urlOnline",this.urlOnline);
|
206 |
state.set("baseurl",this.baseurl); |
207 |
state.set("realm",this.realm); |
208 |
state.set("localPort",this.localPort); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public void loadFromState(PersistentState state) throws PersistenceException { |
213 |
String factoryName = state.getString("factory"); |
214 |
this.factory = DownloaderLocator.getDownloaderManager().getDownloaderAuthenticationFactory(factoryName);
|
215 |
this.clientid = state.getString("clientid"); |
216 |
this.scope = state.getString("scope"); |
217 |
// this.urlOnline = state.getString("urlOnline");
|
218 |
this.baseurl = 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 |
"UserIdentificationKeycloakConfig persistence definition", null, null |
229 |
); |
230 |
definition.addDynFieldString("factory");
|
231 |
definition.addDynFieldString("clientid");
|
232 |
definition.addDynFieldString("scope");
|
233 |
// definition.addDynFieldString("urlOnline");
|
234 |
definition.addDynFieldString("baseurl");
|
235 |
definition.addDynFieldString("realm");
|
236 |
definition.addDynFieldInt("localPort");
|
237 |
} |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public String getLabel() { |
242 |
return String.format("%s - %s", this.baseurl, this.realm); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public DownloaderAuthenticationKeycloakConfig getValue() {
|
247 |
return this; |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public String toString() { |
252 |
return String.format( |
253 |
// "Factory: %s\ngvSIG online URL: %s\nKeycloak URL:%s\nRealm: %s\nLocal port:%d",
|
254 |
"Factory: %s\nKeycloak URL:%s\nRealm: %s\nLocal port:%d",
|
255 |
this.getFactory().getProviderName(),
|
256 |
// this.getUrlOnline(),
|
257 |
this.getBaseurl(),
|
258 |
this.getRealm(),
|
259 |
this.getLocalPort()
|
260 |
); |
261 |
} |
262 |
|
263 |
|
264 |
} |