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

matthew_swift
26.38.2010 145f771eb59ca97a1a6f256a75b2d6b46667fb3d
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 */
package org.opends.arisid;
 
 
 
import static org.opends.server.protocols.asn1.ASN1Constants.*;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
 
import org.opends.messages.Message;
import org.opends.server.controls.ControlDecoder;
import org.opends.server.protocols.asn1.ASN1;
import org.opends.server.protocols.asn1.ASN1Reader;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.types.*;
import org.openliberty.arisid.*;
import org.openliberty.arisid.log.ILogger;
import org.openliberty.arisid.log.LogHandler;
import org.openliberty.arisid.policy.IPolicy;
import org.openliberty.arisid.policy.PolicyHandler;
import org.openliberty.arisid.protocol.ldap.IPrivacyControl;
import org.w3c.dom.Element;
 
 
 
/**
 * IGF ArisID Privacy Control implementation.
 */
public class ArisIDPrivacyControl extends Control implements
    IPrivacyControl
{
 
  /**
   * ControlDecoder implentation to decode this control from a
   * ByteString.
   */
  private final static class Decoder implements
      ControlDecoder<ArisIDPrivacyControl>
  {
    /**
     * Decodes and constructs a Java object representing the
     * encodedValue.
     * <p>
     * ASN.1 encoded value as per Privacy Control Specifiction:
     * http://www.openliberty.org/wiki/index.php/Profile_LDAP#
     * Extended_PolicySequence_Variation
     */
    public ArisIDPrivacyControl decode(boolean isCritical,
        ByteString value) throws DirectoryException
    {
      if (value == null)
      {
        final Message message = Message
            .raw("Control contains no value");
        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
      }
 
      String ixnName = "";
      String appName = "";
      String appUri = "";
      final HashMap<String, IPolicy> polMap = new HashMap<String, IPolicy>();
      final ASN1Reader reader = ASN1.getReader(value);
 
      try
      {
        reader.readStartSequence();
        {
          appName = reader.readOctetStringAsString();
          appUri = reader.readOctetStringAsString();
          ixnName = reader.readOctetStringAsString();
 
          reader.readStartSequence();
          {
            // Get the count of policies coming
            final long policyCount = reader.readInteger();
            if (policyCount > 1)
            {
              for (int i = 0; i < policyCount; i++)
              {
                reader.readStartSequence();
                {
                  final String pname = reader.readOctetStringAsString();
                  final String pStr = reader.readOctetStringAsString();
                  final Element node = phandler
                      .parseStringToElement(pStr);
                  IPolicy pol = null;
                  try
                  {
                    pol = phandler.parseDomPolicy(node);
                  }
                  catch (final Exception e)
                  {
                    logger.error("Error parsing policy: "
                        + e.getMessage(), e);
                  }
                  polMap.put(pname, pol);
                }
                reader.readEndSequence();
              }
            }
          }
          reader.readEndSequence();
        }
        reader.readEndSequence();
      }
      catch (final Exception e1)
      {
        throw new DirectoryException(ResultCode.PROTOCOL_ERROR,
            Message.raw("Unable to decode privacy control: "
                + e1.getMessage()), e1);
      }
 
      return new ArisIDPrivacyControl(isCritical, ixnName, appName,
          appUri, polMap);
    }
 
 
 
    /**
     * {@inheritDoc}
     */
    public String getOID()
    {
      return IPrivacyControl.OID_IGF_CONTROL;
    }
  }
 
 
 
  private static final long serialVersionUID = -2668010326604049964L;
 
  private static final ILogger logger = LogHandler
      .getLogger(ArisIDPrivacyControl.class);
 
  private static PolicyHandler phandler = PolicyHandler.getInstance();
 
  private String _ixnName = "";
 
  private String _appName = "";
 
  private String _appUri = "";
 
  private ArisIdService _asvc = null;
 
  private CarmlDoc _doc = null;
 
  private HashMap<String, IPolicy> _polMap = new HashMap<String, IPolicy>();
 
  /**
   * The Control Decoder that can be used to decode this control.
   */
  public static final ControlDecoder<ArisIDPrivacyControl> DECODER = new Decoder();
 
 
 
  /**
   * Creates a new non-critical IGF Privacy Control using the provided
   * Interaction.
   *
   * @param ixn
   *          The interaction.
   * @throws IGFException
   *           If an error occurred processing the Interaction.
   */
  public ArisIDPrivacyControl(IInteraction ixn) throws IGFException
  {
    super(IPrivacyControl.OID_IGF_CONTROL, false);
    _processCarmlDoc(ixn);
  }
 
 
 
  /**
   * Creates a new IGF Privacy Control using the provided Interaction
   * and criticality.
   *
   * @param ixn
   *          The interaction.
   * @param critical
   *          The criticality.
   * @throws IGFException
   *           If an error occurred processing the Interaction.
   */
  public ArisIDPrivacyControl(IInteraction ixn, boolean critical)
      throws IGFException
  {
    super(IPrivacyControl.OID_IGF_CONTROL, critical);
    _processCarmlDoc(ixn);
  }
 
 
 
  private ArisIDPrivacyControl(boolean isCritical, String _ixnName,
      String _appName, String _appUri, HashMap<String, IPolicy> _polMap)
  {
    super(IPrivacyControl.OID_IGF_CONTROL, isCritical);
    this._ixnName = _ixnName;
    this._appName = _appName;
    this._appUri = _appUri;
    this._polMap = _polMap;
  }
 
 
 
  public String getAppName()
  {
    return this._appName;
  }
 
 
 
  /**
   * @deprecated Use {@link #getCarmlURI()} instead
   */
  @Deprecated
  public URI getAppURI()
  {
    return getCarmlURI();
  }
 
 
 
  /**
   * A convenience method to obtain the CarmlDoc object referenced by
   * this control. For performance reasons, the CarmlDoc object is not
   * instantiated unless {@link #loadCarmlDoc(URI)} is called first.
   *
   * @return A CarmlDoc object containing the Controls referenced
   *         CarmlDoc or null if the document hasn't been loaded.
   */
  public CarmlDoc getCarmlDoc()
  {
 
    return this._doc;
  }
 
 
 
  public URI getCarmlURI()
  {
    try
    {
      return new URI(this._appUri);
    }
    catch (final URISyntaxException e)
    {
      logger
          .warn("Invalid CARML URI syntax exception occurred for value: "
              + this._appUri);
      return null;
    }
  }
 
 
 
  /*
   * (non-Javadoc)
   * @see
   * org.openliberty.arisid.protocol.ldap.IPrivacyControl#getConstraintMap
   * ()
   */
  public Map<String, IPolicy> getConstraintMap()
  {
    return this._polMap;
  }
 
 
 
  /*
   * (non-Javadoc)
   * @seeorg.openliberty.arisid.protocol.ldap.IPrivacyControl#
   * getDynamicConstraints (java.lang.String)
   */
  public IPolicy getDynamicConstraints(String nameId)
  {
    return this._polMap.get(nameId);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  public byte[] getEncodedValue()
  {
    try
    {
      final ByteStringBuilder builder = new ByteStringBuilder();
      final ASN1Writer writer = ASN1.getWriter(builder);
      writeValue(writer);
      writer.close();
      return builder.toByteArray();
    }
    catch (final IOException e)
    {
      // Should never occur.
      throw new RuntimeException(e);
    }
  }
 
 
 
  /*
   * (non-Javadoc)
   * @see org.openliberty.arisid.protocol.ldap.IPrivacyControl#getID()
   */
  public String getID()
  {
    return OID_IGF_CONTROL;
  }
 
 
 
  /*
   * (non-Javadoc)
   * @see
   * org.openliberty.arisid.protocol.ldap.IPrivacyControl#getInteractionName
   * ()
   */
  public String getInteractionName()
  {
    return this._ixnName;
  }
 
 
 
  /**
   * Returns the named transaction constraint.
   *
   * @param nameId
   *          The transaction constraint name.
   * @return The named transaction constraint.
   */
  public IPolicy getTransactionConstraints(String nameId)
  {
    return this._polMap.get(nameId);
  }
 
 
 
  /**
   * This is a convenience method intended for servers/proxies that need
   * to instantiate a CarmlDoc object. The localUri is the URI of a
   * local copy of the controls referenced Carml Document.
   *
   * @param localUri
   *          A URI to a copy of the CARML document matching the
   *          {@link #getAppName()} of this control. If localUri is
   *          null, the method will use the stored URI to load the
   *          document.
   * @throws URISyntaxException
   * @throws IllegalAccessException
   * @throws IGFException
   * @throws InstantiationException
   * @throws AttrSvcInitializedException
   * @throws FileNotFoundException
   */
  public void loadCarmlDoc(URI localUri) throws URISyntaxException,
      FileNotFoundException, AttrSvcInitializedException,
      InstantiationException, IGFException, IllegalAccessException
  {
    URI loadUri = localUri;
    if (loadUri == null)
    {
      loadUri = new URI(this._appUri);
    }
 
    // TODO I wonder if this should come from a static hash to avoid
    // re-parsing?
    this._asvc = ArisIdServiceFactory.parseCarmlOnly(loadUri);
    if (this._asvc != null)
    {
      this._doc = this._asvc.getCarmlDoc();
    }
  }
 
 
 
  /*
   * (non-Javadoc)
   * @seeorg.openliberty.arisid.protocol.ldap.IPrivacyControl#
   * setDynamicConstraints (java.util.Map)
   */
  public void setDynamicConstraints(
      Map<String, IPolicy> dynamicConstraints)
  {
    if (dynamicConstraints != null)
    {
      this._polMap.putAll(dynamicConstraints);
    }
  }
 
 
 
  /*
   * (non-Javadoc)
   * @seeorg.openliberty.arisid.protocol.ldap.IPrivacyControl#
   * setTranasactionConstraints(java.lang.String,
   * org.openliberty.arisid.schema.IPolicy)
   */
  public void setDynamicConstraints(String nameId,
      IPolicy txnConstraints)
  {
    this._polMap.put(nameId, txnConstraints);
  }
 
 
 
  private void _processCarmlDoc(IInteraction ixn) throws IGFException
  {
    // set defaults of empty string
    this._appName = "";
    this._ixnName = "";
    this._appUri = "";
 
    if (ixn == null)
    {
      return;
    }
 
    final ArisIdService asvc = ixn.getAttributeService();
    if (asvc == null)
    {
      return;
    }
 
    final CarmlDoc doc = asvc.getCarmlDoc();
    this._appName = doc.getApplicationNameId();
    if (this._appName == null)
    {
      this._appName = "";
    }
    this._appUri = doc.getCarmlURI().toString();
    if (this._appUri == null)
    {
      this._appUri = "";
    }
    this._ixnName = ixn.getNameId();
    if (this._ixnName == null)
    {
      this._ixnName = "";
    }
 
  }
 
 
 
  /**
   * Encode ASN.1 value. Encoding is as per spec:
   * http://www.openliberty.org/wiki
   * /index.php/Profile_LDAP#Extended_PolicySequence_Variation
   */
  protected void writeValue(ASN1Writer writer) throws IOException
  {
    writer.writeStartSequence(UNIVERSAL_OCTET_STRING_TYPE);
    {
      writer.writeOctetString(_appName);
      writer.writeOctetString(_appUri);
      writer.writeOctetString(_ixnName);
      writer.writeStartSequence();
      {
        writer.writeInteger(_polMap.size());
        for (final Map.Entry<String, IPolicy> entry : _polMap
            .entrySet())
        {
          writer.writeStartSequence();
          {
            final String nameId = entry.getKey();
            final IPolicy pol = this.getTransactionConstraints(nameId);
            final String strPol = phandler.policyToString(pol);
 
            writer.writeOctetString(entry.getKey());
            writer.writeOctetString(strPol);
          }
          writer.writeEndSequence();
        }
      }
      writer.writeEndSequence();
    }
    writer.writeEndSequence();
  }
 
}