-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80a24bd
commit fb80e0b
Showing
34 changed files
with
520 additions
and
431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
server/application-server/src/main/java/de/tum/in/www1/hephaestus/SecurityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package de.tum.in.www1.hephaestus; | ||
|
||
import java.util.Optional; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
/** | ||
* Utility class for Spring Security. | ||
*/ | ||
public final class SecurityUtils { | ||
|
||
private SecurityUtils() {} | ||
|
||
/** | ||
* Get the login of the current user. | ||
* | ||
* @return the login of the current user. | ||
*/ | ||
public static Optional<String> getCurrentUserLogin() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
return Optional.empty(); | ||
} | ||
if (authentication.getPrincipal() instanceof Jwt) { | ||
Jwt jwt = (Jwt) authentication.getPrincipal(); | ||
return Optional.ofNullable(jwt.getClaimAsString("preferred_username")); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rver/src/main/java/de/tum/in/www1/hephaestus/core/exception/AccessForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package de.tum.in.www1.hephaestus.core.exception; | ||
|
||
import java.io.Serial; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
/** | ||
* Generic unchecked exception for access forbidden (i.e. 403) errors. | ||
*/ | ||
@ResponseStatus(HttpStatus.FORBIDDEN) | ||
public class AccessForbiddenException extends RuntimeException { | ||
|
||
public static final String NOT_ALLOWED = "You are not allowed to access this resource"; | ||
|
||
@Serial | ||
private static final long serialVersionUID = 1L; | ||
|
||
public AccessForbiddenException() { | ||
super(NOT_ALLOWED); | ||
} | ||
|
||
public AccessForbiddenException(String message) { | ||
super(message); | ||
} | ||
|
||
public AccessForbiddenException(Throwable cause) { | ||
super(NOT_ALLOWED, cause); | ||
} | ||
|
||
public AccessForbiddenException(String entityType, long entityId) { | ||
super("You are not allowed to access the " + entityType + " with id " + entityId); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...erver/src/main/java/de/tum/in/www1/hephaestus/core/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.tum.in.www1.hephaestus.core.exception; | ||
|
||
import java.io.Serial; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public class EntityNotFoundException extends RuntimeException { | ||
|
||
@Serial | ||
private static final long serialVersionUID = 1L; | ||
|
||
public EntityNotFoundException() { | ||
super(); | ||
} | ||
|
||
public EntityNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public EntityNotFoundException(String entityName, Long entityId) { | ||
super(entityName + " with id: \"" + entityId + "\" does not exist"); | ||
} | ||
|
||
public EntityNotFoundException(String entityName, String entityIdentifier) { | ||
super(entityName + " with identifier: \"" + entityIdentifier + "\" does not exist"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,6 @@ public class Message { | |
private Session session; | ||
|
||
public enum MessageSender { | ||
LLM, USER | ||
MENTOR, USER | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 22 additions & 8 deletions
30
...tion-server/src/main/java/de/tum/in/www1/hephaestus/mentor/session/SessionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
package de.tum.in.www1.hephaestus.mentor.session; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository; | ||
|
||
@RestController | ||
@RequestMapping("/mentor/session") | ||
@RequestMapping("/mentor/sessions") | ||
public class SessionController { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private SessionService sessionService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<SessionDTO>> getSessions(@RequestParam String login) { | ||
List<SessionDTO> sessions = sessionService.findAllSessionsByUser(login); | ||
public ResponseEntity<List<SessionDTO>> getAllSessions() { | ||
var user = userRepository.getCurrentUser(); | ||
if (user.isEmpty()) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
|
||
List<SessionDTO> sessions = sessionService.findAllSessionsByUser(user.get()); | ||
return ResponseEntity.ok(sessions); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<SessionDTO> createSession(@RequestBody String login) { | ||
SessionDTO session = sessionService.createSession(login); | ||
public ResponseEntity<SessionDTO> createNewSession() { | ||
var user = userRepository.getCurrentUser(); | ||
if (user.isEmpty()) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
|
||
SessionDTO session = sessionService.createSession(user.get()); | ||
return ResponseEntity.ok(session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...tion-server/src/main/java/de/tum/in/www1/hephaestus/mentor/session/SessionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package de.tum.in.www1.hephaestus.mentor.session; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import de.tum.in.www1.hephaestus.gitprovider.user.User; | ||
|
||
@Repository | ||
public interface SessionRepository extends JpaRepository<Session, Long> { | ||
|
||
List<Session> findByUserLogin(String login); | ||
List<Session> findByUser(User user); | ||
} |
Oops, something went wrong.