parent
95351233b9
commit
ec69c88e14
@ -0,0 +1,24 @@
|
||||
package allin.ext
|
||||
|
||||
import allin.dto.UserDTO
|
||||
import allin.entities.UsersEntity
|
||||
import allin.model.ApiMessage
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.util.pipeline.*
|
||||
|
||||
suspend fun PipelineContext<*, ApplicationCall>.hasToken(content: suspend (principal: JWTPrincipal) -> Unit) =
|
||||
call.principal<JWTPrincipal>()?.let { content(it) } ?: call.respond(HttpStatusCode.Unauthorized)
|
||||
|
||||
suspend fun PipelineContext<*, ApplicationCall>.verifyUserFromToken(
|
||||
principal: JWTPrincipal,
|
||||
content: suspend (user: UserDTO, password: String) -> Unit
|
||||
) {
|
||||
val username = principal.payload.getClaim("username").asString()
|
||||
val userPassword = UsersEntity.getUserByUsernameAndPassword(username)
|
||||
userPassword.first?.let { content(it, userPassword.second ?: "") }
|
||||
?: call.respond(HttpStatusCode.NotFound, ApiMessage.TokenUserNotFound)
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package allin.model
|
||||
|
||||
object ApiMessage {
|
||||
const val Welcome = "Welcome on AllIn's API !"
|
||||
const val TokenUserNotFound = "User not found with the valid token !"
|
||||
const val UserNotFound = "User not found."
|
||||
const val BetNotFound = "Bet not found."
|
||||
const val BetAlreadyExist = "Bet already exists."
|
||||
const val IncorrectLoginPassword = "Login and/or password incorrect."
|
||||
const val UserAlreadyExist = "Mail and/or username already exists."
|
||||
const val InvalidMail = "Invalid mail."
|
||||
const val ParticipationNotFound = "Participation not found."
|
||||
const val NotEnoughCoins = "Not enough coins."
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package allin.routing
|
||||
|
||||
import allin.ext.hasToken
|
||||
import allin.ext.verifyUserFromToken
|
||||
import allin.model.ApiMessage
|
||||
import allin.model.Participation
|
||||
import allin.model.ParticipationRequest
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import java.util.*
|
||||
|
||||
val participations = mutableListOf<Participation>()
|
||||
|
||||
fun Application.ParticipationRouter() {
|
||||
routing {
|
||||
authenticate {
|
||||
post("/participations/add") {
|
||||
hasToken { principal ->
|
||||
val participation = call.receive<ParticipationRequest>()
|
||||
verifyUserFromToken(principal) { user, _ ->
|
||||
if (user.nbCoins >= participation.stake) {
|
||||
participations.add(
|
||||
Participation(
|
||||
id = UUID.randomUUID().toString(),
|
||||
betId = participation.betId,
|
||||
username = user.username,
|
||||
answer = participation.answer,
|
||||
stake = participation.stake
|
||||
)
|
||||
)
|
||||
call.respond(HttpStatusCode.Created)
|
||||
} else {
|
||||
call.respond(HttpStatusCode.Forbidden, ApiMessage.NotEnoughCoins)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
delete("/participations/delete") {
|
||||
hasToken { principal ->
|
||||
val participationId = call.receive<String>()
|
||||
participations.find { it.id == participationId }?.let { participation ->
|
||||
verifyUserFromToken(principal) { user, _ ->
|
||||
// user.nbCoins += participation.stake
|
||||
participations.remove(participation)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
} ?: call.respond(HttpStatusCode.NotFound, ApiMessage.ParticipationNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,4 +0,0 @@
|
||||
secret="secret"
|
||||
issuer="http://0.0.0.0:8080/"
|
||||
audience="http://0.0.0.0:8080/"
|
||||
realm="Access to main page"
|
@ -1,12 +0,0 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="trace">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
<logger name="org.eclipse.jetty" level="INFO"/>
|
||||
<logger name="io.netty" level="INFO"/>
|
||||
</configuration>
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue