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
.LocaleUtils
;
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 Locale locale
= localeChoice
== null ?
this.defaultLocale
117 : localeChoice
.getSelectedLocale();
118 credentialsBlock
= new Composite(parent
, SWT
.NONE
);
119 credentialsBlock
.setLayout(new GridLayout());
120 credentialsBlock
.setLayoutData(CmsUtils
.fillAll());
122 specificUserUi(credentialsBlock
);
124 Label l
= new Label(credentialsBlock
, SWT
.NONE
);
125 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
126 l
.setText(CmsMsg
.logout
.lead(locale
));
127 GridData lData
= CmsUtils
.fillWidth();
128 lData
.widthHint
= 120;
129 l
.setLayoutData(lData
);
131 l
.addMouseListener(new MouseAdapter() {
132 private static final long serialVersionUID
= 6444395812777413116L;
134 public void mouseDown(MouseEvent e
) {
138 return credentialsBlock
;
141 /** To be overridden */
142 protected void specificUserUi(Composite parent
) {
146 protected Composite
anonymousUi(Composite parent
) {
147 Locale locale
= localeChoice
== null ?
this.defaultLocale
148 : 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
,
189 loginSelectionListener
);
190 if (localeChoice
!= null)
191 createLocalesBlock(credentialsBlock
);
192 return credentialsBlock
;
196 * To be overridden in order to provide custome login button and other
199 protected void extendsCredentialsBlock(Composite credentialsBlock
,
200 Locale selectedLocale
, SelectionListener loginSelectionListener
) {
204 protected void updateLocale(Locale selectedLocale
) {
205 // save already entered values
206 String usernameStr
= usernameT
.getText();
207 char[] pwd
= passwordT
.getTextChars();
209 for (Control child
: parent
.getChildren())
211 createContents(parent
);
212 if (parent
.getParent() != null)
213 parent
.getParent().layout();
216 usernameT
.setText(usernameStr
);
217 passwordT
.setTextChars(pwd
);
220 protected Composite
createLocalesBlock(final Composite parent
) {
221 Composite c
= new Composite(parent
, SWT
.NONE
);
222 c
.setLayout(CmsUtils
.noSpaceGridLayout());
223 c
.setLayoutData(CmsUtils
.fillAll());
225 SelectionListener selectionListener
= new SelectionAdapter() {
226 private static final long serialVersionUID
= 4891637813567806762L;
228 public void widgetSelected(SelectionEvent event
) {
229 Button button
= (Button
) event
.widget
;
230 if (button
.getSelection()) {
231 localeChoice
.setSelectedIndex((Integer
) event
.widget
233 updateLocale(localeChoice
.getSelectedLocale());
238 List
<Locale
> locales
= localeChoice
.getLocales();
239 for (Integer i
= 0; i
< locales
.size(); i
++) {
240 Locale locale
= locales
.get(i
);
241 Button button
= new Button(c
, SWT
.RADIO
);
243 button
.setText(LocaleUtils
.lead(locale
.getDisplayName(locale
),
244 locale
) + " (" + locale
+ ")");
245 // button.addListener(SWT.Selection, listener);
246 button
.addSelectionListener(selectionListener
);
247 if (i
== localeChoice
.getSelectedIndex())
248 button
.setSelection(true);
253 protected void login() {
254 Subject subject
= cmsView
.getSubject();
255 LoginContext loginContext
;
260 new LoginContext(LOGIN_CONTEXT_ANONYMOUS
, subject
).logout();
261 loginContext
= new LoginContext(LOGIN_CONTEXT_USER
, subject
, this);
262 loginContext
.login();
263 } catch (LoginException e1
) {
264 throw new CmsException("Cannot authenticate", e1
);
266 cmsView
.authChange(loginContext
);
269 protected void logout() {
271 cmsView
.navigateTo("~");
275 public void handle(Callback
[] callbacks
) throws IOException
,
276 UnsupportedCallbackException
{
277 for (Callback callback
: callbacks
) {
278 if (callback
instanceof NameCallback
)
279 ((NameCallback
) callback
).setName(usernameT
.getText());
280 else if (callback
instanceof PasswordCallback
)
281 ((PasswordCallback
) callback
).setPassword(passwordT
283 else if (callback
instanceof HttpRequestCallback
)
284 ((HttpRequestCallback
) callback
).setRequest(RWT
.getRequest());
285 else if (callback
instanceof LanguageCallback
286 && localeChoice
!= null)
287 ((LanguageCallback
) callback
).setLocale(localeChoice
288 .getSelectedLocale());