Skip to content

Commit

Permalink
release: 0.3.9 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Apr 28, 2024
2 parents 10baf1c + 1607cb4 commit fd231cf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gitanimals.render.controller

import org.gitanimals.render.app.UserFacade
import org.gitanimals.render.controller.request.AddPersonaRequest
import org.gitanimals.render.controller.response.ErrorResponse
import org.gitanimals.render.controller.response.PersonaResponse
import org.gitanimals.render.controller.response.UserResponse
import org.gitanimals.render.domain.UserService
Expand Down Expand Up @@ -48,4 +49,8 @@ class PersonaController(

return PersonaResponse(persona.id, persona.type, persona.level)
}

@ExceptionHandler(IllegalArgumentException::class)
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse =
ErrorResponse.from(exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class InternalApiInterceptor(
response: HttpServletResponse,
handler: Any,
): Boolean {
val ip = extractIp(request)
return whiteIps.contains(ip)
return whiteIps.contains(extractIp(request))
}

private fun extractIp(request: HttpServletRequest): String {
Expand All @@ -24,22 +23,22 @@ class InternalApiInterceptor(
"X-Real-IP", "X-RealIP", "REMOTE_ADDR"
)

var ip: String = request.getHeader("X-Forwarded-For")
var ip: String? = request.getHeader("X-Forwarded-For")

for (header in headers) {
if (ip.isEmpty() || "unknown".equals(ip, ignoreCase = true)) {
if (ip.isNullOrEmpty() || "unknown".equals(ip, ignoreCase = true)) {
ip = request.getHeader(header)
}
}

if (ip.isEmpty() || "unknown".equals(ip, ignoreCase = true)) {
if (ip.isNullOrEmpty() || "unknown".equals(ip, ignoreCase = true)) {
ip = request.remoteAddr
}

if (ip == "0:0:0:0:0:0:0:1") {
ip = "127.0.0.1"
}

return ip
return ip ?: throw IllegalStateException("Cannot extract ip")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gitanimals.render.controller.response

data class ErrorResponse(
val message: String,
) {

companion object {
fun from(exception: Exception): ErrorResponse =
ErrorResponse(exception.message ?: exception.localizedMessage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class RestIdentityApi : IdentityApi {
.uri("/users")
.header(HttpHeaders.AUTHORIZATION, token)
.exchange { _, response ->
response.bodyTo(IdentityApi.UserResponse::class.java)
?: throw IllegalArgumentException("Unauthorized user")
runCatching {
response.bodyTo(IdentityApi.UserResponse::class.java)
}.getOrElse {
throw IllegalArgumentException("Unauthorized user")
}
}
}
}

0 comments on commit fd231cf

Please sign in to comment.