summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Web/Clients/OpenID
diff options
context:
space:
mode:
authorMicrosoft <aspnet@microsoft.com>2011-12-08 15:50:14 -0800
committerAndrew Arnott <andrewarnott@gmail.com>2012-03-01 19:35:54 -0800
commit67e1a42ffe2ed7ac2bf99c703f17e4406cc35921 (patch)
treeb117701274fea4bb5cfb1342c7ba20605fbaf13d /src/DotNetOpenAuth.Web/Clients/OpenID
parent8f4165ee515728aca3faaa26e8354a40612e85e4 (diff)
downloadDotNetOpenAuth-67e1a42ffe2ed7ac2bf99c703f17e4406cc35921.zip
DotNetOpenAuth-67e1a42ffe2ed7ac2bf99c703f17e4406cc35921.tar.gz
DotNetOpenAuth-67e1a42ffe2ed7ac2bf99c703f17e4406cc35921.tar.bz2
Add DotNetOpenAuth.Web and DotNetOpenAut.WebPages projects. Add commands to build nuget packages for DNOA.
Diffstat (limited to 'src/DotNetOpenAuth.Web/Clients/OpenID')
-rw-r--r--src/DotNetOpenAuth.Web/Clients/OpenID/AxKnownAttributes.cs12
-rw-r--r--src/DotNetOpenAuth.Web/Clients/OpenID/GoogleOpenIdClient.cs55
-rw-r--r--src/DotNetOpenAuth.Web/Clients/OpenID/OpenIDClient.cs141
-rw-r--r--src/DotNetOpenAuth.Web/Clients/OpenID/YahooOpenIdClient.cs48
4 files changed, 256 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Web/Clients/OpenID/AxKnownAttributes.cs b/src/DotNetOpenAuth.Web/Clients/OpenID/AxKnownAttributes.cs
new file mode 100644
index 0000000..1afcc65
--- /dev/null
+++ b/src/DotNetOpenAuth.Web/Clients/OpenID/AxKnownAttributes.cs
@@ -0,0 +1,12 @@
+namespace DotNetOpenAuth.Web.Clients
+{
+ /// <summary>
+ /// Contains namespace values of common attributes used for Attribute Exchange extensions
+ /// </summary>
+ internal static class AxKnownAttributes
+ {
+ public const string FirstName = "http://axschema.org/namePerson/first";
+ public const string LastName = "http://axschema.org/namePerson/last";
+ public const string FullName = "http://axschema.org/namePerson";
+ }
+}
diff --git a/src/DotNetOpenAuth.Web/Clients/OpenID/GoogleOpenIdClient.cs b/src/DotNetOpenAuth.Web/Clients/OpenID/GoogleOpenIdClient.cs
new file mode 100644
index 0000000..61b88ee
--- /dev/null
+++ b/src/DotNetOpenAuth.Web/Clients/OpenID/GoogleOpenIdClient.cs
@@ -0,0 +1,55 @@
+using System.Collections.Generic;
+using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
+using DotNetOpenAuth.OpenId.RelyingParty;
+
+namespace DotNetOpenAuth.Web.Clients
+{
+ /// <summary>
+ /// Represents Google OpenID client.
+ /// </summary>
+ internal sealed class GoogleOpenIdClient : OpenIDClient
+ {
+ public GoogleOpenIdClient() :
+ base("google", "https://www.google.com/accounts/o8/id")
+ {
+ }
+
+ /// <summary>
+ /// Called just before the authentication request is sent to service provider.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
+ {
+ // Attribute Exchange extensions
+ var fetchRequest = new FetchRequest();
+ fetchRequest.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, isRequired: true));
+ fetchRequest.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.HomeAddress.Country, isRequired: false));
+ fetchRequest.Attributes.Add(new AttributeRequest(AxKnownAttributes.FirstName, isRequired: false));
+ fetchRequest.Attributes.Add(new AttributeRequest(AxKnownAttributes.LastName, isRequired: false));
+
+ request.AddExtension(fetchRequest);
+ }
+
+ /// <summary>
+ /// Gets the extra data obtained from the response message when authentication is successful.
+ /// </summary>
+ /// <param name="response">The response message.</param>
+ /// <returns></returns>
+ protected override Dictionary<string, string> GetExtraData(IAuthenticationResponse response)
+ {
+ FetchResponse fetchResponse = response.GetExtension<FetchResponse>();
+ if (fetchResponse != null)
+ {
+ var extraData = new Dictionary<string, string>();
+ extraData.AddItemIfNotEmpty("email", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email));
+ extraData.AddItemIfNotEmpty("country", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.HomeAddress.Country));
+ extraData.AddItemIfNotEmpty("firstName", fetchResponse.GetAttributeValue(AxKnownAttributes.FirstName));
+ extraData.AddItemIfNotEmpty("lastName", fetchResponse.GetAttributeValue(AxKnownAttributes.LastName));
+
+ return extraData;
+ }
+
+ return null;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/DotNetOpenAuth.Web/Clients/OpenID/OpenIDClient.cs b/src/DotNetOpenAuth.Web/Clients/OpenID/OpenIDClient.cs
new file mode 100644
index 0000000..f0f938e
--- /dev/null
+++ b/src/DotNetOpenAuth.Web/Clients/OpenID/OpenIDClient.cs
@@ -0,0 +1,141 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Web;
+using DotNetOpenAuth.OpenId;
+using DotNetOpenAuth.OpenId.RelyingParty;
+using DotNetOpenAuth.Web.Resources;
+
+namespace DotNetOpenAuth.Web.Clients
+{
+ /// <summary>
+ /// Base classes for OpenID clients.
+ /// </summary>
+ internal class OpenIDClient : IAuthenticationClient
+ {
+ private readonly Identifier _providerIdentifier;
+ private readonly string _providerName;
+
+ private static OpenIdRelyingParty _openidRelayingParty =
+ new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore());
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="OpenIDClient"/> class.
+ /// </summary>
+ /// <param name="providerName">Name of the provider.</param>
+ /// <param name="providerIdentifier">The provider identifier, which is the usually the login url of the specified provider.</param>
+ public OpenIDClient(string providerName, string providerIdentifier)
+ {
+ if (String.IsNullOrEmpty(providerIdentifier))
+ {
+ throw new ArgumentException(
+ String.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "providerIdentifier"),
+ "providerIdentifier");
+ }
+
+ if (String.IsNullOrEmpty(providerName))
+ {
+ throw new ArgumentException(
+ String.Format(CultureInfo.CurrentCulture, WebResources.Argument_Cannot_Be_Null_Or_Empty, "providerName"),
+ "providerName");
+ }
+
+ _providerName = providerName;
+ if (!Identifier.TryParse(providerIdentifier, out _providerIdentifier) || _providerIdentifier == null)
+ {
+ throw new ArgumentException(WebResources.OpenIDInvalidIdentifier, "providerIdentifier");
+ }
+ }
+
+ /// <summary>
+ /// Gets the name of the provider which provides authentication service.
+ /// </summary>
+ public string ProviderName
+ {
+ get
+ {
+ return _providerName;
+ }
+ }
+
+ /// <summary>
+ /// Attempts to authenticate users by forwarding them to an external website, and
+ /// upon succcess or failure, redirect users back to the specified url.
+ /// </summary>
+ /// <param name="returnUrl">The return url after users have completed authenticating against external website.</param>
+ [System.Diagnostics.CodeAnalysis.SuppressMessage(
+ "Microsoft.Usage",
+ "CA2234:PassSystemUriObjectsInsteadOfStrings",
+ Justification = "We don't have a Uri object handy.")]
+ public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl)
+ {
+ if (returnUrl == null)
+ {
+ throw new ArgumentNullException("returnUrl");
+ }
+
+ var realm = new Realm(returnUrl.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));
+ IAuthenticationRequest request = _openidRelayingParty.CreateRequest(_providerIdentifier, realm, returnUrl);
+
+ // give subclasses a chance to modify request message, e.g. add extension attributes, etc.
+ OnBeforeSendingAuthenticationRequest(request);
+
+ request.RedirectToProvider();
+ }
+
+ /// <summary>
+ /// Called just before the authentication request is sent to service provider.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ protected virtual void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
+ {
+ }
+
+ /// <summary>
+ /// Check if authentication succeeded after user is redirected back from the service provider.
+ /// </summary>
+ /// <returns>
+ /// An instance of <see cref="AuthenticationResult"/> containing authentication result.
+ /// </returns>
+ public virtual AuthenticationResult VerifyAuthentication(HttpContextBase context)
+ {
+ IAuthenticationResponse response = _openidRelayingParty.GetResponse();
+ if (response == null)
+ {
+ throw new InvalidOperationException(WebResources.OpenIDFailedToGetResponse);
+ }
+
+ if (response.Status == AuthenticationStatus.Authenticated)
+ {
+ string id = response.ClaimedIdentifier;
+ string username;
+
+ Dictionary<string, string> extraData = GetExtraData(response) ?? new Dictionary<string, string>();
+ // try to look up username from the 'username' or 'email' property. If not found, fall back to 'friendly id'
+ if (!extraData.TryGetValue("username", out username) && !extraData.TryGetValue("email", out username))
+ {
+ username = response.FriendlyIdentifierForDisplay;
+ }
+
+ return new AuthenticationResult(
+ true,
+ ProviderName,
+ id,
+ username,
+ extraData);
+ }
+
+ return AuthenticationResult.Failed;
+ }
+
+ /// <summary>
+ /// Gets the extra data obtained from the response message when authentication is successful.
+ /// </summary>
+ /// <param name="response">The response message.</param>
+ /// <returns></returns>
+ protected virtual Dictionary<string, string> GetExtraData(IAuthenticationResponse response)
+ {
+ return null;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/DotNetOpenAuth.Web/Clients/OpenID/YahooOpenIdClient.cs b/src/DotNetOpenAuth.Web/Clients/OpenID/YahooOpenIdClient.cs
new file mode 100644
index 0000000..2235a2b
--- /dev/null
+++ b/src/DotNetOpenAuth.Web/Clients/OpenID/YahooOpenIdClient.cs
@@ -0,0 +1,48 @@
+using System.Collections.Generic;
+using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
+using DotNetOpenAuth.OpenId.RelyingParty;
+
+namespace DotNetOpenAuth.Web.Clients
+{
+ internal sealed class YahooOpenIdClient : OpenIDClient
+ {
+ public YahooOpenIdClient() :
+ base("yahoo", "http://me.yahoo.com")
+ {
+ }
+
+ /// <summary>
+ /// Called just before the authentication request is sent to service provider.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
+ {
+ // Attribute Exchange extensions
+ var fetchRequest = new FetchRequest();
+ fetchRequest.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, isRequired: true));
+ fetchRequest.Attributes.Add(new AttributeRequest(AxKnownAttributes.FullName, isRequired: false));
+
+ request.AddExtension(fetchRequest);
+ }
+
+ /// <summary>
+ /// Gets the extra data obtained from the response message when authentication is successful.
+ /// </summary>
+ /// <param name="response">The response message.</param>
+ /// <returns></returns>
+ protected override Dictionary<string, string> GetExtraData(IAuthenticationResponse response)
+ {
+ FetchResponse fetchResponse = response.GetExtension<FetchResponse>();
+ if (fetchResponse != null)
+ {
+ var extraData = new Dictionary<string, string>();
+ extraData.AddItemIfNotEmpty("email", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email));
+ extraData.AddItemIfNotEmpty("fullName", fetchResponse.GetAttributeValue(AxKnownAttributes.FullName));
+
+ return extraData;
+ }
+
+ return null;
+ }
+ }
+}