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
.LoginContext
;
21 import javax
.security
.auth
.login
.LoginException
;
23 import org
.argeo
.cms
.CmsException
;
24 import org
.argeo
.cms
.CmsMsg
;
25 import org
.argeo
.cms
.CmsStyles
;
26 import org
.argeo
.cms
.CmsView
;
27 import org
.argeo
.cms
.auth
.CurrentUser
;
28 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
29 import org
.argeo
.cms
.i18n
.Msg
;
30 import org
.argeo
.cms
.util
.CmsUtils
;
31 import org
.argeo
.util
.LocaleChoice
;
32 import org
.eclipse
.rap
.rwt
.RWT
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.events
.MouseAdapter
;
35 import org
.eclipse
.swt
.events
.MouseEvent
;
36 import org
.eclipse
.swt
.events
.SelectionAdapter
;
37 import org
.eclipse
.swt
.events
.SelectionEvent
;
38 import org
.eclipse
.swt
.events
.SelectionListener
;
39 import org
.eclipse
.swt
.events
.TraverseEvent
;
40 import org
.eclipse
.swt
.events
.TraverseListener
;
41 import org
.eclipse
.swt
.layout
.GridData
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Button
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.Control
;
46 import org
.eclipse
.swt
.widgets
.Label
;
47 import org
.eclipse
.swt
.widgets
.Shell
;
48 import org
.eclipse
.swt
.widgets
.Text
;
50 public class CmsLogin
implements CmsStyles
, CallbackHandler
{
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 public CmsLogin(CmsView cmsView
) {
62 this.cmsView
= cmsView
;
63 defaultLocale
= getKernelHeader().getDefaultLocale();
64 List
<Locale
> locales
= getKernelHeader().getLocales();
66 localeChoice
= new LocaleChoice(locales
, defaultLocale
);
67 loginSelectionListener
= new SelectionListener() {
68 private static final long serialVersionUID
= -8832133363830973578L;
71 public void widgetSelected(SelectionEvent e
) {
76 public void widgetDefaultSelected(SelectionEvent e
) {
81 protected boolean isAnonymous() {
82 return CurrentUser
.isAnonymous(cmsView
.getSubject());
85 public final void createUi(Composite parent
) {
87 createContents(parent
);
90 protected void createContents(Composite parent
) {
91 defaultCreateContents(parent
);
94 public final void defaultCreateContents(Composite parent
) {
95 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
96 Composite credentialsBlock
= createCredentialsBlock(parent
);
97 if (parent
instanceof Shell
) {
98 credentialsBlock
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
,
103 public final Composite
createCredentialsBlock(Composite parent
) {
105 return anonymousUi(parent
);
107 return userUi(parent
);
111 protected Composite
getCredentialsBlock() {
112 return credentialsBlock
;
115 protected Composite
userUi(Composite parent
) {
116 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
117 credentialsBlock
.setLayout(new GridLayout());
118 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
120 specificUserUi(credentialsBlock
);
122 Label l
= new Label(credentialsBlock
, SWT
.NONE
);
123 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
124 l
.setText(CmsMsg
.logout
.lead());
125 GridData lData
= CmsUtils
.fillWidth();
126 lData
.widthHint
= 120;
127 l
.setLayoutData(lData
);
129 l
.addMouseListener(new MouseAdapter() {
130 private static final long serialVersionUID
= 6444395812777413116L;
132 public void mouseDown(MouseEvent e
) {
136 return credentialsBlock
;
139 /** To be overridden */
140 protected void specificUserUi(Composite parent
) {
144 protected Composite
anonymousUi(Composite parent
) {
145 Locale locale
= localeChoice
== null ?
this.defaultLocale
146 : localeChoice
.getSelectedLocale();
147 // We need a composite for the traversal
148 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
149 credentialsBlock
.setLayout(new GridLayout());
150 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
152 Integer textWidth
= 120;
153 parent
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
155 // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
156 usernameT
= new Text(credentialsBlock
, SWT
.BORDER
);
157 usernameT
.setMessage(username
.lead(locale
));
158 usernameT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
159 GridData gd
= CmsUtils
.fillWidth();
160 gd
.widthHint
= textWidth
;
161 usernameT
.setLayoutData(gd
);
163 // new Label(this, SWT.NONE).setText(CmsMsg.password.lead());
164 passwordT
= new Text(credentialsBlock
, SWT
.BORDER
| SWT
.PASSWORD
);
165 passwordT
.setMessage(password
.lead(locale
));
166 passwordT
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
167 gd
= CmsUtils
.fillWidth();
168 gd
.widthHint
= textWidth
;
169 passwordT
.setLayoutData(gd
);
171 TraverseListener tl
= new TraverseListener() {
172 private static final long serialVersionUID
= -1158892811534971856L;
174 public void keyTraversed(TraverseEvent e
) {
175 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
179 credentialsBlock
.addTraverseListener(tl
);
180 usernameT
.addTraverseListener(tl
);
181 passwordT
.addTraverseListener(tl
);
182 parent
.setTabList(new Control
[] { credentialsBlock
});
183 credentialsBlock
.setTabList(new Control
[] { usernameT
, passwordT
});
184 // credentialsBlock.setFocus();
186 extendsCredentialsBlock(credentialsBlock
, locale
,
187 loginSelectionListener
);
188 if (localeChoice
!= null)
189 createLocalesBlock(credentialsBlock
);
190 return credentialsBlock
;
194 * To be overridden in order to provide custome login button and other
197 protected void extendsCredentialsBlock(Composite credentialsBlock
,
198 Locale selectedLocale
, SelectionListener loginSelectionListener
) {
202 protected void updateLocale(Locale selectedLocale
) {
203 // usernameT.setMessage(username.lead(selectedLocale));
204 // passwordT.setMessage(password.lead(selectedLocale));
205 for (Control child
: parent
.getChildren())
207 createContents(parent
);
208 if (parent
.getParent() != null)
209 parent
.getParent().layout();
214 protected Composite
createLocalesBlock(final Composite parent
) {
215 Composite c
= new Composite(parent
, SWT
.NONE
);
216 c
.setLayout(CmsUtils
.noSpaceGridLayout());
217 c
.setLayoutData(CmsUtils
.fillAll());
219 SelectionListener selectionListener
= new SelectionAdapter() {
220 private static final long serialVersionUID
= 4891637813567806762L;
222 public void widgetSelected(SelectionEvent event
) {
223 Button button
= (Button
) event
.widget
;
224 if (button
.getSelection()) {
225 localeChoice
.setSelectedIndex((Integer
) event
.widget
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(Msg
.lead(locale
.getDisplayName(locale
), locale
)
238 + " (" + locale
+ ")");
239 // button.addListener(SWT.Selection, listener);
240 button
.addSelectionListener(selectionListener
);
241 if (i
== localeChoice
.getSelectedIndex())
242 button
.setSelection(true);
247 protected void login() {
248 Subject subject
= cmsView
.getSubject();
249 LoginContext loginContext
;
254 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
255 loginContext
= new LoginContext(LOGIN_CONTEXT_USER
, subject
, this);
256 loginContext
.login();
257 } catch (LoginException e1
) {
258 throw new CmsException("Cannot authenticate", e1
);
260 cmsView
.authChange(loginContext
);
263 protected void logout() {
265 cmsView
.navigateTo("~");
269 public void handle(Callback
[] callbacks
) throws IOException
,
270 UnsupportedCallbackException
{
271 for (Callback callback
: callbacks
) {
272 if (callback
instanceof NameCallback
)
273 ((NameCallback
) callback
).setName(usernameT
.getText());
274 else if (callback
instanceof PasswordCallback
)
275 ((PasswordCallback
) callback
).setPassword(passwordT
277 else if (callback
instanceof HttpRequestCallback
)
278 ((HttpRequestCallback
) callback
).setRequest(RWT
.getRequest());
279 else if (callback
instanceof LanguageCallback
280 && localeChoice
!= null)
281 ((LanguageCallback
) callback
).setLocale(localeChoice
282 .getSelectedLocale());