@@ -3,6 +3,7 @@ package org.gitanimals.core.auth
33import jakarta.servlet.http.HttpServletRequest
44import org.gitanimals.core.AUTHORIZATION_EXCEPTION
55import org.gitanimals.core.AuthorizationException
6+ import org.gitanimals.core.filter.MDCFilter.Companion.USER_ENTRY_POINT
67import org.gitanimals.core.filter.MDCFilter.Companion.USER_ID
78import org.slf4j.LoggerFactory
89import org.slf4j.MDC
@@ -29,7 +30,7 @@ class InternalAuth(
2930 SecretKeySpec (decodedKey, " AES" )
3031 }
3132
32- fun getUserId (throwOnFailure : () -> Unit = throwCannotGetUserId ): Long {
33+ fun getUserId (throwOnFailure : () -> Unit = throwCannotGetUserInfo ): Long {
3334 val userId = findUserId()
3435
3536 if (userId == null ) {
@@ -80,6 +81,36 @@ class InternalAuth(
8081 return userId
8182 }
8283
84+ fun getUserEntryPoint (throwOnFailure : () -> Unit = throwCannotGetUserInfo): String {
85+ val entryPoint = findUserEntryPoint()
86+
87+ if (entryPoint == null ) {
88+ throwOnFailure.invoke()
89+ }
90+
91+ return entryPoint ? : throw AUTHORIZATION_EXCEPTION
92+ }
93+
94+ fun findUserEntryPoint (): String? {
95+ val entryPointInMdc = runCatching {
96+ MDC .get(USER_ENTRY_POINT )
97+ }.getOrNull()
98+
99+ if (entryPointInMdc != null ) {
100+ return entryPointInMdc
101+ }
102+
103+ httpServletRequest.getHeader(INTERNAL_ENTRY_POINT_KEY )?.let {
104+ return it
105+ }
106+
107+ val token: String = httpServletRequest.getHeader(HttpHeaders .AUTHORIZATION ) ? : return null
108+
109+ return runCatching {
110+ internalAuthClient.getUserByToken(token).entryPoint
111+ }.getOrNull()
112+ }
113+
83114 private fun decrypt (iv : ByteArray , secret : ByteArray ): Long {
84115 val cipher = Cipher .getInstance(" AES/GCM/NoPadding" )
85116 val spec = GCMParameterSpec (128 , iv)
@@ -108,8 +139,9 @@ class InternalAuth(
108139 companion object {
109140 const val INTERNAL_AUTH_IV_KEY = " Internal-Auth-Iv"
110141 const val INTERNAL_AUTH_SECRET_KEY = " Internal-Auth-Secret"
142+ const val INTERNAL_ENTRY_POINT_KEY = " Internal-Entry-Point"
111143
112- private val throwCannotGetUserId : () -> Unit = {
144+ private val throwCannotGetUserInfo : () -> Unit = {
113145 throw AUTHORIZATION_EXCEPTION
114146 }
115147 }
0 commit comments