How to revoke APIToken after a user left

Issue

Sometimes, a Jenkins admin should find useful a way to revoke the APIToken for a given user.

Environment

Resolution

You should be able to take advantage of the script shown below:

import hudson.model.User;
import jenkins.security.ApiTokenProperty;

//If set to true, no changes will be applied
def DRY=true
//You should write down here the name that the user 
def userName="Put_the_user_name_here"

user = User.get(userName)
apiTokenProperty =  user.getProperty(ApiTokenProperty.class)

if(!DRY){
apiTokenProperty.changeApiToken()
user.save() 
}

println DRY?"The test run successfully for "+user.displayName:"APIToken Changed for User:"+user.displayName

You can run this script from the Script console of your Jenkins instance: Manage Jenkins->Script Console. This script is valid for Versions higher than 2.138.1. It is easy to extend this script to be used as part of a parameterized job.

Tested product/plugin versions

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.