Compare commits

..

No commits in common. "develop" and "manual-email-check" have entirely different histories.

9 changed files with 19 additions and 54 deletions

@ -1,12 +0,0 @@
services:
oauth2:
build:
context: https://gitea.dsv.su.se/DMC/oauth2-authorization-server.git#20cd09737d4c57bc1ee8098637cbad1a618bf49e
ports:
- '51337:8080'
environment:
- CLIENT_ID=seshat
- CLIENT_SECRET=n0tS3cr3t
- CLIENT_REDIRECT_URI=http://localhost:8181/login/oauth2/code/seshat
- CLIENT_SCOPES=openid email profile

@ -11,8 +11,17 @@ services:
volumes:
- mariadb_data:/var/lib/mysql
include:
- compose-oauth.yaml
oauth2:
build:
context: https://github.com/dsv-su/toker.git
dockerfile: embedded.Dockerfile
ports:
- '51337:8080'
environment:
- CLIENT_ID=seshat
- CLIENT_SECRET=n0tS3cr3t
- CLIENT_REDIRECT_URI=http://localhost:8181/login/oauth2/code/seshat
volumes:
mariadb_data:

@ -10,12 +10,9 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -186,11 +183,6 @@ public class FileController {
return "redirect:/files/manage";
}
@ModelAttribute("displayName")
public String getDisplayName(@AuthenticationPrincipal OAuth2User oauth2User) {
return oauth2User.getAttribute("name");
}
private static List<FileMetadata> getFileUploadStatuses(List<FileMetadata> uploaded) {
return uploaded.stream()
.filter(file -> file.getJobStatus() != null)

@ -25,7 +25,7 @@ public class AppUser {
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)

@ -12,7 +12,6 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Objects;
@Service
public class CustomOAuth2loginSuccessHandler implements AuthenticationSuccessHandler {
@ -33,13 +32,11 @@ public class CustomOAuth2loginSuccessHandler implements AuthenticationSuccessHan
String username = oAuth2User.getName();
// If the user does not have an email, set it to "no-email". We will not send any eamil notifications to this user.
String email = Objects.requireNonNullElse(oAuth2User.getAttribute("email"), "no-email");
String email = oAuth2User.getAttribute("mail") != null ? oAuth2User.getAttribute("mail") : "no-email";
if(!userService.existsByUsername(username)) {
userService.registerUser(username, email);
} else {
userService.updateEmail(username, email);
}
response.sendRedirect(redirectUrl);
}

@ -27,18 +27,6 @@ public class UserService {
.orElseThrow(() -> new IllegalArgumentException("User not found"));
}
public void updateEmail(String username, String newEmail) {
AppUser user = appUserRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
if(newEmail.equalsIgnoreCase("no-email")) {
return;
}
user.setEmail(newEmail);
appUserRepository.save(user);
}
public boolean existsByUsername(String username) {
return appUserRepository.existsByUsername(username);
}

@ -30,11 +30,12 @@ spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
# OAuth2 properties, remember if you change the registration.provider the provider properties must be updated
spring.security.oauth2.client.provider.docker.issuer-uri=http://localhost:51337
spring.security.oauth2.client.provider.docker.authorization-uri=http://localhost:51337/authorize
spring.security.oauth2.client.provider.docker.token-uri=http://localhost:51337/exchange
spring.security.oauth2.client.provider.docker.user-info-uri=http://localhost:51337/introspect
spring.security.oauth2.client.provider.docker.user-name-attribute=sub
spring.security.oauth2.client.registration.seshat.client-id=seshat
spring.security.oauth2.client.registration.seshat.client-secret=n0tS3cr3t
spring.security.oauth2.client.registration.seshat.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.seshat.provider=docker
spring.security.oauth2.client.registration.seshat.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
spring.security.oauth2.client.registration.seshat.scope=openid,profile,email
spring.security.oauth2.client.registration.seshat.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}

@ -17,7 +17,7 @@
<a class="user-menu text-white text-decoration-none dropdown-toggle" href="#" id="userMenu" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle"></i>
<span th:text="${displayName}">Username</span>
<span th:text="${#authentication.getName()}">Username</span>
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userMenu">
<li><a class="dropdown-item" th:href="@{/logout}">Logout</a></li>

@ -3,21 +3,11 @@ package se.su.dsv.seshat;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.io.File;
@Testcontainers
@SpringBootTest
class SeshatApplicationTests {
@ServiceConnection
private static org.testcontainers.containers.MariaDBContainer<?> dbContainer = new org.testcontainers.containers.MariaDBContainer<>("mariadb:10.11");
@Container
private static ComposeContainer oauth2Container = new ComposeContainer(new File("compose-oauth.yaml"));
@Test
void contextLoads() {
}