Forum Discussion

davecoleman's avatar
davecoleman
Contributor
5 years ago

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/183817 

  • 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()) }
  • 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?

  • richie's avatar
    richie
    Community Hero
    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
    • davecoleman's avatar
      davecoleman
      Contributor

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