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
.callback
.Callback
;
11 import javax
.security
.auth
.callback
.CallbackHandler
;
12 import javax
.security
.auth
.callback
.LanguageCallback
;
13 import javax
.security
.auth
.callback
.NameCallback
;
14 import javax
.security
.auth
.callback
.PasswordCallback
;
15 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
16 import javax
.security
.auth
.login
.LoginContext
;
17 import javax
.security
.auth
.login
.LoginException
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsMsg
;
22 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
23 import org
.argeo
.cms
.i18n
.LocaleUtils
;
24 import org
.argeo
.cms
.ui
.CmsStyles
;
25 import org
.argeo
.cms
.ui
.CmsView
;
26 import org
.argeo
.cms
.ui
.internal
.Activator
;
27 import org
.argeo
.cms
.util
.CmsUtils
;
28 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
29 import org
.argeo
.node
.NodeConstants
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.events
.MouseAdapter
;
32 import org
.eclipse
.swt
.events
.MouseEvent
;
33 import org
.eclipse
.swt
.events
.SelectionAdapter
;
34 import org
.eclipse
.swt
.events
.SelectionEvent
;
35 import org
.eclipse
.swt
.events
.SelectionListener
;
36 import org
.eclipse
.swt
.events
.TraverseEvent
;
37 import org
.eclipse
.swt
.events
.TraverseListener
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Button
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Control
;
43 import org
.eclipse
.swt
.widgets
.Label
;
44 import org
.eclipse
.swt
.widgets
.Shell
;
45 import org
.eclipse
.swt
.widgets
.Text
;
47 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
48 private final static Log log
= LogFactory
.getLog(CmsLogin
.class);
50 private Composite parent
;
51 private Text usernameT
, passwordT
;
52 private Composite credentialsBlock
;
53 private final SelectionListener loginSelectionListener
;
55 private final Locale defaultLocale
;
56 private LocaleChoice localeChoice
= null;
58 private final CmsView cmsView
;
60 public CmsLogin(CmsView cmsView
) {
61 this.cmsView
= cmsView
;
62 defaultLocale
= Activator
.getNodeState().getDefaultLocale();
63 List
<Locale
> locales
= Activator
.getNodeState().getLocales();
65 localeChoice
= new LocaleChoice(locales
, defaultLocale
);
66 loginSelectionListener
= new SelectionListener() {
67 private static final long serialVersionUID
= -8832133363830973578L;
70 public void widgetSelected(SelectionEvent e
) {
75 public void widgetDefaultSelected(SelectionEvent e
) {
80 protected boolean isAnonymous() {
81 return cmsView
.isAnonymous();
84 public final void createUi(Composite parent
) {
86 createContents(parent
);
89 protected void createContents(Composite parent
) {
90 defaultCreateContents(parent
);
93 public final void defaultCreateContents(Composite parent
) {
94 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
95 Composite credentialsBlock
= createCredentialsBlock(parent
);
96 if (parent
instanceof Shell
) {
97 credentialsBlock
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
101 public final Composite
createCredentialsBlock(Composite parent
) {
103 return anonymousUi(parent
);
105 return userUi(parent
);
109 protected Composite
getCredentialsBlock() {
110 return credentialsBlock
;
113 protected Composite
userUi(Composite parent
) {
114 Locale locale
= localeChoice
== null ?
this.defaultLocale
: localeChoice
.getSelectedLocale();
115 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
116 credentialsBlock
.setLayout(new GridLayout());
117 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
119 specificUserUi(credentialsBlock
);
121 Label l
= new Label(credentialsBlock
, SWT
.NONE
);
122 CmsUtils
.style(l
, CMS_USER_MENU_ITEM
);
123 l
.setText(CmsMsg
.logout
.lead(locale
));
124 GridData lData
= CmsUtils
.fillWidth();
125 lData
.widthHint
= 120;
126 l
.setLayoutData(lData
);
128 l
.addMouseListener(new MouseAdapter() {
129 private static final long serialVersionUID
= 6444395812777413116L;
131 public void mouseDown(MouseEvent e
) {
135 return credentialsBlock
;
138 /** To be overridden */
139 protected void specificUserUi(Composite parent
) {
143 protected Composite
anonymousUi(Composite parent
) {
144 Locale locale
= localeChoice
== null ?
this.defaultLocale
: localeChoice
.getSelectedLocale();
145 // We need a composite for the traversal
146 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
147 credentialsBlock
.setLayout(new GridLayout());
148 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
150 Integer textWidth
= 120;
151 CmsUtils
.style(parent
, CMS_USER_MENU
);
153 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
154 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
155 usernameT
.setMessage(username
.lead(locale
));
156 CmsUtils
.style(usernameT
, CMS_LOGIN_DIALOG_USERNAME
);
157 GridData gd
= CmsUtils
.fillWidth();
158 gd
.widthHint
= textWidth
;
159 usernameT
.setLayoutData(gd
);
161 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
162 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
163 passwordT
.setMessage(password
.lead(locale
));
164 CmsUtils
.style(passwordT
, CMS_LOGIN_DIALOG_PASSWORD
);
165 gd
= CmsUtils
.fillWidth();
166 gd
.widthHint
= textWidth
;
167 passwordT
.setLayoutData(gd
);
169 TraverseListener tl
= new TraverseListener() {
170 private static final long serialVersionUID
= -1158892811534971856L;
172 public void keyTraversed(TraverseEvent e
) {
173 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
177 credentialsBlock
.addTraverseListener(tl
);
178 usernameT
.addTraverseListener(tl
);
179 passwordT
.addTraverseListener(tl
);
180 parent
.setTabList(new Control
[] { credentialsBlock
});
181 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
182 // credentialsBlock.setFocus();
184 extendsCredentialsBlock(credentialsBlock
, locale
, loginSelectionListener
);
185 if (localeChoice
!= null)
186 createLocalesBlock(credentialsBlock
);
187 return credentialsBlock
;
191 * To be overridden in order to provide custome login button and other
194 protected void extendsCredentialsBlock(Composite credentialsBlock
, Locale selectedLocale
,
195 SelectionListener loginSelectionListener
) {
199 protected void updateLocale(Locale selectedLocale
) {
200 // save already entered values
201 String usernameStr
= usernameT
.getText();
202 char[] pwd
= passwordT
.getTextChars();
204 for (Control child
: parent
.getChildren())
206 createContents(parent
);
207 if (parent
.getParent() != null)
208 parent
.getParent().layout();
211 usernameT
.setText(usernameStr
);
212 passwordT
.setTextChars(pwd
);
215 protected Composite
createLocalesBlock(final Composite parent
) {
216 Composite c
= new Composite(parent
, SWT
.NONE
);
217 c
.setLayout(CmsUtils
.noSpaceGridLayout());
218 c
.setLayoutData(CmsUtils
.fillAll());
220 SelectionListener selectionListener
= new SelectionAdapter() {
221 private static final long serialVersionUID
= 4891637813567806762L;
223 public void widgetSelected(SelectionEvent event
) {
224 Button button
= (Button
) event
.widget
;
225 if (button
.getSelection()) {
226 localeChoice
.setSelectedIndex((Integer
) event
.widget
.getData());
227 updateLocale(localeChoice
.getSelectedLocale());
232 List
<Locale
> locales
= localeChoice
.getLocales();
233 for (Integer i
= 0; i
< locales
.size(); i
++) {
234 Locale locale
= locales
.get(i
);
235 Button button
= new Button(c
, SWT
.RADIO
);
237 button
.setText(LocaleUtils
.lead(locale
.getDisplayName(locale
), locale
) + " (" + locale
+ ")");
238 // button.addListener(SWT.Selection, listener);
239 button
.addSelectionListener(selectionListener
);
240 if (i
== localeChoice
.getSelectedIndex())
241 button
.setSelection(true);
246 protected boolean login() {
247 // Subject subject = cmsView.getLoginContext().getSubject();
248 // LoginContext loginContext = cmsView.getLoginContext();
253 // loginContext.logout();
254 LoginContext loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, this);
255 loginContext
.login();
256 cmsView
.authChange(loginContext
);
258 } catch (LoginException e
) {
259 if (log
.isTraceEnabled())
260 log
.warn("Login failed: " + e
.getMessage(), e
);
262 log
.warn("Login failed: " + e
.getMessage());
266 } catch (InterruptedException e2
) {
269 // ErrorFeedback.show("Login failed", e);
272 // catch (LoginException e) {
273 // log.error("Cannot login", e);
278 protected void logout() {
280 cmsView
.navigateTo("~");
284 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
285 for (Callback callback
: callbacks
) {
286 if (callback
instanceof NameCallback
&& usernameT
!= null)
287 ((NameCallback
) callback
).setName(usernameT
.getText());
288 else if (callback
instanceof PasswordCallback
&& passwordT
!= null)
289 ((PasswordCallback
) callback
).setPassword(passwordT
.getTextChars());
290 else if (callback
instanceof HttpRequestCallback
) {
291 ((HttpRequestCallback
) callback
).setRequest(UiContext
.getHttpRequest());
292 ((HttpRequestCallback
) callback
).setResponse(UiContext
.getHttpResponse());
293 } else if (callback
instanceof LanguageCallback
&& localeChoice
!= null)
294 ((LanguageCallback
) callback
).setLocale(localeChoice
.getSelectedLocale());