001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth.module;
019:
020: import java.util.Map;
021:
022: import javax.security.auth.login.FailedLoginException;
023: import javax.security.auth.login.LoginException;
024:
025: abstract public class SharedStateManager {
026:
027: private boolean useFirstPass = false;
028:
029: private boolean tryFirstPass = false;
030:
031: private boolean storePass = false;
032:
033: private boolean clearPass = false;
034:
035: protected DebugUtil debugUtil;
036:
037: protected Map<String, Object> sharedState;
038:
039: @SuppressWarnings("unchecked")
040: protected void prepareSharedState(Map<String, ?> sharedState,
041: final Map<String, ?> options) {
042: this .sharedState = (Map<String, Object>) sharedState;
043:
044: useFirstPass = false;
045: tryFirstPass = false;
046: storePass = false;
047: clearPass = false;
048: Object optionValue = null;
049:
050: optionValue = options.get("useFirstPass");
051: if (optionValue != null && optionValue.equals("true")) {
052: useFirstPass = true;
053: }
054:
055: optionValue = options.get("tryFirstPass");
056: if (optionValue != null && optionValue.equals("true")) {
057: tryFirstPass = true;
058: useFirstPass = false;
059: }
060:
061: optionValue = options.get("storePass");
062: if (optionValue != null && optionValue.equals("true")) {
063: storePass = true;
064: }
065:
066: optionValue = options.get("clearPass");
067: if (optionValue != null && optionValue.equals("true")) {
068: clearPass = true;
069: storePass = false;
070: }
071: }
072:
073: protected void loginWithSharedState() throws LoginException {
074: if (useFirstPass || tryFirstPass) {
075: getUserIdentityFromSharedStatus();
076: } else {
077: getUserIdentityFromCallbackHandler();
078: }
079: boolean passAuth = false;
080: passAuth = mainAuthenticationProcess();
081: if (!passAuth) {
082: if (tryFirstPass) {
083: debugUtil.recordDebugInfo("["
084: + getModuleName()
085: + "] tryFirstPass failed with:"
086: + new FailedLoginException("Login incorrect")
087: .toString() + "\n");
088: getUserIdentityFromCallbackHandler();
089: passAuth = mainAuthenticationProcess();
090: if (!passAuth) {
091: debugUtil.recordDebugInfo("[" + getModuleName()
092: + "] regular authentication failed\n");
093: debugUtil.printAndClearDebugInfo();
094: throw new FailedLoginException("Login incorrect");
095: } else {
096: debugUtil.recordDebugInfo("[" + getModuleName()
097: + "] regular authentication succeeded\n");
098: }
099: } else {
100: if (useFirstPass) {
101: debugUtil.recordDebugInfo("["
102: + getModuleName()
103: + "] useFirstPass failed with:"
104: + new FailedLoginException(
105: "Login incorrect").toString()
106: + "\n");
107: } else {
108: debugUtil.recordDebugInfo("[" + getModuleName()
109: + "] regular authentication failed\n");
110: }
111: debugUtil.printAndClearDebugInfo();
112: throw new FailedLoginException("Login incorrect");
113: }
114: } else {
115: if (tryFirstPass) {
116: debugUtil.recordDebugInfo("[" + getModuleName()
117: + "] tryFirstPass ");
118: } else if (useFirstPass) {
119: debugUtil.recordDebugInfo("[" + getModuleName()
120: + "] useFirstPass ");
121: } else {
122: debugUtil.recordDebugInfo("[" + getModuleName()
123: + "] regular authentication ");
124: }
125: debugUtil.recordDebugInfo("succeeded\n");
126: }
127: storePass();
128: }
129:
130: private void getUserIdentityFromSharedStatus()
131: throws LoginException {
132: if (sharedState == null)
133: throw new LoginException("No shared status");
134: String userName = (String) sharedState
135: .get("javax.security.auth.login.name");
136: char[] userPassword = (char[]) sharedState
137: .get("javax.security.auth.login.password");
138: if (userName == null || userPassword == null) {
139: throw new LoginException(
140: "Cannot get user ID or user password from shared state");
141: }
142: setUserName(userName);
143: setUserPassword(userPassword);
144: }
145:
146: protected void storePass() throws LoginException {
147: if (storePass) {
148: if (sharedState == null) {
149: throw new LoginException("No Shared State");
150: }
151: if (sharedState.get("javax.security.auth.login.name") == null) {
152: sharedState.put("javax.security.auth.login.name",
153: getUserName());
154: }
155: if (sharedState.get("javax.security.auth.login.password") == null) {
156: sharedState.put("javax.security.auth.login.password",
157: getUserPassword());
158: }
159: }
160: }
161:
162: protected void clearPass() throws LoginException {
163: if (clearPass) {
164: if (sharedState == null) {
165: throw new LoginException("No Shared State");
166: }
167: sharedState.remove("javax.security.auth.login.name");
168: sharedState.remove("javax.security.auth.login.password");
169: }
170: }
171:
172: abstract protected boolean mainAuthenticationProcess()
173: throws LoginException;
174:
175: abstract protected void getUserIdentityFromCallbackHandler()
176: throws LoginException;
177:
178: abstract protected void setUserName(String userName);
179:
180: abstract protected String getUserName();
181:
182: abstract protected void setUserPassword(char[] userPassword);
183:
184: abstract protected char[] getUserPassword();
185:
186: abstract protected String getModuleName();
187: }
|