]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/widgets/auth/CmsLogin.java
Disable request for basic auth
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / widgets / auth / CmsLogin.java
1 package org.argeo.cms.widgets.auth;
2
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;
8
9 import java.io.IOException;
10 import java.util.List;
11 import java.util.Locale;
12
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;
22
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;
49
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;
55
56 private final Locale defaultLocale;
57 private LocaleChoice localeChoice = null;
58
59 private final CmsView cmsView;
60
61 public CmsLogin(CmsView cmsView) {
62 this.cmsView = cmsView;
63 defaultLocale = getKernelHeader().getDefaultLocale();
64 List<Locale> locales = getKernelHeader().getLocales();
65 if (locales != null)
66 localeChoice = new LocaleChoice(locales, defaultLocale);
67 loginSelectionListener = new SelectionListener() {
68 private static final long serialVersionUID = -8832133363830973578L;
69
70 @Override
71 public void widgetSelected(SelectionEvent e) {
72 login();
73 }
74
75 @Override
76 public void widgetDefaultSelected(SelectionEvent e) {
77 }
78 };
79 }
80
81 protected boolean isAnonymous() {
82 return CurrentUser.isAnonymous(cmsView.getSubject());
83 }
84
85 public final void createUi(Composite parent) {
86 this.parent = parent;
87 createContents(parent);
88 }
89
90 protected void createContents(Composite parent) {
91 defaultCreateContents(parent);
92 }
93
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,
99 true, true));
100 }
101 }
102
103 public final Composite createCredentialsBlock(Composite parent) {
104 if (isAnonymous()) {
105 return anonymousUi(parent);
106 } else {
107 return userUi(parent);
108 }
109 }
110
111 protected Composite getCredentialsBlock() {
112 return credentialsBlock;
113 }
114
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());
121
122 specificUserUi(credentialsBlock);
123
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);
130
131 l.addMouseListener(new MouseAdapter() {
132 private static final long serialVersionUID = 6444395812777413116L;
133
134 public void mouseDown(MouseEvent e) {
135 logout();
136 }
137 });
138 return credentialsBlock;
139 }
140
141 /** To be overridden */
142 protected void specificUserUi(Composite parent) {
143
144 }
145
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());
153
154 Integer textWidth = 120;
155 parent.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
156
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);
164
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);
172
173 TraverseListener tl = new TraverseListener() {
174 private static final long serialVersionUID = -1158892811534971856L;
175
176 public void keyTraversed(TraverseEvent e) {
177 if (e.detail == SWT.TRAVERSE_RETURN)
178 login();
179 }
180 };
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();
187
188 extendsCredentialsBlock(credentialsBlock, locale,
189 loginSelectionListener);
190 if (localeChoice != null)
191 createLocalesBlock(credentialsBlock);
192 return credentialsBlock;
193 }
194
195 /**
196 * To be overridden in order to provide custome login button and other
197 * links.
198 */
199 protected void extendsCredentialsBlock(Composite credentialsBlock,
200 Locale selectedLocale, SelectionListener loginSelectionListener) {
201
202 }
203
204 protected void updateLocale(Locale selectedLocale) {
205 // save already entered values
206 String usernameStr = usernameT.getText();
207 char[] pwd = passwordT.getTextChars();
208
209 for (Control child : parent.getChildren())
210 child.dispose();
211 createContents(parent);
212 if (parent.getParent() != null)
213 parent.getParent().layout();
214 else
215 parent.layout();
216 usernameT.setText(usernameStr);
217 passwordT.setTextChars(pwd);
218 }
219
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());
224
225 SelectionListener selectionListener = new SelectionAdapter() {
226 private static final long serialVersionUID = 4891637813567806762L;
227
228 public void widgetSelected(SelectionEvent event) {
229 Button button = (Button) event.widget;
230 if (button.getSelection()) {
231 localeChoice.setSelectedIndex((Integer) event.widget
232 .getData());
233 updateLocale(localeChoice.getSelectedLocale());
234 }
235 };
236 };
237
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);
242 button.setData(i);
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);
249 }
250 return c;
251 }
252
253 protected void login() {
254 Subject subject = cmsView.getSubject();
255 LoginContext loginContext;
256 try {
257 //
258 // LOGIN
259 //
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);
265 }
266 cmsView.authChange(loginContext);
267 }
268
269 protected void logout() {
270 cmsView.logout();
271 cmsView.navigateTo("~");
272 }
273
274 @Override
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
282 .getTextChars());
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());
289 }
290 }
291
292 }