diff options
Diffstat (limited to 'src/DotNetOpenAuth.OAuth2')
7 files changed, 238 insertions, 3 deletions
diff --git a/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2AuthorizationServerElement.cs b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2AuthorizationServerElement.cs new file mode 100644 index 0000000..fa7b52e --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2AuthorizationServerElement.cs @@ -0,0 +1,55 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuth2AuthorizationServerElement.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Configuration { + using System; + using System.Configuration; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OAuth2.ChannelElements; + + /// <summary> + /// Represents the <oauth2/authorizationServer> element in the host's .config file. + /// </summary> + internal class OAuth2AuthorizationServerElement : ConfigurationElement { + + /// <summary> + /// The name of the <clientAuthenticationModules> sub-element. + /// </summary> + private const string ClientAuthenticationModulesElementName = "clientAuthenticationModules"; + + /// <summary> + /// The built-in set of identifier discovery services. + /// </summary> + private static readonly TypeConfigurationCollection<IClientAuthenticationModule> defaultClientAuthenticationModules = + new TypeConfigurationCollection<IClientAuthenticationModule>(); + + /// <summary> + /// Initializes a new instance of the <see cref="OAuth2AuthorizationServerElement"/> class. + /// </summary> + internal OAuth2AuthorizationServerElement() { + } + + /// <summary> + /// Gets or sets the services to use for discovering service endpoints for identifiers. + /// </summary> + /// <remarks> + /// If no discovery services are defined in the (web) application's .config file, + /// the default set of discovery services built into the library are used. + /// </remarks> + [ConfigurationProperty(ClientAuthenticationModulesElementName, IsDefaultCollection = false)] + [ConfigurationCollection(typeof(TypeConfigurationCollection<IClientAuthenticationModule>))] + internal TypeConfigurationCollection<IClientAuthenticationModule> ClientAuthenticationModules { + get { + var configResult = (TypeConfigurationCollection<IClientAuthenticationModule>)this[ClientAuthenticationModulesElementName]; + return configResult != null && configResult.Count > 0 ? configResult : defaultClientAuthenticationModules; + } + + set { + this[ClientAuthenticationModulesElementName] = value; + } + } + } +} diff --git a/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ClientElement.cs b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ClientElement.cs new file mode 100644 index 0000000..95a7a36 --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ClientElement.cs @@ -0,0 +1,20 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuth2ClientElement.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Configuration { + using System.Configuration; + + /// <summary> + /// Represents the <oauth2/client> element in the host's .config file. + /// </summary> + internal class OAuth2ClientElement : ConfigurationElement { + /// <summary> + /// Initializes a new instance of the <see cref="OAuth2ClientElement"/> class. + /// </summary> + internal OAuth2ClientElement() { + } + } +} diff --git a/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2Element.cs b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2Element.cs new file mode 100644 index 0000000..6ba7e23 --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2Element.cs @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuth2Element.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Configuration { + using System.Configuration; + using System.Diagnostics.Contracts; + + /// <summary> + /// Represents the <oauth> element in the host's .config file. + /// </summary> + internal class OAuth2Element : ConfigurationSection { + /// <summary> + /// The name of the oauth section. + /// </summary> + private const string SectionName = DotNetOpenAuthSection.SectionName + "/oauth2"; + + /// <summary> + /// The name of the <client> sub-element. + /// </summary> + private const string ClientElementName = "client"; + + /// <summary> + /// The name of the <authorizationServer> sub-element. + /// </summary> + private const string AuthorizationServerElementName = "authorizationServer"; + + /// <summary> + /// The name of the <resourceServer> sub-element. + /// </summary> + private const string ResourceServerElementName = "resourceServer"; + + /// <summary> + /// Initializes a new instance of the <see cref="OAuth2Element"/> class. + /// </summary> + internal OAuth2Element() { + } + + /// <summary> + /// Gets the configuration section from the .config file. + /// </summary> + public static OAuth2Element Configuration { + get { + Contract.Ensures(Contract.Result<OAuth2Element>() != null); + return (OAuth2Element)ConfigurationManager.GetSection(SectionName) ?? new OAuth2Element(); + } + } + + /// <summary> + /// Gets or sets the configuration specific for Clients. + /// </summary> + [ConfigurationProperty(ClientElementName)] + internal OAuth2ClientElement Client { + get { return (OAuth2ClientElement)this[ClientElementName] ?? new OAuth2ClientElement(); } + set { this[ClientElementName] = value; } + } + + /// <summary> + /// Gets or sets the configuration specific for Authorization Servers. + /// </summary> + [ConfigurationProperty(AuthorizationServerElementName)] + internal OAuth2AuthorizationServerElement AuthorizationServer { + get { return (OAuth2AuthorizationServerElement)this[AuthorizationServerElementName] ?? new OAuth2AuthorizationServerElement(); } + set { this[AuthorizationServerElementName] = value; } + } + + /// <summary> + /// Gets or sets the configuration specific for Resource Servers. + /// </summary> + [ConfigurationProperty(ResourceServerElementName)] + internal OAuth2ResourceServerElement ResourceServer { + get { return (OAuth2ResourceServerElement)this[ResourceServerElementName] ?? new OAuth2ResourceServerElement(); } + set { this[ResourceServerElementName] = value; } + } + } +} diff --git a/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ResourceServerElement.cs b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ResourceServerElement.cs new file mode 100644 index 0000000..a07e973 --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2/Configuration/OAuth2ResourceServerElement.cs @@ -0,0 +1,20 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuth2ResourceServerElement.cs" company="Outercurve Foundation"> +// Copyright (c) Outercurve Foundation. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Configuration { + using System.Configuration; + + /// <summary> + /// Represents the <oauth2/resourceServer> element in the host's .config file. + /// </summary> + internal class OAuth2ResourceServerElement : ConfigurationElement { + /// <summary> + /// Initializes a new instance of the <see cref="OAuth2ResourceServerElement"/> class. + /// </summary> + internal OAuth2ResourceServerElement() { + } + } +} diff --git a/src/DotNetOpenAuth.OAuth2/DotNetOpenAuth.OAuth2.csproj b/src/DotNetOpenAuth.OAuth2/DotNetOpenAuth.OAuth2.csproj index 921cd84..a3eda22 100644 --- a/src/DotNetOpenAuth.OAuth2/DotNetOpenAuth.OAuth2.csproj +++ b/src/DotNetOpenAuth.OAuth2/DotNetOpenAuth.OAuth2.csproj @@ -18,10 +18,15 @@ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> </PropertyGroup> <ItemGroup> + <Compile Include="Configuration\OAuth2ResourceServerElement.cs" /> + <Compile Include="Configuration\OAuth2Element.cs" /> + <Compile Include="Configuration\OAuth2ClientElement.cs" /> + <Compile Include="Configuration\OAuth2AuthorizationServerElement.cs" /> <Compile Include="GlobalSuppressions.cs" /> <Compile Include="OAuth2\AccessToken.cs" /> <Compile Include="OAuth2\ChannelElements\AuthorizationDataBag.cs" /> <Compile Include="OAuth2\ChannelElements\IAccessTokenCarryingRequest.cs" /> + <Compile Include="OAuth2\ChannelElements\IClientAuthenticationModule.cs" /> <Compile Include="OAuth2\ChannelElements\ScopeEncoder.cs" /> <Compile Include="OAuth2\ChannelElements\IAuthorizationDescription.cs" /> <Compile Include="OAuth2\ChannelElements\IAuthorizationCarryingRequest.cs" /> diff --git a/src/DotNetOpenAuth.OAuth2/OAuth2/ChannelElements/IClientAuthenticationModule.cs b/src/DotNetOpenAuth.OAuth2/OAuth2/ChannelElements/IClientAuthenticationModule.cs new file mode 100644 index 0000000..b7c4792 --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2/OAuth2/ChannelElements/IClientAuthenticationModule.cs @@ -0,0 +1,22 @@ +namespace DotNetOpenAuth.OAuth2.ChannelElements { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Web; + using DotNetOpenAuth.Messaging; + + public enum ClientAuthenticationResult { + NoAuthenticationRecognized, + + ClientIdNotAuthenticated, + + ClientAuthenticated, + + ClientAuthenticationRejected, + } + + public interface IClientAuthenticationModule { + ClientAuthenticationResult TryAuthenticateClient(IDirectedProtocolMessage requestMessage, out string clientIdentifier); + } +} diff --git a/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs b/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs index eb5c8e4..2e83482 100644 --- a/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs +++ b/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs @@ -18,6 +18,8 @@ namespace DotNetOpenAuth.OAuth2 { /// Some common utility methods for OAuth 2.0. /// </summary> public static class OAuthUtilities { + private const string HttpBasicAuthScheme = "Basic "; + /// <summary> /// The <see cref="StringComparer"/> instance to use when comparing scope equivalence. /// </summary> @@ -28,6 +30,8 @@ namespace DotNetOpenAuth.OAuth2 { /// </summary> private static char[] scopeDelimiter = new char[] { ' ' }; + private static readonly char[] ColonSeparator = new char[] { ':' }; + /// <summary> /// The characters that may appear in an access token that is included in an HTTP Authorization header. /// </summary> @@ -35,9 +39,9 @@ namespace DotNetOpenAuth.OAuth2 { /// This is defined in OAuth 2.0 DRAFT 10, section 5.1.1. (http://tools.ietf.org/id/draft-ietf-oauth-v2-10.html#authz-header) /// </remarks> private static string accessTokenAuthorizationHeaderAllowedCharacters = MessagingUtilities.UppercaseLetters + - MessagingUtilities.LowercaseLetters + - MessagingUtilities.Digits + - @"!#$%&'()*+-./:<=>?@[]^_`{|}~\,;"; + MessagingUtilities.LowercaseLetters + + MessagingUtilities.Digits + + @"!#$%&'()*+-./:<=>?@[]^_`{|}~\,;"; /// <summary> /// Determines whether one given scope is a subset of another scope. @@ -129,5 +133,36 @@ namespace DotNetOpenAuth.OAuth2 { Protocol.BearerHttpAuthorizationHeaderFormat, accessToken); } + + private static readonly Encoding HttpBasicEncoding = Encoding.UTF8; + + internal static void ApplyHttpBasicAuth(WebHeaderCollection headers, string userName, string password) { + Requires.NotNull(headers, "headers"); + Requires.NotNullOrEmpty(userName, "userName"); + Requires.NotNull(password, "password"); + + string concat = userName + ":" + password; + byte[] bits = HttpBasicEncoding.GetBytes(concat); + string base64 = Convert.ToBase64String(bits); + string header = HttpBasicAuthScheme + base64; + headers[HttpRequestHeader.Authorization] = header; + } + + internal static NetworkCredential ParseHttpBasicAuth(WebHeaderCollection headers) { + Requires.NotNull(headers, "headers"); + + string authorizationHeader = headers[HttpRequestHeaders.Authorization]; + if (authorizationHeader != null && authorizationHeader.StartsWith(HttpBasicAuthScheme, StringComparison.Ordinal)) { + string base64 = authorizationHeader.Substring(HttpBasicAuthScheme.Length); + byte[] bits = Convert.FromBase64String(base64); + string usernameColonPassword = HttpBasicEncoding.GetString(bits); + string[] usernameAndPassword = usernameColonPassword.Split(ColonSeparator, 2); + if (usernameAndPassword.Length == 2) { + return new NetworkCredential(usernameAndPassword[0], usernameAndPassword[1]); + } + } + + return null; + } } } |