mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
04.49.2009 9fdb95ca9c3c8e3524845760b81a85c7a4c81a45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package org.opends.sdk.extensions;
 
 
 
import static com.sun.opends.sdk.messages.Messages.*;
import static com.sun.opends.sdk.util.StaticUtils.*;
 
import java.io.IOException;
 
import org.opends.sdk.ByteString;
import org.opends.sdk.ByteStringBuilder;
import org.opends.sdk.DecodeException;
import org.opends.sdk.ResultCode;
import org.opends.sdk.asn1.ASN1;
import org.opends.sdk.asn1.ASN1Reader;
import org.opends.sdk.asn1.ASN1Writer;
import org.opends.sdk.requests.AbstractExtendedRequest;
 
import com.sun.opends.sdk.util.Message;
 
 
 
/**
 * This class implements the password modify extended operation response
 * defined in RFC 3062. It includes support for requiring the user's
 * current password as well as for generating a new password if none was
 * provided.
 */
public final class PasswordModifyRequest extends
    AbstractExtendedRequest<PasswordModifyRequest, PasswordModifyResult>
{
  /**
   * The request OID for the password modify extended operation.
   */
  public static final String OID_PASSWORD_MODIFY_REQUEST = "1.3.6.1.4.1.4203.1.11.1";
 
  /**
   * The ASN.1 element type that will be used to encode the userIdentity
   * component in a password modify extended request.
   */
  static final byte TYPE_PASSWORD_MODIFY_USER_ID = (byte) 0x80;
 
  /**
   * The ASN.1 element type that will be used to encode the oldPasswd
   * component in a password modify extended request.
   */
  static final byte TYPE_PASSWORD_MODIFY_OLD_PASSWORD = (byte) 0x81;
 
  /**
   * The ASN.1 element type that will be used to encode the newPasswd
   * component in a password modify extended request.
   */
  static final byte TYPE_PASSWORD_MODIFY_NEW_PASSWORD = (byte) 0x82;
 
  /**
   * The ASN.1 element type that will be used to encode the genPasswd
   * component in a password modify extended response.
   */
  static final byte TYPE_PASSWORD_MODIFY_GENERATED_PASSWORD = (byte) 0x80;
 
  private String userIdentity;
 
  private ByteString oldPassword;
 
  private ByteString newPassword;
 
 
 
  public PasswordModifyRequest()
  {
 
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public String getRequestName()
  {
    return OID_PASSWORD_MODIFY_REQUEST;
  }
 
 
  public ExtendedOperation<PasswordModifyRequest, PasswordModifyResult>
  getExtendedOperation()
  {
    return OPERATION;
  }
 
  public ByteString getNewPassword()
  {
    return newPassword;
  }
 
 
 
  public ByteString getOldPassword()
  {
    return oldPassword;
  }
 
 
 
  public ByteString getRequestValue()
  {
    ByteStringBuilder buffer = new ByteStringBuilder();
    ASN1Writer writer = ASN1.getWriter(buffer);
 
    try
    {
      writer.writeStartSequence();
      if (userIdentity != null)
      {
        writer.writeOctetString(TYPE_PASSWORD_MODIFY_USER_ID,
            userIdentity);
      }
      if (oldPassword != null)
      {
        writer.writeOctetString(TYPE_PASSWORD_MODIFY_OLD_PASSWORD,
            oldPassword);
      }
      if (newPassword != null)
      {
        writer.writeOctetString(TYPE_PASSWORD_MODIFY_NEW_PASSWORD,
            newPassword);
      }
      writer.writeEndSequence();
    }
    catch (IOException ioe)
    {
      // This should never happen unless there is a bug somewhere.
      throw new RuntimeException(ioe);
    }
 
    return buffer.toByteString();
  }
 
 
 
  public String getUserIdentity()
  {
    return userIdentity;
  }
 
 
 
  public PasswordModifyRequest setNewPassword(ByteString newPassword)
  {
    this.newPassword = newPassword;
    return this;
  }
 
 
 
  public PasswordModifyRequest setOldPassword(ByteString oldPassword)
  {
    this.oldPassword = oldPassword;
    return this;
  }
 
 
 
  public PasswordModifyRequest setUserIdentity(String userIdentity)
  {
    this.userIdentity = userIdentity;
    return this;
  }
 
 
 
  public StringBuilder toString(StringBuilder builder)
  {
    builder.append("PasswordModifyExtendedRequest(requestName=");
    builder.append(getRequestName());
    builder.append(", userIdentity=");
    builder.append(userIdentity);
    builder.append(", oldPassword=");
    builder.append(oldPassword);
    builder.append(", newPassword=");
    builder.append(newPassword);
    builder.append(", controls=");
    builder.append(getControls());
    builder.append(")");
    return builder;
  }
 
 
 
  private static final class Operation implements
      ExtendedOperation<PasswordModifyRequest, PasswordModifyResult>
  {
 
    public PasswordModifyRequest decodeRequest(String requestName,
        ByteString requestValue) throws DecodeException
    {
      PasswordModifyRequest request = new PasswordModifyRequest();
      if (requestValue != null)
      {
        try
        {
          ASN1Reader reader = ASN1.getReader(requestValue);
          reader.readStartSequence();
          if (reader.hasNextElement()
              && (reader.peekType() == TYPE_PASSWORD_MODIFY_USER_ID))
          {
            request.setUserIdentity(reader.readOctetStringAsString());
          }
          if (reader.hasNextElement()
              && (reader.peekType() == TYPE_PASSWORD_MODIFY_OLD_PASSWORD))
          {
            request.setOldPassword(reader.readOctetString());
          }
          if (reader.hasNextElement()
              && (reader.peekType() == TYPE_PASSWORD_MODIFY_NEW_PASSWORD))
          {
            request.setNewPassword(reader.readOctetString());
          }
          reader.readEndSequence();
        }
        catch (IOException e)
        {
          Message message = ERR_EXTOP_PASSMOD_CANNOT_DECODE_REQUEST
              .get(getExceptionMessage(e));
          throw DecodeException.error(message, e);
        }
      }
      return request;
    }
 
 
 
    public PasswordModifyResult decodeResponse(ResultCode resultCode,
        String matchedDN, String diagnosticMessage,
        String responseName, ByteString responseValue)
        throws DecodeException
    {
      // TODO: Should we check to make sure OID is null?
      PasswordModifyResult result = new PasswordModifyResult(resultCode)
          .setMatchedDN(matchedDN).setDiagnosticMessage(
              diagnosticMessage);
      if (resultCode == ResultCode.SUCCESS && responseValue != null)
      {
        try
        {
          ASN1Reader asn1Reader = ASN1.getReader(responseValue);
          asn1Reader.readStartSequence();
          if (asn1Reader.peekType() == TYPE_PASSWORD_MODIFY_GENERATED_PASSWORD)
          {
            result.setGenPassword(asn1Reader.readOctetString());
          }
          asn1Reader.readEndSequence();
        }
        catch (IOException e)
        {
          Message message = ERR_EXTOP_PASSMOD_CANNOT_DECODE_REQUEST
              .get(getExceptionMessage(e));
          throw DecodeException.error(message, e);
        }
      }
      return result;
    }
 
 
 
    public PasswordModifyResult decodeResponse(ResultCode resultCode,
        String matchedDN, String diagnosticMessage)
    {
      return new PasswordModifyResult(resultCode).setMatchedDN(
          matchedDN).setDiagnosticMessage(diagnosticMessage);
    }
  }
 
 
 
  // Singleton instance.
  private static final Operation OPERATION = new Operation();
}