cancel
Showing results for 
Search instead for 
Did you mean: 

Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

SOLVED
Highlighted
Occasional Contributor

Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

 

I have setup the script below to request the token for one of our Endpoints within our project

This works well for that endpoint but we have multiple API's within the project and so multiple Authentication profiles.

How can I set this up at Test Suite level or at a project level to request a token when that Test Case / Test Suite is executed?

 

We added this script as Event "SubmitListener.beforeSubmit

 

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.model.support.ModelSupport

def authProfileName = "PROFILE_NAME"

if(!submit.getRequest().getAuthType().asBoolean()){
    return // stop if the auth type is null, for example jdbc requests
}else if(submit.getRequest().getActiveAuthProfile() == null){
    return // stop if the auth profile is null
}else if(authProfileName == submit.getRequest().getActiveAuthProfile().getName()){

    // Set up variables
    def project = ModelSupport.getModelItemProject(context.getModelItem())
    def authProfile = project.getAuthRepository().getEntry(authProfileName)
    def oldToken = authProfile.getAccessToken()
    def tokenType = TokenType.ACCESS

    // Create a facade object
    def oAuthFacade = new OltuOAuth2ClientFacade(tokenType)

    // Request an access token in headless mode
    oAuthFacade.requestAccessToken(authProfile, true, true)

    // Wait until the access token gets updated
    //while(oldToken == authProfile.getAccessToken()) {}

    //The sleep method can be used instead of a while loop
    //sleep(3000)

    for(int i = 0; i<=3000; i++){
        if(oldToken != authProfile.getAccessToken()){
            break
        }
        sleep(1)
    }

    // Post the info to the log
    //log.info("Set new token: " + authProfile.getAccessToken())
}

https://community.smartbear.com/t5/SoapUI-Pro/How-to-really-get-the-OAuth-2-token-automatically/td-p... 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hi @davecoleman 

 

Try to use this script:

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.model.support.ModelSupport


def authType = submit.getRequest().getAuthType()
if(authType == "OAuth 2.0"){ // Set up variables
def authProfileName = submit.getRequest().getActiveAuthProfile().getName() def project = ModelSupport.getModelItemProject(context.getModelItem()) def authProfile = project.getAuthRepository().getEntry(authProfileName) def oldToken = authProfile.getAccessToken() def tokenType = TokenType.ACCESS // Create a facade object def oAuthFacade = new OltuOAuth2ClientFacade(tokenType) // Request an access token in headless mode oAuthFacade.requestAccessToken(authProfile, true, true) // Wait until the access token gets updated //while(oldToken == authProfile.getAccessToken()) {} //The sleep method can be used instead of a while loop //sleep(3000) for(int i = 0; i<=3000; i++){ if(oldToken != authProfile.getAccessToken()){ break } sleep(1) } // Post the info to the log //log.info("Set new token: " + authProfile.getAccessToken()) }

Kirill Zakharov
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓

View solution in original post

7 REPLIES 7
Highlighted
Community Hero

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hi @davecoleman,

Just a quick question fella, regarding where you say you have "multiple API's within the project with multiple auth profiles".
Within a specific testsuite do you have multiple API's requiring different OAuth2 access/bearer tokens or is it only one access/bearer token for any particular testsuite?

Cheers,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

It is only one access/bearer token for any particular testsuite. 

Highlighted
Community Hero

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hey @davecoleman,

I think i have some ideas, but i will post later,

Nice one

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Manager

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

@richie we would be glad to hear your ideasSmiley Happy


Sonya Mihaljova
Community and Education Specialist

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!
Highlighted

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hi @davecoleman 

 

Try to use this script:

// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.model.support.ModelSupport


def authType = submit.getRequest().getAuthType()
if(authType == "OAuth 2.0"){ // Set up variables
def authProfileName = submit.getRequest().getActiveAuthProfile().getName() def project = ModelSupport.getModelItemProject(context.getModelItem()) def authProfile = project.getAuthRepository().getEntry(authProfileName) def oldToken = authProfile.getAccessToken() def tokenType = TokenType.ACCESS // Create a facade object def oAuthFacade = new OltuOAuth2ClientFacade(tokenType) // Request an access token in headless mode oAuthFacade.requestAccessToken(authProfile, true, true) // Wait until the access token gets updated //while(oldToken == authProfile.getAccessToken()) {} //The sleep method can be used instead of a while loop //sleep(3000) for(int i = 0; i<=3000; i++){ if(oldToken != authProfile.getAccessToken()){ break } sleep(1) } // Post the info to the log //log.info("Set new token: " + authProfile.getAccessToken()) }

Kirill Zakharov
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓

View solution in original post

Highlighted
Occasional Contributor

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hi,

This is to request one token only and is what I linked to above. We have the scenario where we have 2 API's in the one project and so have 2 different OAuth tokens to request to access these API's.

 

Thanks.

Re: Managing multiple Auth records - Ability to trigger one but how to complete for subsequent

Hi @davecoleman,

 

The script which I provided generates a token depending on which authentication profile is set. So if your profile is "profile1", then the token will be generated for this profile:

def authType = submit.getRequest().getAuthType()
if(authType == "OAuth 2.0"){ // Set up variables
def authProfileName = submit.getRequest().getActiveAuthProfile().getName()

 

Here, the script gets the type of auth in the first row and only if it's OAutn 2.0 then it will generate the token for the profile that is indicated in the auth setting for the request.

 

If it didn't work for you, let me know what did you mean "2 different OAuth tokens to request to access these API's", do you mean two tokens in one request at the same time?


Kirill Zakharov
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