Create an Oauth2 integration
Last updated
Last updated
In this guide we will create an application that will integrate with Github and will show a list of repositories belonging to a ticket's owner in the ticket sidebar.
The aim of this guide is to document working with Oauth2 and to demonstrate how you can customize the application installer by adding a custom settings screen that validates the Github connection details by requesting an OAuth token.
You can view the complete app we build during this guide here:
If you are not familiar with Github's authentication, it is recommended to read their guide on the matter:
Later on this guide you will have to register an application, so make sure you read also the chapter .
Working with OAuth2 in Deskpro is different based on the type of OAuth2 flow supported by the provider. If the provider offers support for web based applications (2-legged flow) than you don't need to use any secure storage, but if this is not the case you will have to use secure storage and generic OAuth2 client that comes with the SDK. This tutorial focuses on the latter use cases.
Deskpro supports the authorize code
and the refresh token
flows, meaning that you will be able to also refresh tokens if the provider you are working with supports that.
Working with any OAuth2 provider without any support for web applications involves four steps:
configuring OAuth2 in your application manifest
reading application OAuth2 settings and storing a connection to an OAuth2 provider
obtaining an access token
making an authorized request
The SDK offers an client for these operations which will be discussed with examples in the following chapters.
Let's begin by cloning the boilerplate repository:
Open the file package.json
and edit the following properties to replace references to the boilerplate application:
Add the following properties to the file package.json
:
You noticed we added two storage items, oauth:github
and oauth:github:tokens
. The former is used to store the connection information for our provider, Github while the latter is used to store the individual tokens granted to our app users. We also whitelisted requests to the github.com
domain so we can use the proxy to make authenticated requests.
The connection information deserves more coverage. First of all, the connection requires that a storage item named oauth:<PROVIDER IDENTIFIER>
must be defined in the storage section of your manifest. This convention based approach allows the server-side to quickly lookup any connection information from application storage.
The connection information is an object with the following structure:
This object usually includes sensitive information which should not be made public but needs to be used for authentication purposes, which is why it is recommended you always secure it as shown in the example above.
We have to allow an administrator to configure the application by setting their own OAuth2 credentials. We also want to provide a way for us to check their credentials are correct. The easiest way to do this is to add a list of settings and create a custom settings screen that can also verify the credentials by requesting an access token.
Add the following entries to your manifest in package.json
:
Here we have two settings which will provided by the administrator (githubClientId
and githubClientSecret
) and githubCallbackURL
which we will pre-fill as it will be required to register the application with Github.
Since we want to add a custom settings screen we need the installer source files and so we declare a dependency to the installer source package. Later, during the compile and package phases the build tool will discover our settings screen and will compile it along with the installer sources into a custom installer that will be added to our application bundle.
Don't forget to run npm run install
to install the newly added dependency.
We will add our own custom settings screen which will verify the OAuth2 credentials given by the administrator by requesting an OAuth2 token. If the operation is successful, the installation will continue otherwise an error message will be displayed. This is an approach we recommend whenever the administrator is required to configure an application.
Create a new file at src/installer/javascript/index.js
. By convention, this module will be automatically included during the compile phase and its default export must be a React component. Next, set the file contents:
This is just a React component implementation of a settings screen. Functionally, it is not different than the default settings screen. However it illustrates how you can hook into the install process and how you can add your on onSubmit
handler where you can further manipulate the setting values.
If you application is using OAuth2 it is more than likely the administrator will have to create an OAuth2 client on the OAuth provider's website. Many times the provider will ask for a callback url to redirect the browser to after the user has authorized your application and this is also the case for Github.
For our settings screen, we want to show the callback URL in the settings form so the administrator can use it when registering the application on Github. Add the following methods to the ScreenSettings
class after the body of the constructor
method :
Here we are asking for the OAuth settings using our provider's name ( github
) from the Deskpro instance that is hosting our application. Notice that the provider's name, github
is also used in the manifest for the storage key of the OAuth connection, oauth:github
.
We then assign the value of the urlRedirect
property to our githubCallbackURL
setting in the values map.
If we were to package the application and install the application up to this point, we will see the following screen:
On the next page, after clicking Register application
you will be presented with a Client ID
and a Client Secret
which you should copy into the Github client ID
and Github client secret
fields from the settings from in Deskpro.
Replace the onBeforeSubmit
hook with the following code:
A number of things happen here. This hook gets invoked with whatever values we have input into the form and the first thing that we do is create the connection
object for our provider.
If the information is correct, we will receive an OAuth2 token from Github and the next thing we do is hide the actual value of githubClientSecret
since we don't want it to be stored as a setting, which can be read or written to by anybody.
Run the following command:
A file dist/app.zip
was created. Go to the Deskpro Admin interface, open the Apps Menu and upload this file to start the installation process:
Click the green Install App
button to advance to the next screen.
On the next page, after clicking Register application
you will be presented with a Client ID
and a Client Secret
which you should copy into the Github client ID
and Github client secret
fields from the settings from in Deskpro.
Click the green button to finish installing the application
Notice the Dev Mode
button in the upper right corner. Run the following command to start the developer server, then click the button to open a new tab with Deskpro running your application in development mode so we can conclude our tutorial:
Now that we have installed the application and we know our OAuth setings are correct, we can build the actual application. We will need the ticket owner's email to begin with, then we need to check if we have an OAuth2 Github token for our agent. Finally we need to retrieve the list of repositories.
Open the file src/main/javascript/App.jsx
and add this method to read the ticket owner's email:
There's nothing really special going on for this method. We are using the Context API to read the data stored in the ticket's UI Tab and return the email.
Next, we should check if we have a valid OAuth2 token:
This methods first makes a request to the user endpoint using the value stored under the oauth:github:tokens
key. Remember we configured this key to be private and therefore each agent will have their own value. We are using the request placeholder syntax because we do not want to risk the token being exposed.
If the user endpoint responds with an error, then we request another token using oauth.access('github')
which we then store under the key oauth:github:tokens
for further requests. We are simplifying the error handling procedure a bit, in a real application we would check the error.errorData
property for information about the error, for instance we would look for a status code of 401 in our case.
Next let's add the method that reads the ticket owner's repositories:
This method looks similar to the one we added before. Here we are using again the request placeholder syntax to reference our OAuth token. We start by searching for a user with the same email address, then if we get a match we make another request to list the repositories.
Let's connect now everything we've written so far. Add this method that will coordinate the previous three and save the list of repositories so we can then display them:
Finally replace the render method with:
While in development mode, create a ticket using your own email (we know you have a list of repositories in Github) then open it. If you followed this tutorial, you should see the following screen:
Followed by
You have now seen how to create an integration powered by OAuth2. We have covered:
configuring OAuth2 in your application manifest
reading application OAuth2 settings
storing a connection to an OAuth2 provider
obtaining an OAuth2 access token
making authorized requests
creating a custom settings screen
Each application has its own callback URL, which can be retrieved by calling the method. Some of the settings can only be known at runtime, so it is recommended to always use the .
When you get to this screen, your next action should be to copy the value of the Github callback URL fields and head over to Github and register your application: . Paste the value into the Authorization callback URL
field in Github and fill in the other fields:
Then by calling we register the connection and we can immediately test the connection by calling the method. Notice that the provider's name, github
is also used in the manifest for the storage key of the OAuth connection, oauth:github
. The method stores the connection objection into oauth:github
storage key we have declared in the application manifest.
When you get to this screen, your next action should be to copy the value of the Github callback URL fields and head over to Github and register your application: . Paste the value into the Authorization callback URL
field in Github and fill in the other fields: