//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
///
/// An enumeration of the OAuth protocol versions supported by this library.
///
public enum ProtocolVersion {
///
/// OAuth 1.0 specification
///
V10,
///
/// OAuth 1.0a specification
///
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "a", Justification = "By design.")]
V10a,
}
///
/// Constants used in the OAuth protocol.
///
///
/// OAuth Protocol Parameter names and values are case sensitive. Each OAuth Protocol Parameters MUST NOT appear more than once per request, and are REQUIRED unless otherwise noted,
/// per OAuth 1.0 section 5.
///
[DebuggerDisplay("OAuth {Version}")]
internal class Protocol {
///
/// The namespace to use for V1.0 of the protocol.
///
internal const string DataContractNamespaceV10 = "http://oauth.net/core/1.0/";
///
/// The prefix used for all key names in the protocol.
///
internal const string ParameterPrefix = "oauth_";
///
/// The string representation of a instance to be used to represent OAuth 1.0a.
///
internal const string V10aVersion = "1.0.1";
///
/// The scheme to use in Authorization header message requests.
///
internal const string AuthorizationHeaderScheme = "OAuth";
///
/// Gets the instance with values initialized for V1.0 of the protocol.
///
internal static readonly Protocol V10 = new Protocol {
dataContractNamespace = DataContractNamespaceV10,
Version = new Version(1, 0),
ProtocolVersion = ProtocolVersion.V10,
};
///
/// Gets the instance with values initialized for V1.0a of the protocol.
///
internal static readonly Protocol V10a = new Protocol {
dataContractNamespace = DataContractNamespaceV10,
Version = new Version(V10aVersion),
ProtocolVersion = ProtocolVersion.V10a,
};
///
/// A list of all supported OAuth versions, in order starting from newest version.
///
internal static readonly List AllVersions = new List() { V10a, V10 };
///
/// The default (or most recent) supported version of the OAuth protocol.
///
internal static readonly Protocol Default = AllVersions[0];
///
/// The namespace to use for this version of the protocol.
///
private string dataContractNamespace;
///
/// Initializes a new instance of the class.
///
internal Protocol() {
this.PublishedVersion = "1.0";
}
///
/// Gets the OAuth version this instance represents.
///
internal Version Version { get; private set; }
///
/// Gets the version to declare on the wire.
///
internal string PublishedVersion { get; private set; }
///
/// Gets the enum value for the instance.
///
internal ProtocolVersion ProtocolVersion { get; private set; }
///
/// Gets the namespace to use for this version of the protocol.
///
internal string DataContractNamespace {
get { return this.dataContractNamespace; }
}
///
/// Gets the OAuth Protocol instance to use for the given version.
///
/// The OAuth version to get.
/// A matching instance.
public static Protocol Lookup(ProtocolVersion version) {
switch (version) {
case ProtocolVersion.V10: return Protocol.V10;
case ProtocolVersion.V10a: return Protocol.V10a;
default: throw new ArgumentOutOfRangeException("version");
}
}
///
/// Gets the OAuth Protocol instance to use for the given version.
///
/// The OAuth version to get.
/// A matching instance.
internal static Protocol Lookup(Version version) {
Requires.NotNull(version, "version");
Requires.InRange(AllVersions.Any(p => p.Version == version), "version");
return AllVersions.First(p => p.Version == version);
}
}
}