Introduction
OAuth 2.0 interfaces cover a range of authentication options. One option which is of growing popularity is the JSON Web Token standard based on RFC 7519. There is a website jwt.io which describes the standard in user friendly language.
From Iguana 6.0.3 onwards we have a set crypto signing APIs which make it easy to implement this standard.
To provide a practical example of using this API we picked an application called iFormBuilder, which is an application for creating mobile forms on iPhone and Android devices without extensive development. This is the iFormBuilder API guide.
iFormBuilder has many potential applications in healthcare, and it is HIPAA compliant. One could collect data via mobile devices and use Iguana to feed that data into your EMR or other applications without much effort.
You can create a free iFormBuilder account which does give you some API calls – not too many but enough for you to try this example for yourself.
Note: The primary purpose of this code is to demonstrate use of OAuth 2.0 Using a JSON web token for authentication. To demonstrate this it uses iFormBuilder and also uses HTTP caching for performance purposes. As a result this is probably not a piece of code you will reuse in its entirety, so we suggest you study the part of the code you are interested in and copy and adapt that piece.
If you want to do more with iFormBuilder then please refer to the documentation for the iFormBuilder API here: http://docs.iformbuilder.apiary.io/
If you have any questions please contact us at support@interfaceware.com.
Tip: The code for this channel uses stores the the iFormBuilder login information ( client key, client secret, profile id) in plain text this is not best practice.
You can use the encrypt.password.lua and store2.lua modules to store logon information in encrypted format, see the Salesforce.com Adapter for an example of how to do this.
Setup iFormBuilder [top]
- Get a free account:
- Go to www.iformbuilder.com and set up a free account.
- Download the app and try it.
- Set up the API:
- Within your account go into Company->API Applications to this form:
- Click on New Client. It will open up a dialog asking for the name of the new client – put Iguana:
- Then it has a dialog asking which user to select for API access. I suggest selecting yourself.
- After you are done you should see a screen something like this:
- Within your account go into Company->API Applications to this form:
Using the Code [top]
- Import the OAuth 2.0 via JWT iFormBuilder channel from the Builtin: Iguana Webservices repository
- Experiment with the code to find out how it works
- Then add the modules to your Translator project
- Copy the require statement from the channel and add it at the top of your script
Note: The iformbuilder.api.lua module uses require to return a single function
Note: The oauth.jwt.lua module uses require to return a table
Note: The store2.lua module uses require to return a table
Note: The net.http.cache.lua module modifies the global namespace - Adapt the code to your own requirements
- Interactive scripting help is included for this module
This is the github code for the main module:
How it works [top]
- First we call the
work()
function, usingpcall()
to catch the error if the call fails.- If the call fails we concatenate the error with a helpful message and return it using
net.http.respond{}
- If the call fails we concatenate the error with a helpful message and return it using
- In the
work()
function we use theiFormBuilder.connect{}
function to connect to iFormBuilder. - You will need to enter these three parameters into the
iFormBuilder.connect{}
call in your script:
Note: These values are for the iFormBuilder account you setup in Preparation above.- Client key
- Client_secret
- Profile ID
- Once the connection is made return a list of users for iFormBuilder:
- First we use c:users() to query the users
- Then we format the list
- And finally we use
net.http.respond{}
to return the result
Note: The code makes use of HTTP caching to improve performance and to reduce the number of times that Iguana calls the iFormBuilder API which matters since the free version of iFormBuilder only allows a limited number of API calls.
More Information [top]
- Source code for the main module on github
- Source code for the iformbuilder.api.lua module on github
- Source code for the net.http.cache.lua module on github
- Source code for the oauth.jwt.lua module on github
- Source code for the store2.lua module on github
- API documentation for net.http.respond and string.gsub
- Oauth 2.0 on Wikipedia
- The Oauth 2.0 website
- The iFormBuilder website
- iFormBuilder API guide
- iFormBuilder documentation
- JSON web token RFC 7519
- JWT – user friendly description of JSON Web Tokens