//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenId.Extensions.AttributeExchange {
using System;
using System.Diagnostics;
using DotNetOpenAuth.Messaging;
using Validation;
///
/// An individual attribute to be requested of the OpenID Provider using
/// the Attribute Exchange extension.
///
[Serializable]
[DebuggerDisplay("{TypeUri} (required: {IsRequired}) ({Count})")]
public class AttributeRequest {
///
/// Backing field for the property.
///
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int count = 1;
///
/// Initializes a new instance of the class
/// with = false, = 1.
///
public AttributeRequest() {
}
///
/// Initializes a new instance of the class
/// with = false, = 1.
///
/// The unique TypeURI for that describes the attribute being sought.
public AttributeRequest(string typeUri) {
Requires.NotNullOrEmpty(typeUri, "typeUri");
this.TypeUri = typeUri;
}
///
/// Initializes a new instance of the class
/// with = 1.
///
/// The unique TypeURI for that describes the attribute being sought.
/// A value indicating whether the Relying Party considers this attribute to be required for registration.
public AttributeRequest(string typeUri, bool isRequired)
: this(typeUri) {
this.IsRequired = isRequired;
}
///
/// Initializes a new instance of the class.
///
/// The unique TypeURI for that describes the attribute being sought.
/// A value indicating whether the Relying Party considers this attribute to be required for registration.
/// The maximum number of values for this attribute the Relying Party is prepared to receive.
public AttributeRequest(string typeUri, bool isRequired, int count)
: this(typeUri, isRequired) {
this.Count = count;
}
///
/// Gets or sets the URI uniquely identifying the attribute being requested.
///
public string TypeUri { get; set; }
///
/// Gets or sets a value indicating whether the relying party considers this a required field.
/// Note that even if set to true, the Provider may not provide the value.
///
public bool IsRequired { get; set; }
///
/// Gets or sets the maximum number of values for this attribute the
/// Relying Party wishes to receive from the OpenID Provider.
/// A value of int.MaxValue is considered infinity.
///
public int Count {
get {
return this.count;
}
set {
Requires.Range(value > 0, "value");
this.count = value;
}
}
///
/// Used by a Provider to create a response to a request for an attribute's value(s)
/// using a given array of strings.
///
/// The values for the requested attribute.
///
/// The newly created object that should be added to
/// the object.
///
public AttributeValues Respond(params string[] values) {
Requires.NotNull(values, "values");
Requires.That(values.Length <= this.Count, "values", "requires values.Length <= this.Count");
return new AttributeValues(this.TypeUri, values);
}
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
///
/// true if the specified is equal to the current ; otherwise, false.
///
///
/// The parameter is null.
///
public override bool Equals(object obj) {
AttributeRequest other = obj as AttributeRequest;
if (other == null) {
return false;
}
if (this.TypeUri != other.TypeUri) {
return false;
}
if (this.Count != other.Count) {
return false;
}
if (this.IsRequired != other.IsRequired) {
return false;
}
return true;
}
///
/// Serves as a hash function for a particular type.
///
///
/// A hash code for the current .
///
public override int GetHashCode() {
int hashCode = this.IsRequired ? 1 : 0;
unchecked {
hashCode += this.Count;
if (this.TypeUri != null) {
hashCode += this.TypeUri.GetHashCode();
}
}
return hashCode;
}
}
}