Exemplo n.º 1
0
        public static IApplicationBuilder UseSpotifyAuthentication(
            [NotNull] this IApplicationBuilder app,
            [NotNull] Action <SpotifyAuthenticationOptions> configuration)
        {
            var options = new SpotifyAuthenticationOptions();

            configuration(options);

            return(app.UseSpotifyAuthentication(options));
        }
        /// <summary>
        /// Adds the <see cref="SpotifyAuthenticationMiddleware"/> middleware to the specified
        /// <see cref="IApplicationBuilder"/>, which enables Spotify authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="options">A <see cref="SpotifyAuthenticationOptions"/> that specifies options for the middleware.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseSpotifyAuthentication(
            [NotNull] this IApplicationBuilder app,
            [NotNull] SpotifyAuthenticationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(app.UseMiddleware <SpotifyAuthenticationMiddleware>(Options.Create(options)));
        }
        /// <summary>
        /// Adds the <see cref="SpotifyAuthenticationMiddleware"/> middleware to the specified
        /// <see cref="IApplicationBuilder"/>, which enables Spotify authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="configuration">An action delegate to configure the provided <see cref="SpotifyAuthenticationOptions"/>.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseSpotifyAuthentication(
            [NotNull] this IApplicationBuilder app,
            [NotNull] Action <SpotifyAuthenticationOptions> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var options = new SpotifyAuthenticationOptions();

            configuration(options);

            return(app.UseMiddleware <SpotifyAuthenticationMiddleware>(Options.Create(options)));
        }
        public static void AddSpotifyAuthentiation(this AuthenticationBuilder builder, SpotifyAuthenticationOptions spotifyAuthenticationOptions)
        {
            var defaults = SpotifyAuthenticationOptions.Default;

            builder.AddOAuth(SpotifyAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.AuthorizationEndpoint = spotifyAuthenticationOptions?.AuthorizationEndpoint ?? defaults.AuthorizationEndpoint;
                options.CallbackPath          = spotifyAuthenticationOptions?.CallbackPath ?? defaults.CallbackPath;
                options.ClientId                = spotifyAuthenticationOptions?.ClientId ?? defaults.ClientId;
                options.ClientSecret            = spotifyAuthenticationOptions.ClientSecret ?? defaults.ClientSecret;
                options.TokenEndpoint           = spotifyAuthenticationOptions?.TokenEndpoint ?? defaults.TokenEndpoint;
                options.UserInformationEndpoint = spotifyAuthenticationOptions?.UserInformationEndpoint ?? defaults.UserInformationEndpoint;
                foreach (var item in SpotifyAuthorizationScopes.All)
                {
                    options.Scope.Add(item);
                }

                options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "display_name");
                options.ClaimActions.MapJsonKey(ClaimTypes.Name, "display_name");
                options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");

                options.SaveTokens = true;

                options.Events = new OAuthEvents
                {
                    OnCreatingTicket = async context =>
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                        request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", context.AccessToken);

                        var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                        response.EnsureSuccessStatusCode();

                        var content = await response.Content.ReadAsStringAsync();
                        using (var user = System.Text.Json.JsonDocument.Parse(content))
                        {
                            context.RunClaimActions(user.RootElement);
                        }

                        var tokenService      = context.HttpContext.RequestServices.GetService <ITokenWriterService>();
                        tokenService.ApiToken = context.AccessToken;
                    }
                };
            });
        }
Exemplo n.º 5
0
 public static IApplicationBuilder UseSpotifyAuthentication(
     [NotNull] this IApplicationBuilder app,
     [NotNull] SpotifyAuthenticationOptions options)
 {
     return(app.UseMiddleware <SpotifyAuthenticationMiddleware>(options));
 }