Return NEEDS_ORG when no org exists
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
This commit is contained in:
@@ -32,7 +32,12 @@ class Auth(
|
||||
request: HttpServletRequest
|
||||
): AuthResponse {
|
||||
logger.info("Auth verify hit, principalPresent={}", principal != null)
|
||||
return buildAuthResponse(principal ?: resolvePrincipalFromHeader(request))
|
||||
val resolved = principal ?: resolvePrincipalFromHeader(request)
|
||||
return if (resolved == null) {
|
||||
AuthResponse(status = "NEEDS_ORG")
|
||||
} else {
|
||||
buildAuthResponse(resolved)
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
@@ -40,13 +45,15 @@ class Auth(
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
request: HttpServletRequest
|
||||
): AuthResponse {
|
||||
return buildAuthResponse(principal ?: resolvePrincipalFromHeader(request))
|
||||
val resolved = principal ?: resolvePrincipalFromHeader(request)
|
||||
return if (resolved == null) {
|
||||
AuthResponse(status = "NEEDS_ORG")
|
||||
} else {
|
||||
buildAuthResponse(resolved)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildAuthResponse(principal: MyPrincipal?): AuthResponse {
|
||||
if (principal == null) {
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing principal")
|
||||
}
|
||||
private fun buildAuthResponse(principal: MyPrincipal): AuthResponse {
|
||||
val user = appUserRepo.findById(principal.userId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found")
|
||||
}
|
||||
@@ -58,6 +65,7 @@ class Auth(
|
||||
)
|
||||
}
|
||||
return AuthResponse(
|
||||
status = "OK",
|
||||
user = UserResponse(
|
||||
id = user.id!!,
|
||||
orgId = user.org.id!!,
|
||||
@@ -70,7 +78,7 @@ class Auth(
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolvePrincipalFromHeader(request: HttpServletRequest): MyPrincipal {
|
||||
private fun resolvePrincipalFromHeader(request: HttpServletRequest): MyPrincipal? {
|
||||
val header = request.getHeader("Authorization") ?: throw ResponseStatusException(
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
"Missing Authorization token"
|
||||
@@ -86,12 +94,14 @@ class Auth(
|
||||
logger.warn("Auth verify failed: {}", ex.message)
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid token")
|
||||
}
|
||||
val user = appUserRepo.findByFirebaseUid(decoded.uid) ?: run {
|
||||
val user = appUserRepo.findByFirebaseUid(decoded.uid)
|
||||
if (user == null) {
|
||||
val orgs = organizationRepo.findAll()
|
||||
if (orgs.size != 1) {
|
||||
logger.warn("Auth verify user not found for uid={}, orgCount={}", decoded.uid, orgs.size)
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found")
|
||||
if (orgs.isEmpty()) {
|
||||
logger.warn("Auth verify user not found for uid={}, orgCount=0", decoded.uid)
|
||||
return null
|
||||
}
|
||||
if (orgs.size == 1) {
|
||||
val org = orgs.first()
|
||||
val phone = decoded.claims["phone_number"] as? String
|
||||
val name = decoded.claims["name"] as? String
|
||||
@@ -104,7 +114,13 @@ class Auth(
|
||||
)
|
||||
)
|
||||
logger.warn("Auth verify auto-created user uid={}, userId={}, orgId={}", decoded.uid, created.id, org.id)
|
||||
created
|
||||
return MyPrincipal(
|
||||
userId = created.id ?: throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "User id missing"),
|
||||
firebaseUid = decoded.uid
|
||||
)
|
||||
}
|
||||
logger.warn("Auth verify user not found for uid={}, orgCount={}", decoded.uid, orgs.size)
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found")
|
||||
}
|
||||
logger.warn("Auth verify resolved uid={}, userId={}", decoded.uid, user.id)
|
||||
return MyPrincipal(
|
||||
@@ -115,6 +131,7 @@ class Auth(
|
||||
}
|
||||
|
||||
data class AuthResponse(
|
||||
val user: UserResponse,
|
||||
val properties: List<PropertyUserResponse>
|
||||
val status: String,
|
||||
val user: UserResponse? = null,
|
||||
val properties: List<PropertyUserResponse> = emptyList()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user