import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.usertoken.RevokeWsRequest;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_REVOKE;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_LOGIN;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_NAME;
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION_REVOKE)
.setDescription("Revoke a user access token. <br/>"+
- "It requires administration permissions.")
+ "If the login is set, it requires administration permissions. Otherwise, a token is generated for the authenticated user.")
.setSince("5.3")
.setPost(true)
.setHandler(this);
action.createParam(PARAM_LOGIN)
- .setRequired(true)
.setDescription("User login")
.setExampleValue("g.hopper");
}
private void doHandle(RevokeWsRequest request) {
- userSession.checkLoggedIn().checkPermission(SYSTEM_ADMIN);
+ TokenPermissionsValidator.validate(userSession, request.getLogin());
DbSession dbSession = dbClient.openSession(false);
try {
}
}
- private static RevokeWsRequest toRevokeWsRequest(Request request) {
- return new RevokeWsRequest()
- .setLogin(request.mandatoryParam(PARAM_LOGIN))
+ private RevokeWsRequest toRevokeWsRequest(Request request) {
+ RevokeWsRequest revokeWsRequest = new RevokeWsRequest()
+ .setLogin(request.param(PARAM_LOGIN))
.setName(request.mandatoryParam(PARAM_NAME));
+ if (revokeWsRequest.getLogin() == null) {
+ revokeWsRequest.setLogin(userSession.getLogin());
+ }
+ return revokeWsRequest;
}
}
*/
package org.sonar.server.usertoken.ws;
+import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.DbTester;
import org.sonar.db.user.UserTokenDto;
import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.DbTests;
static final String ADA_LOVELACE = "ada.lovelace";
static final String TOKEN_NAME = "token-name";
-
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
}
@Test
- public void does_not_fail_when_incorrect_login_or_name() {
- insertUserToken(newUserToken().setLogin(GRACE_HOPPER).setName(TOKEN_NAME));
+ public void user_can_delete_its_own_tokens() {
+ userSession.login(GRACE_HOPPER).setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
+ insertUserToken(newUserToken().setLogin(GRACE_HOPPER).setName("token-to-delete"));
- newRequest(ADA_LOVELACE, "another-token-name");
+ String response = newRequest(null, "token-to-delete");
+
+ assertThat(response).isEmpty();
+ assertThat(dbClient.userTokenDao().selectByLogin(dbSession, GRACE_HOPPER)).isEmpty();
}
@Test
- public void fail_if_not_logged_in() {
- userSession.anonymous();
+ public void does_not_fail_when_incorrect_login_or_name() {
insertUserToken(newUserToken().setLogin(GRACE_HOPPER).setName(TOKEN_NAME));
- expectedException.expect(UnauthorizedException.class);
- newRequest(GRACE_HOPPER, TOKEN_NAME);
+ newRequest(ADA_LOVELACE, "another-token-name");
}
@Test
newRequest(GRACE_HOPPER, TOKEN_NAME);
}
- private String newRequest(String login, String name) {
- return ws.newRequest()
- .setParam(PARAM_LOGIN, login)
- .setParam(PARAM_NAME, name)
- .execute().getInput();
+ private String newRequest(@Nullable String login, String name) {
+ TestRequest testRequest = ws.newRequest()
+ .setParam(PARAM_NAME, name);
+ if (login != null) {
+ testRequest.setParam(PARAM_LOGIN, login);
+ }
+
+ return testRequest.execute().getInput();
}
private void insertUserToken(UserTokenDto userToken) {