1 package org
.argeo
.cms
.widgets
.auth
;
3 import static org
.argeo
.cms
.CmsMsg
.password
;
4 import static org
.argeo
.cms
.CmsMsg
.username
;
5 import static org
.argeo
.cms
.auth
.AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
;
6 import static org
.argeo
.cms
.auth
.AuthConstants
.LOGIN_CONTEXT_USER
;
7 import static org
.argeo
.cms
.internal
.kernel
.Activator
.getKernelHeader
;
9 import java
.io
.IOException
;
10 import java
.util
.List
;
11 import java
.util
.Locale
;
13 import javax
.security
.auth
.Subject
;
14 import javax
.security
.auth
.callback
.Callback
;
15 import javax
.security
.auth
.callback
.CallbackHandler
;
16 import javax
.security
.auth
.callback
.LanguageCallback
;
17 import javax
.security
.auth
.callback
.NameCallback
;
18 import javax
.security
.auth
.callback
.PasswordCallback
;
19 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
20 import javax
.security
.auth
.login
.FailedLoginException
;
21 import javax
.security
.auth
.login
.LoginContext
;
22 import javax
.security
.auth
.login
.LoginException
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.cms
.CmsMsg
;
27 import org
.argeo
.cms
.CmsStyles
;
28 import org
.argeo
.cms
.CmsView
;
29 import org
.argeo
.cms
.auth
.CurrentUser
;
30 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
31 import org
.argeo
.cms
.i18n
.LocaleUtils
;
32 import org
.argeo
.cms
.util
.CmsUtils
;
33 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
34 import org
.argeo
.util
.LocaleChoice
;
35 import org
.eclipse
.rap
.rwt
.RWT
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.events
.MouseAdapter
;
38 import org
.eclipse
.swt
.events
.MouseEvent
;
39 import org
.eclipse
.swt
.events
.SelectionAdapter
;
40 import org
.eclipse
.swt
.events
.SelectionEvent
;
41 import org
.eclipse
.swt
.events
.SelectionListener
;
42 import org
.eclipse
.swt
.events
.TraverseEvent
;
43 import org
.eclipse
.swt
.events
.TraverseListener
;
44 import org
.eclipse
.swt
.layout
.GridData
;
45 import org
.eclipse
.swt
.layout
.GridLayout
;
46 import org
.eclipse
.swt
.widgets
.Button
;
47 import org
.eclipse
.swt
.widgets
.Composite
;
48 import org
.eclipse
.swt
.widgets
.Control
;
49 import org
.eclipse
.swt
.widgets
.Label
;
50 import org
.eclipse
.swt
.widgets
.Shell
;
51 import org
.eclipse
.swt
.widgets
.Text
;
53 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
54 private final static Log log
= LogFactory
.getLog(CmsLogin
.class);
56 private Composite parent
;
57 private Text usernameT
, passwordT
;
58 private Composite credentialsBlock
;
59 private final SelectionListener loginSelectionListener
;
61 private final Locale defaultLocale
;
62 private LocaleChoice localeChoice
= null;
64 private final CmsView cmsView
;
66 public CmsLogin(CmsView cmsView
) {
67 this.cmsView
= cmsView
;
68 defaultLocale
= getKernelHeader().getDefaultLocale();
69 List
<Locale
> locales
= getKernelHeader().getLocales();
71 localeChoice
= new LocaleChoice(locales
, defaultLocale
);
72 loginSelectionListener
= new SelectionListener() {
73 private static final long serialVersionUID
= -8832133363830973578L;
76 public void widgetSelected(SelectionEvent e
) {
81 public void widgetDefaultSelected(SelectionEvent e
) {
86 protected boolean isAnonymous() {
87 return CurrentUser
.isAnonymous(cmsView
.getSubject());
90 public final void createUi(Composite parent
) {
92 createContents(parent
);
95 protected void createContents(Composite parent
) {
96 defaultCreateContents(parent
);
99 public final void defaultCreateContents(Composite parent
) {
100 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
101 Composite credentialsBlock
= createCredentialsBlock(parent
);
102 if (parent
instanceof Shell
) {
103 credentialsBlock
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
,
108 public final Composite
createCredentialsBlock(Composite parent
) {
110 return anonymousUi(parent
);
112 return userUi(parent
);
116 protected Composite
getCredentialsBlock() {
117 return credentialsBlock
;
120 protected Composite
userUi(Composite parent
) {
121 Locale locale
= localeChoice
== null ?
this.defaultLocale
122 : localeChoice
.getSelectedLocale();
123 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
124 credentialsBlock
.setLayout(new GridLayout());
125 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
127 specificUserUi(credentialsBlock
);
129 Label l
= new Label(credentialsBlock
, SWT
.NONE
);
130 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
131 l
.setText(CmsMsg
.logout
.lead(locale
));
132 GridData lData
= CmsUtils
.fillWidth();
133 lData
.widthHint
= 120;
134 l
.setLayoutData(lData
);
136 l
.addMouseListener(new MouseAdapter() {
137 private static final long serialVersionUID
= 6444395812777413116L;
139 public void mouseDown(MouseEvent e
) {
143 return credentialsBlock
;
146 /** To be overridden */
147 protected void specificUserUi(Composite parent
) {
151 protected Composite
anonymousUi(Composite parent
) {
152 Locale locale
= localeChoice
== null ?
this.defaultLocale
153 : localeChoice
.getSelectedLocale();
154 // We need a composite for the traversal
155 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
156 credentialsBlock
.setLayout(new GridLayout());
157 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
159 Integer textWidth
= 120;
160 parent
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
162 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
163 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
164 usernameT
.setMessage(username
.lead(locale
));
165 usernameT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
166 GridData gd
= CmsUtils
.fillWidth();
167 gd
.widthHint
= textWidth
;
168 usernameT
.setLayoutData(gd
);
170 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
171 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
172 passwordT
.setMessage(password
.lead(locale
));
173 passwordT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
174 gd
= CmsUtils
.fillWidth();
175 gd
.widthHint
= textWidth
;
176 passwordT
.setLayoutData(gd
);
178 TraverseListener tl
= new TraverseListener() {
179 private static final long serialVersionUID
= -1158892811534971856L;
181 public void keyTraversed(TraverseEvent e
) {
182 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
186 credentialsBlock
.addTraverseListener(tl
);
187 usernameT
.addTraverseListener(tl
);
188 passwordT
.addTraverseListener(tl
);
189 parent
.setTabList(new Control
[] { credentialsBlock
});
190 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
191 // credentialsBlock.setFocus();
193 extendsCredentialsBlock(credentialsBlock
, locale
,
194 loginSelectionListener
);
195 if (localeChoice
!= null)
196 createLocalesBlock(credentialsBlock
);
197 return credentialsBlock
;
201 * To be overridden in order to provide custome login button and other
204 protected void extendsCredentialsBlock(Composite credentialsBlock
,
205 Locale selectedLocale
, SelectionListener loginSelectionListener
) {
209 protected void updateLocale(Locale selectedLocale
) {
210 // save already entered values
211 String usernameStr
= usernameT
.getText();
212 char[] pwd
= passwordT
.getTextChars();
214 for (Control child
: parent
.getChildren())
216 createContents(parent
);
217 if (parent
.getParent() != null)
218 parent
.getParent().layout();
221 usernameT
.setText(usernameStr
);
222 passwordT
.setTextChars(pwd
);
225 protected Composite
createLocalesBlock(final Composite parent
) {
226 Composite c
= new Composite(parent
, SWT
.NONE
);
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
238 updateLocale(localeChoice
.getSelectedLocale());
243 List
<Locale
> locales
= localeChoice
.getLocales();
244 for (Integer i
= 0; i
< locales
.size(); i
++) {
245 Locale locale
= locales
.get(i
);
246 Button button
= new Button(c
, SWT
.RADIO
);
248 button
.setText(LocaleUtils
.lead(locale
.getDisplayName(locale
),
249 locale
) + " (" + locale
+ ")");
250 // button.addListener(SWT.Selection, listener);
251 button
.addSelectionListener(selectionListener
);
252 if (i
== localeChoice
.getSelectedIndex())
253 button
.setSelection(true);
258 protected boolean login() {
259 Subject subject
= cmsView
.getSubject();
260 LoginContext loginContext
;
265 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
266 loginContext
= new LoginContext(LOGIN_CONTEXT_USER
, subject
, this);
267 loginContext
.login();
268 } catch (FailedLoginException e
) {
269 log
.warn(e
.getMessage());
272 } catch (InterruptedException e2
) {
275 ErrorFeedback
.show("Login failed", e
);
277 } catch (LoginException e
) {
278 log
.error("Cannot login", e
);
281 cmsView
.authChange(loginContext
);
285 protected void logout() {
287 cmsView
.navigateTo("~");
291 public void handle(Callback
[] callbacks
) throws IOException
,
292 UnsupportedCallbackException
{
293 for (Callback callback
: callbacks
) {
294 if (callback
instanceof NameCallback
)
295 ((NameCallback
) callback
).setName(usernameT
.getText());
296 else if (callback
instanceof PasswordCallback
)
297 ((PasswordCallback
) callback
).setPassword(passwordT
299 else if (callback
instanceof HttpRequestCallback
)
300 ((HttpRequestCallback
) callback
).setRequest(RWT
.getRequest());
301 else if (callback
instanceof LanguageCallback
302 && localeChoice
!= null)
303 ((LanguageCallback
) callback
).setLocale(localeChoice
304 .getSelectedLocale());