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
;
8 import java
.io
.IOException
;
10 import java
.util
.Locale
;
12 import javax
.security
.auth
.Subject
;
13 import javax
.security
.auth
.callback
.Callback
;
14 import javax
.security
.auth
.callback
.CallbackHandler
;
15 import javax
.security
.auth
.callback
.LanguageCallback
;
16 import javax
.security
.auth
.callback
.NameCallback
;
17 import javax
.security
.auth
.callback
.PasswordCallback
;
18 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
19 import javax
.security
.auth
.login
.FailedLoginException
;
20 import javax
.security
.auth
.login
.LoginContext
;
21 import javax
.security
.auth
.login
.LoginException
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.cms
.CmsMsg
;
26 import org
.argeo
.cms
.auth
.CurrentUser
;
27 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
28 import org
.argeo
.cms
.i18n
.LocaleUtils
;
29 import org
.argeo
.cms
.ui
.CmsStyles
;
30 import org
.argeo
.cms
.ui
.CmsView
;
31 import org
.argeo
.cms
.ui
.internal
.Activator
;
32 import org
.argeo
.cms
.util
.CmsUtils
;
33 import org
.eclipse
.rap
.rwt
.RWT
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.MouseAdapter
;
36 import org
.eclipse
.swt
.events
.MouseEvent
;
37 import org
.eclipse
.swt
.events
.SelectionAdapter
;
38 import org
.eclipse
.swt
.events
.SelectionEvent
;
39 import org
.eclipse
.swt
.events
.SelectionListener
;
40 import org
.eclipse
.swt
.events
.TraverseEvent
;
41 import org
.eclipse
.swt
.events
.TraverseListener
;
42 import org
.eclipse
.swt
.layout
.GridData
;
43 import org
.eclipse
.swt
.layout
.GridLayout
;
44 import org
.eclipse
.swt
.widgets
.Button
;
45 import org
.eclipse
.swt
.widgets
.Composite
;
46 import org
.eclipse
.swt
.widgets
.Control
;
47 import org
.eclipse
.swt
.widgets
.Label
;
48 import org
.eclipse
.swt
.widgets
.Shell
;
49 import org
.eclipse
.swt
.widgets
.Text
;
51 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
52 private final static Log log
= LogFactory
.getLog(CmsLogin
.class);
54 private Composite parent
;
55 private Text usernameT
, passwordT
;
56 private Composite credentialsBlock
;
57 private final SelectionListener loginSelectionListener
;
59 private final Locale defaultLocale
;
60 private LocaleChoice localeChoice
= null;
62 private final CmsView cmsView
;
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 CurrentUser
.isAnonymous(cmsView
.getSubject());
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 l
.setData(RWT
.CUSTOM_VARIANT
, 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());
154 Integer textWidth
= 120;
155 parent
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
157 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
158 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
159 usernameT
.setMessage(username
.lead(locale
));
160 usernameT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
161 GridData gd
= CmsUtils
.fillWidth();
162 gd
.widthHint
= textWidth
;
163 usernameT
.setLayoutData(gd
);
165 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
166 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
167 passwordT
.setMessage(password
.lead(locale
));
168 passwordT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
169 gd
= CmsUtils
.fillWidth();
170 gd
.widthHint
= textWidth
;
171 passwordT
.setLayoutData(gd
);
173 TraverseListener tl
= new TraverseListener() {
174 private static final long serialVersionUID
= -1158892811534971856L;
176 public void keyTraversed(TraverseEvent e
) {
177 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
181 credentialsBlock
.addTraverseListener(tl
);
182 usernameT
.addTraverseListener(tl
);
183 passwordT
.addTraverseListener(tl
);
184 parent
.setTabList(new Control
[] { credentialsBlock
});
185 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
186 // credentialsBlock.setFocus();
188 extendsCredentialsBlock(credentialsBlock
, locale
, loginSelectionListener
);
189 if (localeChoice
!= null)
190 createLocalesBlock(credentialsBlock
);
191 return credentialsBlock
;
195 * To be overridden in order to provide custome login button and other
198 protected void extendsCredentialsBlock(Composite credentialsBlock
, Locale selectedLocale
,
199 SelectionListener loginSelectionListener
) {
203 protected void updateLocale(Locale selectedLocale
) {
204 // save already entered values
205 String usernameStr
= usernameT
.getText();
206 char[] pwd
= passwordT
.getTextChars();
208 for (Control child
: parent
.getChildren())
210 createContents(parent
);
211 if (parent
.getParent() != null)
212 parent
.getParent().layout();
215 usernameT
.setText(usernameStr
);
216 passwordT
.setTextChars(pwd
);
219 protected Composite
createLocalesBlock(final Composite parent
) {
220 Composite c
= new Composite(parent
, SWT
.NONE
);
221 c
.setLayout(CmsUtils
.noSpaceGridLayout());
222 c
.setLayoutData(CmsUtils
.fillAll());
224 SelectionListener selectionListener
= new SelectionAdapter() {
225 private static final long serialVersionUID
= 4891637813567806762L;
227 public void widgetSelected(SelectionEvent event
) {
228 Button button
= (Button
) event
.widget
;
229 if (button
.getSelection()) {
230 localeChoice
.setSelectedIndex((Integer
) event
.widget
.getData());
231 updateLocale(localeChoice
.getSelectedLocale());
236 List
<Locale
> locales
= localeChoice
.getLocales();
237 for (Integer i
= 0; i
< locales
.size(); i
++) {
238 Locale locale
= locales
.get(i
);
239 Button button
= new Button(c
, SWT
.RADIO
);
241 button
.setText(LocaleUtils
.lead(locale
.getDisplayName(locale
), locale
) + " (" + locale
+ ")");
242 // button.addListener(SWT.Selection, listener);
243 button
.addSelectionListener(selectionListener
);
244 if (i
== localeChoice
.getSelectedIndex())
245 button
.setSelection(true);
250 protected boolean login() {
251 Subject subject
= cmsView
.getSubject();
252 LoginContext loginContext
;
257 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
258 loginContext
= new LoginContext(LOGIN_CONTEXT_USER
, subject
, this);
259 loginContext
.login();
260 } catch (FailedLoginException e
) {
261 log
.warn(e
.getMessage());
264 } catch (InterruptedException e2
) {
267 // ErrorFeedback.show("Login failed", e);
269 } catch (LoginException e
) {
270 log
.error("Cannot login", e
);
273 cmsView
.authChange(loginContext
);
277 protected void logout() {
279 cmsView
.navigateTo("~");
283 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
284 for (Callback callback
: callbacks
) {
285 if (callback
instanceof NameCallback
)
286 ((NameCallback
) callback
).setName(usernameT
.getText());
287 else if (callback
instanceof PasswordCallback
)
288 ((PasswordCallback
) callback
).setPassword(passwordT
.getTextChars());
289 else if (callback
instanceof HttpRequestCallback
)
290 ((HttpRequestCallback
) callback
).setRequest(RWT
.getRequest());
291 else if (callback
instanceof LanguageCallback
&& localeChoice
!= null)
292 ((LanguageCallback
) callback
).setLocale(localeChoice
.getSelectedLocale());