package org.sonar.db.user;
import static org.sonar.db.user.UserTokenValidator.checkTokenHash;
-import static org.sonar.db.user.UserTokenValidator.checkTokenName;
public class UserTokenDto {
private String login;
}
public UserTokenDto setName(String name) {
- this.name = checkTokenName(name);
+ this.name = name;
return this;
}
*/
package org.sonar.db.user;
-import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
public class UserTokenValidator {
- private static final int MAX_TOKEN_NAME_LENGTH = 100;
private static final int MAX_TOKEN_HASH_LENGTH = 255;
private UserTokenValidator() {
// utility methods
}
- public static String checkTokenName(String name) {
- checkArgument(name.length() <= MAX_TOKEN_NAME_LENGTH, "Token name length (%s) is longer than the maximum authorized (%s)", name.length(), MAX_TOKEN_NAME_LENGTH);
- return name;
- }
-
static String checkTokenHash(String hash) {
- checkArgument(hash.length() <= MAX_TOKEN_HASH_LENGTH, "Token hash length (%s) is longer than the maximum authorized (%s)", hash.length(), MAX_TOKEN_HASH_LENGTH);
+ checkState(hash.length() <= MAX_TOKEN_HASH_LENGTH, "Token hash length (%s) is longer than the maximum authorized (%s)", hash.length(), MAX_TOKEN_HASH_LENGTH);
return hash;
}
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
- @Test
- public void fail_if_name_is_longer_than_100_characters() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Token name length (101) is longer than the maximum authorized (100)");
-
- new UserTokenDto().setName(randomAlphabetic(101));
- }
-
@Test
public void fail_if_token_hash_is_longer_than_255_characters() {
- expectedException.expect(IllegalArgumentException.class);
+ expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Token hash length (256) is longer than the maximum authorized (255)");
new UserTokenDto().setTokenHash(randomAlphabetic(256));
import org.sonarqube.ws.client.usertoken.GenerateWsRequest;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
-import static org.sonar.db.user.UserTokenValidator.checkTokenName;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_NAME;
public class GenerateAction implements UserTokensWsAction {
+ private static final int MAX_TOKEN_NAME_LENGTH = 100;
private final DbClient dbClient;
private final UserSession userSession;
private final System2 system;
action.createParam(PARAM_NAME)
.setRequired(true)
+ .setMaximumLength(MAX_TOKEN_NAME_LENGTH)
.setDescription("Token name")
.setExampleValue("Project scan on Travis");
}
}
private void checkWsRequest(DbSession dbSession, GenerateWsRequest request) {
- checkTokenName(request.getName());
checkLoginExists(dbSession, request);
Optional<UserTokenDto> userTokenDto = dbClient.userTokenDao().selectByLoginAndName(dbSession, request.getLogin(), request.getName());
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsUserTokens.GenerateWsResponse;
-import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
assertThat(response.getLogin()).isEqualTo(GRACE_HOPPER);
}
- @Test
- public void fail_if_name_is_longer_than_100_characters() {
- logInAsSystemAdministrator();
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Token name length (101) is longer than the maximum authorized (100)");
-
- newRequest(GRACE_HOPPER, randomAlphabetic(101));
- }
-
@Test
public void fail_if_login_does_not_exist() {
logInAsSystemAdministrator();