001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.web.auth;
018:
019: import java.util.Map;
020:
021: import javax.servlet.ServletContext;
022:
023: import org.apache.velocity.tools.view.context.ViewContext;
024:
025: import velosurf.web.VelosurfTool;
026: import velosurf.web.auth.BaseAuthenticator;
027: import velosurf.context.DBReference;
028: import velosurf.context.Instance;
029: import velosurf.util.Logger;
030:
031: /** <p>Authenticator basic implementation.</p>
032: * <p>It accepts the four following parameters in <code>toolbox.xml</code>:</p>
033: * <ul>
034: * <li><code>method</code> (inherited from <code>BaseAuthenticator</code>) the encryption method to use (default to none,
035: * an example client-side javascript encryption is provided for the method HmacMD5).</li>
036: * <li><code>user-by-login</code> name of the Velosurf root attribute that returns a user given its login.</li>
037: * <li><code>login-parameter</code> name of the external parameter 'login' in the previous attribute.</li>
038: * <li><code>password-field</code> name of the password field.</li>
039: * </ul>
040: *
041: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
042: */
043:
044: public class SimpleDBAuthenticator extends BaseAuthenticator {
045:
046: /** database. */
047: protected DBReference db = null;
048:
049: /** key used in toolbox.xml to indicate the "user by login" root attribute. */
050: private static final String USER_BY_LOGIN_KEY = "user-by-login";
051:
052: /** key used in toolbox.xml to indicate the name of the login parameter in the "user by login" attribute. */
053: private static final String LOGIN_PARAMETER_KEY = "login-parameter";
054:
055: /** key used in toolbox.xml to indicate the name of the password field in the "user by login" attribute. */
056: private static final String PASSWORD_FIELD_KEY = "password-field";
057:
058: /** default name of the "user by login" root attribute. */
059: private static final String USER_BY_LOGIN_DEFAULT = "user_by_login";
060:
061: /** default name for the "login" parameter. */
062: private static final String LOGIN_PARAMETER_DEFAULT = "login";
063:
064: /** default name of the "password" field. */
065: private static final String PASSWORD_FIELD_DEFAULT = "password";
066:
067: /** configuration. */
068: private Map config = null;
069:
070: /** "user by login" root attribute name. */
071: private String userByLogin = USER_BY_LOGIN_DEFAULT;
072:
073: /** login parameter name */
074: private String loginParameter = LOGIN_PARAMETER_DEFAULT;
075:
076: /** password field name */
077: private String passwordField = PASSWORD_FIELD_DEFAULT;
078:
079: /**
080: * initialize this tool.
081: * @param initData a view context
082: */
083: public void init(Object initData) {
084: super .init(initData);
085:
086: // init only if there was no error in super class
087: if (initData instanceof ViewContext) {
088: if (db == null) {
089: initDB(((ViewContext) initData).getServletContext());
090: }
091: }
092:
093: if (config != null) {
094: String value;
095: value = (String) config.get(USER_BY_LOGIN_KEY);
096: if (value != null) {
097: userByLogin = value;
098: }
099: value = (String) config.get(PASSWORD_FIELD_KEY);
100: if (value != null) {
101: passwordField = value;
102: }
103: value = (String) config.get(LOGIN_PARAMETER_KEY);
104: if (value != null) {
105: loginParameter = value;
106: }
107: }
108: }
109:
110: protected void initDB(ServletContext ctx) {
111: db = VelosurfTool.getDefaultInstance(ctx);
112: }
113:
114: /**
115: * externally set the db reference
116: * @param db DBReference
117: */
118: public void setDBReference(DBReference db) {
119: this .db = db;
120: }
121:
122: /**
123: * get the password for this login.
124: * @param login login
125: * @return password or null
126: */
127: public String getPassword(String login) {
128: Map user = null;
129: synchronized (db) {
130: db.put(loginParameter, login);
131: user = (Map) db.get(userByLogin);
132: }
133: if (user != null) {
134: return (String) user.get(passwordField);
135: }
136: return null;
137: }
138:
139: /**
140: * get the user object for this login.
141: * @param login login
142: * @return user object
143: */
144: public Object getUser(String login) {
145: synchronized (db) {
146: db.put(loginParameter, login);
147: return db.get(userByLogin);
148: }
149: }
150:
151: /**
152: * configure this tool.
153: * @param map
154: */
155: public void configure(Map map) {
156: super.configure(map);
157: config = map;
158: }
159: }
|