-
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.
#525 [release] v1.1.0
- Loading branch information
Showing
115 changed files
with
1,139 additions
and
406 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
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
36 changes: 36 additions & 0 deletions
36
module-api/src/main/java/com/mile/common/auth/JwtTokenUpdater.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,36 @@ | ||
package com.mile.common.auth; | ||
|
||
import com.mile.writername.service.vo.WriterNameInfo; | ||
import com.mile.jwt.service.TokenService; | ||
import com.mile.writername.domain.MoimRole; | ||
import com.mile.writername.service.WriterNameRetriever; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtTokenUpdater { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final TokenService tokenService; | ||
private final WriterNameRetriever writerNameRetriever; | ||
|
||
public String setAccessToken(final Long userId, final Long moimId, final Long writerNameId, final MoimRole moimRole) { | ||
|
||
Map<Long, WriterNameInfo> moimRoleMap = writerNameRetriever.getJoinedRoleFromUserId(userId); | ||
|
||
tokenService.deleteRefreshToken(userId); | ||
|
||
moimRoleMap.put(moimId, WriterNameInfo.of(writerNameId, moimRole)); | ||
|
||
String newAccessToken = jwtTokenProvider.issueAccessToken(userId, moimRoleMap); | ||
String newRefreshToken = jwtTokenProvider.issueRefreshToken(userId, moimRoleMap); | ||
|
||
tokenService.saveRefreshToken(userId, newRefreshToken); | ||
|
||
return newAccessToken; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
module-api/src/main/java/com/mile/common/auth/annotation/UserAuthAnnotation.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,14 @@ | ||
package com.mile.common.auth.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
@Documented | ||
public @interface UserAuthAnnotation { | ||
UserAuthenticationType value() default UserAuthenticationType.USER; | ||
} |
7 changes: 7 additions & 0 deletions
7
module-api/src/main/java/com/mile/common/auth/annotation/UserAuthenticationType.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,7 @@ | ||
package com.mile.common.auth.annotation; | ||
|
||
public enum UserAuthenticationType { | ||
OWNER, | ||
WRITER_NAME, | ||
USER | ||
} |
13 changes: 13 additions & 0 deletions
13
module-api/src/main/java/com/mile/common/auth/dto/AccessTokenDto.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,13 @@ | ||
package com.mile.common.auth.dto; | ||
|
||
public record AccessTokenDto<T>( | ||
String accessToken, | ||
T response | ||
) { | ||
public static <T> AccessTokenDto<T> of(final T data, final String accessToken) { | ||
return new AccessTokenDto<>(accessToken, data); | ||
} | ||
public static AccessTokenDto of(final String accessToken) { | ||
return new AccessTokenDto(accessToken, null); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.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,96 @@ | ||
package com.mile.common.interceptor; | ||
|
||
import com.mile.common.auth.JwtTokenProvider; | ||
import com.mile.common.auth.annotation.UserAuthAnnotation; | ||
import com.mile.common.utils.thread.WriterNameContextUtil; | ||
import com.mile.common.utils.SecureUrlUtil; | ||
import com.mile.exception.message.ErrorMessage; | ||
import com.mile.exception.model.ForbiddenException; | ||
import com.mile.exception.model.UnauthorizedException; | ||
import com.mile.writername.domain.MoimRole; | ||
import com.mile.writername.service.vo.WriterNameInfo; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.springframework.web.servlet.HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MoimAuthInterceptor implements HandlerInterceptor { | ||
|
||
private static final String MOIM_ID = "moimId"; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final SecureUrlUtil secureUrlUtil; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (handler instanceof ResourceHttpRequestHandler || Objects.equals(request.getMethod(), "OPTIONS")) { | ||
return true; | ||
} | ||
HandlerMethod method = (HandlerMethod) handler; | ||
|
||
UserAuthAnnotation annotation = method.getMethodAnnotation(UserAuthAnnotation.class); | ||
|
||
if (annotation != null) { | ||
final String userToken = getUserTokenFromHeader(request); | ||
|
||
final HashMap<Long, WriterNameInfo> roleFromUser = jwtTokenProvider.getJoinedRoleFromHeader(userToken); | ||
final Map<String, String> pathVariables = (Map<String, String>) request.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE); | ||
|
||
return authenticateUserFromMap(annotation, roleFromUser, pathVariables); | ||
} | ||
return true; | ||
} | ||
|
||
private String getUserTokenFromHeader(final HttpServletRequest request) { | ||
final String userToken = request.getHeader("Authorization"); | ||
|
||
if (userToken == null) { | ||
throw new UnauthorizedException(ErrorMessage.UN_LOGIN_EXCEPTION); | ||
} | ||
|
||
return userToken; | ||
} | ||
|
||
private boolean authenticateUserFromMap(final UserAuthAnnotation annotation, | ||
final HashMap<Long, WriterNameInfo> userRoles, | ||
final Map<String, String> pathVariables) { | ||
switch (annotation.value()) { | ||
case OWNER -> { | ||
final Long requestMoimId = secureUrlUtil.decodeUrl(pathVariables.get(MOIM_ID)); | ||
if (!userRoles.containsKey(requestMoimId) || !userRoles.get(requestMoimId).moimRole().equals(MoimRole.OWNER)) { | ||
throw new ForbiddenException(ErrorMessage.MOIM_OWNER_AUTHENTICATION_ERROR); | ||
} | ||
WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId()); | ||
return true; | ||
} | ||
case WRITER_NAME -> { | ||
final Long requestMoimId = secureUrlUtil.decodeUrl(pathVariables.get(MOIM_ID)); | ||
if (!userRoles.containsKey(requestMoimId)) { | ||
throw new ForbiddenException(ErrorMessage.USER_MOIM_AUTHENTICATE_ERROR); | ||
} | ||
WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId()); | ||
return true; | ||
} | ||
case USER -> { | ||
WriterNameContextUtil.setMoimWriterNameMapContext(userRoles); | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { | ||
WriterNameContextUtil.clear(); | ||
} | ||
} |
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
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
Oops, something went wrong.