<img height="1" width="1" src="https://www.facebook.com/tr?id=1101141206686180&amp;ev=PageView &amp;noscript=1">

Blog

Revisiting OAuth 2 in LabVIEW

Recap

In my previous blog, we took a look at how to implement OAuth2 in LabVIEW. OAuth2 has an easy workflow where we launch a browser to a specific login URL, capture the redirect message from the browser, and finally request an access token.

Previously, we implemented a local webserver to capture the redirect message. While it was easy to do, it is not a very re-usable solution. What if we wanted to create a reusable library to handle a specific API? From my research, I have not found a way to distribute a webserver set-up with my library.

In this post, we will revisit steps 1 and 2 and look at a better way to handle the OAuth2 redirect.

 

The Hypertext Transfer Protocol

HTTP (Hypertext Transfer Protocol) is a set of rules for transferring data over the web. HTTP uses a set of request/response pairs to allow our web browser to request files from a web server and receive the response.

Knowing the details of HTTP is not necessary. However, the most important detail to pay attention to is the fact that HTTP is built on top of the TCP/IP suite. What does this mean? It means we don’t need a webserver. We just need to open a TCP socket!

 

Processing the Redirect

In my previous post, we registered an application (or integration) directly on Xero’s website. As part of that set-up, we specified a redirect URL for:

https://localhost:5002/OAuth/Redirect

This URL tells Xero to send an HTTP GET request to an open TCP socket on 127.0.0.1 using port 5002. So, after we launch the web browser to the authorization request URL, let’s open that socket ourselves!

Picture1-Jul-22-2022-06-55-21-49-PM

Figure 1 - Opening the TCP Socket

 

In Figure 1, we create a TCP listener for localhost on port 5002. For this example, I am not specifying a timeout. This means the socket will listen indefinitely for incoming connections. If the user closes the browser and cancels out of the authentication process, this will block execution of the rest of our code and sit forever. It is recommended that you add a timeout and place the listener in a loop to provide a way for your application to cancel the authentication process.

The next step is to read from the socket.

Picture2-Jul-22-2022-07-02-52-09-PM

Figure 2 - Reading the TCP Socket

 

I I chose an arbitrary 2000 for the bytes to read property. The reality is, we’ll only need the first line of the response. A typical response will look like this:

Picture3-Jul-22-2022-07-02-52-24-PM

Figure 3 - The HTTP Response

The orange section is the only section we care about. It contains the full GET request URL and most importantly the “code” we need to handle the rest of our authentication.

Parsing this response is relatively easy. While my example in Figure 4 uses regex, it is not necessary at all. Everything in the orange box is terminated with a carriage return and line feed combination allowing you to split the string there. You can then further search for the keyword “code” and grab the data after the equal sign.

Picture4-Jul-22-2022-07-02-52-24-PM

Figure 4 - Parsing the HTTP Response

 

Cleaning Up

 

Now that we can receive the redirect response and parse it, let’s add some feedback to the browser so the user knows we’re done.

Picture5-4

Figure 5 - Responding to the Browser

 

This will return some HTML for the opened browser to render. The HTML in Figure 5 is boring and not visually appealing so you will want to modify it for the look and feel that you desire.

And with that, we are done?

 

Wait, this isn’t HTTPS!

 

If we try to run this, we won’t get any valid response. OAuth2 requires transfer of data via SSL. If we try to configure Xero with an HTTP URL, it will refuse it.

The solution? Let’s add TLS to our TCP Socket.

 

Implementing TLS

The first step in adding TLS support, is to generate certificates. Generation of certificates is outside of the scope of this post. However, National Instruments has a great example of TLS communication that details how to generate your certificates. Search for the “TLS Client and Server with Self-Signed Server Certificate” example VI.

Picture6-4

Figure 6 - TLS Example Finder

 

IIn the example, you will find comments by National Instruments detailing how to use the cfssl utility to generate the srv-key.pem and chain.pem files necessary to configure TLS.

Picture7-2

Figure 7 - CFSSL Comments

 

With our pem files generated, let’s apply them to the TCP socket. The steps to take here are:

  • Load the private key (srv-key.pem) into memory
  • Load the certificates (chain.pem) into memory
  • Create a new TLS configuration
  • Add the private key and certificates to the configuration
  • Make the configuration immutable
  • Configure the TCP socket to accept TLS
  • Read the response normally


    MicrosoftTeams-image (19)

     

Figure 8 - Adding TLS




 

 

Recent Posts:

Filtering Basics: Importance of Linear Phase
Publish Date 26 Jul 2022 Jason ThaiJason Thai

Linear phase and computation/memory complexity are important characteristics to [..]

Revisiting OAuth 2 in LabVIEW
Publish Date 26 Jul 2022 John AmstadtJohn Amstadt

Recap In my previous blog, we took a look at how to implement OAuth2 in LabVIEW. [..]

Engineering a Better 3D Print (Part 1)
Publish Date 26 Jul 2022 Michael MaloneyMichael Maloney

3D Printing and its widespread use has been a long time coming and seems to have [..]

NVCC – Intro to Utilizing GPU Power to Offload the CPU Part 3
Publish Date 26 Jul 2022 Jack BakerJack Baker

Assumptions: Machine has a Nvidia CUDA Core GPU (such as a GeForce) with installed [..]

How I Learn New Skills for Personal Growth
Publish Date 26 Jul 2022 Bryce UrestiBryce Uresti

Learning new skills can be quite the task especially when there's already so much [..]

Controlling the Supply Chain Dream
Publish Date 26 Jul 2022 Thomas MathewThomas Mathew

You are out with your friends, bird watching, and nothing could be more peaceful. [..]

Aligning Data in WAVE
Publish Date 26 Jul 2022 Rohama KhadijaRohama Khadija

When analyzing data from multiple sources, we often find that the time series data [..]