Skip to content

Instantly share code, notes, and snippets.

@ryanvalentin
Last active January 3, 2016 04:19
Show Gist options
  • Save ryanvalentin/8408536 to your computer and use it in GitHub Desktop.
Save ryanvalentin/8408536 to your computer and use it in GitHub Desktop.
How to include social auth and user registration layers in Disqus OAuth grants
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace DisqusSDK.WP8.Controls
{
/// <summary>
/// The UI markup of this page contains a WebBrowser control named "AuthenticationBrowser" (hidden by default)
/// and 4 buttons covering Disqus, Twitter, Facebook, and Google authentication.
///
/// When the user clicks one of the login options, we load the WebBrowser control the the correct URL
/// </summary>
public partial class LoginWindow : UserControl
{
public LoginWindow()
{
InitializeComponent();
}
#region Private variables
//
// The URL used to show the Disqus login/register window
private string _disqusLoginUri = "https://disqus.com/next/login/?forum={0}";
//
// The URL used to show social auth login window
private string _disqusAxBaseUri = "http://disqus.com/_ax/{0}/begin/?forum={1}";
//
// URLs that browser will navigate to when the user successfully logs in
private string[] _completionPatterns = { "disqus.com/next/login-success", "disqus.com/_ax/google/complete", "disqus.com/_ax/twitter/complete", "disqus.com/_ax/facebook/complete" };
//
// URL that our server-side auth will navigate to once the access token cookie has been set
// TODO provide your own URL here
private string _oAuthSuccessUrl = "example.com/disqus-oauth-success/";
//
// Your Disqus shortname. Not important, but good for referral statistics
// TODO provide your own Disqus shortname
private string _disqusShortname = "example";
//
// User agent to pass into the browser
private string _userAgent = "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone " + Convert.ToString(Environment.OSVersion.Version) + "; Trident/6.0; IEMobile/10.0; ARM; Touch; " + DeviceStatus.DeviceManufacturer + "; " + DeviceStatus.DeviceName + ")";
#endregion
#region Overrides and event hanadlers
//
// When the user clicks the "Facebook" button in your UI
private async void FacebookLoginButton_Click(object sender, RoutedEventArgs e)
{
try
{
await NavigateToProviderLogin("facebook");
}
catch { }
}
//
// When the user clicks the "Twitter" button in your UI
private async void TwitterLoginButton_Click(object sender, RoutedEventArgs e)
{
try
{
await NavigateToProviderLogin("twitter");
}
catch { }
}
//
// When the user clicks the "Google" button in your UI
private async void GoogleLoginButton_Click(object sender, RoutedEventArgs e)
{
try
{
await NavigateToProviderLogin("google");
}
catch { }
}
//
// When the user clicks the "Disqus" button in your UI
private async void DisqusLoginButton_Click(object sender, RoutedEventArgs e)
{
try
{
await NavigateToProviderLogin("disqus");
}
catch { }
}
/// <summary>
/// Occurs when the document being navigated to has finished downloading.
/// This is where we look to see if a user logged in successfully
/// </summary>
private void AuthenticationBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
if (_completionPatterns.Any(e.Uri.OriginalString.Contains))
{
// User completed provider authentication, go to your server-side OAuth URL
NavigateToOAuthAuthorization();
}
}
/// <summary>
/// Occurs just before navigation to a document.
/// </summary>
private void AuthenticationBrowser_Navigating(object sender, NavigatingEventArgs e)
{
this.LoadingCircle.IsIndeterminate = true;
LoadingBackgroundGrid.Visibility = Visibility.Visible;
if (e.Uri.OriginalString.Contains(_oAuthSuccessUrl)) // This means the access token has been granted and is available
{
e.Cancel = true; // Stop navigation to this page
//
// Get the payload from the URL (our server-side script redirects to a page with the access token in the URL)
string payload = e.Uri.Query.Split('?').LastOrDefault(); // Converts to "access_token=USER_ACCESS_TOKEN"
string access_token = payload.Replace("access_token=", "");
//
//
//
// Everything is done. Do what you need with the access token here
//
//
//
}
}
#endregion
#region Methods
/// <summary>
/// This function tells the browser to navigate to the selected provider login, which will allow them to
/// log in or register using Disqus or a social service (features the OAuth dialog doesn't allow).
/// </summary>
private async Task NavigateToProviderLogin(string provider)
{
//
// Reset cookies in case there was a previous authorization
await ClearCookies();
//
// Set URL depending on what service the user chose
string navigationUri = string.Empty;
if (provider == "disqus")
navigationUri = string.Format(_disqusLoginUri, _disqusShortname);
else
navigationUri = string.Format(_disqusAxBaseUri, provider, _disqusShortname);
//
// Navigates to the correct login URL. You must set the user-agent for Facebook login to work
this.AuthenticationBrowser.Navigate(new Uri(navigationUri, UriKind.Absolute), null, _userAgent);
}
/// <summary>
/// Navigates to the final client for authorizing a user. Use this after user is logged in to Disqus
/// </summary>
private void NavigateToOAuthAuthorization()
{
string oauthUrl = String.Format("https://disqus.com/api/oauth/2.0/authorize/?client_id={0}&scope=read,write&response_type=code&redirect_uri={1}", "YOUR_PUBLIC_KEY", "http://www.example.com/oauth_redirect");
this.AuthenticationBrowser.Navigate(new Uri(oauthUrl, UriKind.Absolute), null, _userAgent);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment