diff --git a/src/main/java/com/penguineering/gartenplus/auth/GithubOidcUserService.java b/src/main/java/com/penguineering/gartenplus/auth/GithubOidcUserService.java index 09a2af1..b3b4bd9 100644 --- a/src/main/java/com/penguineering/gartenplus/auth/GithubOidcUserService.java +++ b/src/main/java/com/penguineering/gartenplus/auth/GithubOidcUserService.java @@ -70,7 +70,7 @@ private UserDTO createUser(OAuth2User origin) { final String origin_id = Optional.ofNullable(origin.getAttribute("id")) .map(Object::toString) .orElseThrow(() -> new IllegalArgumentException("id attribute is missing")); - final String origin_name = origin.getAttribute("name"); + final String origin_name = determineDisplayName(origin); final String origin_email = origin.getAttribute("email"); final String origin_avatar_url = origin.getAttribute("avatar_url"); @@ -92,7 +92,7 @@ private UserDTO createUser(OAuth2User origin) { } private UserEntity updateUserFromOIDC(OAuth2User origin, UserEntity user) { - final String origin_name = origin.getAttribute("name"); + final String origin_name = determineDisplayName(origin); final String origin_email = origin.getAttribute("email"); final String origin_avatar_url = origin.getAttribute("avatar_url"); @@ -105,4 +105,11 @@ private UserEntity updateUserFromOIDC(OAuth2User origin, UserEntity user) { return userEntityService.save(user); } + + private String determineDisplayName(OAuth2User origin) { + return Optional.ofNullable(origin.getAttribute("name")) + .or(() -> Optional.ofNullable(origin.getAttribute("login"))) + .map(String::valueOf) + .orElseThrow(() -> new IllegalArgumentException("Name and login attribute are missing")); + } } diff --git a/src/main/java/com/penguineering/gartenplus/auth/user/UserDTO.java b/src/main/java/com/penguineering/gartenplus/auth/user/UserDTO.java index 5024c24..c29bc2e 100644 --- a/src/main/java/com/penguineering/gartenplus/auth/user/UserDTO.java +++ b/src/main/java/com/penguineering/gartenplus/auth/user/UserDTO.java @@ -17,10 +17,6 @@ public record UserDTO( @JsonProperty("email") String email, @JsonProperty("avatar_url") URI avatarUrl) { - public UserDTO { - Objects.requireNonNull(displayName, "displayName must not be null"); - } - @Override public String toString() { return displayName + "(" + id + ")";