1 package org
.argeo
.cms
.widgets
.auth
;
3 import static org
.argeo
.cms
.CmsMsg
.password
;
4 import static org
.argeo
.cms
.CmsMsg
.username
;
6 import java
.io
.IOException
;
8 import java
.util
.Locale
;
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
;
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
.LocaleUtils
;
24 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
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
;
48 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
49 private final static Log log
= LogFactory
.getLog(CmsLogin
.class);
51 private Composite parent
;
52 private Text usernameT
, passwordT
;
53 private Composite credentialsBlock
;
54 private final SelectionListener loginSelectionListener
;
56 private final Locale defaultLocale
;
57 private LocaleChoice localeChoice
= null;
59 private final CmsView cmsView
;
61 // optional subject to be set explicitly
62 private Subject subject
= null;
64 public CmsLogin(CmsView cmsView
) {
65 this.cmsView
= cmsView
;
66 defaultLocale
= Activator
.getNodeState().getDefaultLocale();
67 List
<Locale
> locales
= Activator
.getNodeState().getLocales();
69 localeChoice
= new LocaleChoice(locales
, defaultLocale
);
70 loginSelectionListener
= new SelectionListener() {
71 private static final long serialVersionUID
= -8832133363830973578L;
74 public void widgetSelected(SelectionEvent e
) {
79 public void widgetDefaultSelected(SelectionEvent e
) {
84 protected boolean isAnonymous() {
85 return cmsView
.isAnonymous();
88 public final void createUi(Composite parent
) {
90 createContents(parent
);
93 protected void createContents(Composite parent
) {
94 defaultCreateContents(parent
);
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));
105 public final Composite
createCredentialsBlock(Composite parent
) {
107 return anonymousUi(parent
);
109 return userUi(parent
);
113 protected Composite
getCredentialsBlock() {
114 return credentialsBlock
;
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());
123 specificUserUi(credentialsBlock
);
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
);
132 l
.addMouseListener(new MouseAdapter() {
133 private static final long serialVersionUID
= 6444395812777413116L;
135 public void mouseDown(MouseEvent e
) {
139 return credentialsBlock
;
142 /** To be overridden */
143 protected void specificUserUi(Composite parent
) {
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 CmsUtils
.style(credentialsBlock
, CMS_LOGIN_DIALOG
);
155 Integer textWidth
= 120;
156 if (parent
instanceof Shell
)
157 CmsUtils
.style(parent
, CMS_USER_MENU
);
158 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
159 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
160 usernameT
.setMessage(username
.lead(locale
));
161 CmsUtils
.style(usernameT
, CMS_LOGIN_DIALOG_USERNAME
);
162 GridData gd
= CmsUtils
.fillWidth();
163 gd
.widthHint
= textWidth
;
164 usernameT
.setLayoutData(gd
);
166 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
167 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
168 passwordT
.setMessage(password
.lead(locale
));
169 CmsUtils
.style(passwordT
, CMS_LOGIN_DIALOG_PASSWORD
);
170 gd
= CmsUtils
.fillWidth();
171 gd
.widthHint
= textWidth
;
172 passwordT
.setLayoutData(gd
);
174 TraverseListener tl
= new TraverseListener() {
175 private static final long serialVersionUID
= -1158892811534971856L;
177 public void keyTraversed(TraverseEvent e
) {
178 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
182 credentialsBlock
.addTraverseListener(tl
);
183 usernameT
.addTraverseListener(tl
);
184 passwordT
.addTraverseListener(tl
);
185 parent
.setTabList(new Control
[] { credentialsBlock
});
186 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
189 Button loginButton
= new Button(credentialsBlock
, SWT
.PUSH
);
190 loginButton
.setText(CmsMsg
.login
.lead(locale
));
191 loginButton
.setLayoutData(CmsUtils
.fillWidth());
192 loginButton
.addSelectionListener(loginSelectionListener
);
194 extendsCredentialsBlock(credentialsBlock
, locale
, loginSelectionListener
);
195 if (localeChoice
!= null)
196 createLocalesBlock(credentialsBlock
);
197 return credentialsBlock
;
201 * To be overridden in order to provide custom login button and other links.
203 protected void extendsCredentialsBlock(Composite credentialsBlock
, Locale selectedLocale
,
204 SelectionListener loginSelectionListener
) {
208 protected void updateLocale(Locale selectedLocale
) {
209 // save already entered values
210 String usernameStr
= usernameT
.getText();
211 char[] pwd
= passwordT
.getTextChars();
213 for (Control child
: parent
.getChildren())
215 createContents(parent
);
216 if (parent
.getParent() != null)
217 parent
.getParent().layout();
220 usernameT
.setText(usernameStr
);
221 passwordT
.setTextChars(pwd
);
224 protected Composite
createLocalesBlock(final Composite parent
) {
225 Composite c
= new Composite(parent
, SWT
.NONE
);
226 CmsUtils
.style(c
, CMS_USER_MENU_ITEM
);
227 c
.setLayout(CmsUtils
.noSpaceGridLayout());
228 c
.setLayoutData(CmsUtils
.fillAll());
230 SelectionListener selectionListener
= new SelectionAdapter() {
231 private static final long serialVersionUID
= 4891637813567806762L;
233 public void widgetSelected(SelectionEvent event
) {
234 Button button
= (Button
) event
.widget
;
235 if (button
.getSelection()) {
236 localeChoice
.setSelectedIndex((Integer
) event
.widget
.getData());
237 updateLocale(localeChoice
.getSelectedLocale());
242 List
<Locale
> locales
= localeChoice
.getLocales();
243 for (Integer i
= 0; i
< locales
.size(); i
++) {
244 Locale locale
= locales
.get(i
);
245 Button button
= new Button(c
, SWT
.RADIO
);
246 CmsUtils
.style(button
, CMS_USER_MENU_ITEM
);
248 button
.setText(LocaleUtils
.lead(locale
.getDisplayName(locale
), locale
) + " (" + locale
+ ")");
249 // button.addListener(SWT.Selection, listener);
250 button
.addSelectionListener(selectionListener
);
251 if (i
== localeChoice
.getSelectedIndex())
252 button
.setSelection(true);
257 protected boolean login() {
258 // TODO use CmsVie in order to retrieve subject?
259 // Subject subject = cmsView.getLoginContext().getSubject();
260 // LoginContext loginContext = cmsView.getLoginContext();
265 // loginContext.logout();
266 LoginContext loginContext
;
268 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, this);
270 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, subject
, this);
271 loginContext
.login();
272 cmsView
.authChange(loginContext
);
274 } catch (LoginException e
) {
275 if (log
.isTraceEnabled())
276 log
.warn("Login failed: " + e
.getMessage(), e
);
278 log
.warn("Login failed: " + e
.getMessage());
282 } catch (InterruptedException e2
) {
285 // ErrorFeedback.show("Login failed", e);
288 // catch (LoginException e) {
289 // log.error("Cannot login", e);
294 protected void logout() {
296 cmsView
.navigateTo("~");
300 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
301 for (Callback callback
: callbacks
) {
302 if (callback
instanceof NameCallback
&& usernameT
!= null)
303 ((NameCallback
) callback
).setName(usernameT
.getText());
304 else if (callback
instanceof PasswordCallback
&& passwordT
!= null)
305 ((PasswordCallback
) callback
).setPassword(passwordT
.getTextChars());
306 else if (callback
instanceof HttpRequestCallback
) {
307 ((HttpRequestCallback
) callback
).setRequest(UiContext
.getHttpRequest());
308 ((HttpRequestCallback
) callback
).setResponse(UiContext
.getHttpResponse());
309 } else if (callback
instanceof LanguageCallback
) {
311 if (localeChoice
!= null)
312 toUse
= localeChoice
.getSelectedLocale();
313 else if (defaultLocale
!= null)
314 toUse
= defaultLocale
;
317 ((LanguageCallback
) callback
).setLocale(toUse
);
318 UiContext
.setLocale(toUse
);
325 public void setSubject(Subject subject
) {
326 this.subject
= subject
;