Using fixed bearer tokens for authorization

Using fixed bearer tokens for authorization

For some connections it is necessary to use a bearer token for authorization purposes. In many cases this token is not fixed and it is probably better to use the OAuth2 gateway instead of this HTTP gateway. But in some cases the Authorization token is fixed and you can use the HTTP Gateway to send your request.

In the next example we will make a connection to a specific service sending a GET request: http://grafana.example.com/api/folders.

In Grafana, you have to send a bearer token with your request to authenticate. To do this within the HTTP Gateway we have to create the following components:

  • A script
  • Header Enricher
  • HTTP Gateway

The script

We have to create a script to create a variable which we will inject into the header of the message. This script will look something like this:

// Please enter javascript here
// Available objects :
//	log : Object of class org.slf4j.Logger. Can be used to write logmessages to the interface logfile.
//	connectmsg : Object of class com.opdion.myesb.eaf.pojo.interfaces.ConnectMessage. Holds the incoming ConnectMessage
//	properties : Object of class java.util.Properties. Holds the properties of the incoming ConnectMessage

// main
(function(){
    var output = "Bearer BNHY_PDGFlGz1Rq3pk4z9S8dKspcRAZXD5Om_10a5bfq8";


    return output;
})();

In this script we output the bearer token. Because this has alwasy a fixed layout (as Bearer <token>), we created this script, which we will use in the Header Enricher.

The Header Enricher

In the Header Enricher, we create a header with the name Authorization. This value we add the output of the script as shown below.

Now the bearer token is set and we can use this with our request.

The HTTP Gateway

Now we have our Authorization header, we can use it in our HTTP Gateway.

Just fill in your URL, set the method on GET (in our example) and refer to Authorization in the field Mapped Request Headers.

Now you can run the request and get a response back.

[{"id":6,"uid":"cVcuYRh4z","title":"ConnectAgents - TEST"}]