Compare commits

..

No commits in common. "488cab4ac7bfdb411af9f67228f3e9a528f42dd4" and "8e417f02f26067de9d1de4fe0911da8cb6ad3fae" have entirely different histories.

2 changed files with 3 additions and 40 deletions

View File

@ -17,12 +17,10 @@ fun main() {
fun Application.module() {
val config = ApplicationConfig("application.conf")
val dbUrl = config.property("ktor.database.url").getString()
val username = config.property("ktor.database.username").getString()
val password = config.property("ktor.database.password").getString()
val secret = config.property("ktor.jwt.secret").getString()
val dbUrl = config.property("ktor.database.url").getString();
val username = config.property("ktor.database.username").getString();
val password = config.property("ktor.database.password").getString();
configureDatabases(dbUrl, username, password)
configureSecurity(secret)
configureRouting(dbUrl, username, password)
configureSerialization()

View File

@ -1,35 +0,0 @@
package eu.maiora.plugins
import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
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.*
fun Application.configureSecurity(secret: String) {
install(Authentication) {
jwt ("auth-jwt"){
verifier(
JWT
.require(Algorithm.HMAC256(secret))
.build())
validate { credential ->
val expiresAt = credential.payload.expiresAt?.time ?: 0
val now = System.currentTimeMillis()
// Verifica se il token ? scaduto
if (expiresAt >= now) {
JWTPrincipal(credential.payload)
}
else {
null
}
}
challenge { defaultScheme, realm ->
call.respond(HttpStatusCode.Unauthorized, "Token non valido o scaduto")
}
}
}
}