{"id":623,"date":"2018-11-08T07:30:03","date_gmt":"2018-11-08T12:30:03","guid":{"rendered":"http:\/\/salzlechner.com\/dev\/?p=623"},"modified":"2018-11-08T07:30:03","modified_gmt":"2018-11-08T12:30:03","slug":"interfacing-desktop-dataflex-oauth2","status":"publish","type":"post","link":"http:\/\/salzlechner.com\/dev\/2018\/11\/08\/interfacing-desktop-dataflex-oauth2\/","title":{"rendered":"Interfacing Desktop DataFlex &#8211; oAuth2"},"content":{"rendered":"<h1>What is oAuth2<\/h1>\n<p>oAuth2 is a authentication framework that allows third party applications to authenticate with services without the need for the user to provide credentials to the third party application.<\/p>\n<p>For example if our application would like to connect to an end users google account we can get authorization by asking the user to authenticate with google and google giving our application a security token. The application never knows the credentials used but gets access using the token provided by google.<\/p>\n<p>Normally authentication would involve a login screen that allows the user to type in a username and password and then the application would use these credentials to talk to the third party service.<\/p>\n<p>In order to hide the actual credentials from the application the following process is used by oAuth2 to authenticate an application<\/p>\n<ol>\n<li>User instructs application to connect to a service secured by oAuth2<\/li>\n<li>Application starts Authentication Process using your client id and\/or client secret by calling the authentication web service<\/li>\n<li>Depending on the options the call returns a URL or web page and that url or webpage which contains the actual login needs to be shown to the user<\/li>\n<li>The application uses one of the available methods to get a response from oAuth2<\/li>\n<li>The user now authenticates on the web page.<\/li>\n<li>in the meantime the application has to wait and continuously check for a response from the authentication<\/li>\n<li>once the user authenticates the application can then get an access token that can be used later to authenticate with the service<\/li>\n<\/ol>\n<p>Interfacing DataFlex with oAuth2 authentication is a bit complicated mainly due to 2 missing parts of the language<\/p>\n<ol>\n<li>DataFlex is single threaded which makes it hard to wait for things while keeping the user interface from being locked up<\/li>\n<li>DataFlex does not have any support for TCPIP communication which is needed to listen to a port on localhost<\/li>\n<\/ol>\n<p>One way to make life easier is to use the Chilkat oAuth2 component. Chilkat solves the second issue by handling all the TCPIP communication for us<\/p>\n<h1>Registering the application<\/h1>\n<p>In order to use oAuth2 for any service the application has to be registered with that service in our example Google. This registration will give us a client id as well as a client secret for our application that will be used to authenticate<\/p>\n<p>For this example we will create 2 views. View 1 will be our view requesting authentication, view 2 will contain the browser window. You could also call the browser as a separate program.<\/p>\n<p>To show the two part process we will split the functions over two buttons. First button will initiate the authentication process, second button will then after successful authentication get the token.<\/p>\n<p>At the top of the view we will need to use the chilkat package. Of course you will need to download and install the chilkat library<\/p>\n<pre class=\"lang:default decode:true\">\/\/\r\n\/\/ use chilkat\r\nUse ChilkatAx-9.5.0-win32.pkg\r\n\r\nProcedure ChilkatGlobalUnlock\r\n    Handle hoGlob\r\n    Boolean iSuccess\r\n    Integer iStatus\r\n    String sTemp1\r\n\r\n    Get Create (RefClass(cComChilkatGlobal)) to hoGlob\r\n    If (not(IsComObjectCreated(hoGlob))) Begin\r\n        Send CreateComObject of hoGlob\r\n    End\r\n    Get ComUnlockBundle of hoGlob \"Anything for 30-day trial\" to iSuccess\r\n    If (iSuccess &lt;&gt; True) Begin\r\n        Get ComLastErrorText of hoGlob to sTemp1\r\n        Showln sTemp1\r\n        Procedure_Return\r\n    End\r\n\r\n    Get ComUnlockStatus of hoGlob to iStatus\r\n    If (iStatus = 2) Begin\r\n        Showln \"Unlocked using purchased unlock code.\"\r\n    End\r\n    Else Begin\r\n        Showln \"Unlocked in trial mode.\"\r\n    End\r\nEnd_Procedure\r\n\r\n<\/pre>\n<p>the code above will globally unlock all chilkat classes using your credentials supplied by chilkat<\/p>\n<p>Step 1 &#8211; Start Authentication<\/p>\n<pre class=\"lang:default decode:true\">Procedure OnClick\r\n    Handle hoOauth2 hoSbJson\r\n    Boolean iSuccess bTemp1\r\n    String sUrl sTemp1\r\n    Integer iNumMsWaited iTemp1\r\n\r\n    \/\/ make sure chilkat is unlocked            \r\n    Send ChilkatGlobalUnlock            \r\n    \r\n    \/\/ create oAuth2 object\r\n    Get Create (RefClass(cComChilkatOAuth2)) to hoOauth2\r\n    If (not(IsComObjectCreated(hoOauth2))) Begin\r\n        Send CreateComObject of hoOauth2\r\n        \r\n    End\r\n    \/\/ remember for step2\r\n    Set phoOAuth2 to hoOauth2\r\n    \r\n    \/\/  For Google OAuth2, set the listen port equal to the port used\r\n    \/\/  in the Authorized Redirect URL for the Client ID.\r\n    \/\/  For example, in this case the Authorized Redirect URL would be http:\/\/localhost:55568\/\r\n    \/\/  Your app should choose a port not likely not used by any other application.\r\n    Set ComListenPort of hoOauth2 to 55568\r\n    \r\n    Set ComAuthorizationEndpoint of hoOauth2 to \"https:\/\/accounts.google.com\/o\/oauth2\/v2\/auth\"\r\n    Set ComTokenEndpoint of hoOauth2 to \"https:\/\/www.googleapis.com\/oauth2\/v4\/token\"\r\n    \r\n    \/\/  Replace these with actual values.\r\n    Set ComClientId of hoOauth2 to \"YOUR APPS CLIENT ID HERE\"\r\n    Set ComClientSecret of hoOauth2 to \"YOUR APPS CLIENT SECRET HERE\"\r\n    \r\n    Set ComCodeChallenge of hoOauth2 to True\r\n    Set ComCodeChallengeMethod of hoOauth2 to \"S256\"\r\n    \r\n    \/\/  This is the scope for Google Drive.\r\n    \/\/  See https:\/\/developers.google.com\/identity\/protocols\/googlescopes\r\n    Set ComScope of hoOauth2 to \"https:\/\/www.googleapis.com\/auth\/drive\"\r\n    \r\n    \/\/  Begin the OAuth2 three-legged flow.  This returns a URL that should be loaded in a browser.\r\n    Get ComStartAuth of hoOauth2 to sUrl\r\n    Get ComLastMethodSuccess of hoOauth2 to bTemp1\r\n    If (bTemp1 &lt;&gt; True) Begin\r\n        Get ComLastErrorText of hoOauth2 to sTemp1\r\n        Showln sTemp1\r\n        Procedure_Return\r\n    End\r\n    \r\n    Send ShowGoogleLogin sUrl\r\nEnd_Procedure<\/pre>\n<p>The OnClick procedure above first creates a chilkat oAuth2 object. We remeber the object id in a property for step 2 later.<\/p>\n<p>oAuth2 uses a local port to communicate. In this case we use a port that is likely not used. A better implementation would be to find a random unused port.<\/p>\n<p>We set the endpoints to the google endpoints<\/p>\n<p>We also need to set the client id and client secret. These are supplied to you when registering your application with google<\/p>\n<p>We also need to set the scope of our authentication which will tell Google what services we are trying to authenticate for.<\/p>\n<p>The next step is to start the authentication process. This will return a URL that needs to be shown to the user.<\/p>\n<p>The last line\u00a0<em>Send ShowGoogleLogin sUrl<\/em> will be handled in our next step<\/p>\n<h1>Show the Url in the browser<\/h1>\n<p>We need to be able to show the url returned by the authentication process to the user.<\/p>\n<p>For this example i decided to use a web browser in a view but you can also simply launch a web browser as a separate application. There is no connection needed between our application and the browser<\/p>\n<p>The code for the browser view is as follows<\/p>\n<pre class=\"lang:default decode:true \">Use Windows.pkg\r\nUse DFClient.pkg\r\nUse cComWebBrowser.pkg\r\n\r\nActivate_View Activate_oBrowserView for oBrowserView\r\nObject oBrowserView is a dbView\r\n    Property String psUrlToNavigate \"\"\r\n    \r\n    Set Border_Style to Border_Thick\r\n    Set Size to 200 300\r\n    Set Location to 2 2\r\n    Set Label to \"BrowserView\"\r\n\r\n    Object oWebBrowser is a cComWebBrowser\r\n        Set Size to 254 497\r\n        Set Location to 34 21\r\n        Set peAnchors to anAll\r\n        \r\n        \/\/ When the control is created, point to a default web site\r\n        Procedure OnCreate\r\n            Forward Send OnCreate\r\n            Send ComNavigate (psUrlToNavigate(Self)) 0 0 0 0\r\n        End_Procedure\r\n    End_Object\r\n\r\nEnd_Object\r\n\r\nProcedure ShowGoogleLogin String sURL\r\n    Set psUrlToNavigate of oBrowserView to sURL\r\n    Send Activate_oBrowserView\r\nEnd_Procedure<\/pre>\n<p>Now we need to create the second part of our authentication that needs to be executed after the user actually authenticates<\/p>\n<pre class=\"lang:default decode:true \">Procedure OnClick\r\n    Handle hoOauth2\r\n    Integer iNumMsWaited iTemp1 iSuccess\r\n    String sTemp1\r\n    \r\n    Get phoOAuth2 to hoOauth2\r\n    \r\n    \/\/  Now wait for the authorization.\r\n    \/\/  We'll wait for a max of 30 seconds.\r\n    Move 0 to iNumMsWaited\r\n    While ((iNumMsWaited &lt; 30000) and ((ComAuthFlowState(hoOauth2)) &lt; 3))\r\n        Send ComSleepMs to hoOauth2 100\r\n        Move (iNumMsWaited + 100) to iNumMsWaited\r\n    Loop\r\n    \r\n    \/\/  If there was no response from the browser within 30 seconds, then\r\n    \/\/  the AuthFlowState will be equal to 1 or 2.\r\n    \/\/  1: Waiting for Redirect. The OAuth2 background thread is waiting to receive the redirect HTTP request from the browser.\r\n    \/\/  2: Waiting for Final Response. The OAuth2 background thread is waiting for the final access token response.\r\n    \/\/  In that case, cancel the background task started in the call to StartAuth.\r\n    Get ComAuthFlowState of hoOauth2 to iTemp1\r\n    If (iTemp1 &lt; 3) Begin\r\n        Get ComCancel of hoOauth2 to iSuccess\r\n        Showln \"No response from the browser!\"\r\n        Procedure_Return\r\n    End\r\n    \r\n    \r\n    \/\/  Check the AuthFlowState to see if authorization was granted, denied, or if some error occurred\r\n    \/\/  The possible AuthFlowState values are:\r\n    \/\/  3: Completed with Success. The OAuth2 flow has completed, the background thread exited, and the successful JSON response is available in AccessTokenResponse property.\r\n    \/\/  4: Completed with Access Denied. The OAuth2 flow has completed, the background thread exited, and the error JSON is available in AccessTokenResponse property.\r\n    \/\/  5: Failed Prior to Completion. The OAuth2 flow failed to complete, the background thread exited, and the error information is available in the FailureInfo property.\r\n    Get ComAuthFlowState of hoOauth2 to iTemp1\r\n    If (iTemp1 = 5) Begin\r\n        Showln \"OAuth2 failed to complete.\"\r\n        Get ComFailureInfo of hoOauth2 to sTemp1\r\n        Showln sTemp1\r\n        Procedure_Return\r\n    End\r\n    \r\n    Get ComAuthFlowState of hoOauth2 to iTemp1\r\n    If (iTemp1 = 4) Begin\r\n        Showln \"OAuth2 authorization was denied.\"\r\n        Get ComAccessTokenResponse of hoOauth2 to sTemp1\r\n        Showln sTemp1\r\n        Procedure_Return\r\n    End\r\n    \r\n    Get ComAuthFlowState of hoOauth2 to iTemp1\r\n    If (iTemp1 &lt;&gt; 3) Begin\r\n        Get ComAuthFlowState of hoOauth2 to iTemp1\r\n        Showln \"Unexpected AuthFlowState:\" iTemp1\r\n        Procedure_Return\r\n    End\r\n    \r\n    \/\/  Save the full JSON access token response to a file.\r\n    Integer hoSbJson\r\n    Get Create (RefClass(cComChilkatStringBuilder)) to hoSbJson\r\n    If (not(IsComObjectCreated(hoSbJson))) Begin\r\n        Send CreateComObject of hoSbJson\r\n    End\r\n    Get ComAccessTokenResponse of hoOauth2 to sTemp1\r\n    Get ComAppend of hoSbJson sTemp1 to iSuccess\r\n    Get ComWriteFile of hoSbJson \"qa_data\/tokens\/googleDrive.json\" \"utf-8\" False to iSuccess\r\n    \r\n    Showln \"OAuth2 authorization granted!\"\r\n    Get ComAccessToken of hoOauth2 to sTemp1\r\n    Showln \"Access Token = \" sTemp1\r\n    \r\n    Send Destroy to hoSbJson \r\n    Send Destroy to hoOauth2\r\n    Set phoOAuth2 to 0\r\nEnd_Procedure\r\n\r\n<\/pre>\n<p>After validating that there was a proper response from the authentication server we get the response that includes the token and other information<\/p>\n<p>To run this example click the first button. This will initiate the authentication process and then show the browser window with a goggle login (the login may be skipped if you are already logged in to google).<\/p>\n<p>After logging in google will ask if you want to authorize the application. Allow the authorization and then close the browser window.<\/p>\n<p>Now you can click the second button and the application will then get the authentication token that can be used to authenticate for any service calls later.<\/p>\n\n\t\t<div class='author-shortcodes'>\n\t\t\t<div class='author-inner'>\n\t\t\t\t<div class='author-image'>\n\t\t\t<img src='http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/mike5crop-566174_60x60.jpg' alt='' \/>\n\t\t\t<div class='author-overlay'><\/div>\n\t\t<\/div> \n\t\t<div class='author-info'>\n\t\t\tMichael Salzlechner is the CEO of StarZen Technologies, Inc.<\/p>\n<p>He was part of the Windows Team at Data Access Worldwide that created the DataFlex for Windows Product before joining\u00a0<a href=\"http:\/\/starzen.com\">StarZen Technologies<\/a>. StarZen Technologies provides consulting services as well as custom Application development and third party products specifically for DataFlex developers<\/p>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is oAuth2 oAuth2 is a authentication framework that allows third party applications to authenticate with services without the need for the user to provide credentials to the third party application. For example if our application would like to connect to an end users google account we can get authorization by asking the user to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","ngg_post_thumbnail":0,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-623","post","type-post","status-publish","format-standard","hentry","category-dataflex"],"_links":{"self":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":2,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":625,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/623\/revisions\/625"}],"wp:attachment":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}