01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software distributed under the License
12: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13: * or implied. See the License for the specific language governing permissions and limitations under
14: * the License.
15: *
16: */
17:
18: package org.apache.lenya.ac.impl;
19:
20: import org.apache.avalon.framework.logger.AbstractLogEnabled;
21: import org.apache.cocoon.environment.Request;
22: import org.apache.lenya.ac.AccessControlException;
23: import org.apache.lenya.ac.AccreditableManager;
24: import org.apache.lenya.ac.Authenticator;
25: import org.apache.lenya.ac.Identity;
26: import org.apache.lenya.ac.User;
27:
28: /**
29: * The anonymous authenticator authenticates to an anonymous user with no password
30: * (you just have to add a user named 'anonymous' with an arbitrary password and the permissions
31: * you'd like via the admin screen). This is useful in conjunction with client certificates.
32: * @version $Id: AnonymousAuthenticator.java 473861 2006-11-12 03:51:14Z gregor $
33: */
34: public class AnonymousAuthenticator extends AbstractLogEnabled
35: implements Authenticator {
36:
37: /**
38: * @see org.apache.lenya.ac.Authenticator#authenticate(org.apache.lenya.ac.AccreditableManager,
39: * org.apache.cocoon.environment.Request)
40: */
41: public boolean authenticate(
42: AccreditableManager accreditableManager, Request request)
43: throws AccessControlException {
44:
45: String username = "anonymous";
46:
47: if (getLogger().isDebugEnabled()) {
48: getLogger().debug(
49: "Authenticating username [" + username + "]");
50: }
51:
52: Identity identity = (Identity) request.getSession(false)
53: .getAttribute(Identity.class.getName());
54:
55: User user = accreditableManager.getUserManager().getUser(
56: username);
57:
58: boolean authenticated = false;
59: if (user != null) {
60: if (getLogger().isDebugEnabled()) {
61: getLogger().debug("User [" + user + "] authenticated.");
62: }
63:
64: if (!identity.contains(user)) {
65: User oldUser = identity.getUser();
66: if (oldUser != null) {
67: if (getLogger().isDebugEnabled()) {
68: getLogger().debug(
69: "Removing user [" + oldUser
70: + "] from identity.");
71: }
72: identity.removeIdentifiable(oldUser);
73: }
74: identity.addIdentifiable(user);
75: }
76: authenticated = true;
77: } else {
78: if (getLogger().isDebugEnabled()) {
79: if (user == null) {
80: getLogger().debug(
81: "No such user: [" + username + "]");
82: }
83: getLogger().debug(
84: "User [" + username + "] not authenticated.");
85: }
86: }
87: return authenticated;
88: }
89: }
|