Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 인증 로직 수정 #206

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController

//TODO: 정식 릴리즈 후에는 dev 서버에서만 가능하게
@RestController
@RequestMapping("/api")
@RequestMapping("/api/v1")
class DevAuthController(
private val authenticationManager: AuthenticationManager,
private val userRepository: UserRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.wafflestudio.csereal.core.news.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.common.mockauth.CustomPrincipal
import com.wafflestudio.csereal.core.news.dto.NewsDto
import com.wafflestudio.csereal.core.news.dto.NewsSearchResponse
import com.wafflestudio.csereal.core.news.service.NewsService
Expand All @@ -13,7 +15,7 @@ import org.hibernate.validator.constraints.Length
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -30,10 +32,16 @@ class NewsController(
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false) pageNum: Int?,
@RequestParam(required = false, defaultValue = "10") pageSize: Int,
@AuthenticationPrincipal oidcUser: OidcUser?
authentication: Authentication?
): ResponseEntity<NewsSearchResponse> {
val isStaff = oidcUser?.let {
val username = it.idToken.getClaim<String>("username")
val principal = authentication?.principal

val isStaff = principal?.let {
val username = when (principal) {
is OidcUser -> principal.idToken.getClaim("username")
is CustomPrincipal -> principal.userEntity.username
else -> throw CserealException.Csereal401("Unsupported principal type")
}
Comment on lines +37 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 로직이 공통되서 나타나는거 같은데 나중에 리펙터링 할때 따로 묶어서 빼는것도 좋을듯!

val user = userRepository.findByUsername(username)
user?.role == Role.ROLE_STAFF
} ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.wafflestudio.csereal.core.notice.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.common.mockauth.CustomPrincipal
import com.wafflestudio.csereal.core.notice.dto.*
import com.wafflestudio.csereal.core.notice.service.NoticeService
import com.wafflestudio.csereal.core.user.database.Role
Expand All @@ -12,7 +14,7 @@ import org.hibernate.validator.constraints.Length
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -29,10 +31,16 @@ class NoticeController(
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false) pageNum: Int?,
@RequestParam(required = false, defaultValue = "20") pageSize: Int,
@AuthenticationPrincipal oidcUser: OidcUser?
authentication: Authentication?
): ResponseEntity<NoticeSearchResponse> {
val isStaff = oidcUser?.let {
val username = it.idToken.getClaim<String>("username")
val principal = authentication?.principal

val isStaff = principal?.let {
val username = when (principal) {
is OidcUser -> principal.idToken.getClaim("username")
is CustomPrincipal -> principal.userEntity.username
else -> throw CserealException.Csereal401("Unsupported principal type")
}
val user = userRepository.findByUsername(username)
user?.role == Role.ROLE_STAFF
} ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.wafflestudio.csereal.core.reservation.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.aop.AuthenticatedForReservation
import com.wafflestudio.csereal.common.mockauth.CustomPrincipal
import com.wafflestudio.csereal.core.reservation.dto.ReservationDto
import com.wafflestudio.csereal.core.reservation.dto.ReserveRequest
import com.wafflestudio.csereal.core.reservation.dto.SimpleReservationDto
import com.wafflestudio.csereal.core.reservation.service.ReservationService
import com.wafflestudio.csereal.core.user.database.Role
import com.wafflestudio.csereal.core.user.database.UserRepository
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
Expand Down Expand Up @@ -54,10 +56,16 @@ class ReservationController(
@GetMapping("/{reservationId}")
fun getReservation(
@PathVariable reservationId: Long,
@AuthenticationPrincipal oidcUser: OidcUser?
authentication: Authentication?
): ResponseEntity<ReservationDto> {
val isStaff = oidcUser?.let {
val username = it.idToken.getClaim<String>("username")
val principal = authentication?.principal

val isStaff = principal?.let {
val username = when (principal) {
is OidcUser -> principal.idToken.getClaim("username")
is CustomPrincipal -> principal.userEntity.username
else -> throw CserealException.Csereal401("Unsupported principal type")
}
val user = userRepository.findByUsername(username)
user?.role == Role.ROLE_STAFF
} ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.wafflestudio.csereal.core.seminar.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.common.mockauth.CustomPrincipal
import com.wafflestudio.csereal.core.seminar.dto.SeminarDto
import com.wafflestudio.csereal.core.seminar.dto.SeminarSearchResponse
import com.wafflestudio.csereal.core.seminar.service.SeminarService
Expand All @@ -9,7 +11,7 @@ import com.wafflestudio.csereal.core.user.database.UserRepository
import jakarta.validation.Valid
import org.springframework.data.domain.PageRequest
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -25,10 +27,16 @@ class SeminarController(
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false) pageNum: Int?,
@RequestParam(required = false, defaultValue = "10") pageSize: Int,
@AuthenticationPrincipal oidcUser: OidcUser?
authentication: Authentication?
): ResponseEntity<SeminarSearchResponse> {
val isStaff = oidcUser?.let {
val username = it.idToken.getClaim<String>("username")
val principal = authentication?.principal

val isStaff = principal?.let {
val username = when (principal) {
is OidcUser -> principal.idToken.getClaim("username")
is CustomPrincipal -> principal.userEntity.username
else -> throw CserealException.Csereal401("Unsupported principal type")
}
val user = userRepository.findByUsername(username)
user?.role == Role.ROLE_STAFF
} ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.wafflestudio.csereal.core.user.api

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.mockauth.CustomPrincipal
import com.wafflestudio.csereal.core.user.dto.StaffAuthResponse
import com.wafflestudio.csereal.core.user.service.UserService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
Expand All @@ -17,15 +18,19 @@ class UserController(
) {

@GetMapping("/is-staff")
fun isStaff(@AuthenticationPrincipal oidcUser: OidcUser?): ResponseEntity<StaffAuthResponse> {
if (oidcUser == null) {
throw CserealException.Csereal401("로그인이 필요합니다.")
fun isStaff(authentication: Authentication?): ResponseEntity<StaffAuthResponse> {
val principal = authentication?.principal ?: throw CserealException.Csereal401("로그인이 필요합니다.")

val username = when (principal) {
is OidcUser -> principal.idToken.getClaim("username")
is CustomPrincipal -> principal.userEntity.username
else -> throw CserealException.Csereal401("Unsupported principal type")
}
val username = oidcUser.idToken.getClaim<String>("username")
if (userService.checkStaffAuth(username)) {
return ResponseEntity.ok(StaffAuthResponse(true))

return if (userService.checkStaffAuth(username)) {
ResponseEntity.ok(StaffAuthResponse(true))
} else {
return ResponseEntity.ok(StaffAuthResponse(false))
ResponseEntity.ok(StaffAuthResponse(false))
}
}
}
Loading