How to authenticate/login as a user in a Scriptrunner Groovy script?

mnd April 3, 2020

I'm trying to create a Jira issue from a Scriptrunner script, but on create I'm given this error message:

Anonymous users do not have permission to create issues in this project. Please try logging in first.

I have credentials I can use to authenticate, but I don't know how to set them. I've tried the following calls, but they are not working (some appear to only check if you *know* the credentials, not to actually authenticate.

LoginService.authenticate(ApplicationUser, "password")
AuthenticationContext.setLoggedInUser(ApplicationUser)
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(ApplicationUser)

Any ideas how to authenticate?

1 answer

1 vote
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 3, 2020

Hi @mnd ,

would you please paste your code here? I will try to fix it for you. It is hard to tell, where the problem is, there could be different approaches.

When exactly do you try to create the issue? Is it during some transition (i .e. do you use special scripted postfunction)?

Thank you.

mnd April 6, 2020

@Hana Kučerová thank you for the response! I was trying to prepare the simplest version of the code to show you, and it seems like I might have stumbled across a pattern that worked. In my new code I'm using the following block, and I believe it works.

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("<<user name>>")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)

Unfortunately I'm now getting a StackOverflowError in my code when it is creating the issue, which I imagine is unrelated, but I'm not fully sure. Here's the other question I asked related to that (in the odd event that they are at all related)

https://community.atlassian.com/t5/Jira-questions/Creating-an-issue-through-ScriptRunner-causes-StackOverflowError/qaq-p/1341932#M417320

Thanks again! 

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2020

Hi @mnd ,

yes, that looks good.

I've tried to help with the second problem, I hope it will work.

Suggest an answer

Log in or Sign up to answer