Add JWT auth
This commit is contained in:
parent
66e84d9913
commit
942fb1a1ec
@ -16,6 +16,7 @@
|
||||
<mariadb_version>2.6.0</mariadb_version>
|
||||
<kodein_version>6.5.4</kodein_version>
|
||||
<flyway_version>6.3.3</flyway_version>
|
||||
<javajwt_version>3.10.2</javajwt_version>
|
||||
|
||||
<kotlin.code.style>official</kotlin.code.style>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -79,6 +80,11 @@
|
||||
<artifactId>ktor-jackson</artifactId>
|
||||
<version>${ktor_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ktor</groupId>
|
||||
<artifactId>ktor-auth-jwt</artifactId>
|
||||
<version>${ktor_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ktor</groupId>
|
||||
<artifactId>ktor-client-core</artifactId>
|
||||
|
||||
@ -15,3 +15,8 @@ database {
|
||||
user = "test"
|
||||
password = "test"
|
||||
}
|
||||
|
||||
jwt {
|
||||
secret = "thisisasecret"
|
||||
secret = ${?SECRET}
|
||||
}
|
||||
21
api/src/auth/AuthenticationModule.kt
Normal file
21
api/src/auth/AuthenticationModule.kt
Normal file
@ -0,0 +1,21 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import be.vandewalleh.kodein
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.install
|
||||
import io.ktor.auth.Authentication
|
||||
import io.ktor.auth.UserIdPrincipal
|
||||
import io.ktor.auth.jwt.jwt
|
||||
import org.kodein.di.generic.instance
|
||||
|
||||
fun Application.authenticationModule() {
|
||||
install(Authentication) {
|
||||
jwt {
|
||||
val simpleJwt: SimpleJWT by kodein.instance()
|
||||
verifier(simpleJwt.verifier)
|
||||
validate {
|
||||
UserIdPrincipal(it.payload.getClaim("name").asString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
api/src/auth/SimpleJWT.kt
Normal file
19
api/src/auth/SimpleJWT.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.JWTVerifier
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import java.util.*
|
||||
|
||||
class SimpleJWT(secret: String) {
|
||||
private val validityInMs = 36_000_00 * 1
|
||||
private val algorithm = Algorithm.HMAC256(secret)
|
||||
|
||||
val verifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
fun sign(name: String): String = JWT.create()
|
||||
.withClaim("name", name)
|
||||
.withExpiresAt(getExpiration())
|
||||
.sign(algorithm)
|
||||
|
||||
private fun getExpiration() = Date(System.currentTimeMillis() + validityInMs)
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package be.vandewalleh.features
|
||||
|
||||
import be.vandewalleh.auth.SimpleJWT
|
||||
import io.ktor.application.Application
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
@ -27,7 +28,11 @@ fun Application.configurationFeature() {
|
||||
setPassword(password)
|
||||
}
|
||||
}
|
||||
|
||||
val simpleJwt = SimpleJWT(environment.config.property("jwt.secret").getString())
|
||||
|
||||
configurationModule = Kodein.Module("Configuration") {
|
||||
bind<DataSource>() with instance(dataSource)
|
||||
bind<SimpleJWT>() with instance(simpleJwt)
|
||||
}
|
||||
}
|
||||
@ -1,14 +1,18 @@
|
||||
package be.vandewalleh.features
|
||||
|
||||
import be.vandewalleh.auth.authenticationModule
|
||||
import io.ktor.application.Application
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.KodeinAware
|
||||
|
||||
fun Application.features() {
|
||||
// must be first to be loaded
|
||||
configurationFeature()
|
||||
|
||||
locationFeature()
|
||||
corsFeature()
|
||||
contentNegotiationFeature()
|
||||
configurationFeature()
|
||||
authenticationModule()
|
||||
}
|
||||
|
||||
abstract class Feature(override val kodein: Kodein) : KodeinAware {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user