Add JWT auth
This commit is contained in:
parent
66e84d9913
commit
942fb1a1ec
@ -16,6 +16,7 @@
|
|||||||
<mariadb_version>2.6.0</mariadb_version>
|
<mariadb_version>2.6.0</mariadb_version>
|
||||||
<kodein_version>6.5.4</kodein_version>
|
<kodein_version>6.5.4</kodein_version>
|
||||||
<flyway_version>6.3.3</flyway_version>
|
<flyway_version>6.3.3</flyway_version>
|
||||||
|
<javajwt_version>3.10.2</javajwt_version>
|
||||||
|
|
||||||
<kotlin.code.style>official</kotlin.code.style>
|
<kotlin.code.style>official</kotlin.code.style>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@ -79,6 +80,11 @@
|
|||||||
<artifactId>ktor-jackson</artifactId>
|
<artifactId>ktor-jackson</artifactId>
|
||||||
<version>${ktor_version}</version>
|
<version>${ktor_version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ktor</groupId>
|
||||||
|
<artifactId>ktor-auth-jwt</artifactId>
|
||||||
|
<version>${ktor_version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ktor</groupId>
|
<groupId>io.ktor</groupId>
|
||||||
<artifactId>ktor-client-core</artifactId>
|
<artifactId>ktor-client-core</artifactId>
|
||||||
|
|||||||
@ -14,4 +14,9 @@ database {
|
|||||||
name = "Notes"
|
name = "Notes"
|
||||||
user = "test"
|
user = "test"
|
||||||
password = "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
|
package be.vandewalleh.features
|
||||||
|
|
||||||
|
import be.vandewalleh.auth.SimpleJWT
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
import org.kodein.di.generic.bind
|
import org.kodein.di.generic.bind
|
||||||
@ -27,7 +28,11 @@ fun Application.configurationFeature() {
|
|||||||
setPassword(password)
|
setPassword(password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val simpleJwt = SimpleJWT(environment.config.property("jwt.secret").getString())
|
||||||
|
|
||||||
configurationModule = Kodein.Module("Configuration") {
|
configurationModule = Kodein.Module("Configuration") {
|
||||||
bind<DataSource>() with instance(dataSource)
|
bind<DataSource>() with instance(dataSource)
|
||||||
|
bind<SimpleJWT>() with instance(simpleJwt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,14 +1,18 @@
|
|||||||
package be.vandewalleh.features
|
package be.vandewalleh.features
|
||||||
|
|
||||||
|
import be.vandewalleh.auth.authenticationModule
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
import org.kodein.di.KodeinAware
|
import org.kodein.di.KodeinAware
|
||||||
|
|
||||||
fun Application.features() {
|
fun Application.features() {
|
||||||
|
// must be first to be loaded
|
||||||
|
configurationFeature()
|
||||||
|
|
||||||
locationFeature()
|
locationFeature()
|
||||||
corsFeature()
|
corsFeature()
|
||||||
contentNegotiationFeature()
|
contentNegotiationFeature()
|
||||||
configurationFeature()
|
authenticationModule()
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class Feature(override val kodein: Kodein) : KodeinAware {
|
abstract class Feature(override val kodein: Kodein) : KodeinAware {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user