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.io.IOException;
021: import java.io.InputStream;
022: import java.util.Arrays;
023:
024: import javax.security.auth.login.LoginException;
025:
026: public class LoginModuleUtils {
027:
028: public static enum ACTION {
029: no_action, login, commit, logout
030: };
031:
032: public final static class LoginModuleStatus {
033:
034: private static enum PHASE {
035: uninitialized, initialized, logined, committed
036: };
037:
038: private PHASE phase;
039:
040: public LoginModuleStatus() {
041: phase = PHASE.uninitialized;
042: }
043:
044: public void initialized() {
045: phase = PHASE.initialized;
046: }
047:
048: public void logined() {
049: phase = PHASE.logined;
050: }
051:
052: public void committed() {
053: phase = PHASE.committed;
054: }
055:
056: public void logouted() {
057: phase = PHASE.logined;
058: }
059:
060: public boolean isLoggined() {
061: return phase.equals(PHASE.logined)
062: || phase.equals(PHASE.committed);
063: }
064:
065: public boolean isCommitted() {
066: return phase.equals(PHASE.committed);
067: }
068:
069: public ACTION checkAbout() {
070: switch (phase) {
071: case uninitialized:
072: case initialized:
073: return ACTION.no_action;
074: default:
075: return ACTION.logout;
076: }
077: }
078:
079: public ACTION checkLogin() throws LoginException {
080: switch (phase) {
081: case uninitialized:
082: throw new LoginException(
083: "Login Module is not initialized.");
084: case initialized:
085: return ACTION.login;
086: default:
087: return ACTION.no_action;
088: }
089: }
090:
091: public ACTION checkCommit() throws LoginException {
092: switch (phase) {
093: case uninitialized:
094: throw new LoginException(
095: "Login Module is not initialized.");
096: case initialized:
097: return ACTION.logout;
098: case logined:
099: return ACTION.commit;
100: default:
101: return ACTION.no_action;
102: }
103: }
104:
105: public ACTION checkLogout() throws LoginException {
106: switch (phase) {
107: case uninitialized:
108: throw new LoginException(
109: "Login Module is not initialized.");
110: case initialized:
111: case logined:
112: return ACTION.no_action;
113: default:
114: return ACTION.logout;
115: }
116: }
117: }
118:
119: /**
120: * Reads the password stored in an inputstream to a char array.
121: *
122: * @param in
123: * an inputstream which stores the password.
124: * @return a char array which contains the password.
125: * @throws IOException
126: */
127: public static char[] getPassword(InputStream in) throws IOException {
128: char[] buffer = new char[512];
129:
130: // 1.Just ASCII encoding is supported. bytes read from inputstream is
131: // cast and put into char array.
132: // 2.just read one line.
133: int length = 0;
134: int nextChar = -1;
135: boolean hasCarriage = false;
136:
137: do {
138: nextChar = in.read();
139: if (nextChar == -1 || nextChar == '\n') {
140: break;
141: }
142:
143: if (hasCarriage) {
144: buffer = appendChars(buffer, '\r', length++);
145: hasCarriage = false;
146: }
147:
148: if (nextChar == '\r') {
149: hasCarriage = true;
150: } else {
151: buffer = appendChars(buffer, (char) nextChar, length++);
152: }
153:
154: } while (true);
155:
156: if (length == 0) {
157: return null;
158: }
159:
160: char[] password = new char[length];
161: System.arraycopy(buffer, 0, password, 0, length);
162: return password;
163: }
164:
165: private static char[] appendChars(char[] src, char c, int position) {
166: char[] dest = src;
167: if (position == src.length) {
168: dest = new char[src.length * 2];
169: System.arraycopy(src, 0, dest, 0, src.length);
170: }
171: dest[position] = c;
172: return dest;
173: }
174:
175: /**
176: * Cleans the password stored in one char array.
177: *
178: * @param password
179: * the char array which contains password.
180: */
181: public static void clearPassword(char[] password) {
182: if (password == null || password.length == 0) {
183: return;
184: }
185: Arrays.fill(password, '\0');
186: }
187: }
|