001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring.security.sasl;
006:
007: import javax.security.auth.callback.*;
008: import javax.security.sasl.Sasl;
009: import javax.security.sasl.SaslClient;
010: import javax.security.sasl.SaslException;
011: import java.io.IOException;
012:
013: public class PlainClient implements SaslClient {
014: public PlainClient(CallbackHandler callbackHandler) {
015: this .callbackHandler = callbackHandler;
016: }
017:
018: public String getMechanismName() {
019: return SaslContext.MECHANISM_NAME;
020: }
021:
022: public final boolean hasInitialResponse() {
023: return true;
024: }
025:
026: private int getResponseSize() {
027: int responseSize = 0;
028: if (authorizationId != null) {
029: responseSize += authorizationId.length + 1;
030: }
031: if (userId != null) {
032: responseSize += userId.length + 1;
033: }
034: if (password != null) {
035: responseSize += password.length;
036: }
037:
038: return responseSize;
039: }
040:
041: private byte[] evaluate() throws SaslException {
042: final String userPrompt = "PortalServerPlain: UserId";
043: final String passwordPrompt = "PortalServerPlain: Password";
044: final NameCallback cbName = new NameCallback(userPrompt);
045: final PasswordCallback cbPassword = new PasswordCallback(
046: passwordPrompt, false);
047:
048: try {
049: callbackHandler
050: .handle(new Callback[] { cbName, cbPassword });
051: userId = cbName.getName().getBytes("UTF-8");
052: password = new String(cbPassword.getPassword())
053: .getBytes("UTF-8");
054: } catch (IOException ioe) {
055: throw new SaslException(ioe.toString());
056: } catch (UnsupportedCallbackException uce) {
057: throw new SaslException(uce.toString());
058: }
059:
060: final int responseSize = getResponseSize();
061: final byte[] result = new byte[responseSize];
062:
063: if (authorizationId != null) {
064: System.arraycopy(authorizationId, 0, result, 0,
065: authorizationId.length);
066: result[authorizationId.length] = SEPARATOR;
067: }
068:
069: if (authorizationId == null) {
070: System.arraycopy(userId, 0, result, 0, userId.length);
071: result[userId.length] = SEPARATOR;
072: } else {
073: System.arraycopy(userId, 0, result,
074: authorizationId.length + 1, userId.length);
075: result[authorizationId.length + 1 + userId.length] = SEPARATOR;
076: }
077:
078: if (authorizationId == null) {
079: System.arraycopy(password, 0, result, userId.length + 1,
080: password.length);
081: } else {
082: System.arraycopy(password, 0, result,
083: authorizationId.length + 1 + userId.length + 1,
084: password.length);
085: }
086: clearPassword();
087:
088: return result;
089: }
090:
091: public final byte[] evaluateChallenge(final byte[] challenge)
092: throws SaslException {
093: if (complete.booleanValue()) {
094: throw new SaslException("Authentication already complete!");
095: } else {
096: complete = Boolean.TRUE;
097: return evaluate();
098: }
099: }
100:
101: public final boolean isComplete() {
102: return complete.booleanValue();
103: }
104:
105: public final byte[] unwrap(final byte[] bytes, final int i,
106: final int i1) throws SaslException {
107: if (complete.booleanValue()) {
108: throw new SaslException("This mechanism supports QOP <"
109: + QUALITY_OF_PROTECTION + "> only!");
110: } else {
111: throw new SaslException("Authentication not yet complete!");
112: }
113: }
114:
115: public final byte[] wrap(final byte[] bytes, final int i,
116: final int i1) throws SaslException {
117: if (complete.booleanValue()) {
118: throw new SaslException("This mechanism supports QOP <"
119: + QUALITY_OF_PROTECTION + "> only!");
120: } else {
121: throw new SaslException("Authentication not yet complete!");
122: }
123: }
124:
125: public final Object getNegotiatedProperty(final String propertyName) {
126: if (propertyName.equals(Sasl.QOP)) {
127: return QUALITY_OF_PROTECTION;
128: } else {
129: return null;
130: }
131: }
132:
133: private void clearPassword() {
134: password = null;
135: }
136:
137: public final void dispose() throws SaslException {
138: clearPassword();
139: }
140:
141: private Boolean complete = Boolean.FALSE;
142: private final String QUALITY_OF_PROTECTION = "auth";
143: private final byte SEPARATOR = 0;
144:
145: private byte[] authorizationId = null;
146: private byte[] userId = null;
147: private byte[] password = null;
148:
149: private CallbackHandler callbackHandler;
150: }
|