cancel
Showing results for 
Search instead for 
Did you mean: 

Custom connector with two step authentication

Giorda62
Deputy Chef I
Deputy Chef I

Hello,

I'm pretty new to workato and not quite familiar with Ruby.

I need to write a custom connector that authenticates using a POST request passing username/password and receives back an auth token; the Token will then be used in the header of following requests; I'm struggling in identifying the correct form to write the "authorization" block of the "connection" section; I've tried using the following:

 

  "connection": {
    "fields": [
      {
        "name": "username",
        "label": "Username",
        "hint": "Enter HDA username for authentication.",
        "optional": false
      },
      {
        "name": "password",
        "label": "Password",
        "control_type": 'password',
        "hint": "Enter your password for authentication.",
        "optional": false
      },
      {
        "name": "BaseURL",
        "label": "Base URL",
        "control_type": 'URL',
        "hint": "Enter base URL for HDA integration.",
        "optional": false
      }
    ],
    "authorization": {
      "type": "custom_auth",
      "acquire": {
        "code": {
            "post": {
            "url": "https://XXXXXXXXXXX/api/login",
            "headers": {
                "Content-Type": "application/json"
            },
            "payload": {
                "username": "::username::",
                "password": "::password::"
            },
            "response_format": "json"
            },

            "response_mapping": {
            "token": "token"
            }
        }
      },
      "base_uri": "https://XXXXXXXXXXX/api",
      "apply": {
        "headers": {
          "Authorization": "::token::"
          }
      }
    }
  },
  test: lambda do |_connection|
    get("::BaseURL::")
  end,
 
But I get the following error: Expected lambda for 'acquire' in hooks but got hash.
Any hint or suggestion?
Thanks a lot,
Giordano
 
1 REPLY 1

Bhagya_pola
Executive Chef I
Executive Chef I

Hii @Giorda62 ,
There are a few syntax issues in your code. I’ve adjusted the acquire block below

authorization: {
type: 'custom_auth',

acquire: lambda do |connection|
response = post("https://XXXXXXXXXXX/api/login").
headers('Content-Type': 'application/json').
payload(
username: connection['username'],
password: connection['password']
).request_format_json

{
token: response['token']
}
end,

refresh_on: [401, 403],

apply: lambda do |connection|
headers(Authorization: "Bearer #{connection['token']}")
end
},

base_uri: lambda do |connection|
'https://XXXXXXXXXXX/api'
end
},

test: lambda do |connection|
get("#{connection['BaseURL']}")
end

I hope this works :).