public static IOAuthToken PerformTokenExchange(string serverUrl, string oauthCallbackUrl, OAuthAuthorizationCallback callback) { var oauth_connection = new OAuthConnection (serverUrl); string link_to_open_for_user = ""; link_to_open_for_user = oauth_connection.GetAuthorizationUrl (oauthCallbackUrl); string verifier = callback (link_to_open_for_user); bool result = oauth_connection.GetAccessAfterAuthorization (verifier); if (result == false) throw new UnauthorizedAccessException (); var token = oauth_connection.AccessToken; return token; }
public static void AuthorizeAction (NSWindow window, String serverURL) { if (String.IsNullOrEmpty (serverURL) || String.IsNullOrWhiteSpace (serverURL)) { NSAlert alert = new NSAlert () { MessageText = "Incorrect URL", InformativeText = "The Sync URL cannot be empty", AlertStyle = NSAlertStyle.Warning }; alert.AddButton ("OK"); alert.BeginSheet (window, null, null, IntPtr.Zero); //SyncURL.StringValue = ""; return; } else { if (!serverURL.EndsWith ("/")) serverURL += "/"; } HttpListener listener = new HttpListener (); string callbackURL = "http://localhost:9001/"; listener.Prefixes.Add (callbackURL); listener.Start (); var callback_delegate = new OAuthAuthorizationCallback ( url => { Process.Start (url); // wait (block) until the HttpListener has received a request var context = listener.GetContext (); // if we reach here the authentication has most likely been successfull and we have the // oauth_identifier in the request url query as a query parameter var request_url = context.Request.Url; string oauth_verifier = System.Web.HttpUtility.ParseQueryString (request_url.Query).Get("oauth_verifier"); if (string.IsNullOrEmpty (oauth_verifier)) { // authentication failed or error context.Response.StatusCode = 500; context.Response.StatusDescription = "Error"; context.Response.Close(); throw new ArgumentException ("oauth_verifier"); } else { // authentication successfull context.Response.StatusCode = 200; using (var writer = new StreamWriter (context.Response.OutputStream)) { writer.WriteLine("<h1>Authorization successfull!</h1>Go back to the Tomboy application window."); } context.Response.Close(); return oauth_verifier; } }); try{ //FIXME: see http://mono-project.com/UsingTrustedRootsRespectfully for SSL warning ServicePointManager.CertificatePolicy = new DummyCertificateManager (); IOAuthToken access_token = WebSyncServer.PerformTokenExchange (serverURL, callbackURL, callback_delegate); AppDelegate.settings.webSyncURL = serverURL; AppDelegate.settings.token = access_token.Token; AppDelegate.settings.secret = access_token.Secret; SettingsSync.Write (AppDelegate.settings); Console.WriteLine ("Received token {0} with secret key {1}",access_token.Token, access_token.Secret); listener.Stop (); NSAlert success = new NSAlert () { MessageText = "Authentication Successful", InformativeText = "The authentication with the server has been successful. You can sync with the web server now.", AlertStyle = NSAlertStyle.Informational }; success.AddButton ("OK"); success.BeginSheet (window, window, null, IntPtr.Zero); return; } catch (Exception ex) { if (ex is WebException || ex is System.Runtime.Serialization.SerializationException) { NSAlert alert = new NSAlert () { MessageText = "Incorrect URL", InformativeText = "The URL entered " + serverURL + " is not valid for syncing", AlertStyle = NSAlertStyle.Warning }; alert.AddButton ("OK"); alert.BeginSheet (window, null, null, IntPtr.Zero); listener.Abort (); return; } else { NSAlert alert = new NSAlert () { MessageText = "Some Issue has occured", InformativeText = "Something does not seem right!", AlertStyle = NSAlertStyle.Warning }; alert.AddButton ("OK"); alert.BeginSheet (window, null, null, IntPtr.Zero); listener.Abort (); } } }