Exemplo n.º 1
0
        /// <summary>
        /// Authenticates a user to enable the user data APIs.
        /// </summary>
        /// <param name="client">The MixRadio client.</param>
        /// <param name="clientSecret">The client secret obtained during app registration</param>
        /// <param name="scopes">The scopes requested.</param>
        /// <param name="browser">The browser control to use to drive authentication.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <param name="oauthRedirectUri">The OAuth completed URI.</param>
        /// <returns>
        /// An AuthResultCode value indicating the result
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// browser;You must supply a web browser to allow user interaction
        /// or
        /// clientSecret;You must supply your app client secret obtained during app registration
        /// </exception>
        /// <remarks>
        /// Sorry, this method is messy due to the platform differences
        /// </remarks>
        public static async Task <AuthResultCode> AuthenticateUserAsync(this MusicClient client, string clientSecret, Scope scopes, WebBrowser browser, CancellationToken?cancellationToken = null, string oauthRedirectUri = MusicClient.DefaultOAuthRedirectUri)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser", "You must supply a web browser to allow user interaction");
            }

            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret", "You must supply your app client secret obtained during app registration");
            }

            // See if we have a cached token...
            AuthResultCode cachedResult = await AuthenticateUserAsync(client, clientSecret, cancellationToken).ConfigureAwait(false);

            if (cachedResult == AuthResultCode.Success)
            {
                return(cachedResult);
            }

            var browserController = new OAuthBrowserController();

            CancellationToken token = cancellationToken ?? CancellationToken.None;

            await Task.Run(() => browserController.DriveAuthProcess(browser, client.GetAuthenticationUri(scopes), oauthRedirectUri, token), token);

            AuthResultCode authResult = browserController.ResultCode;

            if (authResult != AuthResultCode.Cancelled)
            {
                // Grab the results and kill the browser controller
                string code = browserController.AuthorizationCode;
                browserController = null;

                // Move on to obtain a token
                if (authResult == AuthResultCode.Success)
                {
                    var authToken = await client.GetAuthenticationTokenAsync(clientSecret, code, cancellationToken);

                    if (authToken != null)
                    {
                        await StoreOAuthToken(authToken, client.ClientId, clientSecret, cancellationToken).ConfigureAwait(false);
                    }
                }
            }

            return(authResult);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Authenticates a user to enable the user data APIs.
        /// </summary>
        /// <param name="client">The MixRadio client.</param>
        /// <param name="clientSecret">The client secret obtained during app registration</param>
        /// <param name="scopes">The scopes requested.</param>
        /// <param name="browser">The browser control to use to drive authentication.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        /// <param name="oauthRedirectUri">The OAuth completed URI.</param>
        /// <returns>
        /// An AuthResultCode value indicating the result
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// browser;You must supply a web browser to allow user interaction
        /// or
        /// clientSecret;You must supply your app client secret obtained during app registration
        /// </exception>
        /// <remarks>
        /// Sorry, this method is messy due to the platform differences
        /// </remarks>
        public static async Task<AuthResultCode> AuthenticateUserAsync(this MusicClient client, string clientSecret, Scope scopes, WebBrowser browser, CancellationToken? cancellationToken = null, string oauthRedirectUri = MusicClient.DefaultOAuthRedirectUri)
        {
            if (browser == null)
            {
                throw new ArgumentNullException("browser", "You must supply a web browser to allow user interaction");
            }

            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret", "You must supply your app client secret obtained during app registration");
            }

            // See if we have a cached token...
            AuthResultCode cachedResult = await AuthenticateUserAsync(client, clientSecret, cancellationToken).ConfigureAwait(false);
            if (cachedResult == AuthResultCode.Success)
            {
                return cachedResult;
            }

            var browserController = new OAuthBrowserController();

            CancellationToken token = cancellationToken ?? CancellationToken.None;

            await Task.Run(() => browserController.DriveAuthProcess(browser, client.GetAuthenticationUri(scopes), oauthRedirectUri, token), token);

            AuthResultCode authResult = browserController.ResultCode;
            if (authResult != AuthResultCode.Cancelled)
            {
                // Grab the results and kill the browser controller
                string code = browserController.AuthorizationCode;
                browserController = null;

                // Move on to obtain a token
                if (authResult == AuthResultCode.Success)
                {
                    var authToken = await client.GetAuthenticationTokenAsync(clientSecret, code, cancellationToken);
                    if (authToken != null)
                    {
                        await StoreOAuthToken(authToken, client.ClientId, clientSecret, cancellationToken).ConfigureAwait(false);
                    }
                }
            }

            return authResult;
        }