~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/CollectionStore/.svn/text-base/Identity.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 | Author: Mike Lasky <mlasky@novell.com>
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.IO;
27
 
using System.Security.Cryptography;
28
 
using System.Text;
29
 
using System.Xml;
30
 
 
31
 
using Simias.Client;
32
 
using Persist = Simias.Storage.Provider;
33
 
 
34
 
namespace Simias.Storage
35
 
{
36
 
        /// <summary>
37
 
        /// Class that represents a user identity in the Collection Store.
38
 
        /// </summary>
39
 
        public class Identity : Node
40
 
        {
41
 
                #region Class Members
42
 
                /// <summary>
43
 
                /// Used to log messages.
44
 
                /// </summary>
45
 
                static private readonly ISimiasLog log = SimiasLogManager.GetLogger( typeof( Identity ) );
46
 
 
47
 
                /// <summary>
48
 
                /// This is used to keep from generating a new key set everytime a new RSACryptoSecurityProvider
49
 
                /// object is instantiated. This is passed as a parameter to the constructor and will initially
50
 
                /// use the dummy key set until the real key set is imported.
51
 
                /// </summary>
52
 
                static private CspParameters DummyParameters;
53
 
 
54
 
                /// <summary>
55
 
                /// Xml tags used to store the domain mapping information.
56
 
                /// </summary>
57
 
                static private readonly string MappingTag = "Mapping";
58
 
                static private readonly string DomainTag = "Domain";
59
 
                static private readonly string UserTag = "User";
60
 
                static private readonly string CredentialTag = "Credential";
61
 
                static private readonly string TypeTag = "Type";
62
 
                static private readonly string PassPhraseTag = "PassPhrase";
63
 
                static private readonly string PassPhraseTypeTag = "PassPhraseType";
64
 
                static private readonly string RememberPassPhraseTag = "RememberPassPhrase";
65
 
 
66
 
                /// <summary>
67
 
                /// Handle to the store.
68
 
                /// </summary>
69
 
                private Store store = null;
70
 
                #endregion
71
 
 
72
 
                #region Properties
73
 
                /// <summary>
74
 
                /// Gets the store handle.
75
 
                /// </summary>
76
 
                private Store StoreReference
77
 
                {
78
 
                        get
79
 
                        {
80
 
                                if ( store == null )
81
 
                                {
82
 
                                        store = Store.GetStore();
83
 
                                }
84
 
 
85
 
                                return store;
86
 
                        }
87
 
                }
88
 
 
89
 
                /// <summary>
90
 
                /// Gets the public/private key values for the local identity.
91
 
                /// </summary>
92
 
                public RSACryptoServiceProvider Credential
93
 
                {
94
 
                        get
95
 
                        {
96
 
                                RSACryptoServiceProvider credential = null;
97
 
 
98
 
                                // Lookup the credential property on the identity.
99
 
                                XmlDocument mapDoc = GetDocumentByDomain( StoreReference.LocalDomain );
100
 
                                if ( mapDoc != null )
101
 
                                {
102
 
                                        credential = DummyCsp;
103
 
                                        credential.FromXmlString( mapDoc.DocumentElement.GetAttribute( CredentialTag ) );
104
 
                                }
105
 
 
106
 
                                return credential;
107
 
                        }
108
 
                }
109
 
 
110
 
                /// <summary>
111
 
                /// Returns the number of subscribed domains.
112
 
                /// </summary>
113
 
                internal int DomainCount
114
 
                {
115
 
                        get
116
 
                        {
117
 
                                MultiValuedList mvl = properties.GetProperties( PropertyTags.Domain );
118
 
                                return mvl.Count;
119
 
                        }
120
 
                }
121
 
 
122
 
                /// <summary>
123
 
                /// Gets the public key for the Identity object.
124
 
                /// </summary>
125
 
                public RSACryptoServiceProvider PublicKey
126
 
                {
127
 
                        get
128
 
                        {
129
 
                                // Export the public key from the credential set.
130
 
                                RSACryptoServiceProvider pk = null;
131
 
                                RSACryptoServiceProvider credential = Credential;
132
 
                                if ( credential != null )
133
 
                                {
134
 
                                        pk = DummyCsp;
135
 
                                        pk.ImportParameters( credential.ExportParameters( false ) );
136
 
                                }
137
 
 
138
 
                                return pk;
139
 
                        }
140
 
                }
141
 
 
142
 
                /// <summary>
143
 
                /// Gets the CSP for the dummy key container.
144
 
                /// </summary>
145
 
                static internal RSACryptoServiceProvider DummyCsp
146
 
                {
147
 
                        get
148
 
                        {
149
 
                                RSACryptoServiceProvider csp = null;
150
 
 
151
 
                                lock( DummyParameters )
152
 
                                {
153
 
                                        try
154
 
                                        {
155
 
                                                csp = new RSACryptoServiceProvider( DummyParameters );
156
 
                                        }
157
 
                                        catch ( CryptographicException e )
158
 
                                        {
159
 
                                                log.Debug( e, "Corrupt cryptographic key container." );
160
 
#if WINDOWS
161
 
                                                IntPtr phProv = IntPtr.Zero;
162
 
                                                if ( CryptAcquireContext(
163
 
                                                        ref phProv,
164
 
                                                        DummyParameters.KeyContainerName,
165
 
                                                        "Microsoft Strong Cryptographic Provider",
166
 
                                                        1, // PROV_RSA_FULL
167
 
                                                        0x10) ) // CRYPT_DELETEKEYSET
168
 
                                                {
169
 
                                                        csp = new RSACryptoServiceProvider( DummyParameters );
170
 
                                                }
171
 
#endif
172
 
                                        }
173
 
                                }
174
 
 
175
 
                                return csp;
176
 
                        }
177
 
                }
178
 
                #endregion
179
 
 
180
 
                #region Win32APIs
181
 
#if WINDOWS
182
 
                [System.Runtime.InteropServices.DllImport( "advapi32.dll", SetLastError=true )]
183
 
                static extern bool CryptAcquireContext( ref IntPtr phProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags );
184
 
#endif
185
 
                #endregion
186
 
 
187
 
                #region Constructors
188
 
                /// <summary>
189
 
                /// Static constructor for the object.
190
 
                /// </summary>
191
 
                static Identity()
192
 
                {
193
 
                        // Set up the dummy key store so that it will contain a dummy key set.
194
 
                        DummyParameters = new CspParameters();
195
 
                        DummyParameters.KeyContainerName = "DummyKeyStore";
196
 
                        RSACryptoServiceProvider csp = DummyCsp;
197
 
                }
198
 
 
199
 
                /// <summary>
200
 
                /// Constructor for creating a new Identity object.
201
 
                /// </summary>
202
 
                /// <param name="store">A handle to the store.</param>
203
 
                /// <param name="userName">User name of the identity.</param>
204
 
                /// <param name="userGuid">Unique identifier for the user.</param>
205
 
                internal Identity( Store store, string userName, string userGuid ) :
206
 
                        base ( userName, userGuid, NodeTypes.IdentityType )
207
 
                {
208
 
                        this.store = store;     
209
 
                }
210
 
 
211
 
                /// <summary>
212
 
                /// Constructor that creates an Identity object from a Node object.
213
 
                /// </summary>
214
 
                /// <param name="node">Node object to create the Identity object from.</param>
215
 
                internal Identity( Node node ) :
216
 
                        base( node )
217
 
                {
218
 
                        if ( type != NodeTypes.IdentityType )
219
 
                        {
220
 
                                throw new CollectionStoreException( String.Format( "Cannot construct an object type of {0} from an object of type {1}.", NodeTypes.IdentityType, type ) );
221
 
                        }
222
 
                }
223
 
 
224
 
                /// <summary>
225
 
                /// Constructor that creates an Identity object from a ShallowNode object.
226
 
                /// </summary>
227
 
                /// <param name="collection">Collection that the specified Node object belongs to.</param>
228
 
                /// <param name="shallowNode">ShallowNode object to create the Identity object from.</param>
229
 
                internal Identity( Collection collection, ShallowNode shallowNode ) :
230
 
                        base( collection, shallowNode )
231
 
                {
232
 
                        if ( type != NodeTypes.IdentityType )
233
 
                        {
234
 
                                throw new CollectionStoreException( String.Format( "Cannot construct an object type of {0} from an object of type {1}.", NodeTypes.IdentityType, type ) );
235
 
                        }
236
 
                }
237
 
 
238
 
                /// <summary>
239
 
                /// Constructor that creates an Identity object from an Xml document object.
240
 
                /// </summary>
241
 
                /// <param name="document">Xml document object to create the Identity object from.</param>
242
 
                internal Identity( XmlDocument document ) :
243
 
                        base( document )
244
 
                {
245
 
                        if ( type != NodeTypes.IdentityType )
246
 
                        {
247
 
                                throw new CollectionStoreException( String.Format( "Cannot construct an object type of {0} from an object of type {1}.", NodeTypes.IdentityType, type ) );
248
 
                        }
249
 
                }
250
 
                #endregion
251
 
 
252
 
                #region Private Methods
253
 
                /// <summary>
254
 
                /// Decrypts the credential.
255
 
                /// </summary>
256
 
                /// <param name="encryptedCredential">A string object that contain the encrypted credential.</param>
257
 
                /// <returns>A string object containing the clear credential.</returns>
258
 
                private string DecryptCredential( string encryptedCredential )
259
 
                {
260
 
                        // Decrypt the byte array and convert it back into a string.
261
 
                        byte[] buffer = Credential.Decrypt( Convert.FromBase64String( encryptedCredential ), false );
262
 
                        return new UTF8Encoding().GetString( buffer );
263
 
                }
264
 
 
265
 
                /// <summary>
266
 
                /// Encrypts the credential.
267
 
                /// </summary>
268
 
                /// <param name="credential">Credential to encrypt.</param>
269
 
                /// <returns>A string object containing the encrypted credential.</returns>
270
 
                private string EncryptCredential( string credential )
271
 
                {
272
 
                        // Convert the string to a byte array.
273
 
                        UTF8Encoding encoding = new UTF8Encoding();
274
 
                        int byteCount = encoding.GetByteCount( credential );
275
 
                        byte[] buffer = new byte[ byteCount ];
276
 
                        encoding.GetBytes( credential, 0, credential.Length, buffer, 0 );
277
 
 
278
 
                        // Encrypt the byte array and turn it into a string.
279
 
                        return Convert.ToBase64String( Credential.Encrypt( buffer, false ) );
280
 
                }
281
 
 
282
 
                /// <summary>
283
 
                /// Gets the XML document that contains the specified Domain property.
284
 
                /// </summary>
285
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
286
 
                /// <returns>An XmlDocument object containing the found domain property.</returns>
287
 
                private XmlDocument GetDocumentByDomain( string domainID )
288
 
                {
289
 
                        XmlDocument document = null;
290
 
 
291
 
                        MultiValuedList mvl = properties.GetProperties( PropertyTags.Domain );
292
 
                        foreach ( Property p in mvl )
293
 
                        {
294
 
                                XmlDocument mapDoc = p.Value as XmlDocument;
295
 
                                if ( mapDoc.DocumentElement.GetAttribute( DomainTag ) == domainID )
296
 
                                {
297
 
                                        document = mapDoc;
298
 
                                        break;
299
 
                                }
300
 
                        }
301
 
 
302
 
                        return document;
303
 
                }
304
 
 
305
 
                /// <summary>
306
 
                /// Gets the XML document that contains the specified Domain property.
307
 
                /// </summary>
308
 
                /// <param name="userID">User ID to use to discover domain property.</param>
309
 
                /// <returns>An XmlDocument object containing the found domain property.</returns>
310
 
                private XmlDocument GetDocumentByUserID( string userID )
311
 
                {
312
 
                        XmlDocument document = null;
313
 
 
314
 
                        MultiValuedList mvl = properties.GetProperties( PropertyTags.Domain );
315
 
                        foreach ( Property p in mvl )
316
 
                        {
317
 
                                XmlDocument mapDoc = p.Value as XmlDocument;
318
 
                                if ( mapDoc.DocumentElement.GetAttribute( UserTag ) == userID )
319
 
                                {
320
 
                                        document = mapDoc;
321
 
                                        break;
322
 
                                }
323
 
                        }
324
 
 
325
 
                        return document;
326
 
                }
327
 
 
328
 
                /// <summary>
329
 
                /// Gets the specified Domain property.
330
 
                /// </summary>
331
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
332
 
                /// <returns>A Property object containing the found domain property.</returns>
333
 
                private Property GetPropertyByDomain( string domainID )
334
 
                {
335
 
                        Property property = null;
336
 
 
337
 
                        MultiValuedList mvl = properties.GetProperties( PropertyTags.Domain );
338
 
                        foreach ( Property p in mvl )
339
 
                        {
340
 
                                XmlDocument mapDoc = p.Value as XmlDocument;
341
 
                                if ( mapDoc.DocumentElement.GetAttribute( DomainTag ) == domainID )
342
 
                                {
343
 
                                        property = p;
344
 
                                        break;
345
 
                                }
346
 
                        }
347
 
 
348
 
                        return property;
349
 
                }
350
 
 
351
 
                /// <summary>
352
 
                /// Gets the specified Domain property.
353
 
                /// </summary>
354
 
                /// <param name="userID">User ID to use to discover domain property.</param>
355
 
                /// <returns>A Property object containing the found domain property.</returns>
356
 
                private Property GetPropertyByUserID( string userID )
357
 
                {
358
 
                        Property property = null;
359
 
 
360
 
                        MultiValuedList mvl = properties.GetProperties( PropertyTags.Domain );
361
 
                        foreach ( Property p in mvl )
362
 
                        {
363
 
                                XmlDocument mapDoc = p.Value as XmlDocument;
364
 
                                if ( mapDoc.DocumentElement.GetAttribute( UserTag ) == userID )
365
 
                                {
366
 
                                        property = p;
367
 
                                        break;
368
 
                                }
369
 
                        }
370
 
 
371
 
                        return property;
372
 
                }
373
 
                #endregion
374
 
 
375
 
                #region Internal Methods
376
 
                /// <summary>
377
 
                /// Adds a domain identity property to the Identity object.
378
 
                /// </summary>
379
 
                /// <param name="userID">Identity that this user is known as in the specified domain.</param>
380
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
381
 
                /// <returns>The modified identity object.</returns>
382
 
                internal Identity AddDomainIdentity( string userID, string domainID )
383
 
                {
384
 
                        return AddDomainIdentity( userID, domainID, null, CredentialType.None );
385
 
                }
386
 
 
387
 
                /// <summary>
388
 
                /// Adds a domain identity property to the Identity object.
389
 
                /// </summary>
390
 
                /// <param name="userID">Identity that this user is known as in the specified domain.</param>
391
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
392
 
                /// <param name="credentials">Credentials for this domain. This may be null.</param>
393
 
                /// <param name="type">The type of credentials stored.</param>
394
 
                /// <returns>The modified identity object.</returns>
395
 
                internal Identity AddDomainIdentity( string userID, string domainID, string credentials, CredentialType type )
396
 
                {
397
 
                        XmlDocument mapDoc = null;
398
 
                        
399
 
                        // Check to see if the domain already exists.
400
 
                        Property p = GetPropertyByDomain( domainID );
401
 
                        if ( p != null )
402
 
                        {
403
 
                                mapDoc = p.Value as XmlDocument;
404
 
                        }
405
 
                        else
406
 
                        {
407
 
                                mapDoc = new XmlDocument();
408
 
                                XmlElement root = mapDoc.CreateElement( MappingTag );
409
 
                                mapDoc.AppendChild( root );
410
 
                                mapDoc.DocumentElement.SetAttribute( DomainTag, domainID );
411
 
 
412
 
                                p = new Property( PropertyTags.Domain, mapDoc );
413
 
                                properties.AddNodeProperty( p );
414
 
                        }
415
 
 
416
 
                        mapDoc.DocumentElement.SetAttribute( UserTag, userID );
417
 
                        mapDoc.DocumentElement.SetAttribute( TypeTag, type.ToString() );
418
 
 
419
 
                        if ( ( credentials != null ) && ( type != CredentialType.None ) )
420
 
                        {
421
 
                                if ( type == CredentialType.Basic )
422
 
                                {
423
 
                                        mapDoc.DocumentElement.SetAttribute( CredentialTag, EncryptCredential( credentials ) );
424
 
                                }
425
 
                                else
426
 
                                {
427
 
                                        mapDoc.DocumentElement.SetAttribute( CredentialTag, credentials );
428
 
                                }
429
 
                        }
430
 
 
431
 
                        p.SetPropertyValue( mapDoc );
432
 
                        return this;
433
 
                }
434
 
 
435
 
                /// <summary>
436
 
                /// Removes the specified domain mapping from the identity object.
437
 
                /// </summary>
438
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
439
 
                /// <returns>The modified identity object.</returns>
440
 
                internal Identity DeleteDomainIdentity( string domainID )
441
 
                {
442
 
                        // Do not allow the local domain to be deleted.
443
 
                        if ( domainID == StoreReference.LocalDomain )
444
 
                        {
445
 
                                throw new CollectionStoreException( "Cannot remove the local domain." );
446
 
                        }
447
 
 
448
 
                        // Find the property to be deleted.
449
 
                        Property p = GetPropertyByDomain( domainID );
450
 
                        if ( p != null )
451
 
                        {
452
 
                                p.DeleteProperty();
453
 
                        }
454
 
 
455
 
                        return this;
456
 
                }
457
 
 
458
 
                /// <summary>
459
 
                /// Gets the domain associated with the specified user ID.
460
 
                /// </summary>
461
 
                /// <param name="userID">User ID to find the associated domain for.</param>
462
 
                /// <returns>Domain name associated with the specified user ID if it exists. Otherwise null is returned.</returns>
463
 
                internal string GetDomainFromUserID( string userID )
464
 
                {
465
 
                        string domainID = null;
466
 
 
467
 
                        // Find the property associated with the user ID.
468
 
                        XmlDocument document = GetDocumentByUserID( userID );
469
 
                        if ( document != null )
470
 
                        {
471
 
                                domainID = document.DocumentElement.GetAttribute( DomainTag );
472
 
                        }
473
 
 
474
 
                        return ( ( domainID != null ) && ( domainID != String.Empty ) ) ? domainID : null;
475
 
                }
476
 
 
477
 
                /// <summary>
478
 
                /// Gets the user ID associated with the specified domain ID.
479
 
                /// </summary>
480
 
                /// <param name="domainID">Well known identity for the specified domain.</param>
481
 
                /// <returns>User ID associated with the specified domain ID if it exists. Otherwise null is returned.</returns>
482
 
                internal string GetUserIDFromDomain( string domainID )
483
 
                {
484
 
                        string userID = null;
485
 
 
486
 
                        // Find the property associated with the user ID.
487
 
                        XmlDocument document = GetDocumentByDomain( domainID );
488
 
                        if ( document != null )
489
 
                        {
490
 
                                userID = document.DocumentElement.GetAttribute( UserTag );
491
 
                        }
492
 
 
493
 
                        return ( ( userID != null ) && ( userID != String.Empty ) ) ? userID : null;
494
 
                }
495
 
 
496
 
                /// <summary>
497
 
                /// Gets the user identifier and credentials for the specified domain.
498
 
                /// </summary>
499
 
                /// <param name="domainID">The identifier for the domain.</param>
500
 
                /// <param name="userID">Gets the userID of the user associated with the specified domain.</param>
501
 
                /// <param name="credentials">Gets the credentials for the user.</param>
502
 
                /// <returns>CredentialType enumerated object.</returns>
503
 
                internal CredentialType GetDomainCredentials( string domainID, out string userID, out string credentials )
504
 
                {
505
 
                        // Find the property associated with the domain.
506
 
                        XmlDocument document = GetDocumentByDomain( domainID );
507
 
                        if ( document == null )
508
 
                        {
509
 
                                throw new CollectionStoreException( "The specified domain does not exist." );
510
 
                        }
511
 
 
512
 
                        // Return the User ID.
513
 
                        userID = document.DocumentElement.GetAttribute( UserTag );
514
 
 
515
 
                        // Get the credential type.
516
 
                        string credTypeString = document.DocumentElement.GetAttribute( TypeTag );
517
 
                        CredentialType credType = ( CredentialType )Enum.Parse( typeof( CredentialType ), credTypeString, true );
518
 
 
519
 
                        // Return the credentials.
520
 
                        credentials = document.DocumentElement.GetAttribute( CredentialTag );
521
 
                        if ( credentials != String.Empty )
522
 
                        {
523
 
                                if ( credType == CredentialType.Basic )
524
 
                                {
525
 
                                        credentials = DecryptCredential( credentials );
526
 
                                }
527
 
                        }
528
 
                        else
529
 
                        {
530
 
                                credentials = null;
531
 
                        }
532
 
 
533
 
                        return credType;
534
 
                }
535
 
 
536
 
                /// <summary>
537
 
                /// Gets the user identifier and  pass-phrase for the specified domain.
538
 
                /// </summary>
539
 
                /// <param name="domainID">The identifier for the domain.</param>
540
 
                /// <param name="userID">Gets the userID of the user associated with the specified domain.</param>
541
 
                /// <param name="credentials">Gets the credentials for the user.</param>
542
 
                /// <returns>CredentialType enumerated object.</returns>
543
 
                internal bool GetRememberOption( string domainID)
544
 
                {
545
 
                        string remember;
546
 
                        // Find the property associated with the domain.
547
 
                        XmlDocument document = GetDocumentByDomain( domainID );
548
 
                        if ( document == null )
549
 
                        {
550
 
                                throw new CollectionStoreException( "The specified domain does not exist." );
551
 
                        }
552
 
 
553
 
                        // Return the remember 
554
 
                        remember = document.DocumentElement.GetAttribute( RememberPassPhraseTag );
555
 
                        if (remember == "true")
556
 
                                return true;
557
 
                        else
558
 
                                return false;
559
 
                                
560
 
                }
561
 
 
562
 
                /// <summary>
563
 
                /// Gets the user identifier and  pass-phrase for the specified domain.
564
 
                /// </summary>
565
 
                /// <param name="domainID">The identifier for the domain.</param>
566
 
                /// <param name="userID">Gets the userID of the user associated with the specified domain.</param>
567
 
                /// <param name="credentials">Gets the credentials for the user.</param>
568
 
                /// <returns>CredentialType enumerated object.</returns>
569
 
                internal string GetPassPhrase( string domainID)
570
 
                {
571
 
                        // Find the property associated with the domain.
572
 
                        XmlDocument document = GetDocumentByDomain( domainID );
573
 
                        if ( document == null )
574
 
                        {
575
 
                                throw new CollectionStoreException( "The specified domain does not exist." );
576
 
                        }
577
 
 
578
 
                        // Get the credential type.
579
 
                        string credTypeString = document.DocumentElement.GetAttribute( PassPhraseTypeTag );
580
 
                        if( credTypeString == null || credTypeString == String.Empty)
581
 
                        {
582
 
                                return null;
583
 
                        }
584
 
                        CredentialType credType = ( CredentialType )Enum.Parse( typeof( CredentialType ), credTypeString, true );
585
 
 
586
 
                        // Return the credentials.
587
 
                        string passPhrase = document.DocumentElement.GetAttribute( PassPhraseTag );
588
 
                        if ( passPhrase != null && passPhrase != String.Empty )
589
 
                        {
590
 
                                if ( credType == CredentialType.Basic )
591
 
                                {
592
 
                                        passPhrase = DecryptCredential( passPhrase );
593
 
                                }
594
 
                        }
595
 
                        else
596
 
                        {
597
 
                                passPhrase = null;
598
 
                        }
599
 
                        return passPhrase;
600
 
                }
601
 
 
602
 
                /// <summary>
603
 
                /// Sets the credentials for the specified domain.
604
 
                /// </summary>
605
 
                /// <param name="domainID">The domain to set the password for.</param>
606
 
                /// <param name="credentials">The domain credentials.</param>
607
 
                /// <param name="type">Type of credentials.</param>
608
 
                /// <returns>The modified identity object.</returns>
609
 
                internal Identity SetDomainCredentials( string domainID, string credentials, CredentialType type )
610
 
                {
611
 
                        Property p = GetPropertyByDomain( domainID );
612
 
                        if ( p == null )
613
 
                        {
614
 
                                throw new CollectionStoreException( "There is no mapping for this domain." );
615
 
                        }
616
 
 
617
 
                        // Set the password on the mapping.
618
 
                        XmlDocument mapDoc = p.Value as XmlDocument;
619
 
                        if ( type == CredentialType.None )
620
 
                        {
621
 
                                if ( domainID == StoreReference.LocalDomain )
622
 
                                {
623
 
                                        throw new CollectionStoreException( "Cannot remove the local domain credentials." );
624
 
                                }
625
 
 
626
 
                                mapDoc.DocumentElement.RemoveAttribute( CredentialTag );
627
 
                        }
628
 
                        else
629
 
                        {
630
 
                                if ( type == CredentialType.Basic )
631
 
                                {
632
 
                                        mapDoc.DocumentElement.SetAttribute( CredentialTag, EncryptCredential( credentials ) );
633
 
                                }
634
 
                                else
635
 
                                {
636
 
                                        mapDoc.DocumentElement.SetAttribute( CredentialTag, credentials );
637
 
                                }
638
 
                        }
639
 
 
640
 
                        mapDoc.DocumentElement.SetAttribute( TypeTag, type.ToString() );
641
 
                        p.SetPropertyValue( mapDoc );
642
 
                        return this;
643
 
                }
644
 
 
645
 
                /// <summary>
646
 
                /// Stores the passphrase for the specified domain.
647
 
                /// </summary>
648
 
                /// <param name="domainID">The domain to store the passphrase for.</param>
649
 
                /// <param name="passPhrase">The domain passphrase.</param>
650
 
                /// <param name="type">Type of credentials.</param>
651
 
                /// <returns>The modified identity object.</returns>
652
 
                internal Identity StorePassPhrase( string domainID, string passPhrase, CredentialType type, bool rememberPassPhrase)
653
 
                {
654
 
                        Property p = GetPropertyByDomain( domainID );
655
 
                        if ( p == null )
656
 
                        {
657
 
                                throw new CollectionStoreException( "There is no mapping for this domain." );
658
 
                        }
659
 
 
660
 
                        // Set the password on the mapping.
661
 
                        XmlDocument mapDoc = p.Value as XmlDocument;
662
 
                        if ( type == CredentialType.None )
663
 
                        {
664
 
                                mapDoc.DocumentElement.RemoveAttribute( PassPhraseTag );
665
 
                                mapDoc.DocumentElement.RemoveAttribute(RememberPassPhraseTag);
666
 
                        }
667
 
                        else
668
 
                        {
669
 
                                if ( type == CredentialType.Basic )
670
 
                                {
671
 
                                        if( passPhrase != null && passPhrase != "")
672
 
                                                mapDoc.DocumentElement.SetAttribute( PassPhraseTag, EncryptCredential( passPhrase ) );
673
 
                                }
674
 
                                else
675
 
                                {
676
 
                                        mapDoc.DocumentElement.SetAttribute( PassPhraseTag, passPhrase );
677
 
                                }
678
 
                                if(rememberPassPhrase)
679
 
                                        mapDoc.DocumentElement.SetAttribute( RememberPassPhraseTag, "true");
680
 
                                else
681
 
                                        mapDoc.DocumentElement.SetAttribute( RememberPassPhraseTag, "false");
682
 
                        }
683
 
 
684
 
                        mapDoc.DocumentElement.SetAttribute( PassPhraseTypeTag, type.ToString() );
685
 
                        p.SetPropertyValue( mapDoc );
686
 
                        return this;
687
 
                }
688
 
 
689
 
                public RSACryptoServiceProvider GetDomainCredential(string domainID)
690
 
                {
691
 
                        RSACryptoServiceProvider credential = null;
692
 
 
693
 
                        // Lookup the credential property on the identity.
694
 
                        XmlDocument mapDoc = GetDocumentByDomain( domainID );
695
 
                        if ( mapDoc != null )
696
 
                        {
697
 
                                credential = DummyCsp;
698
 
                                credential.FromXmlString( mapDoc.DocumentElement.GetAttribute( CredentialTag ) );
699
 
                        }
700
 
                        return credential;
701
 
                }
702
 
 
703
 
                #endregion
704
 
        }
705
 
}