Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated code lifecycle: Add access tokens for authentication to LocalVC repositories #8929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
126 commits
Select commit Hold shift + click to select a range
2f3afe9
migration
icelord42 Jun 23, 2024
90dfd6d
stash commit
icelord42 Jun 26, 2024
0094cba
remove unneccessary migration
icelord42 Jun 28, 2024
c86a593
added participation access token table
icelord42 Jun 30, 2024
463016f
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 1, 2024
472e1c5
removed adding token to user
icelord42 Jul 1, 2024
17b5e09
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 1, 2024
5b85662
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 3, 2024
7b71766
fixed injection of vcs token
icelord42 Jul 5, 2024
0b5636c
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 5, 2024
64097b3
removed logs
icelord42 Jul 5, 2024
5708cbd
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
icelord42 Jul 5, 2024
ade6e01
removed logs
icelord42 Jul 5, 2024
08a91dc
Update src/main/java/de/tum/in/www1/artemis/service/connectors/localv…
SimonEntholzer Jul 5, 2024
9cf72a5
Update src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab…
SimonEntholzer Jul 5, 2024
a776bdf
Update src/main/java/de/tum/in/www1/artemis/service/connectors/localv…
SimonEntholzer Jul 5, 2024
a687a74
removed debugging log
icelord42 Jul 5, 2024
d4cf294
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
icelord42 Jul 5, 2024
90324f5
fixed master.xml
icelord42 Jul 5, 2024
8bc779d
removed unused resource
icelord42 Jul 5, 2024
a1c0e04
removed unused resource
icelord42 Jul 5, 2024
5948f5d
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 8, 2024
a8277cd
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 10, 2024
537f510
fixed build error
icelord42 Jul 10, 2024
118a92c
renamed yml arguments for clarity
icelord42 Jul 11, 2024
e0e4192
fixed bugs and renamed variable to better reflect its use
icelord42 Jul 12, 2024
13a526a
removed unused dependencies
icelord42 Jul 12, 2024
c7b58db
re-alinged naming of useVersionControlAccessToken variable (removed g…
icelord42 Jul 12, 2024
3432b07
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 12, 2024
75b81b3
removed unused variables
icelord42 Jul 12, 2024
36ec48f
Merge branch 'refs/heads/develop' into feature/programming-exercises/…
icelord42 Jul 15, 2024
6430446
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 16, 2024
e783902
removed unused variables and code
icelord42 Jul 16, 2024
58b9124
fixed merge
icelord42 Jul 16, 2024
659934e
Fix style, add javadoc
icelord42 Jul 16, 2024
ab3f050
added more integration tests
icelord42 Jul 16, 2024
f084550
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 16, 2024
4999f00
fix architecture violation
icelord42 Jul 16, 2024
91f05b7
adjust use of access token
icelord42 Jul 16, 2024
bd687e6
fixed grammar
icelord42 Jul 16, 2024
1be21cc
improve secure token generation
icelord42 Jul 16, 2024
b984289
fix profileInfo refactor
icelord42 Jul 16, 2024
c9b6f5c
adjusted contributor
icelord42 Jul 16, 2024
9563955
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 16, 2024
8b50156
removed debugging console log
icelord42 Jul 16, 2024
ebf73e0
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
icelord42 Jul 16, 2024
38c0c31
fix client test
icelord42 Jul 16, 2024
def35a2
removed console logs
icelord42 Jul 17, 2024
396b961
added suggestions
icelord42 Jul 17, 2024
c92e9bd
always delete token
icelord42 Jul 17, 2024
5747a7e
simpler cast
icelord42 Jul 17, 2024
4ea207b
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 17, 2024
bf476f6
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
icelord42 Jul 17, 2024
13d9db2
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 17, 2024
227efee
enabled tokens
icelord42 Jul 17, 2024
1614378
fixed user being undefined because of race condition
icelord42 Jul 17, 2024
c72ca76
improved maintainability
icelord42 Jul 17, 2024
fbfd519
re-enable use of yml variable
icelord42 Jul 17, 2024
cddfaa2
add migration to generate tokens for existing programming-exercise st…
icelord42 Jul 17, 2024
d34fdff
fix achitecture requirement
icelord42 Jul 17, 2024
3c8e06f
Improve test coverage
icelord42 Jul 18, 2024
abacc45
simplify creation of missing tokens
icelord42 Jul 18, 2024
8595825
removed migration for simplicity
icelord42 Jul 18, 2024
676b674
fix test
icelord42 Jul 18, 2024
393f32e
improved error handling
icelord42 Jul 18, 2024
20e51f7
set use-version-control-access-token to true
icelord42 Jul 18, 2024
bcfb404
fix reafactor
icelord42 Jul 18, 2024
9c05cce
test improvement
icelord42 Jul 18, 2024
c4585b0
added change request changes
icelord42 Jul 18, 2024
868c74c
added on-demand token creation
icelord42 Jul 18, 2024
1cfc021
improved coverage and added on-demand loading
icelord42 Jul 19, 2024
f1dbeec
added repository improvement
icelord42 Jul 19, 2024
a2ef0fb
improved repository calls
icelord42 Jul 19, 2024
c03a7de
minor refactor
icelord42 Jul 19, 2024
c81d559
minor refactor and added check if tokens shuold actually be used
icelord42 Jul 19, 2024
803c330
add javadoc
icelord42 Jul 19, 2024
f60f410
fix practice mode participation access
icelord42 Jul 20, 2024
17dd18f
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 20, 2024
153811e
Improved migration, added indexes for faster lookup, and delete token…
icelord42 Jul 20, 2024
e7b7011
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
icelord42 Jul 20, 2024
a101d1d
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 20, 2024
e36a313
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 21, 2024
6b8d049
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 22, 2024
bf0dd6a
more client tests
icelord42 Jul 22, 2024
ea03de2
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 26, 2024
2db3eb9
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 27, 2024
02c1967
added unique constraint to migration
icelord42 Jul 27, 2024
322601c
refactored LocalVCPersonalAccessTokenManagementService
icelord42 Jul 27, 2024
204f79a
refactored e2e test
icelord42 Jul 27, 2024
207e51f
refactored test and team exercise participation
icelord42 Jul 27, 2024
23e82c2
clean up
icelord42 Jul 27, 2024
9284293
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 27, 2024
5cbfea9
fix
icelord42 Jul 27, 2024
f780676
undo rename
icelord42 Jul 27, 2024
69cd6bf
removed unique constraint
icelord42 Jul 27, 2024
8bf1007
re-added changelog
icelord42 Jul 27, 2024
9493012
fix migration
icelord42 Jul 28, 2024
e96f85a
fix migration 2
SimonEntholzer Jul 28, 2024
b47a70f
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Jul 28, 2024
def042b
completely remove migration for now
SimonEntholzer Jul 28, 2024
a900dd9
re-add migration
SimonEntholzer Jul 28, 2024
b78af67
remove migration for testing
SimonEntholzer Jul 29, 2024
93acbac
fix fetching tokens for multiple participations
SimonEntholzer Jul 29, 2024
30571d3
reload tokens when new participations arrive in ngOnChanges
SimonEntholzer Jul 29, 2024
3a6002b
fix showing link with token for instructors with token
SimonEntholzer Aug 2, 2024
61fed08
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 2, 2024
bcdf122
Merge remote-tracking branch 'refs/remotes/origin/feature/programming…
SimonEntholzer Aug 2, 2024
fd6b847
re-added migration
SimonEntholzer Aug 2, 2024
4edc17a
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 3, 2024
9152a37
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 3, 2024
5d72185
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 4, 2024
56f32a8
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 5, 2024
cc4784f
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 6, 2024
8cb81b3
Merge branch 'develop' into feature/programming-exercises/access-toke…
SimonEntholzer Aug 7, 2024
7745a85
Merge branch 'develop' into feature/programming-exercises/access-toke…
krusche Aug 11, 2024
4c4f595
Merge branch 'develop' into feature/programming-exercises/access-toke…
Aug 11, 2024
577f135
fix an issue in a server test
Aug 11, 2024
94a3492
avoid too many unnecessary exceptions in logs
Aug 11, 2024
6ffb604
Merge branch 'refs/heads/develop' into feature/programming-exercises/…
SimonEntholzer Aug 12, 2024
746fd90
add option to not use token in code button dropdown
SimonEntholzer Aug 12, 2024
62a7f67
adjusted tests
SimonEntholzer Aug 12, 2024
b9e8b6d
fix import
SimonEntholzer Aug 12, 2024
1e6f019
fix highlighting
SimonEntholzer Aug 12, 2024
29705aa
fixed programming exam summary not handing down participation to the …
SimonEntholzer Aug 12, 2024
b91b915
don't show token URL if there is no token (or no participation)
SimonEntholzer Aug 12, 2024
7485fdd
added possibility to disable showing the URL without a token
SimonEntholzer Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.tum.in.www1.artemis.aop.logging;

import java.util.Arrays;
import java.util.Objects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
Expand All @@ -13,6 +14,7 @@
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;

import de.tum.in.www1.artemis.exception.localvc.LocalVCAuthException;
import de.tum.in.www1.artemis.service.connectors.vcs.AbstractVersionControlService;
import tech.jhipster.config.JHipsterConstants;

Expand Down Expand Up @@ -62,6 +64,17 @@ public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
return;
}

