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

cafe-08-end 셋팅 #8

Open
wants to merge 1 commit into
base: lecture/cafe-08-start
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/main/kotlin/com/example/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.example.config.configureDependencyInjection
import com.example.config.configureHttp
import com.example.config.configureLogging
import com.example.config.configureRouting
import com.example.config.configureSecurity
import com.example.config.configureSerialization
import com.example.config.configureSession
import io.ktor.server.application.*
Expand All @@ -18,6 +19,7 @@ fun Application.module() {
configureDependencyInjection()
configureHttp()
configureSession()
configureSecurity()
configureSerialization()
configureRouting()
configureLogging()
Expand Down
64 changes: 34 additions & 30 deletions src/main/kotlin/com/example/config/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.example.shared.dto.OrderDto
import com.example.shared.dto.UserDto
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.http.content.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
Expand All @@ -20,41 +22,39 @@ fun Application.configureRouting() {
val loginService by inject<LoginService>()

routing {
get("/") {
call.respondText("Hello World!")
}

route("/api") {
get("/menus") {
val list = menuService.findAll()
call.respond(list)
}
post("/orders") {
val request = call.receive<OrderDto.CreateRequest>()
val selectedMenu = menuService.getMenu(request.menuId)
val order = OrderDto.DisplayResponse(
orderCode = "ordercode1",
menuName = selectedMenu.name,
customerName = "홍길동",
price = selectedMenu.price,
status = CafeOrderStatus.READY,
orderedAt = LocalDateTime.now(),
id = 1
)
call.respond(order)
}
get("/orders/{orderCode}") {
val orderCode = call.parameters["orderCode"]!!
val order = OrderDto.DisplayResponse(
orderCode = orderCode,
menuName = "아이스라떼",
customerName = "홍길동",
price = 1000,
status = CafeOrderStatus.READY,
orderedAt = LocalDateTime.now(),
id = 1
)
call.respond(order)
authenticate(AuthenticatedUser.CUSTOMER_REQUIRED) {
post("/orders") {
val request = call.receive<OrderDto.CreateRequest>()
val selectedMenu = menuService.getMenu(request.menuId)
val order = OrderDto.DisplayResponse(
orderCode = "ordercode1",
menuName = selectedMenu.name,
customerName = "홍길동",
price = selectedMenu.price,
status = CafeOrderStatus.READY,
orderedAt = LocalDateTime.now(),
id = 1
)
call.respond(order.orderCode)
}
get("/orders/{orderCode}") {
val orderCode = call.parameters["orderCode"]!!
val order = OrderDto.DisplayResponse(
orderCode = orderCode,
menuName = "아이스라떼",
customerName = "홍길동",
price = 1000,
status = CafeOrderStatus.READY,
orderedAt = LocalDateTime.now(),
id = 1
)
call.respond(order)
}
}

get("/me") {
Expand All @@ -78,5 +78,9 @@ fun Application.configureRouting() {
call.respond(HttpStatusCode.OK)
}
}

singlePageApplication {
react("frontend")
}
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/example/config/Security.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.config

import com.example.shared.CafeUserRole
import com.example.config.AuthenticatedUser.Companion.CUSTOMER_REQUIRED
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.response.*

fun Application.configureSecurity() {
install(Authentication) {
session<AuthenticatedUser>(CUSTOMER_REQUIRED) {
validate { session: AuthenticatedUser ->
session.takeIf { it.userRoles.contains(CafeUserRole.CUSTOMER) }
}
challenge {
call.respond(HttpStatusCode.Forbidden, "only for customer");
}
}
}
}

fun ApplicationCall.authenticatedUser(): AuthenticatedUser = authentication.principal<AuthenticatedUser>()!!
4 changes: 3 additions & 1 deletion src/main/kotlin/com/example/config/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.config

import com.example.shared.CafeUserRole
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.sessions.*
import kotlinx.serialization.Serializable

Expand All @@ -18,10 +19,11 @@ fun Application.configureSession() {
data class AuthenticatedUser(
val userId: Long,
val userRoles: List<CafeUserRole>
) {
): Principal {
companion object {
fun none() = AuthenticatedUser(0, listOf())

const val SESSION_NAME = "CU_SESSION_ID"
const val CUSTOMER_REQUIRED = "customer-required"
}
}