Skip to content

Commit e887d6d

Browse files
authored
refactor : 리팩터링 (#128)
* refactor : auth class * refactor : util class * reafactor : id to non nullable * refactor : etcs * fix compile err * fix : add suppress
1 parent 0115db9 commit e887d6d

File tree

69 files changed

+160
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+160
-197
lines changed

src/main/kotlin/io/csbroker/apiserver/auth/AuthToken.kt

+11-13
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@ class AuthToken(
1818
private val key: Key,
1919
) {
2020
constructor(email: String, expiry: Date, key: Key, role: String? = null) : this("", key) {
21-
if (role != null) {
22-
token = createAuthToken(email, expiry, role)
21+
token = if (role != null) {
22+
createAuthToken(email, expiry, role)
2323
} else {
24-
token = createAuthToken(email, expiry)
24+
createAuthToken(email, expiry)
2525
}
2626
}
2727

2828
val tokenClaims: Claims?
2929
get() {
3030
try {
31-
return Jwts.parserBuilder()
32-
.setSigningKey(key)
33-
.build()
34-
.parseClaimsJws(token)
35-
.body
31+
return parseJwt()
3632
} catch (e: SecurityException) {
3733
log.error("Invalid JWT signature.")
3834
} catch (e: MalformedJwtException) {
@@ -52,11 +48,7 @@ class AuthToken(
5248
val expiredTokenClaims: Claims?
5349
get() {
5450
try {
55-
Jwts.parserBuilder()
56-
.setSigningKey(key)
57-
.build()
58-
.parseClaimsJws(token)
59-
.body
51+
parseJwt()
6052
} catch (e: ExpiredJwtException) {
6153
log.info("Expired JWT token.")
6254
return e.claims
@@ -77,6 +69,12 @@ class AuthToken(
7769
val isValid: Boolean
7870
get() = tokenClaims != null
7971

72+
private fun parseJwt(): Claims? = Jwts.parserBuilder()
73+
.setSigningKey(key)
74+
.build()
75+
.parseClaimsJws(token)
76+
.body
77+
8078
private fun createAuthToken(email: String, expiry: Date): String {
8179
return Jwts.builder()
8280
.setSubject(email)

src/main/kotlin/io/csbroker/apiserver/auth/AuthTokenProvider.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class AuthTokenProvider(
3030
.map(::SimpleGrantedAuthority)
3131
.toList()
3232
return UsernamePasswordAuthenticationToken(User(claims.subject, "", authorities), authToken, authorities)
33-
} else {
34-
throw UnAuthorizedException(ErrorCode.TOKEN_INVALID, "올바르지 않은 Token입니다.")
3533
}
34+
35+
throw UnAuthorizedException(ErrorCode.TOKEN_INVALID, "올바르지 않은 Token입니다.")
3636
}
3737
}

src/main/kotlin/io/csbroker/apiserver/auth/GithubOAuth2UserInfo.kt

+6-18
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ package io.csbroker.apiserver.auth
22

33
class GithubOAuth2UserInfo(
44
attributes: MutableMap<String, Any>,
5-
) : OAuth2UserInfo(attributes) {
6-
7-
override fun getId(): String {
8-
return (attributes["id"] as Int).toString()
9-
}
10-
11-
override fun getName(): String {
12-
return attributes["login"] as String
13-
}
14-
15-
override fun getEmail(): String {
16-
return attributes["email"] as String
17-
}
18-
19-
override fun getImageUrl(): String {
20-
return attributes["avatar_url"] as String
21-
}
22-
}
5+
) : OAuth2UserInfo(
6+
id = (attributes["id"] as Int).toString(),
7+
name = attributes["login"] as String,
8+
email = attributes["email"] as String,
9+
imageUrl = attributes["avatar_url"] as String,
10+
)

src/main/kotlin/io/csbroker/apiserver/auth/GoogleOAuth2UserInfo.kt

+6-18
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ package io.csbroker.apiserver.auth
22

33
class GoogleOAuth2UserInfo(
44
attributes: MutableMap<String, Any>,
5-
) : OAuth2UserInfo(attributes) {
6-
7-
override fun getId(): String {
8-
return attributes["sub"] as String
9-
}
10-
11-
override fun getName(): String {
12-
return attributes["name"] as String
13-
}
14-
15-
override fun getEmail(): String {
16-
return attributes["email"] as String
17-
}
18-
19-
override fun getImageUrl(): String {
20-
return attributes["picture"] as String
21-
}
22-
}
5+
) : OAuth2UserInfo(
6+
id = attributes["sub"] as String,
7+
name = attributes["name"] as String,
8+
email = attributes["email"] as String,
9+
imageUrl = attributes["picture"] as String,
10+
)
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package io.csbroker.apiserver.auth
22

33
abstract class OAuth2UserInfo(
4-
val attributes: MutableMap<String, Any>,
5-
) {
6-
abstract fun getId(): String
7-
8-
abstract fun getName(): String
9-
10-
abstract fun getEmail(): String
11-
12-
abstract fun getImageUrl(): String
13-
}
4+
val id: String,
5+
val name: String,
6+
val email: String,
7+
val imageUrl: String,
8+
)

src/main/kotlin/io/csbroker/apiserver/auth/OAuth2UserInfoFactory.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ class OAuth2UserInfoFactory {
88
return when (providerType) {
99
ProviderType.GOOGLE -> GoogleOAuth2UserInfo(attributes)
1010
ProviderType.GITHUB -> GithubOAuth2UserInfo(attributes)
11-
else -> throw OAuthProviderMissMatchException(
12-
"프로바이더 타입이 일치하지 않습니다. ${providerType.name}",
13-
)
11+
else -> throw OAuthProviderMissMatchException("프로바이더 타입이 일치하지 않습니다. ${providerType.name}")
1412
}
1513
}
1614
}

src/main/kotlin/io/csbroker/apiserver/common/filter/TokenAuthenticationFilter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TokenAuthenticationFilter(
1717
response: HttpServletResponse,
1818
filterChain: FilterChain,
1919
) {
20-
val tokenStr = getAccessToken(request)
20+
val tokenStr = request.getAccessToken()
2121

2222
tokenStr ?: run {
2323
filterChain.doFilter(request, response)

src/main/kotlin/io/csbroker/apiserver/common/handler/OAuth2AuthenticationSuccessHandler.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class OAuth2AuthenticationSuccessHandler(
8888
val user = authentication.principal as OidcUser
8989
val userInfo = OAuth2UserInfoFactory.getOauth2UserInfo(providerType, user.attributes)
9090

91-
return userRepository.findByEmailOrProviderId(userInfo.getEmail(), userInfo.getId())
91+
return userRepository.findByEmailOrProviderId(userInfo.email, userInfo.id)
9292
?: throw EntityNotFoundException(
93-
"유저를 찾을 수 없습니다. email = [${userInfo.getEmail()}], providerId = [${userInfo.getId()}] )",
93+
"유저를 찾을 수 없습니다. email = [${userInfo.email}], providerId = [${userInfo.id}] )",
9494
)
9595
}
9696

src/main/kotlin/io/csbroker/apiserver/common/util/CookieUtil.kt

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.csbroker.apiserver.common.util
22

3+
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest
34
import org.springframework.util.SerializationUtils
45
import java.util.Base64
56
import javax.servlet.http.Cookie
@@ -21,19 +22,17 @@ fun addCookie(response: HttpServletResponse, name: String, value: String, maxAge
2122
}
2223

2324
fun deleteCookie(request: HttpServletRequest, response: HttpServletResponse, name: String) {
24-
request.cookies?.let {
25-
it.find { cookie -> cookie.name == name }?.let { cookie ->
26-
cookie.value = ""
27-
cookie.path = "/"
28-
cookie.maxAge = 0
29-
response.addCookie(cookie)
30-
}
25+
request.cookies?.find { cookie -> cookie.name == name }?.let { cookie ->
26+
cookie.value = ""
27+
cookie.path = "/"
28+
cookie.maxAge = 0
29+
response.addCookie(cookie)
3130
}
3231
}
3332

34-
fun serialize(obj: Any): String {
33+
fun OAuth2AuthorizationRequest.serialize(): String {
3534
return Base64.getUrlEncoder()
36-
.encodeToString(SerializationUtils.serialize(obj))
35+
.encodeToString(SerializationUtils.serialize(this))
3736
}
3837

3938
fun <T> deserialize(cookie: Cookie, cls: Class<T>): T {

src/main/kotlin/io/csbroker/apiserver/common/util/HeaderUtil.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import javax.servlet.http.HttpServletRequest
55
const val HEADER_AUTHORIZATION = "Authorization"
66
const val TOKEN_PREFIX = "Bearer "
77

8-
fun getAccessToken(request: HttpServletRequest): String? {
9-
val headerValue = request.getHeader(HEADER_AUTHORIZATION)
8+
fun HttpServletRequest.getAccessToken(): String? {
9+
val headerValue = this.getHeader(HEADER_AUTHORIZATION)
1010
return if (headerValue?.startsWith(TOKEN_PREFIX) == true) headerValue.substring(TOKEN_PREFIX.length) else null
1111
}

src/main/kotlin/io/csbroker/apiserver/common/util/UuidUtil.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package io.csbroker.apiserver.common.util
33
import java.nio.ByteBuffer
44
import java.util.UUID
55

6-
fun uuidAsByte(uuid: UUID?): ByteArray? {
7-
uuid ?: return null
8-
6+
fun UUID.asByte(): ByteArray? {
97
val byteBufferWrapper = ByteBuffer.wrap(ByteArray(16))
10-
byteBufferWrapper.putLong(uuid.mostSignificantBits)
11-
byteBufferWrapper.putLong(uuid.leastSignificantBits)
8+
byteBufferWrapper.putLong(this.mostSignificantBits)
9+
byteBufferWrapper.putLong(this.leastSignificantBits)
1210
return byteBufferWrapper.array()
1311
}

src/main/kotlin/io/csbroker/apiserver/controller/v1/post/response/PostResponseDto.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ data class PostResponseDto(
1313
val comments: List<CommentResponseDto>,
1414
) {
1515
constructor(post: Post, likeCount: Long, isLiked: Boolean, comments: List<CommentResponseDto>) : this(
16-
id = post.id!!,
16+
id = post.id,
1717
content = post.content,
1818
username = post.user.username,
1919
likeCount = likeCount,
@@ -29,7 +29,7 @@ data class CommentResponseDto(
2929
val createdAt: LocalDateTime,
3030
) {
3131
constructor(comment: Comment) : this(
32-
id = comment.id!!,
32+
id = comment.id,
3333
content = comment.content,
3434
username = comment.user.username,
3535
createdAt = comment.createdAt!!,

src/main/kotlin/io/csbroker/apiserver/controller/v1/problem/ProblemController.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ class ProblemController(
3535
@RequestParam("page") page: Int,
3636
@RequestParam("size") size: Int,
3737
): ApiResponse<ProblemPageResponseDto> {
38-
var solvedBy: String? = null
39-
40-
if (isSolved != null) {
41-
solvedBy = getEmailFromSecurityContextHolder()
38+
val solvedBy = isSolved?.let {
39+
getEmailFromSecurityContextHolder()
4240
?: throw UnAuthorizedException(ErrorCode.FORBIDDEN, "사용자 권한이 없습니다.")
4341
}
44-
4542
val searchDto = ProblemSearchDto(tags, solvedBy, isSolved, query, type, isGradable, page, size)
4643
val foundProblems = commonProblemService.findProblems(searchDto)
4744

src/main/kotlin/io/csbroker/apiserver/controller/v2/problem/response/ShortProblemDetailResponseV2Dto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.csbroker.apiserver.controller.v2.problem.response
22

3-
class ShortProblemDetailResponseV2Dto(
3+
data class ShortProblemDetailResponseV2Dto(
44
val id: Long,
55
val title: String,
66
val tags: List<String>,

src/main/kotlin/io/csbroker/apiserver/dto/problem/grade/KeywordGradingRequestDto.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ data class KeywordGradingRequestDto(
2020
companion object {
2121
fun createKeywordGradingRequestDto(problem: LongProblem, answer: String): KeywordGradingRequestDto {
2222
return KeywordGradingRequestDto(
23-
problem.id!!,
23+
problem.id,
2424
answer,
2525
problem.gradingStandards.filter {
2626
it.type == GradingStandardType.KEYWORD
2727
}.map {
2828
GradingKeyword(
29-
it.id!!,
29+
it.id,
3030
it.content,
3131
)
3232
},

src/main/kotlin/io/csbroker/apiserver/dto/problem/grade/LongProblemGradingRequestToAiServerDto.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ data class LongProblemGradingRequestToAiServerDto(
2727
companion object {
2828
fun createGradingRequestDto(problem: LongProblem, answer: String): LongProblemGradingRequestToAiServerDto {
2929
return LongProblemGradingRequestToAiServerDto(
30-
problem.id!!,
30+
problem.id,
3131
answer,
3232
problem.gradingStandards.filter {
3333
it.type == GradingStandardType.KEYWORD
3434
}.map {
3535
GradingKeyword(
36-
it.id!!,
36+
it.id,
3737
it.content,
3838
)
3939
},
4040
problem.gradingStandards.filter {
4141
it.type == GradingStandardType.CONTENT
4242
}.map {
4343
GradingContent(
44-
it.id!!,
44+
it.id,
4545
it.content,
4646
)
4747
},

src/main/kotlin/io/csbroker/apiserver/dto/problem/longproblem/LongProblemGradingHistoryDto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ data class LongProblemGradingHistoryDto(
4141

4242
return LongProblemGradingHistoryDto(
4343
gradingHistoryId,
44-
problem.id!!,
44+
problem.id,
4545
problem.title,
4646
tags,
4747
problem.description,

src/main/kotlin/io/csbroker/apiserver/dto/problem/multiplechoiceproblem/MultipleChoiceProblemGradingHistoryDto.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ data class MultipleChoiceProblemGradingHistoryDto(
3030

3131
val choices = problem.choicesList.map {
3232
ChoiceResponseDto(
33-
it.id!!,
33+
it.id,
3434
it.content,
3535
)
3636
}
3737

3838
return MultipleChoiceProblemGradingHistoryDto(
3939
gradingHistoryId,
40-
problem.id!!,
40+
problem.id,
4141
problem.title,
4242
commonDetail.tags,
4343
problem.description,

src/main/kotlin/io/csbroker/apiserver/dto/problem/shortproblem/ShortProblemGradingHistoryDto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ data class ShortProblemGradingHistoryDto(
3131

3232
return ShortProblemGradingHistoryDto(
3333
gradingHistoryId,
34-
problem.id!!,
34+
problem.id,
3535
problem.title,
3636
commonDetail.tags,
3737
problem.description,

src/main/kotlin/io/csbroker/apiserver/dto/user/GradingStandardResponseDto.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ data class GradingStandardResponseDto(
1212
companion object {
1313
fun fromGradingStandard(gradingStandard: GradingStandard): GradingStandardResponseDto {
1414
return GradingStandardResponseDto(
15-
id = gradingStandard.id!!,
15+
id = gradingStandard.id,
1616
content = gradingStandard.content,
1717
score = gradingStandard.score,
1818
type = gradingStandard.type,

src/main/kotlin/io/csbroker/apiserver/dto/useranswer/UserAnswerResponseDto.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ data class UserAnswerResponseDto(
2727
.map { GradingStandardResponseDto.fromGradingStandard(it) }
2828

2929
val selectedGradingStandards = userAnswer.userAnswerGradingStandards.map {
30-
it.gradingStandard.id!!
30+
it.gradingStandard.id
3131
}
3232

3333
return UserAnswerResponseDto(
34-
userAnswer.id!!,
35-
userAnswer.problem.id!!,
34+
userAnswer.id,
35+
userAnswer.problem.id,
3636
userAnswer.problem.title,
3737
userAnswer.problem.description,
3838
userAnswer.answer,

src/main/kotlin/io/csbroker/apiserver/model/Challenge.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Challenge(
1616
@Id
1717
@GeneratedValue(strategy = GenerationType.IDENTITY)
1818
@Column(name = "challenge_id")
19-
val id: Long? = null,
19+
val id: Long = 0,
2020

2121
@ManyToOne(fetch = FetchType.LAZY)
2222
@JoinColumn(name = "problem_id")

src/main/kotlin/io/csbroker/apiserver/model/ChatResult.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ChatResult(
1313
@Id
1414
@GeneratedValue(strategy = GenerationType.IDENTITY)
1515
@Column(name = "chat_result_id")
16-
val id: Long? = null,
16+
val id: Long = 0,
1717

1818
// 연관관계로 엮을 수 있으나, 단순 데이터 저장용이기 때문에.. 굳이 엮지 않겠음.
1919
@Column(name = "email")

0 commit comments

Comments
 (0)