moneyworks://auth/ URL scheme for external OAuth2

Some OAuth2 providers refuse to work in a webview and require that the OAuth2 redirect be loaded in a browser. This is highly inconvenient for a desktop app.

In MoneyWorks 9.2.7 and later. the solution for this is the moneyworks://auth/ URL scheme, which allows a web page to redirect back to the MoneyWorks app.

The URL form is moneyworks://auth/somedomain/somedata

When the system loads such a URL and it is received by MoneyWorks, this will cause an automatic call of any ProcessAuthURL_somedomain(url) MWScript handler.

note: the trailing slash after somedomain is required by the scheme. Do not change it to ?.

The complication is that the system may launch a new instance of MoneyWorks (it will do this on Windows) or you may already be running multiple instances of MoneyWorks at the time you need to receive that redirect. in MoneyWorks Gold 9.2.7 and later, this problem is solved by MoneyWorks instances cooperating to pass the redirect message to every currently running instance. Every instance will automatically call ProcessAuthURL_somedomain(url) if it exists in that instance.

Securing this flow so that ONLY your instance gets the callback

If the somedomain is hardcoded, then every instance of MoneyWorks that has your script (or any other script with that handler name) loaded will get the callback. Ideally you only want YOUR instance to get the callback, and only when you are expecting it.

This is possible by using SwizzleHandler to make a nonce callback handler name, and passing the nonce as state to the OAuth provider. Your website callback will then construct the URL using the nonce.

constant meta = "OAuth scheme sample"

/*
    moneyworks://auth/ scheme example
    
    How to securely get called back from a browser-involved OAuth flow

 */

property callbacknonce

on ProcessAuthURL(url)
    alert("got url " + url)        
    SwizzleHandler("ProcessAuthURL_" + callbacknonce, "")    // remove secure name
end

on DoAuthFlow    
    let callbacknonce = "X" + Left(Checksum(Random(100000000) + " " + Random(100000000)), 15)
    // add secure callback name (full handler name must be <= 31 chars)
    SwizzleHandler("ProcessAuthURL", "ProcessAuthURL_" + callbacknonce, SwizzleDuplicate) 
    /* Invoke OAuth flow and include state = callbacknonce, which must get passed to
       your website redirecter so that it can construct a callback url

       moneyworks://auth/{callbacknonce}/...                                   */
       
    // MyInvokeOAuthflow(callbacknonce, ...)
    
    // To try this, load this url in a browser:
    let url = "moneyworks://auth/" + callbacknonce + "/blah"
    PutClipboardText(url)
    Alert(url)
end

on LoadFromScriptEditor
    DoAuthFlow()
end

Also in WebViews

The same scheme can also be used in WebViews with OAuth2 providers that allow schemes other than http/s for redirects. In that situation, there is no need for an external web page to provide the scheme redirect; The callback to you handler is done directly when the WebView sees the moneyworks:// scheme redirect.

Posted in Uncategorized | Comments Off on moneyworks://auth/ URL scheme for external OAuth2