KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > AnonymousAuthenticator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  *
14  */

15
16 package org.apache.lenya.ac.impl;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.cocoon.environment.Request;
20 import org.apache.lenya.ac.AccessControlException;
21 import org.apache.lenya.ac.AccreditableManager;
22 import org.apache.lenya.ac.Authenticator;
23 import org.apache.lenya.ac.Identity;
24 import org.apache.lenya.ac.User;
25
26
27 /**
28  * The anonymous authenticator authenticates to an anonymous user with no password
29  * (you just have to add a user named 'anonymous' with an arbitrary password and the permissions
30  * you'd like via the admin screen). This is useful in conjunction with client certificates.
31  * @version $Id: UserAuthenticator.java 43241 2004-08-16 16:36:57Z andreas $
32  */

33 public class AnonymousAuthenticator extends AbstractLogEnabled implements Authenticator {
34
35     
36     /**
37      * @see org.apache.lenya.ac.Authenticator#authenticate(org.apache.lenya.ac.AccreditableManager,
38      * org.apache.cocoon.environment.Request)
39      */

40     public boolean authenticate(AccreditableManager accreditableManager, Request request)
41             throws AccessControlException {
42
43     String JavaDoc username = "anonymous";
44
45         if (getLogger().isDebugEnabled()) {
46             getLogger().debug(
47                     "Authenticating username [" + username + "]");
48         }
49
50         Identity identity = (Identity) request.getSession(false).getAttribute(Identity.class.getName());
51
52         User user = accreditableManager.getUserManager().getUser(username);
53
54         boolean authenticated = false;
55         if (user != null) {
56             if (getLogger().isDebugEnabled()) {
57                 getLogger().debug("User [" + user + "] authenticated.");
58             }
59
60             if (!identity.contains(user)) {
61                 User oldUser = identity.getUser();
62                 if (oldUser != null) {
63                     if (getLogger().isDebugEnabled()) {
64                         getLogger().debug("Removing user [" + oldUser + "] from identity.");
65                     }
66                     identity.removeIdentifiable(oldUser);
67                 }
68                 identity.addIdentifiable(user);
69             }
70             authenticated = true;
71         } else {
72             if (getLogger().isDebugEnabled()) {
73                 if (user == null) {
74                     getLogger().debug("No such user: [" + username + "]");
75                 }
76                 getLogger().debug("User [" + username + "] not authenticated.");
77             }
78         }
79         return authenticated;
80     }
81 }
Popular Tags