A simple example code: function CaptchaReset(username)

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface

import java.sql.Connection
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")

@BaseScript CustomEndpointDelegate delegate

CaptchaReset(httpMethod: "GET", groups: ["jira-users"]) { MultivaluedMap queryParams->
    def username = queryParams.getFirst("username") as String
    if ( username == null ) {  return Response.serverError().entity([error:"NO_USERNAME"]).build();}
    
    def sqlStmt = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username') and attribute_name = 'login.totalFailedCount';"
    def sqlStmt2 = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username') and attribute_name = 'login.currentFailedCount';"
    
	Connection conn = ConnectionFactory.getConnection(helperName)
	Sql sql = new Sql(conn)
	try {
    	StringBuffer sb = new StringBuffer()
    	sql.execute(sqlStmt) 
        sql.execute(sqlStmt2)
	}
	finally {
    	sql.close()
	}
    return Response.ok(new JsonBuilder([result: "OK"]).toString()).build();
}

Example query: /rest/scriptrunner/latest/custom/CaptchaReset?username=mraddon

Example result:

{"result":"OK"}

By MrAddon

.'s avatar
Posted by:.

2 replies on “REST Endpoint to RESET CAPTCHA of users in Jira Server using Scriptrunner

  1. IMPORTANT NOTE: If we have more than one Directory, we need to change the SQL updates to only apply to the enabled one!
    Example: (Note the DIRECTORY_ID = ‘10800’ in the Subquery)

     def sqlStmt = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username' and DIRECTORY_ID = '10800') and attribute_name = 'login.totalFailedCount';"
        def sqlStmt2 = "update cwd_user_attributes set attribute_value = '0' where user_id = (select max(id) from cwd_user where user_name = '$username' and DIRECTORY_ID = '10800') and attribute_name = 'login.currentFailedCount';"
    

    More info: https://developer.atlassian.com/server/jira/platform/database-user-and-group-tables/

    Like

  2. Hi Mr. Addon!

    I created this rest endpoint using scritprunner and I can reset the login failure without having to change the database.

    import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
    import groovy.json.JsonOutput
    import groovy.transform.BaseScript
    import javax.ws.rs.core.MultivaluedMap
    import javax.ws.rs.core.Response
    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.security.login.LoginManager

    @BaseScript CustomEndpointDelegate delegate

    captcha(httpMethod: “GET”) { MultivaluedMap queryParams ->

    def username = queryParams.getFirst(“username”) as String
    def total_failed = 0

    def loginManager = ComponentAccessor.getComponent(LoginManager)
    def user = ComponentAccessor.userManager.getUserByName(username)
    if (user != null){
    def userLoginInfo = loginManager.getLoginInfo(username)
    if(userLoginInfo.getCurrentFailedLoginCount() > 0) {
    total_failed = userLoginInfo.getCurrentFailedLoginCount()
    log.info(“reseting failed count for ” + username)
    loginManager.resetFailedLoginCount(user)
    }
    }

    def flag = [
    username : username,
    CurrentFailedLogin : total_failed,
    captcha: “reset”
    ]

    Response.ok(JsonOutput.toJson(flag)).build()
    }

    in the browser calls the url:

    https://%5Bjira_url%5D/rest/scriptrunner/latest/custom/captcha?username=%5Blogin%5D

    Like

Leave a reply to . Cancel reply