diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-04-18 19:55:50 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-04-18 19:55:50 -0700 |
commit | 2ddd19d9f037bebbbdc80d7de35ce4d899710859 (patch) | |
tree | baf8ad19d4799b1a1284b9fc668afd53e3b008a4 /src/DotNetOpenAuth.OAuth2/OAuth2 | |
parent | bd0de8217763d02759815b91588cd578becf496b (diff) | |
download | DotNetOpenAuth-2ddd19d9f037bebbbdc80d7de35ce4d899710859.zip DotNetOpenAuth-2ddd19d9f037bebbbdc80d7de35ce4d899710859.tar.gz DotNetOpenAuth-2ddd19d9f037bebbbdc80d7de35ce4d899710859.tar.bz2 |
We have HTTP Basic client authentication working now in OAuth 2.
Diffstat (limited to 'src/DotNetOpenAuth.OAuth2/OAuth2')
-rw-r--r-- | src/DotNetOpenAuth.OAuth2/OAuth2/ChannelElements/IClientAuthenticationModule.cs | 22 | ||||
-rw-r--r-- | src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs | 41 |
2 files changed, 60 insertions, 3 deletions
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; + } } } |