Beispiel #1
0
        private void OnAuthReceived(object sender, Token token)
        {
            if (string.IsNullOrEmpty(token?.AccessToken))
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Exchange token not returned by server."));
                return;
            }

            if (token.HasError())
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs(token.Error));
                return;
            }

            _lastToken = token;
            _lastWebApi?.Dispose();
            _lastWebApi = new SpotifyWebAPI()
            {
                Token = _lastToken
            };

            _lastAuth.Stop();

            OnAuthSuccess?.Invoke(this, AuthSuccessEventArgs.Empty);
            _authWait.Set();
        }
Beispiel #2
0
        /// <summary>
        /// Refreshes the access for a SpotifyWebAPI returned by this factory.
        /// </summary>
        /// <returns></returns>
        public async Task RefreshAuthAsync()
        {
            Token token = await lastAuth.RefreshAuthAsync(lastToken.RefreshToken);

            if (token == null)
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs($"Token not returned by server."));
            }
            else if (token.HasError())
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs($"{token.Error} {token.ErrorDescription}"));
            }
            else if (string.IsNullOrEmpty(token.AccessToken))
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Token had no access token attached."));
            }
            else
            {
                lastWebApi.AccessToken = token.AccessToken;
                OnAuthSuccess?.Invoke(this, new AuthSuccessEventArgs());
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets an authorized and ready to use SpotifyWebAPI by following the SecureAuthorizationCodeAuth process with its current settings.
        /// </summary>
        /// <returns></returns>
        public SpotifyWebAPI GetWebApi()
        {
            _lastAuth = new TokenSwapAuth(ExchangeServerUri, HostServerUri, Scope, HtmlResponse)
            {
                ShowDialog         = ShowDialog,
                MaxGetTokenRetries = MaxGetTokenRetries,
                TimeAccessExpiry   = true
            };

            _lastAuth.Start();

            _lastAuth.AuthReceived         += OnAuthReceived;
            _lastAuth.OnAccessTokenExpired += async(sender, e) =>
            {
                OnAccessTokenExpired?.Invoke(sender, AccessTokenExpiredEventArgs.Empty);

                if (AutoRefresh)
                {
                    await RefreshAuthAsync();
                }
            };

            OnExchangeReady?.Invoke(this, new ExchangeReadyEventArgs {
                ExchangeUri = _lastAuth.GetUri()
            });

            if (OpenBrowser)
            {
                _lastAuth.OpenBrowser();
            }

            if (!_authWait.WaitOne(Timeout * 1000))
            {
                OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Authorization request has timed out."));
            }

            return(_lastWebApi);
        }
Beispiel #4
0
        /// <summary>
        /// Gets an authorized and ready to use SpotifyWebAPI by following the SecureAuthorizationCodeAuth process with its current settings.
        /// </summary>
        /// <returns></returns>
        public async Task <SpotifyWebAPI> GetWebApiAsync()
        {
            return(await Task <SpotifyWebAPI> .Factory.StartNew(() =>
            {
                bool currentlyAuthorizing = true;

                // Cancel any ongoing get web API requests
                CancelGetWebApiRequest();

                lastAuth = new TokenSwapAuth(
                    exchangeServerUri: ExchangeServerUri,
                    serverUri: HostServerUri,
                    scope: Scope,
                    htmlResponse: HtmlResponse)
                {
                    ShowDialog = ShowDialog,
                    MaxGetTokenRetries = MaxGetTokenRetries,
                    TimeAccessExpiry = AutoRefresh || TimeAccessExpiry
                };
                lastAuth.AuthReceived += async(sender, response) =>
                {
                    if (!string.IsNullOrEmpty(response.Error) || string.IsNullOrEmpty(response.Code))
                    {
                        // We only want one auth failure to be fired, if the request timed out then don't bother.
                        if (!webApiTimeoutTimer.Enabled)
                        {
                            return;
                        }

                        OnAuthFailure?.Invoke(this, new AuthFailureEventArgs(response.Error));
                        currentlyAuthorizing = false;
                        return;
                    }

                    lastToken = await lastAuth.ExchangeCodeAsync(response.Code);

                    if (lastToken == null || lastToken.HasError() || string.IsNullOrEmpty(lastToken.AccessToken))
                    {
                        // We only want one auth failure to be fired, if the request timed out then don't bother.
                        if (!webApiTimeoutTimer.Enabled)
                        {
                            return;
                        }

                        OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Exchange token not returned by server."));
                        currentlyAuthorizing = false;
                        return;
                    }

                    if (lastWebApi != null)
                    {
                        lastWebApi.Dispose();
                    }
                    lastWebApi = new SpotifyWebAPI()
                    {
                        TokenType = lastToken.TokenType,
                        AccessToken = lastToken.AccessToken
                    };

                    lastAuth.Stop();

                    OnAuthSuccess?.Invoke(this, AuthSuccessEventArgs.Empty);
                    currentlyAuthorizing = false;
                };
                lastAuth.OnAccessTokenExpired += async(sender, e) =>
                {
                    if (TimeAccessExpiry)
                    {
                        OnAccessTokenExpired?.Invoke(sender, AccessTokenExpiredEventArgs.Empty);
                    }

                    if (AutoRefresh)
                    {
                        await RefreshAuthAsync();
                    }
                };
                lastAuth.Start();
                OnExchangeReady?.Invoke(this, new ExchangeReadyEventArgs {
                    ExchangeUri = lastAuth.GetUri()
                });
                if (OpenBrowser)
                {
                    lastAuth.OpenBrowser();
                }

                webApiTimeoutTimer = new System.Timers.Timer
                {
                    AutoReset = false,
                    Enabled = true,
                    Interval = Timeout * 1000
                };

                while (currentlyAuthorizing && webApiTimeoutTimer.Enabled)
                {
                    ;
                }

                // If a timeout occurred
                if (lastWebApi == null && currentlyAuthorizing)
                {
                    OnAuthFailure?.Invoke(this, new AuthFailureEventArgs("Authorization request has timed out."));
                }

                return lastWebApi;
            }));
        }