1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.extras.controls.ppolicy_impl;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.Asn1Object;
26 import org.apache.directory.api.asn1.DecoderException;
27 import org.apache.directory.api.asn1.EncoderException;
28 import org.apache.directory.api.asn1.ber.Asn1Decoder;
29 import org.apache.directory.api.asn1.ber.tlv.BerValue;
30 import org.apache.directory.api.asn1.ber.tlv.TLV;
31 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.api.i18n.I18n;
33 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
34 import org.apache.directory.api.ldap.codec.api.LdapApiService;
35 import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
36 import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicyImpl;
37 import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicyResponse;
38
39
40
41
42
43
44
45 public class PasswordPolicyDecorator extends ControlDecorator<PasswordPolicy> implements PasswordPolicy
46 {
47
48 private static final Asn1Decoder decoder = new Asn1Decoder();
49
50
51 private int ppolicySeqLength = 0;
52 private int warningLength = 0;
53 private int timeBeforeExpirationValueLength;
54 private int graceAuthNsRemainingValueLength;
55
56
57 public PasswordPolicyDecorator( LdapApiService codec )
58 {
59 super( codec, new PasswordPolicyImpl() );
60 }
61
62
63 public PasswordPolicyDecorator( LdapApiService codec, boolean hasResponse )
64 {
65 super( codec, new PasswordPolicyImpl( hasResponse ) );
66 }
67
68
69 public PasswordPolicyDecorator( LdapApiService codec, PasswordPolicy policy )
70 {
71 super( codec, policy );
72 }
73
74
75
76
77
78 @Override
79 public void setValue( byte[] value )
80 {
81 if ( value == null || value.length <= 2 )
82 {
83 setResponse( null );
84 }
85 else if ( value != null && !hasResponse() )
86 {
87 setResponse( true );
88 }
89
90 super.setValue( value );
91 }
92
93
94 @Override
95 public int computeLength()
96 {
97
98 valueLength = 0;
99 ppolicySeqLength = 0;
100 warningLength = 0;
101 timeBeforeExpirationValueLength = 0;
102 graceAuthNsRemainingValueLength = 0;
103
104 if ( !hasResponse() )
105 {
106 return 0;
107 }
108
109 if ( getResponse().getTimeBeforeExpiration() >= 0 )
110 {
111 timeBeforeExpirationValueLength = BerValue.getNbBytes( getResponse().getTimeBeforeExpiration() );
112 warningLength = 1 + TLV.getNbBytes( timeBeforeExpirationValueLength ) + timeBeforeExpirationValueLength;
113 }
114 else if ( getResponse().getGraceAuthNRemaining() >= 0 )
115 {
116 graceAuthNsRemainingValueLength = BerValue.getNbBytes( getResponse().getGraceAuthNRemaining() );
117 warningLength = 1 + TLV.getNbBytes( graceAuthNsRemainingValueLength ) + graceAuthNsRemainingValueLength;
118 }
119
120 if ( warningLength != 0 )
121 {
122 ppolicySeqLength = 1 + TLV.getNbBytes( warningLength ) + warningLength;
123 }
124
125 if ( getResponse().getPasswordPolicyError() != null )
126 {
127 ppolicySeqLength += 1 + 1 + 1;
128 }
129
130 valueLength = 1 + TLV.getNbBytes( ppolicySeqLength ) + ppolicySeqLength;
131
132 return valueLength;
133 }
134
135
136 @Override
137 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
138 {
139 if ( !hasResponse() )
140 {
141 return buffer;
142 }
143
144 if ( buffer == null )
145 {
146 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
147 }
148
149
150 buffer.put( UniversalTag.SEQUENCE.getValue() );
151 buffer.put( TLV.getBytes( ppolicySeqLength ) );
152
153 if ( ( getResponse().getTimeBeforeExpiration() < 0 ) && ( getResponse().getGraceAuthNRemaining() < 0 ) && (
154 getResponse().getPasswordPolicyError() == null ) )
155 {
156 return buffer;
157 }
158 else
159 {
160 if ( warningLength > 0 )
161 {
162
163 buffer.put( ( byte ) PasswordPolicyTags.PPOLICY_WARNING_TAG.getValue() );
164 buffer.put( TLV.getBytes( warningLength ) );
165
166 if ( getResponse().getTimeBeforeExpiration() >= 0 )
167 {
168 BerValue.encode(
169 buffer,
170 ( byte ) PasswordPolicyTags.TIME_BEFORE_EXPIRATION_TAG.getValue(),
171 getResponse().getTimeBeforeExpiration() );
172 }
173 else if ( getResponse().getGraceAuthNRemaining() >= 0 )
174 {
175 BerValue.encode(
176 buffer,
177 ( byte ) PasswordPolicyTags.GRACE_AUTHNS_REMAINING_TAG.getValue(),
178 getResponse().getGraceAuthNRemaining() );
179 }
180 }
181
182 if ( getResponse().getPasswordPolicyError() != null )
183 {
184 BerValue.encode(
185 buffer,
186 ( byte ) PasswordPolicyTags.PPOLICY_ERROR_TAG.getValue(),
187 getResponse().getPasswordPolicyError().getValue() );
188 }
189 }
190
191 return buffer;
192 }
193
194
195 @Override
196 public String toString()
197 {
198 StringBuilder sb = new StringBuilder();
199
200 sb.append( " PasswordPolicyResponse control :\n" );
201 sb.append( " oid : '" ).append( getOid() ).append( '\n' );
202
203 if ( hasResponse() && getResponse().getTimeBeforeExpiration() >= 0 )
204 {
205 sb.append( " timeBeforeExpiration : '" ).append( getResponse().getTimeBeforeExpiration() )
206 .append( '\n' );
207 }
208 else if ( hasResponse() && getResponse().getGraceAuthNRemaining() >= 0 )
209 {
210 sb.append( " graceAuthNsRemaining : '" ).append( getResponse().getGraceAuthNRemaining() )
211 .append( '\n' );
212 }
213
214 if ( hasResponse() && getResponse().getPasswordPolicyError() != null )
215 {
216 sb.append( " ppolicyError : '" ).append( getResponse().getPasswordPolicyError().toString() )
217 .append( '\n' );
218 }
219
220 return sb.toString();
221 }
222
223
224
225
226
227 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
228 {
229 if ( !hasResponse() )
230 {
231 return this;
232 }
233
234 ByteBuffer bb = ByteBuffer.wrap( controlBytes );
235 PasswordPolicyContainer container = new PasswordPolicyContainer( getCodecService(), this );
236 decoder.decode( bb, container );
237 return this;
238 }
239
240
241
242
243
244
245 public boolean hasResponse()
246 {
247 return getDecorated().hasResponse();
248 }
249
250
251
252
253
254
255 public void setResponse( PasswordPolicyResponse response )
256 {
257 getDecorated().setResponse( response );
258 }
259
260
261
262
263
264
265 public PasswordPolicyResponse setResponse( boolean hasResponse )
266 {
267 return getDecorated().setResponse( hasResponse );
268 }
269
270
271
272
273
274
275 public PasswordPolicyResponse getResponse()
276 {
277 return getDecorated().getResponse();
278 }
279 }