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.jndi.provider.ldap;
019:
020: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
021: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
022: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
023: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
024: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
025: import org.apache.harmony.security.asn1.ASN1Integer;
026:
027: /**
028: * Ldap Bind operation
029: */
030: public class BindOp implements LdapOperation {
031:
032: private String name;
033:
034: private byte[] serverSaslCreds; // server's challenge
035:
036: private LdapResult result; // result from this Bind operation
037:
038: AuthenticationChoice authChoice;
039:
040: private class SaslCredentials implements ASN1Encodable {
041:
042: private String mechanism;
043:
044: private byte[] credentials;
045:
046: public SaslCredentials(String mech, byte[] creds) {
047: this .mechanism = mech;
048: this .credentials = creds;
049: }
050:
051: public void encodeValues(Object[] values) {
052: values[0] = Utils.getBytes(mechanism);
053: values[1] = credentials;
054: }
055:
056: public void setMechanism(String mechanism) {
057: this .mechanism = mechanism;
058: }
059:
060: public void setCredentials(byte[] credentials) {
061: this .credentials = credentials;
062: }
063:
064: public byte[] getCredentials() {
065: return credentials;
066: }
067:
068: }
069:
070: private class AuthenticationChoice implements ASN1Encodable {
071:
072: public AuthenticationChoice(int index, SaslCredentials sasl) {
073: this .index = index;
074: this .sasl = sasl;
075: }
076:
077: public AuthenticationChoice(int index, String password) {
078: this .index = index;
079: this .password = password;
080: }
081:
082: private int index;
083:
084: private SaslCredentials sasl;
085:
086: private String password;
087:
088: public void encodeValues(Object[] values) {
089: Object value;
090:
091: if (index == 0) {
092: value = Utils.getBytes(password);
093: } else {
094: value = sasl;
095: }
096: values[0] = new ChosenValue(index, value);
097:
098: }
099:
100: public int getIndex() {
101: return index;
102: }
103:
104: public byte[] getSaslCredentials() {
105: return sasl.getCredentials();
106: }
107:
108: public void setSaslCredentials(byte[] credentials) {
109: sasl.setCredentials(credentials);
110: }
111:
112: }
113:
114: public BindOp(String dn, String pwd, String saslMechanism,
115: byte[] res) {
116: this .name = dn;
117:
118: if (saslMechanism == null) {
119: authChoice = new AuthenticationChoice(0, pwd);
120: } else {
121: SaslCredentials saslCreds = new SaslCredentials(
122: saslMechanism, res);
123: authChoice = new AuthenticationChoice(1, saslCreds);
124: }
125: }
126:
127: public ASN1Encodable getRequest() {
128: return new ASN1Encodable() {
129: public void encodeValues(Object[] values) {
130: values[0] = ASN1Integer.fromIntValue(3);
131: values[1] = Utils.getBytes(name);
132: values[2] = authChoice;
133: }
134: };
135: }
136:
137: public ASN1Decodable getResponse() {
138:
139: return new ASN1Decodable() {
140: public void decodeValues(Object[] values) {
141: result = new LdapResult();
142: result.decodeValues(values);
143: if (values[4] != null) {
144: serverSaslCreds = (byte[]) values[4];
145:
146: }
147:
148: }
149:
150: };
151: }
152:
153: public int getRequestId() {
154: return LdapASN1Constant.OP_BIND_REQUEST;
155: }
156:
157: public int getResponseId() {
158: return LdapASN1Constant.OP_BIND_RESPONSE;
159: }
160:
161: public void setSaslCredentials(byte[] credentials) {
162: authChoice.setSaslCredentials(credentials);
163: }
164:
165: public LdapResult getResult() {
166: return result;
167: }
168:
169: public byte[] getServerSaslCreds() {
170: return serverSaslCreds;
171: }
172: }
|