Coverage Summary for Class: SecurityKt (dev.suresh.plugins)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
SecurityKt |
0%
(0/6)
|
|
0%
(0/14)
|
0%
(0/89)
|
SecurityKt$configureSecurity$$inlined$provideDelegate$1 |
0%
(0/1)
|
|
SecurityKt$configureSecurity$1$1$1 |
0%
(0/1)
|
0%
(0/4)
|
0%
(0/3)
|
0%
(0/27)
|
SecurityKt$configureSecurity$1$2$1 |
0%
(0/1)
|
0%
(0/2)
|
0%
(0/3)
|
0%
(0/19)
|
SecurityKt$configureSecurity$1$3$2 |
0%
(0/1)
|
0%
(0/2)
|
0%
(0/1)
|
0%
(0/19)
|
SecurityKt$configureSecurity$1$3$3 |
0%
(0/1)
|
|
0%
(0/1)
|
0%
(0/21)
|
Total |
0%
(0/11)
|
0%
(0/8)
|
0%
(0/22)
|
0%
(0/175)
|
package dev.suresh.plugins
import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import dev.suresh.di.Auth
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.auth.jwt.JWTPrincipal
import io.ktor.server.auth.jwt.jwt
import io.ktor.server.plugins.di.dependencies
fun Application.configureSecurity() {
val auth: Auth by dependencies
authentication {
basic("admin") {
realm = "App Admin"
validate { credentials ->
when (credentials.name == auth.admin.user && credentials.password == auth.admin.password) {
true -> UserIdPrincipal(credentials.name)
else -> null
}
}
}
bearer("auth-bearer") {
realm = "Ktor App"
authenticate { tokenCredential ->
when (tokenCredential.token) {
auth.api.bearerToken -> UserIdPrincipal(auth.api.user)
else -> null
}
}
}
jwt("auth-jwt") {
realm = "Ktor App"
verifier {
JWT.require(Algorithm.HMAC256(auth.api.bearerToken)).withIssuer(BuildConfig.name).build()
}
validate { cred -> cred.payload.subject?.let { JWTPrincipal(cred.payload) } }
challenge { defaultScheme, realm ->
call.respondError(Unauthorized, "Token is not valid or has expired")
}
}
}
}