]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/CmsLogin.java
Introduce OSGi-filter based context function
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / widgets / auth / CmsLogin.java
1 package org.argeo.cms.widgets.auth;
2
3 import static org.argeo.cms.CmsMsg.password;
4 import static org.argeo.cms.CmsMsg.username;
5
6 import java.io.IOException;
7 import java.util.List;
8 import java.util.Locale;
9
10 import javax.security.auth.Subject;
11 import javax.security.auth.callback.Callback;
12 import javax.security.auth.callback.CallbackHandler;
13 import javax.security.auth.callback.LanguageCallback;
14 import javax.security.auth.callback.NameCallback;
15 import javax.security.auth.callback.PasswordCallback;
16 import javax.security.auth.callback.UnsupportedCallbackException;
17 import javax.security.auth.login.LoginContext;
18 import javax.security.auth.login.LoginException;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.argeo.cms.CmsMsg;
23 import org.argeo.cms.auth.HttpRequestCallback;
24 import org.argeo.cms.i18n.LocaleUtils;
25 import org.argeo.cms.ui.CmsStyles;
26 import org.argeo.cms.ui.CmsView;
27 import org.argeo.cms.ui.internal.Activator;
28 import org.argeo.cms.util.CmsUtils;
29 import org.argeo.eclipse.ui.specific.UiContext;
30 import org.argeo.node.NodeConstants;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.MouseAdapter;
33 import org.eclipse.swt.events.MouseEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.events.TraverseEvent;
38 import org.eclipse.swt.events.TraverseListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.swt.widgets.Shell;
46 import org.eclipse.swt.widgets.Text;
47
48 public class CmsLogin implements CmsStyles, CallbackHandler {
49 private final static Log log = LogFactory.getLog(CmsLogin.class);
50
51 private Composite parent;
52 private Text usernameT, passwordT;
53 private Composite credentialsBlock;
54 private final SelectionListener loginSelectionListener;
55
56 private final Locale defaultLocale;
57 private LocaleChoice localeChoice = null;
58
59 private final CmsView cmsView;
60
61 // optional subject to be set explicitly
62 private Subject subject = null;
63
64 public CmsLogin(CmsView cmsView) {
65 this.cmsView = cmsView;
66 defaultLocale = Activator.getNodeState().getDefaultLocale();
67 List<Locale> locales = Activator.getNodeState().getLocales();
68 if (locales != null)
69 localeChoice = new LocaleChoice(locales, defaultLocale);
70 loginSelectionListener = new SelectionListener() {
71 private static final long serialVersionUID = -8832133363830973578L;
72
73 @Override
74 public void widgetSelected(SelectionEvent e) {
75 login();
76 }
77
78 @Override
79 public void widgetDefaultSelected(SelectionEvent e) {
80 }
81 };
82 }
83
84 protected boolean isAnonymous() {
85 return cmsView.isAnonymous();
86 }
87
88 public final void createUi(Composite parent) {
89 this.parent = parent;
90 createContents(parent);
91 }
92
93 protected void createContents(Composite parent) {
94 defaultCreateContents(parent);
95 }
96
97 public final void defaultCreateContents(Composite parent) {
98 parent.setLayout(CmsUtils.noSpaceGridLayout());
99 Composite credentialsBlock = createCredentialsBlock(parent);
100 if (parent instanceof Shell) {
101 credentialsBlock.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
102 }
103 }
104
105 public final Composite createCredentialsBlock(Composite parent) {
106 if (isAnonymous()) {
107 return anonymousUi(parent);
108 } else {
109 return userUi(parent);
110 }
111 }
112
113 protected Composite getCredentialsBlock() {
114 return credentialsBlock;
115 }
116
117 protected Composite userUi(Composite parent) {
118 Locale locale = localeChoice == null ? this.defaultLocale : localeChoice.getSelectedLocale();
119 credentialsBlock = new Composite(parent, SWT.NONE);
120 credentialsBlock.setLayout(new GridLayout());
121 credentialsBlock.setLayoutData(CmsUtils.fillAll());
122
123 specificUserUi(credentialsBlock);
124
125 Label l = new Label(credentialsBlock, SWT.NONE);
126 CmsUtils.style(l, CMS_USER_MENU_ITEM);
127 l.setText(CmsMsg.logout.lead(locale));
128 GridData lData = CmsUtils.fillWidth();
129 lData.widthHint = 120;
130 l.setLayoutData(lData);
131
132 l.addMouseListener(new MouseAdapter() {
133 private static final long serialVersionUID = 6444395812777413116L;
134
135 public void mouseDown(MouseEvent e) {
136 logout();
137 }
138 });
139 return credentialsBlock;
140 }
141
142 /** To be overridden */
143 protected void specificUserUi(Composite parent) {
144
145 }
146
147 protected Composite anonymousUi(Composite parent) {
148 Locale locale = localeChoice == null ? this.defaultLocale : localeChoice.getSelectedLocale();
149 // We need a composite for the traversal
150 credentialsBlock = new Composite(parent, SWT.NONE);
151 credentialsBlock.setLayout(new GridLayout());
152 credentialsBlock.setLayoutData(CmsUtils.fillAll());
153
154 Integer textWidth = 120;
155 CmsUtils.style(parent, CMS_USER_MENU);
156 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
157 usernameT = new Text(credentialsBlock, SWT.BORDER);
158 usernameT.setMessage(username.lead(locale));
159 CmsUtils.style(usernameT, CMS_LOGIN_DIALOG_USERNAME);
160 GridData gd = CmsUtils.fillWidth();
161 gd.widthHint = textWidth;
162 usernameT.setLayoutData(gd);
163
164 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
165 passwordT = new Text(credentialsBlock, SWT.BORDER | SWT.PASSWORD);
166 passwordT.setMessage(password.lead(locale));
167 CmsUtils.style(passwordT, CMS_LOGIN_DIALOG_PASSWORD);
168 gd = CmsUtils.fillWidth();
169 gd.widthHint = textWidth;
170 passwordT.setLayoutData(gd);
171
172 TraverseListener tl = new TraverseListener() {
173 private static final long serialVersionUID = -1158892811534971856L;
174
175 public void keyTraversed(TraverseEvent e) {
176 if (e.detail == SWT.TRAVERSE_RETURN)
177 login();
178 }
179 };
180 credentialsBlock.addTraverseListener(tl);
181 usernameT.addTraverseListener(tl);
182 passwordT.addTraverseListener(tl);
183 parent.setTabList(new Control[] { credentialsBlock });
184 credentialsBlock.setTabList(new Control[] { usernameT, passwordT });
185 // credentialsBlock.setFocus();
186
187 extendsCredentialsBlock(credentialsBlock, locale, loginSelectionListener);
188 if (localeChoice != null)
189 createLocalesBlock(credentialsBlock);
190 return credentialsBlock;
191 }
192
193 /**
194 * To be overridden in order to provide custom login button and other links.
195 */
196 protected void extendsCredentialsBlock(Composite credentialsBlock, Locale selectedLocale,
197 SelectionListener loginSelectionListener) {
198
199 }
200
201 protected void updateLocale(Locale selectedLocale) {
202 // save already entered values
203 String usernameStr = usernameT.getText();
204 char[] pwd = passwordT.getTextChars();
205
206 for (Control child : parent.getChildren())
207 child.dispose();
208 createContents(parent);
209 if (parent.getParent() != null)
210 parent.getParent().layout();
211 else
212 parent.layout();
213 usernameT.setText(usernameStr);
214 passwordT.setTextChars(pwd);
215 }
216
217 protected Composite createLocalesBlock(final Composite parent) {
218 Composite c = new Composite(parent, SWT.NONE);
219 c.setLayout(CmsUtils.noSpaceGridLayout());
220 c.setLayoutData(CmsUtils.fillAll());
221
222 SelectionListener selectionListener = new SelectionAdapter() {
223 private static final long serialVersionUID = 4891637813567806762L;
224
225 public void widgetSelected(SelectionEvent event) {
226 Button button = (Button) event.widget;
227 if (button.getSelection()) {
228 localeChoice.setSelectedIndex((Integer) event.widget.getData());
229 updateLocale(localeChoice.getSelectedLocale());
230 }
231 };
232 };
233
234 List<Locale> locales = localeChoice.getLocales();
235 for (Integer i = 0; i < locales.size(); i++) {
236 Locale locale = locales.get(i);
237 Button button = new Button(c, SWT.RADIO);
238 button.setData(i);
239 button.setText(LocaleUtils.lead(locale.getDisplayName(locale), locale) + " (" + locale + ")");
240 // button.addListener(SWT.Selection, listener);
241 button.addSelectionListener(selectionListener);
242 if (i == localeChoice.getSelectedIndex())
243 button.setSelection(true);
244 }
245 return c;
246 }
247
248 protected boolean login() {
249 // TODO use CmsVie in order to retrieve subject?
250 // Subject subject = cmsView.getLoginContext().getSubject();
251 // LoginContext loginContext = cmsView.getLoginContext();
252 try {
253 //
254 // LOGIN
255 //
256 // loginContext.logout();
257 LoginContext loginContext;
258 if (subject == null)
259 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, this);
260 else
261 loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, this);
262 loginContext.login();
263 cmsView.authChange(loginContext);
264 return true;
265 } catch (LoginException e) {
266 if (log.isTraceEnabled())
267 log.warn("Login failed: " + e.getMessage(), e);
268 else
269 log.warn("Login failed: " + e.getMessage());
270
271 try {
272 Thread.sleep(3000);
273 } catch (InterruptedException e2) {
274 // silent
275 }
276 // ErrorFeedback.show("Login failed", e);
277 return false;
278 }
279 // catch (LoginException e) {
280 // log.error("Cannot login", e);
281 // return false;
282 // }
283 }
284
285 protected void logout() {
286 cmsView.logout();
287 cmsView.navigateTo("~");
288 }
289
290 @Override
291 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
292 for (Callback callback : callbacks) {
293 if (callback instanceof NameCallback && usernameT != null)
294 ((NameCallback) callback).setName(usernameT.getText());
295 else if (callback instanceof PasswordCallback && passwordT != null)
296 ((PasswordCallback) callback).setPassword(passwordT.getTextChars());
297 else if (callback instanceof HttpRequestCallback) {
298 ((HttpRequestCallback) callback).setRequest(UiContext.getHttpRequest());
299 ((HttpRequestCallback) callback).setResponse(UiContext.getHttpResponse());
300 } else if (callback instanceof LanguageCallback) {
301 Locale toUse = null;
302 if (localeChoice != null)
303 toUse = localeChoice.getSelectedLocale();
304 else if (defaultLocale != null)
305 toUse = defaultLocale;
306
307 if (toUse != null) {
308 ((LanguageCallback) callback).setLocale(toUse);
309 UiContext.setLocale(toUse);
310 }
311
312 }
313 }
314 }
315
316 public void setSubject(Subject subject) {
317 this.subject = subject;
318 }
319
320 }