if (e instanceof LocalVCAuthException) {
if (Objects.equals(e.getMessage(), "No authorization header provided")) {
// ignore, this is a common case and does not need to be logged
return;
}
else if (e.getMessage() != null && e.getMessage().startsWith("The username has to be")) {
// ignore, this is a common case and does not need to be logged
return;
}
}

if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
log.error("Exception in {}.{}() with cause = '{}' and exception = '{}'", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(),
e.getCause() != null ? e.getCause() : "NULL", e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ public final class Constants {

public static final String INFO_SSH_KEYS_URL_DETAIL = "sshKeysURL";

public static final String INFO_VERSION_CONTROL_ACCESS_TOKEN_DETAIL = "versionControlAccessToken";
public static final String INFO_VERSION_CONTROL_ACCESS_TOKEN_DETAIL = "useVersionControlAccessToken";
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

public static final String INFO_SHOW_CLONE_URL_WITHOUT_TOKEN = "showCloneUrlWithoutToken";

public static final String REGISTRATION_ENABLED = "registrationEnabled";

Expand Down
JohannesStoehr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.tum.in.www1.artemis.domain.participation;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import de.tum.in.www1.artemis.domain.DomainObject;
import de.tum.in.www1.artemis.domain.User;

/**
* A ParticipationVcsAccessToken.
*/
@Entity
@Table(name = "participation_vcs_access_token", uniqueConstraints = { @UniqueConstraint(columnNames = { "user_id", "participation_id" }) })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

public class ParticipationVCSAccessToken extends DomainObject {

@ManyToOne
private User user;

@ManyToOne
private Participation participation;

@Column(name = "vcs_access_token", length = 50)
private String vcsAccessToken;
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

public void setUser(User user) {
this.user = user;
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

public User getUser() {
return user;
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

public void setParticipation(Participation participation) {
this.participation = participation;
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

public Participation getParticipation() {
return participation;
}

public String getVcsAccessToken() {
return vcsAccessToken;
}

public void setVcsAccessToken(String vcsAccessToken) {
this.vcsAccessToken = vcsAccessToken;
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public LocalVCAuthException() {
public LocalVCAuthException(Throwable cause) {
super(cause);
}

public LocalVCAuthException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public LocalVCOperationException() {
public LocalVCOperationException(Throwable cause) {
super(cause);
}

public LocalVCOperationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.tum.in.www1.artemis.repository;

import static de.tum.in.www1.artemis.config.Constants.PROFILE_CORE;

import java.util.Optional;

import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import de.tum.in.www1.artemis.domain.participation.ParticipationVCSAccessToken;
import de.tum.in.www1.artemis.repository.base.ArtemisJpaRepository;

@Profile(PROFILE_CORE)
@Repository
public interface ParticipationVCSAccessTokenRepository extends ArtemisJpaRepository<ParticipationVCSAccessToken, Long> {

/**
* Delete all participation vcs access token that belong to the given participation
*
* @param participationId the id of the participation where the tokens should be deleted
*/
@Transactional // ok because of delete
@Modifying
void deleteByParticipationId(long participationId);

/**
* Delete all tokens of a user
*
* @param userId The id of the user
*/
@Transactional // ok because of delete
@Modifying
void deleteAllByUserId(long userId);

@Query("""
SELECT DISTINCT p
FROM ParticipationVCSAccessToken p
LEFT JOIN FETCH p.participation
LEFT JOIN FETCH p.user
WHERE p.user.id = :userId AND p.participation.id = :participationId
""")
Optional<ParticipationVCSAccessToken> findByUserIdAndParticipationId(@Param("userId") long userId, @Param("participationId") long participationId);

default ParticipationVCSAccessToken findByUserIdAndParticipationIdOrElseThrow(long userId, long participationId) {
return getValueElseThrow(findByUserIdAndParticipationId(userId, participationId));
}

default void findByUserIdAndParticipationIdAndThrowIfExists(long userId, long participationId) {
findByUserIdAndParticipationId(userId, participationId).ifPresent(token -> {
throw new IllegalStateException();
});
}
}
dfuchss marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Optional<ProgrammingExerciseStudentParticipation> findByIdWithAllResultsAndRelat

Optional<ProgrammingExerciseStudentParticipation> findByExerciseIdAndStudentLogin(long exerciseId, String username);

List<ProgrammingExerciseStudentParticipation> findAllByExerciseIdAndStudentLogin(long exerciseId, String username);

default ProgrammingExerciseStudentParticipation findByExerciseIdAndStudentLoginOrThrow(long exerciseId, String username) {
return getValueElseThrow(findByExerciseIdAndStudentLogin(exerciseId, username));
}
Expand Down Expand Up @@ -113,6 +115,16 @@ Optional<ProgrammingExerciseStudentParticipation> findWithEagerStudentsByExercis
Optional<ProgrammingExerciseStudentParticipation> findWithSubmissionsAndEagerStudentsByExerciseIdAndTeamShortName(@Param("exerciseId") long exerciseId,
@Param("teamShortName") String teamShortName);

@Query("""
SELECT DISTINCT participation
FROM ProgrammingExerciseStudentParticipation participation
LEFT JOIN FETCH participation.team team
LEFT JOIN FETCH team.students student
WHERE participation.exercise.id = :exerciseId
AND student.id = :studentId
""")
Optional<ProgrammingExerciseStudentParticipation> findTeamParticipationByExerciseIdAndStudentId(@Param("exerciseId") long exerciseId, @Param("studentId") long studentId);

List<ProgrammingExerciseStudentParticipation> findByExerciseId(long exerciseId);

@EntityGraph(type = LOAD, attributePaths = { "submissions", "team.students" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class ParticipationService {

private final ProfileService profileService;

private final ParticipationVcsAccessTokenService participationVCSAccessTokenService;

private final CompetencyProgressService competencyProgressService;

public ParticipationService(GitService gitService, Optional<ContinuousIntegrationService> continuousIntegrationService, Optional<VersionControlService> versionControlService,
Expand All @@ -109,7 +111,8 @@ public ParticipationService(GitService gitService, Optional<ContinuousIntegratio
SubmissionRepository submissionRepository, TeamRepository teamRepository, UriService uriService, ResultService resultService,
CoverageReportRepository coverageReportRepository, BuildLogStatisticsEntryRepository buildLogStatisticsEntryRepository,
ParticipantScoreRepository participantScoreRepository, StudentScoreRepository studentScoreRepository, TeamScoreRepository teamScoreRepository,
Optional<SharedQueueManagementService> localCISharedBuildJobQueueService, ProfileService profileService, CompetencyProgressService competencyProgressService) {
Optional<SharedQueueManagementService> localCISharedBuildJobQueueService, ProfileService profileService,
ParticipationVcsAccessTokenService participationVCSAccessTokenService, CompetencyProgressService competencyProgressService) {
this.gitService = gitService;
this.continuousIntegrationService = continuousIntegrationService;
this.versionControlService = versionControlService;
Expand All @@ -129,6 +132,7 @@ public ParticipationService(GitService gitService, Optional<ContinuousIntegratio
this.teamScoreRepository = teamScoreRepository;
this.localCISharedBuildJobQueueService = localCISharedBuildJobQueueService;
this.profileService = profileService;
this.participationVCSAccessTokenService = participationVCSAccessTokenService;
this.competencyProgressService = competencyProgressService;
}

Expand Down Expand Up @@ -229,11 +233,18 @@ private StudentParticipation createNewParticipationWithInitializationDate(Exerci
participation.setInitializationState(InitializationState.UNINITIALIZED);
participation.setExercise(exercise);
participation.setParticipant(participant);

// StartedDate is used to link a Participation to a test exam exercise
if (initializationDate != null) {
participation.setInitializationDate(initializationDate);
}
return studentParticipationRepository.saveAndFlush(participation);
participation = studentParticipationRepository.saveAndFlush(participation);

if (exercise instanceof ProgrammingExercise && participant instanceof User user && profileService.isLocalVcsActive()) {
participationVCSAccessTokenService.createParticipationVCSAccessToken(user, participation);
}

return participation;
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -326,6 +337,9 @@ public StudentParticipation startPracticeMode(Exercise exercise, Participant par
participation.setParticipant(participant);
participation.setPracticeMode(true);
participation = studentParticipationRepository.saveAndFlush(participation);
if (participant instanceof User user) {
participationVCSAccessTokenService.createParticipationVCSAccessToken(user, participation);
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
}
else {
// make sure participation and exercise are connected
Expand Down Expand Up @@ -810,6 +824,8 @@ public void delete(long participationId, boolean deleteBuildPlan, boolean delete
}
// delete local repository cache
gitService.deleteLocalRepository(repositoryUri);

participationVCSAccessTokenService.deleteByParticipationId(participationId);
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
}

// If local CI is active, remove all queued jobs for participation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package de.tum.in.www1.artemis.service;

import static de.tum.in.www1.artemis.config.Constants.PROFILE_CORE;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import de.tum.in.www1.artemis.domain.User;
import de.tum.in.www1.artemis.domain.participation.ParticipationVCSAccessToken;
import de.tum.in.www1.artemis.domain.participation.StudentParticipation;
import de.tum.in.www1.artemis.repository.ParticipationVCSAccessTokenRepository;
import de.tum.in.www1.artemis.repository.ProgrammingExerciseStudentParticipationRepository;
import de.tum.in.www1.artemis.service.connectors.localvc.LocalVCPersonalAccessTokenManagementService;

@Profile(PROFILE_CORE)
@Service
public class ParticipationVcsAccessTokenService {

private static final Logger log = LoggerFactory.getLogger(ParticipationVcsAccessTokenService.class);

private final ParticipationVCSAccessTokenRepository participationVcsAccessTokenRepository;

private final ProgrammingExerciseStudentParticipationRepository programmingExerciseStudentParticipationRepository;

public ParticipationVcsAccessTokenService(ParticipationVCSAccessTokenRepository participationVCSAccessTokenRepository,
ProgrammingExerciseStudentParticipationRepository programmingExerciseStudentParticipationRepository) {
this.participationVcsAccessTokenRepository = participationVCSAccessTokenRepository;
this.programmingExerciseStudentParticipationRepository = programmingExerciseStudentParticipationRepository;
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a vcs access token for a User,Participation pair and stores it in the database
*
* @param user the user which is owner of the token
* @param participation the participation which belongs to the token
* @return the newly created ParticipationVCSAccessToken
*/
public ParticipationVCSAccessToken createParticipationVCSAccessToken(User user, StudentParticipation participation) {
ParticipationVCSAccessToken participationVCSAccessToken = new ParticipationVCSAccessToken();
participationVCSAccessToken.setUser(user);
participationVCSAccessToken.setParticipation(participation);
participationVCSAccessToken.setVcsAccessToken(LocalVCPersonalAccessTokenManagementService.generateSecureVCSAccessToken());
return participationVcsAccessTokenRepository.save(participationVCSAccessToken);
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieves the participationVCSAccessToken for a User,Participation pair if it exists
*
* @param userId the user's id which is owner of the token
* @param participationId the participation's id which the token belongs to
* @return an Optional participationVCSAccessToken,
*/
public ParticipationVCSAccessToken findByUserIdAndParticipationIdOrElseThrow(long userId, long participationId) {
return participationVcsAccessTokenRepository.findByUserIdAndParticipationIdOrElseThrow(userId, participationId);
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

/**
* Checks if the participationVCSAccessToken for a User,Participation pair exists, and creates a new one if not
*
* @param user the user's id which is owner of the token
* @param participationId the participation's id which the token belongs to
* @return an Optional participationVCSAccessToken,
*/
public ParticipationVCSAccessToken createVcsAccessTokenForUserAndParticipationIdOrElseThrow(User user, long participationId) {
participationVcsAccessTokenRepository.findByUserIdAndParticipationIdAndThrowIfExists(user.getId(), participationId);
var participation = programmingExerciseStudentParticipationRepository.findByIdElseThrow(participationId);
return createParticipationVCSAccessToken(user, participation);
}
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved

/**
* Deletes the token connected to a participation
*
* @param participationId the participation id for which the token should get deleted
*/
public void deleteByParticipationId(long participationId) {
participationVcsAccessTokenRepository.deleteByParticipationId(participationId);
}

/**
* Deletes all participationVcsAccessTokens of a user
*
* @param userId The user's id
*/
public void deleteAllByUserId(long userId) {
participationVcsAccessTokenRepository.deleteAllByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class GitLabPersonalAccessTokenManagementService extends VcsTokenManageme
/**
* The config parameter for enabling VCS access tokens.
*/
@Value("${artemis.version-control.version-control-access-token:#{false}}")
private Boolean versionControlAccessToken;
@Value("${artemis.version-control.use-version-control-access-token:#{false}}")
SimonEntholzer marked this conversation as resolved.
Show resolved Hide resolved
private boolean useVersionControlAccessToken;

public GitLabPersonalAccessTokenManagementService(UserRepository userRepository, GitLabApi gitlabApi, @Qualifier("gitlabRestTemplate") RestTemplate restTemplate) {
this.userRepository = userRepository;
Expand All @@ -72,7 +72,7 @@ public GitLabPersonalAccessTokenManagementService(UserRepository userRepository,
*/
@Override
public void createAccessToken(User user, Duration lifetime) {
if (versionControlAccessToken) {
if (useVersionControlAccessToken) {
if (user.getVcsAccessToken() != null) {
throw new IllegalArgumentException("User already has an access token");
}
Expand Down Expand Up @@ -115,7 +115,7 @@ private void savePersonalAccessTokenOfUser(ImpersonationToken token, User user)
*/
@Override
public void renewAccessToken(User user, Duration newLifetime) {
if (versionControlAccessToken) {
if (useVersionControlAccessToken) {
if (user.getVcsAccessToken() == null) {
throw new IllegalArgumentException("User has no VCS access token to be renewed");
}
Expand Down
Loading
Loading