summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Extensions/AttributeExchange/StoreRequest.cs
blob: 6b5ce2b9a1aedb2be543c270d91604e04c29c404 (plain)
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
using System;
using System.Collections.Generic;
using System.Text;
using DotNetOpenId.RelyingParty;
using System.Globalization;

namespace DotNetOpenId.Extensions.AttributeExchange {
	/// <summary>
	/// The Attribute Exchange Store message, request leg.
	/// </summary>
	public sealed class StoreRequest : IExtensionRequest {
		readonly string Mode = "store_request";

		List<AttributeValues> attributesProvided = new List<AttributeValues>();
		/// <summary>
		/// Lists all the attributes that are included in the store request.
		/// </summary>
		public IEnumerable<AttributeValues> Attributes {
			get { return attributesProvided; }
		}
		
		/// <summary>
		/// Used by the Relying Party to add a given attribute with one or more values 
		/// to the request for storage.
		/// </summary>
		public void AddAttribute(AttributeValues attribute) {
			if (attribute == null) throw new ArgumentNullException("attribute");
			if (containsAttribute(attribute.TypeUri)) throw new ArgumentException(
				  string.Format(CultureInfo.CurrentCulture, Strings.AttributeAlreadyAdded, attribute.TypeUri));
			attributesProvided.Add(attribute);
		}
		/// <summary>
		/// Used by the Relying Party to add a given attribute with one or more values 
		/// to the request for storage.
		/// </summary>
		public void AddAttribute(string typeUri, params string[] values) {
			AddAttribute(new AttributeValues(typeUri, values));
		}
		/// <summary>
		/// Used by the Provider to gets the value(s) associated with a given attribute
		/// that should be stored.
		/// </summary>
		public AttributeValues GetAttribute(string attributeTypeUri) {
			foreach (var att in attributesProvided) {
				if (att.TypeUri == attributeTypeUri)
					return att;
			}
			return null;
		}
		bool containsAttribute(string typeUri) {
			return GetAttribute(typeUri) != null;
		}

		#region IExtensionRequest Members
		string IExtension.TypeUri { get { return Constants.TypeUri; } }
		IEnumerable<string> IExtension.AdditionalSupportedTypeUris {
			get { return new string[0]; }
		}

		IDictionary<string, string> IExtensionRequest.Serialize(RelyingParty.IAuthenticationRequest authenticationRequest) {
			var fields = new Dictionary<string, string> {
				{ "mode", Mode },
			};

			FetchResponse.SerializeAttributes(fields, attributesProvided);

			return fields;
		}

		bool IExtensionRequest.Deserialize(IDictionary<string, string> fields, DotNetOpenId.Provider.IRequest request, string typeUri) {
			if (fields == null) return false;
			string mode;
			fields.TryGetValue("mode", out mode);
			if (mode != Mode) return false;

			foreach (var att in FetchResponse.DeserializeAttributes(fields))
				AddAttribute(att);

			return true;
		}

		#endregion
	}
}