// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Net.Http; using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; using Microsoft.AspNet.Security.DataHandler; using Microsoft.AspNet.Security.Infrastructure; using Microsoft.AspNet.Security.OAuth; using Microsoft.Framework.Logging; using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Security.Google { /// /// An ASP.NET middleware for authenticating users using Google OAuth 2.0. /// [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Middleware are not disposable.")] public class GoogleAuthenticationMiddleware : OAuthAuthenticationMiddleware { /// /// Initializes a new . /// /// The next middleware in the HTTP pipeline to invoke. /// /// /// /// Configuration options for the middleware. public GoogleAuthenticationMiddleware( RequestDelegate next, IServiceProvider services, IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions externalOptions, IOptions options, ConfigureOptions configureOptions = null) : base(next, services, dataProtectionProvider, loggerFactory, externalOptions, options, configureOptions) { if (Options.Notifications == null) { Options.Notifications = new GoogleAuthenticationNotifications(); } if (Options.Scope.Count == 0) { // Google OAuth 2.0 asks for non-empty scope. If user didn't set it, set default scope to // "openid profile email" to get basic user information. // TODO: Should we just add these by default when we create the Options? Options.Scope.Add("openid"); Options.Scope.Add("profile"); Options.Scope.Add("email"); } } /// /// Provides the object for processing authentication-related requests. /// /// An configured with the supplied to the constructor. protected override AuthenticationHandler CreateHandler() { return new GoogleAuthenticationHandler(Backchannel, Logger); } } }