Exemple #1
0
        public AuthUtils()
        {
            _httpClient = new HttpClient();
            _auth       = new TokenSwapAuth("http://40.68.75.212:80/spotify/index.php", "http://40.68.75.212:80/",
                                            Scope.PlaylistReadPrivate | Scope.UserReadRecentlyPlayed | Scope.UserReadPrivate | Scope.AppRemoteControl |
                                            Scope.UserReadCurrentlyPlaying | Scope.UserReadPlaybackState | Scope.Streaming | Scope.UserModifyPlaybackState)
            {
                ShowDialog = !Preferences.Get("AutoLogin", false)
            };


            _auth.AuthReceived += async(sender, response) =>
            {
                _lastToken = await _auth.ExchangeCodeAsync(response.Code);

                _api = new SpotifyWebAPI()
                {
                    TokenType   = _lastToken.TokenType,
                    AccessToken = _lastToken.AccessToken
                };
            };
            _auth.OnAccessTokenExpired += async(sender, e) =>
                                          _api.AccessToken = (await _auth.RefreshAuthAsync(_lastToken.RefreshToken)).AccessToken;



            ServerUri = _auth.GetUri();
            _auth.Start();
        }
        public async Task GetAuth()
        {
            string state = Request.QueryString["state"];
            SpotifyAuthServer <AuthorizationCodeResponse> auth = TokenSwapAuth.GetByState(state);

            string code  = null;
            string error = Request.QueryString["error"];

            if (error == null)
            {
                code = Request.QueryString["code"];
            }

            AuthorizationCodeResponse authcode = new AuthorizationCodeResponse
            {
                Code  = code,
                Error = error
            };

            TokenSwapAuth au = (TokenSwapAuth)auth;

            auth?.TriggerAuth(await au.ExchangeCodeAsync(authcode.Code));

            await HttpContext.SendStandardHtmlAsync(200, (tw) =>
            {
                tw.WriteLine(au.HtmlResponse);
                tw.Flush();
            });
        }
Exemple #3
0
        public static async void DoAuthAsync()
        {
            if (Settings.Settings.UseOwnApp)
            {
                _auth = new TokenSwapAuth(
                    exchangeServerUri: "https://songify.rocks/auth/auth.php?id=" + Settings.Settings.ClientId + "&secret=" + Settings.Settings.ClientSecret,
                    serverUri: "http://*****:*****@"Own ID");
            }
            else
            {
                _auth = new TokenSwapAuth(
                    exchangeServerUri: "https://songify.rocks/auth/_index.php",
                    serverUri: "http://*****:*****@"Songify ID");
            }

            try
            {
                // Execute the authentication flow and subscribe the timer elapsed event
                AuthRefresh.Elapsed += AuthRefresh_Elapsed;

                // If Refresh and Access-token are present, just refresh the auth
                if (!string.IsNullOrEmpty(Settings.Settings.RefreshToken) && !string.IsNullOrEmpty(Settings.Settings.AccessToken))
                {
                    Authed  = true;
                    Spotify = new SpotifyWebAPI()
                    {
                        TokenType   = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).TokenType,
                        AccessToken = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken
                    };
                    Spotify.AccessToken = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken;
                }
                else
                {
                    Authed = false;
                }

                // if the auth was successful save the new tokens and
                _auth.AuthReceived += async(sender, response) =>
                {
                    if (Authed)
                    {
                        return;
                    }

                    LastToken = await _auth.ExchangeCodeAsync(response.Code);

                    // Save tokens
                    Settings.Settings.RefreshToken = LastToken.RefreshToken;
                    Settings.Settings.AccessToken  = LastToken.AccessToken;
                    // create ne Spotify object
                    Spotify = new SpotifyWebAPI()
                    {
                        TokenType   = LastToken.TokenType,
                        AccessToken = LastToken.AccessToken
                    };
                    Authenticated = true;
                    _auth.Stop();
                    Authed = true;
                    AuthRefresh.Start();
                    await Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
                    {
                        foreach (Window window in Application.Current.Windows)
                        {
                            if (window.GetType() != typeof(Window_Settings))
                            {
                                continue;
                            }
                            ((Window_Settings)window).SetControls();
                        }
                    }));
                };

                // automatically refreshes the token after it expires
                _auth.OnAccessTokenExpired += async(sender, e) =>
                {
                    Spotify.AccessToken            = (await _auth.RefreshAuthAsync(Settings.Settings.RefreshToken)).AccessToken;
                    Settings.Settings.RefreshToken = LastToken.RefreshToken;
                    Settings.Settings.AccessToken  = Spotify.AccessToken;
                };

                _auth.Start();

                if (Authed)
                {
                    AuthRefresh.Start();
                    return;
                }
                _auth.OpenBrowser();
            }
            catch (Exception ex)
            {
                Logger.LogExc(ex);
            }
        }