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

Lines changed: 11 additions & 13 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 18 deletions
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

Lines changed: 6 additions & 18 deletions
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+
)
Lines changed: 5 additions & 10 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 9 deletions
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)