You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectBadgesSupport.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.badge.ws;
  21. import javax.annotation.Nullable;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.component.BranchDto;
  27. import org.sonar.db.project.ProjectBadgeTokenDto;
  28. import org.sonar.db.project.ProjectDto;
  29. import org.sonar.server.component.ComponentFinder;
  30. import org.sonar.server.exceptions.NotFoundException;
  31. import static org.sonar.db.component.BranchType.BRANCH;
  32. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  33. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  34. import static org.sonar.server.ws.KeyExamples.PROJECT_BADGE_TOKEN_EXAMPLE;
  35. public class ProjectBadgesSupport {
  36. private static final String PARAM_PROJECT = "project";
  37. private static final String PARAM_BRANCH = "branch";
  38. private static final String PARAM_TOKEN = "token";
  39. public static final String PROJECT_HAS_NOT_BEEN_FOUND = "Project has not been found";
  40. private final ComponentFinder componentFinder;
  41. private final DbClient dbClient;
  42. public ProjectBadgesSupport(ComponentFinder componentFinder, DbClient dbClient) {
  43. this.componentFinder = componentFinder;
  44. this.dbClient = dbClient;
  45. }
  46. void addProjectAndBranchParams(WebService.NewAction action) {
  47. action.createParam(PARAM_PROJECT)
  48. .setDescription("Project or application key")
  49. .setRequired(true)
  50. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  51. action
  52. .createParam(PARAM_BRANCH)
  53. .setDescription("Branch key")
  54. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  55. action
  56. .createParam(PARAM_TOKEN)
  57. .setDescription("Project badge token")
  58. .setExampleValue(PROJECT_BADGE_TOKEN_EXAMPLE);
  59. }
  60. BranchDto getBranch(DbSession dbSession, Request request) {
  61. try {
  62. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  63. String branchName = request.param(PARAM_BRANCH);
  64. ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
  65. BranchDto branch;
  66. if (branchName == null) {
  67. branch = componentFinder.getMainBranch(dbSession, project);
  68. } else {
  69. branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null);
  70. }
  71. if (!branch.getBranchType().equals(BRANCH)) {
  72. throw generateInvalidProjectException();
  73. }
  74. return branch;
  75. } catch (NotFoundException e) {
  76. throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND);
  77. }
  78. }
  79. private static ProjectBadgesException generateInvalidProjectException() {
  80. return new ProjectBadgesException("Project is invalid");
  81. }
  82. public void validateToken(Request request) {
  83. try (DbSession dbSession = dbClient.openSession(false)) {
  84. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  85. ProjectDto projectDto;
  86. try {
  87. projectDto = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
  88. } catch (NotFoundException e) {
  89. throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND);
  90. }
  91. String token = request.param(PARAM_TOKEN);
  92. if (projectDto.isPrivate() && !isTokenValid(dbSession, projectDto, token)) {
  93. throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND);
  94. }
  95. }
  96. }
  97. private boolean isTokenValid(DbSession dbSession, ProjectDto projectDto, @Nullable String token) {
  98. ProjectBadgeTokenDto projectBadgeTokenDto = dbClient.projectBadgeTokenDao().selectTokenByProject(dbSession, projectDto);
  99. return token != null && projectBadgeTokenDto != null && token.equals(projectBadgeTokenDto.getToken());
  100. }
  101. }