aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-auth/src/main
diff options
context:
space:
mode:
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>2022-04-05 12:33:05 +0200
committersonartech <sonartech@sonarsource.com>2022-04-05 20:03:16 +0000
commit975758278cf78e56f8f536a05f211ca4eb1c017a (patch)
treefd27e61dd2bf5296dcb69da6947747ab0b57f8bb /server/sonar-webserver-auth/src/main
parent3b478c9a27a4bb225ba32a86ba90286a7d95c62f (diff)
downloadsonarqube-975758278cf78e56f8f536a05f211ca4eb1c017a.tar.gz
sonarqube-975758278cf78e56f8f536a05f211ca4eb1c017a.zip
SONAR-16227 changed generation of tokens in sonarqube to include identifier
Diffstat (limited to 'server/sonar-webserver-auth/src/main')
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java2
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java15
-rw-r--r--server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java34
3 files changed, 46 insertions, 5 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
index 8891969dea3..b41644530bf 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGenerator.java
@@ -35,7 +35,7 @@ public interface TokenGenerator {
* must not contain colon character ":".
*
*/
- String generate();
+ String generate(TokenType tokenType);
/**
* Hash a token.<br/>
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
index 01e0d8f5534..235a2fb0285 100644
--- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenGeneratorImpl.java
@@ -24,12 +24,19 @@ import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
public class TokenGeneratorImpl implements TokenGenerator {
+
+ private static final String SONARQUBE_TOKEN_PREFIX = "sq";
+
@Override
- public String generate() {
+ public String generate(TokenType tokenType) {
SecureRandom random = new SecureRandom();
- byte[] bytes = new byte[20];
- random.nextBytes(bytes);
- return Hex.encodeHexString(bytes);
+ byte[] randomBytes = new byte[20];
+ random.nextBytes(randomBytes);
+ return buildIdentifiablePartOfToken(tokenType) + Hex.encodeHexString(randomBytes);
+ }
+
+ private static String buildIdentifiablePartOfToken(TokenType tokenType) {
+ return SONARQUBE_TOKEN_PREFIX + tokenType.getIdentifier() + "_";
}
@Override
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java
new file mode 100644
index 00000000000..8251bc8015c
--- /dev/null
+++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/TokenType.java
@@ -0,0 +1,34 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.usertoken;
+
+public enum TokenType {
+ USER_TOKEN("u");
+
+ private final String identifier;
+
+ TokenType(String identifier) {
+ this.identifier = identifier;
+ }
+
+ public String getIdentifier() {
+ return identifier;
+ }
+}