001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.security;
030:
031: import com.caucho.config.Config;
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.program.ContainerProgram;
034: import com.caucho.log.Log;
035: import com.caucho.util.L10N;
036:
037: import javax.servlet.ServletException;
038: import java.util.logging.Logger;
039:
040: /**
041: * Configuration for the login-config.
042: */
043: public class LoginConfig {
044: static final Logger log = Log.open(LoginConfig.class);
045: static final L10N L = new L10N(LoginConfig.class);
046:
047: private String _authMethod = "basic";
048: private String _realmName;
049: private Class _customType;
050: private ContainerProgram _formLoginConfig;
051: private ContainerProgram _init;
052:
053: private ServletAuthenticator _authenticator;
054:
055: /**
056: * Creates the login-config.
057: */
058: public LoginConfig() {
059: }
060:
061: /**
062: * Sets the auth-method
063: */
064: public void setAuthMethod(String method) {
065: _authMethod = method;
066: }
067:
068: /**
069: * Gets the auth-method
070: */
071: public String getAuthMethod() {
072: return _authMethod;
073: }
074:
075: /**
076: * Sets the authenticator.
077: */
078: public void setAuthenticator(ServletAuthenticator auth) {
079: _authenticator = auth;
080: }
081:
082: /**
083: * Sets the custom type
084: */
085: public void setType(Class type) throws ConfigException {
086: _customType = type;
087:
088: Config.validate(type, AbstractLogin.class);
089: }
090:
091: /**
092: * Sets the realm-name
093: */
094: public void setRealmName(String realmName) {
095: _realmName = realmName;
096: }
097:
098: /**
099: * Gets the realm-name
100: */
101: public String getRealmName() {
102: return _realmName;
103: }
104:
105: /**
106: * Creates the form-login-config
107: */
108: public ContainerProgram createFormLoginConfig() {
109: if (_formLoginConfig == null)
110: _formLoginConfig = new ContainerProgram();
111:
112: return _formLoginConfig;
113: }
114:
115: /**
116: * Creates the init
117: */
118: public ContainerProgram createInit() {
119: if (_init == null)
120: _init = new ContainerProgram();
121:
122: return _init;
123: }
124:
125: /**
126: * Returns the login.
127: */
128: public AbstractLogin getLogin() throws Exception {
129: /*
130: if (auth == null)
131: throw new ServletException(L.l("Login needs an authenticator resource with JNDI name java:comp/env/caucho/auth"));
132: */
133:
134: AbstractLogin login;
135:
136: if (_customType != null) {
137: login = (AbstractLogin) _customType.newInstance();
138:
139: if (_init != null)
140: _init.configure(login);
141: } else if (_authMethod.equalsIgnoreCase("basic")) {
142: BasicLogin basicLogin = new BasicLogin();
143: basicLogin.setRealmName(_realmName);
144: login = basicLogin;
145: } else if (_authMethod.equalsIgnoreCase("digest")) {
146: DigestLogin digestLogin = new DigestLogin();
147: digestLogin.setRealmName(_realmName);
148: login = digestLogin;
149: } else if (_authMethod.equalsIgnoreCase("client-cert")) {
150: ClientCertLogin certLogin = new ClientCertLogin();
151: login = certLogin;
152: } else if (_authMethod.equalsIgnoreCase("form")) {
153: login = new FormLogin();
154:
155: if (_formLoginConfig == null)
156: throw new ConfigException(L
157: .l("'form' authentication requires form-login"));
158:
159: _formLoginConfig.configure(login);
160: } else
161: throw new ServletException(L.l(
162: "'{0}' is an unknown auth-type.", _authMethod));
163:
164: if (_authenticator != null)
165: login.setAuthenticator(_authenticator);
166:
167: login.init();
168:
169: return login;
170: }
171: }
|