forked from maiora/backend-api
Compare commits
No commits in common. "488cab4ac7bfdb411af9f67228f3e9a528f42dd4" and "8e417f02f26067de9d1de4fe0911da8cb6ad3fae" have entirely different histories.
488cab4ac7
...
8e417f02f2
@ -17,12 +17,10 @@ fun main() {
|
|||||||
|
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
val config = ApplicationConfig("application.conf")
|
val config = ApplicationConfig("application.conf")
|
||||||
val dbUrl = config.property("ktor.database.url").getString()
|
val dbUrl = config.property("ktor.database.url").getString();
|
||||||
val username = config.property("ktor.database.username").getString()
|
val username = config.property("ktor.database.username").getString();
|
||||||
val password = config.property("ktor.database.password").getString()
|
val password = config.property("ktor.database.password").getString();
|
||||||
val secret = config.property("ktor.jwt.secret").getString()
|
|
||||||
configureDatabases(dbUrl, username, password)
|
configureDatabases(dbUrl, username, password)
|
||||||
configureSecurity(secret)
|
|
||||||
configureRouting(dbUrl, username, password)
|
configureRouting(dbUrl, username, password)
|
||||||
configureSerialization()
|
configureSerialization()
|
||||||
|
|
||||||
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user