]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/auth/AnonymousLoginModule.java
Use message instead of label in user menu
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / auth / AnonymousLoginModule.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.internal.auth;
17
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Locale;
22
23 import javax.security.auth.callback.Callback;
24 import javax.security.auth.callback.CallbackHandler;
25 import javax.security.auth.callback.UnsupportedCallbackException;
26 import javax.security.auth.login.LoginException;
27
28 import org.argeo.cms.KernelHeader;
29 import org.argeo.cms.internal.kernel.Activator;
30 import org.argeo.util.LocaleCallback;
31 import org.argeo.util.LocaleUtils;
32 import org.springframework.security.authentication.AnonymousAuthenticationToken;
33 import org.springframework.security.core.Authentication;
34
35 /** Login module which caches one subject per thread. */
36 public class AnonymousLoginModule extends AbstractLoginModule {
37 /** Comma separated list of locales */
38 private String availableLocales = null;
39
40 @Override
41 protected Authentication processLogin(CallbackHandler callbackHandler)
42 throws LoginException, UnsupportedCallbackException, IOException,
43 InterruptedException {
44 Locale selectedLocale = null;
45 // multi locale
46 if (callbackHandler != null)
47 if (availableLocales != null && !availableLocales.trim().equals("")) {
48 LocaleCallback localeCallback = new LocaleCallback(
49 availableLocales);
50 callbackHandler.handle(new Callback[] { localeCallback });
51 selectedLocale = localeCallback.getSelectedLocale();
52 } else {
53 callbackHandler.handle(new Callback[] {});
54 }
55
56 List<GrantedAuthorityPrincipal> authorities = Collections
57 .singletonList(new GrantedAuthorityPrincipal(
58 KernelHeader.ROLE_ANONYMOUS));
59 AnonymousAuthenticationToken anonymousToken = new AnonymousAuthenticationToken(
60 Activator.getSystemKey(), KernelHeader.USERNAME_ANONYMOUS,
61 authorities);
62
63 Authentication auth = getAuthenticationManager().authenticate(
64 anonymousToken);
65
66 if (selectedLocale != null)
67 LocaleUtils.threadLocale.set(selectedLocale);
68 return auth;
69 }
70 }