โ04-29-2021 12:00 PM
I previously wrote about integrating with the DocuSign eSignature API with the REST Snap Pack and OAuth 2.0 authentication, but for those that wish to control their access a bit more, DocuSign supports authenticating with JSON Web Tokens (JWTs) too.
I followed the instructions from the โHow to get an access token with JWT Grant authenticationโ DocuSign Developer portal.
Setup
As before, DocuSign makes it very easy to get started with their free Developer Account signup.
Again, in the sidebar under โINTEGRATIONSโ, select โApps and Keysโ. One that page is up, choose the โAdd App & Integration Keyโ button and provide an application name (I chose โSnapLogic Community JWT Demoโ this time).
The โIntegration Keyโ that is generated is also known as the Client ID and JWT will also refer to it as the "iss"
value.
You can keep the User Application selected as Authorization Code Grant but instead of adding a Secret Key, this time weโre going to be choosing a Service Integration:
I wanted DocuSign to generate the public/private key pair for me, so I selected the โGenerate RSAโ button.
A new dialog will open this the generated keys - you must copy these values and store them somewhere as this will be your only chance to do so:
Finally, I set a throwaway Redirect URI, http://localhost
(this isnโt really used - the goal here is to first authorized the app you created but JWT will take over from that point). Click โSaveโ to create the Integration App.
Grant Consent
The first thing you want to do is to get consent from a DocuSign user (it could be you) for this App to impersonate them (thatโs what a Service account does). It follows this URI syntax:
https://account-d.docusign.com/oauth/auth?
response_type=code
&scope=YOUR_REQUESTED_SCOPES
&client_id=YOUR_INTEGRATION_KEY
&state=YOUR_CUSTOM_STATE
&redirect_uri=YOUR_REDIRECT_URI
so I opened the following URL in my browser (substitute your client ID/integration key, state, scopes and redirect URI that you wish to use):
Youโll be asked to consent to the application:
and then redirected to that throwaway localhost
redirect URI (you donโt need to save anything from this response).
Generating the JWT
Now that consent has been granted, itโs time to move to JWT. This is where the JWT Snap Pack comes into play.
Itโs outside the scope of this topic to fully describe JWT, but in short it is a token value that encodes header and payload/body data that is signed with keys. The DocuSign Developer docs take you through how its constructed.
The JWT Generate Snap will take care of generated the correct header ("kid"
is the Key Alias ID - more on that later, "alg"
has to be "RS256"
, and "typ":"JWT"
is implied by the JWT spec).
The main action is to generate the JWT Payload/Body that matches what DocuSign wants:
{
"iss": "5c2b8d7e-xxxx-xxxx-xxxx-cda8a50dd73f",
"sub": "464f7988-xxxx-xxxx-xxxx-781ee556ab7a",
"aud": "account-d.docusign.com",
"iat": 1598383123,
"exp": 1598390123,
"scope": "signature impersonation"
}
and there are few ways to do that.
You could build the above JSON object manually than use it directly in the โCustom Metadataโ section, but Iโll show how to leverage the various fields in the JWT Generate Snap and the JWT Account.
"aud"
and "sub"
stand for Audience and Subject respectively and can be set directly on the JWT Generate Snap:
"sub"
is the most difficult value to get - in fact, you have to look it up by using the API to call the /userinfo
endpoint and that means using the OAuth 2.0 flow. Luckily, my first post described exactly how to do this - follow the setup instructions and see the โGET User Infoโ section.
Note:
"aud"
must be list of strings (hence the array). It will be disregarded silently if it is not and your auth will fail. We will investigate a usability enhancement here.
The "iat"
value will be generated automatically (it defaults to โnowโ in Unix Epoch timestamp format).
"iss"
is the issuer, and this will be the Client ID/Integration Key created when you registered your App in the DocuSign Developer portal.
You configure the "iss"
and "exp"
values by creating a JWT Account and leveraging the โJWT Issuerโ and โToken TTLโ fields respectively (the latter is added to the generated "iat"
value).
That leaves the "scopes"
(which will always include impersonation
and most DocuSign APIs want the signature
scope too, so Iโve include them both, space-separated), and that can be directly configured on the โCustom Metadataโ field in the JWT Generate Snap.
All this configuration between the Snap and Account settings will be combined to form the Payload (you may also see a "nbf"
payload field automatically added, which is โnot beforeโ and defaults to 120 seconds before "iat"
and โToken IDโ becomes "jti"
; DocuSign will ignore these):
Finally, we need to configure the JWT Generate Snapโs Account for signing. This is where the public/private keypair saved earlier will be used. Weโll want to create a protected keystore for use by the account. There are a variety of ways to do this, but Iโll demonstrate with openssl
.
The first step is to combine the public and private keys together into one PEM file:
Then weโll use openssl
to create a X.509 certificate:
And then weโll combine the certificate and the private key in the PEM file to create a PKCS 12 (.p12) file format:
Upload the .p12
file to the Accountโs โKey Storeโ field, also providing the password you used to protect it. You can use the suggest bubble on the โKey Aliasโ field to refer to your entry by name (this will be the "kid"
value in the JWT Header).
Decoding the Generated JWT
The output of the JWT Generate Snap will be the JWT token under the "access_token"
field (donโt confuse this with a DocuSign API access token, that comes later):
You can copy this value and use jwt.io to see the decoded values:
Exchanging the JWT for a DocuSign API Access Token
Alright, itโs finally time to actually get an access token from DocuSign and start using their API!
We are going to use the REST POST Snap to send a form-encoded request to the DocuSign token endpoint:
The changes are minimal - the HTTP Entity is "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion="+$access_token
(using the JWT you just created) and you need to set the appropriate Content-Type
HTTP Header value of application/x-www-form-urlencoded
.
The Service URL is https://account-d.docusign.com/oauth/token
and note that this needs to match the Audience value you used earlier (it is also the Demo environment value - Production is different; see DocuSignโs docs linked above).
If everything has been configured correctly, youโll get an API access token:
And like in the first post, you can use this token with the REST Snap Pack (e.g. GET), albeit this time setting it directly via the Authorization HTTP Header:
and it should return data successfully:
In the end, this pipeline looked simply like this:
Cheers!
โ06-03-2021 08:01 AM
Thank You Robin. This Post has been helpful and we were able to successfully integrate with DocuSign using the steps provided.