Forum Discussion

RamboMadCow's avatar
11 years ago

Passing a dynamic authentication token

So I've been using SoapUI for just a day. My business decided it would be a good tool to use for our Rest service API's we've developed. The tool seems quite in depth and I haven't figured everything out just yet, so I do apologize if the solution to my issue is simple and/or mundane.

I've created two Rest resources. One that grabs two important authentication variables and one that passes the authentication variables along with the request. But I've only gotten this working when I statically set this. Our authentication tokens reset daily, so I'm looking for a way to run the token method, and then pass the two variables to the next action method.

I have successfully figured out how to use the PropertyTransfer tool, but I can not seem to figure out how to concatenate the two values into one, and then pass that in along with the URL for the following action.

To give a very general idea, here's a quick breakdown:

TokenRequest -> returns token_type="bearer" and access_token: "random large string".

RequestMethod -> sends token_type + " " + access_token + " " request_string

I have not figured out how to concatenate the token_type and access_token into one authorization variable.

Any advice/help is certainly appreciate.
  • dasjestyr's avatar
    dasjestyr
    New Contributor

    RamboMadCow

     

    Here's what I've done

     

    1. Create a step that goes out and gets your token. Name it "get_token"
    2. Create the next step as a groovy script, and name it "xfer_bearer_token". In the script area, you can right click>GetData>... then point it at the previous step's response/property. This will prompt you create a variable in the groovy code. Then just return your concatenation like this 
      2015-11-29_15-33-18.png
    3. Create a step that calls an endpoint that needs the token. Set up a property in this step called 'Authorization' and set the style to 'HEADER'. Right click inside of the property value>GetData>... then find your groovy script that you just made. Choose the result of the groovy script

     

    This worked for me

    • NamrataMane's avatar
      NamrataMane
      Occasional Contributor

      HI there,

       

      could you please let me know what step we need to use in below step to get auth token.

       

      1. Create a step that goes out and gets your token. Name it "get_token"
      • nmrao's avatar
        nmrao
        Icon for Champion Level 2 rankChampion Level 2
        NamrataMane,
        Suggested you to create a new question with details so that some one can understand the issue better. Use screen shot if required.
    • Crazy_Chris's avatar
      Crazy_Chris
      Occasional Contributor

      Hi dasjestyrm,

       

      Although this is somewhat late, I would like to thank you for the proposed solution.

      This was just was I was after! Simple, short and to the point :)

       

      To Smartbear:

      Although I have to say that it seems unnecessary difficult to achieve this. Why can't I use get data to the element in another test and extract the fields that way without going through (although rather simple and short) groovy script to define new values and then use Get Data that is somehow able to extract the same information I wanted in the first place... 

      This makes zero sense from a user perspective, why are headers and ressources excluded or limited with what data can be retrieved? It's obviously there, or it would be impossible even through a groovy script.

      • PaulMS's avatar
        PaulMS
        Super Contributor

        It is possible to concatenate the token_type and access_token without adding a groovy script step.

         

        Using this example add header style parameter

        Name: Authorization

        Value: ${get_token#Response#$.token_type} ${get_token#Response#$.access_token}

        or if the type is always bearer

        Value: Bearer ${get_token#Response#$.access_token}

         

        An advantage of using groovy script step is that if you need to use the same authorization header for many requests then property expansion to get data from the response is only processed once and will be consistent.

    • dasjestyr's avatar
      dasjestyr
      New Contributor

      I don't think this answered the question. The video shows GetData... as being pretty much the same functionality as Transferring a property. What the OP asked is how he can append something to the beginning of the property being copied.

       

      For each, if you have a test that goes off to a service to retrieve a token, and it gets returned in the response as "ABCD1234", he needs to transfer it to a property (maybe a header) as "Bearer ABCD1234" but there doesn't seem to be any clear way to do something like this except maybe a groovy script? Either way, I don't think you answered the question. Could you please clarify?