| | | 1 | | using Microsoft.AspNetCore.Authentication; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 3 | | using ProjectTemplate.Web.Authentication.Options; |
| | | 4 | | |
| | | 5 | | namespace ProjectTemplate.Web.Authentication.Providers.Google; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for registering Google authentication provider services. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class GoogleAuthenticationServiceExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds the Google authentication provider registration. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="builder">The authentication builder used to register authentication handlers.</param> |
| | | 16 | | /// <param name="options">The Google provider options.</param> |
| | | 17 | | /// <returns>The same <see cref="AuthenticationBuilder"/> instance for chaining.</returns> |
| | | 18 | | public static AuthenticationBuilder AddGoogleAuthentication( |
| | | 19 | | this AuthenticationBuilder builder, |
| | | 20 | | ApplicationExternalAuthenticationProviderOptions options) |
| | | 21 | | { |
| | 186 | 22 | | ArgumentNullException.ThrowIfNull(builder); |
| | 184 | 23 | | ArgumentNullException.ThrowIfNull(options); |
| | | 24 | | |
| | 182 | 25 | | if (!options.Enabled) |
| | | 26 | | { |
| | 174 | 27 | | return builder; |
| | | 28 | | } |
| | | 29 | | |
| | 8 | 30 | | builder.AddGoogle(options.Scheme, options.DisplayName, googleOptions => |
| | 8 | 31 | | { |
| | 2 | 32 | | googleOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; |
| | 2 | 33 | | googleOptions.ClientId = options.ClientId; |
| | 2 | 34 | | googleOptions.ClientSecret = options.ClientSecret; |
| | 2 | 35 | | googleOptions.CallbackPath = options.CallbackPath; |
| | 8 | 36 | | |
| | 24 | 37 | | foreach (string scope in options.Scopes.Where(scope => !string.IsNullOrWhiteSpace(scope))) |
| | 8 | 38 | | { |
| | 6 | 39 | | if (!googleOptions.Scope.Contains(scope, StringComparer.Ordinal)) |
| | 8 | 40 | | { |
| | 2 | 41 | | googleOptions.Scope.Add(scope); |
| | 8 | 42 | | } |
| | 8 | 43 | | } |
| | 10 | 44 | | }); |
| | | 45 | | |
| | 8 | 46 | | return builder; |
| | | 47 | | } |
| | | 48 | | } |