Returns ProfilePicture Image Url in UserResponse
This commit is contained in:
@@ -15,6 +15,11 @@ public class GithubOAuthMap implements OAuthMap {
|
||||
return oAuth2User.getAttribute("login");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProfilePicture() {
|
||||
return this.oAuth2User.getAttribute("avatar_url");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider getProvider() {
|
||||
return Provider.GITHUB;
|
||||
|
||||
@@ -8,11 +8,16 @@ import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||
@AllArgsConstructor
|
||||
public class GoogleOAuthMap implements OAuthMap {
|
||||
|
||||
private OAuth2User oauthUser;
|
||||
private OAuth2User oAuth2User;
|
||||
|
||||
@Override
|
||||
public String getPrincipal() {
|
||||
return this.oauthUser.getAttribute("given_name");
|
||||
return this.oAuth2User.getAttribute("given_name");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProfilePicture() {
|
||||
return this.oAuth2User.getAttribute("picture");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -20,5 +25,4 @@ public class GoogleOAuthMap implements OAuthMap {
|
||||
return Provider.GOOGLE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ public interface OAuthMap {
|
||||
|
||||
String getPrincipal();
|
||||
|
||||
String getProfilePicture();
|
||||
|
||||
Provider getProvider();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.hideyoshi.backendportfolio.base.security.oauth.mapper;
|
||||
|
||||
import com.hideyoshi.backendportfolio.base.user.entity.Provider;
|
||||
import lombok.Getter;
|
||||
|
||||
public enum OAuthMapEnum {
|
||||
public enum OAuthMapper {
|
||||
|
||||
GOOGLE(GoogleOAuthMap.class, Provider.GOOGLE),
|
||||
|
||||
@@ -10,9 +11,10 @@ public enum OAuthMapEnum {
|
||||
|
||||
private final Class oAuthMap;
|
||||
|
||||
@Getter
|
||||
private final Provider provider;
|
||||
|
||||
private OAuthMapEnum(Class oAuthMap, Provider provider) {
|
||||
private OAuthMapper(Class oAuthMap, Provider provider) {
|
||||
this.oAuthMap = oAuthMap;
|
||||
this.provider = provider;
|
||||
}
|
||||
@@ -21,12 +23,8 @@ public enum OAuthMapEnum {
|
||||
return oAuthMap;
|
||||
}
|
||||
|
||||
public Provider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
public static OAuthMapEnum byValue(String name) {
|
||||
for (OAuthMapEnum e : values()) {
|
||||
public static OAuthMapper byValue(String name) {
|
||||
for (OAuthMapper e : values()) {
|
||||
if (e.getProvider().getName().equals(name)) {
|
||||
return e;
|
||||
}
|
||||
@@ -6,12 +6,13 @@ import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.hideyoshi.backendportfolio.base.security.oauth.mapper.OAuthMap;
|
||||
import com.hideyoshi.backendportfolio.base.security.oauth.mapper.OAuthMapEnum;
|
||||
import com.hideyoshi.backendportfolio.base.security.oauth.mapper.OAuthMapper;
|
||||
import com.hideyoshi.backendportfolio.base.user.entity.Provider;
|
||||
import com.hideyoshi.backendportfolio.base.user.entity.Role;
|
||||
import com.hideyoshi.backendportfolio.base.user.model.TokenDTO;
|
||||
import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
|
||||
import com.hideyoshi.backendportfolio.base.user.service.UserService;
|
||||
import com.hideyoshi.backendportfolio.microservice.storageService.service.StorageService;
|
||||
import com.hideyoshi.backendportfolio.util.exception.BadRequestException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
@@ -56,6 +57,8 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final StorageService storageService;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("handlerExceptionResolver")
|
||||
private HandlerExceptionResolver resolver;
|
||||
@@ -172,11 +175,36 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
user.setProvider(Provider.LOCAL);
|
||||
|
||||
return this.generateUserWithTokens(
|
||||
UserDTO authenticatedUser = this.generateUserWithTokens(
|
||||
this.userService.saveUser(user),
|
||||
request
|
||||
);
|
||||
|
||||
authenticatedUser.setProfilePictureUrl(
|
||||
this.storageService.getFileUrl(authenticatedUser.getUsername(), "profile")
|
||||
.getPresignedUrl()
|
||||
);
|
||||
|
||||
return authenticatedUser;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loginUser(HttpServletRequest request, HttpServletResponse response, @Valid UserDTO user) throws IOException {
|
||||
|
||||
UserDTO authenticatedUser = this.generateUserWithTokens(
|
||||
user,
|
||||
request
|
||||
);
|
||||
|
||||
authenticatedUser.setProfilePictureUrl(
|
||||
this.storageService.getFileUrl(authenticatedUser.getUsername(), "profile")
|
||||
.getPresignedUrl()
|
||||
);
|
||||
|
||||
response.setContentType(APPLICATION_JSON_VALUE);
|
||||
new ObjectMapper()
|
||||
.writeValue(response.getOutputStream(), authenticatedUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,20 +232,6 @@ public class AuthServiceImpl implements AuthService {
|
||||
}
|
||||
|
||||
return this.generateUserWithTokens(user, request);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loginUser(HttpServletRequest request, HttpServletResponse response, @Valid UserDTO user) throws IOException {
|
||||
|
||||
UserDTO authenticatedUser = this.generateUserWithTokens(
|
||||
user,
|
||||
request
|
||||
);
|
||||
|
||||
response.setContentType(APPLICATION_JSON_VALUE);
|
||||
new ObjectMapper()
|
||||
.writeValue(response.getOutputStream(), authenticatedUser);
|
||||
}
|
||||
|
||||
public void loginOAuthUser(HttpServletRequest request,
|
||||
@@ -229,7 +243,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
OAuthMap oauthMap = null;
|
||||
try {
|
||||
oauthMap = (OAuthMap) OAuthMapEnum.byValue(clientId).getMap()
|
||||
oauthMap = (OAuthMap) OAuthMapper.byValue(clientId).getMap()
|
||||
.getDeclaredConstructor(OAuth2User.class).newInstance(oauthUser);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("No Such Provider");
|
||||
@@ -238,6 +252,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
UserDTO user = null;
|
||||
try {
|
||||
user = this.userService.getUser(oauthMap.getPrincipal());
|
||||
user.setProfilePictureUrl(oauthMap.getProfilePicture());
|
||||
} catch (BadRequestException e) {
|
||||
user = UserDTO.builder()
|
||||
.name(oauthUser.getAttribute("name"))
|
||||
@@ -245,6 +260,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
.email(oauthUser.getAttribute("email"))
|
||||
.roles(Arrays.asList(Role.USER))
|
||||
.provider(oauthMap.getProvider())
|
||||
.profilePictureUrl(oauthMap.getProfilePicture())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,14 +17,16 @@ public class SessionManagerServiceImpl implements SessionManagerService {
|
||||
@Override
|
||||
public UserDTO validateSession(HttpSession session) {
|
||||
|
||||
UserDTO sessionObjects = (UserDTO) session.getAttribute("user");
|
||||
UserDTO sessionObject = (UserDTO) session.getAttribute("user");
|
||||
|
||||
if (Objects.nonNull(sessionObjects)) {
|
||||
return this.userService.getUser(sessionObjects.getUsername())
|
||||
.toResponse(sessionObjects.getAccessToken(), sessionObjects.getRefreshToken());
|
||||
if (Objects.nonNull(sessionObject)) {
|
||||
sessionObject = sessionObject.toResponse(
|
||||
sessionObject.getAccessToken(),
|
||||
sessionObject.getRefreshToken()
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
return sessionObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -83,16 +83,6 @@ public class UserController {
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/profile-picture")
|
||||
@UserResourceGuard(accessType = UserResourceGuardEnum.USER)
|
||||
public StorageServiceDownloadResponse getProfilePicture() {
|
||||
UserDTO user = this.authService.getLoggedUser();
|
||||
return this.storageService.getFileUrl(
|
||||
user.getUsername(),
|
||||
"profile"
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/profile-picture/proccess")
|
||||
@UserResourceGuard(accessType = UserResourceGuardEnum.USER)
|
||||
public void processProfilePicture() {
|
||||
|
||||
@@ -53,6 +53,8 @@ public class UserDTO implements UserDetails {
|
||||
@Size(min=1)
|
||||
private List<Role> roles;
|
||||
|
||||
private String profilePictureUrl;
|
||||
|
||||
private TokenDTO accessToken;
|
||||
|
||||
private TokenDTO refreshToken;
|
||||
@@ -121,6 +123,7 @@ public class UserDTO implements UserDetails {
|
||||
.email(this.email)
|
||||
.username(this.username)
|
||||
.provider(this.provider)
|
||||
.profilePictureUrl(this.profilePictureUrl)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -132,6 +135,7 @@ public class UserDTO implements UserDetails {
|
||||
.username(this.username)
|
||||
.provider(this.provider)
|
||||
.roles(this.roles)
|
||||
.profilePictureUrl(this.profilePictureUrl)
|
||||
.accessToken(accessToken)
|
||||
.refreshToken(refreshToken)
|
||||
.build();
|
||||
|
||||
@@ -2,12 +2,9 @@ package com.hideyoshi.backendportfolio.microservice.storageService.model;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class StorageServiceDownloadResponse {
|
||||
|
||||
@@ -4,8 +4,10 @@ package com.hideyoshi.backendportfolio.microservice.storageService.model;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class StorageServiceUploadResponse {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user