cancel
Showing results for 
Search instead for 
Did you mean: 

Auto Token Generation for OAuth2.0

Sriraj
Contributor

Auto Token Generation for OAuth2.0

Hi ,

 

I have couple of application profiles something like in the attachment.

 

Each of these are API connection profiles for different applications calling the API via a Gateway. They use OAuth 2.0 with different client credentials.

 

In my E2E suite i use all of these profiles and i end up generating these token manually every time before i run my E2E suite .

 

Is there an option in ReadyAPI where it it generates these tokens automatically ? Say every 30mins 

14 REPLIES 14
msiadak
Community Hero

Re: Auto Token Generation for OAuth2.0

While I haven't done this myself, I have found usually if I need to do something similar I can usually find a way to access these parts in a groovy script test step using the ReadyAPI api. While that's not a direct answer, you might be able to explore that to at least have something you can put into an event / startup script. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
New2API
Frequent Contributor

Re: Auto Token Generation for OAuth2.0

Hi Sriraj, I was also ran into similar issue and one of the community member helped with groovy method. In my example I have only one Auth profile but I could have  multiple in future.

 

Implementation:

I used this in event handler - TestRunListener.beforeRun - by doing this, script is checking at the beginning of a testcase if token is expired or not. if expired then retrieves from the server.

 

Script:

 

import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.impl.rest.OAuth2Profile.*



//Get Project
def project = testRunner.testCase.testSuite.getProject()

//Get oAuth Profile Name
def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList()
def oAuthProfileName

if(oAuthProfilelist.size() == 1){
oAuthProfileName = oAuthProfilelist[0]
log.info "oAuth Profile name is: $oAuthProfileName"
}

//Get Token Status
def oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName")
def TokenStatus = oAuthProfile.accessTokenStatus.toString()
log.info "Access Token Status is: $TokenStatus"


//Get Access Token from the server if expired
if (TokenStatus != 'RETRIEVED_FROM_SERVER'){
def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS)
oAuthClientFacade.requestAccessToken(oAuthProfile, true)
log.info "Access Token Status is "+ oAuthProfile.accessTokenStatus
}

 

since you have multiple profiles, you could iterate through them and update token.

 

Hope this helps!

Sriraj
Contributor

Re: Auto Token Generation for OAuth2.0

@New2API Ideally if your script works at test step level , It must work for me as well. Let me give it a try.

New2API
Frequent Contributor

Re: Auto Token Generation for OAuth2.0

Sriraj, give it a try. Currently it works like charm for me. Just one correction - this scripts runs just before a testcase as my event handler is TestRunListener.beforeRun. 

 

thanks!

Sriraj
Contributor

Re: Auto Token Generation for OAuth2.0

i get the below error when i try it for one 

 

ERROR:java.lang.NullPointerException: Cannot get property 'accessTokenStatus' on null object

testhrishi
Frequent Contributor

Re: Auto Token Generation for OAuth2.0

The script provided in the help doc also leads me to the same error.

anyone else tried to get passed this step?

New2API
Frequent Contributor

Re: Auto Token Generation for OAuth2.0

Have you created an auth profile under project/Auth Manager?

Error message indicates that token status can't be fetched because of non existing Auth Profile.

jameseg3
New Contributor

Re: Auto Token Generation for OAuth2.0

Is this still working for you?  I'm seeing that AuthRepository is missing from the API docs.  Looks like a lot of this has been deprecated.  Have you found a work-around?  (I can't even use the getAuthRepository() method).

 

Thanks in advance, 

 

-James

New2API
Frequent Contributor

Re: Auto Token Generation for OAuth2.0

Hi James, I am still using the same script and it works perfectly. Currently, I am on ReadyAPI 2.2.

Also, I do see Auth Repository tab under Auth Manager section.

 

What's version of SoapUI you are running?

 

for OS version, there is a small change:

import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.impl.rest.OAuth2Profile.*

//Get Project
def project = testRunner.testCase.testSuite.getProject() 

//Get oAuth2 Profile Name
def authContainer
def oAuthProfilelist
def oAuthProfile 
def oAuthProfileName
def TokenStatus
def oAuthClientFacade
def i = 0

//Get oAuth profile name list
authContainer = testRunner.testCase.testSuite.project.OAuth2ProfileContainer
oAuthProfilelist = authContainer.getOAuth2ProfileList()
                            
oAuthProfilelist.each{
    	                 oAuthProfileName = oAuthProfilelist[i].name
     	            log.info "oAuth Profile name is:  $oAuthProfileName"
 
                      //Get Token Status     
                      oAuthProfile = authContainer.getProfileByName("$oAuthProfileName")
                      TokenStatus = oAuthProfile.accessTokenStatus.toString()
                      log.info "Access Token Status for $oAuthProfileName is:  $TokenStatus"
                                                                    
                      //Get Access Token from the server if expired   
                      if (TokenStatus != 'Retrieved from server'){
                                                                  oAuthClientFacade = new OltuOAuth2ClientFacade().requestAccessToken(oAuthProfile)
                                                                  log.info "Access Token Status is "+ oAuthProfile.accessTokenStatus
                     }  
                     i++
}

 

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors