using System; using System.Collections.Generic; using System.Text; using DotNetOpenId.RelyingParty; using System.Globalization; namespace DotNetOpenId.Extensions.AttributeExchange { /// /// The Attribute Exchange Store message, request leg. /// public sealed class StoreRequest : IExtensionRequest { readonly string Mode = "store_request"; List attributesProvided = new List(); /// /// Lists all the attributes that are included in the store request. /// public IEnumerable Attributes { get { return attributesProvided; } } /// /// Used by the Relying Party to add a given attribute with one or more values /// to the request for storage. /// 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); } /// /// Used by the Relying Party to add a given attribute with one or more values /// to the request for storage. /// public void AddAttribute(string typeUri, params string[] values) { AddAttribute(new AttributeValues(typeUri, values)); } /// /// Used by the Provider to gets the value(s) associated with a given attribute /// that should be stored. /// 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 IExtension.AdditionalSupportedTypeUris { get { return new string[0]; } } IDictionary IExtensionRequest.Serialize(RelyingParty.IAuthenticationRequest authenticationRequest) { var fields = new Dictionary { { "mode", Mode }, }; FetchResponse.SerializeAttributes(fields, attributesProvided); return fields; } bool IExtensionRequest.Deserialize(IDictionary 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 } }