Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ dependencies {

implementation(libs.ktor.swagger.ui)
implementation(libs.swagger.parser)
testImplementation(libs.mockk)
testImplementation(libs.coroutine.test)
testImplementation(libs.ktor.client.mock)
}
tasks.create("stage") {
dependsOn("installDist")
Expand Down
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ commons-io-version = "2.11.0"
ktor-swagger-ui-version = "3.3.1"
swagger-parser-version = "2.1.22"
shadow-version = "8.1.1"
coroutine = "1.9.0"
mockk = "1.13.5"

[libraries]
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor-version" }
Expand Down Expand Up @@ -46,6 +48,9 @@ valiktor-core = { module = "org.valiktor:valiktor-core", version.ref = "valiktor
commons-io = { module = "commons-io:commons-io", version.ref = "commons-io-version" }
ktor-swagger-ui = { module = "io.github.smiley4:ktor-swagger-ui", version.ref = "ktor-swagger-ui-version" }
swagger-parser= { module = "io.swagger.parser.v3:swagger-parser", version.ref = "swagger-parser-version" }
coroutine-test= { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutine" }
mockk= { module = "io.mockk:mockk", version.ref = "mockk" }
ktor-client-mock= { module = "io.ktor:ktor-client-mock", version.ref = "ktor-version" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }
Expand Down
13 changes: 0 additions & 13 deletions src/test/kotlin/com/piashcse/ApplicationTest.kt

This file was deleted.

76 changes: 76 additions & 0 deletions src/test/kotlin/com/piashcse/ConfigureAuth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.piashcse

import com.auth0.jwt.JWT
import com.auth0.jwt.JWTVerifier
import com.auth0.jwt.algorithms.Algorithm
import com.piashcse.plugins.RoleManagement
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.auth.jwt.*

fun Application.configureAuthTest() {
val authRealm = "piashcse"
install(Authentication) {
jwt(RoleManagement.ADMIN.role) {
realm = authRealm
verifier(
jwtBuilder()
)
validate { credential ->
// Accepting multiple roles
val role = credential.payload.getClaim("role").asString()
if (role in listOf("ADMIN")) {
UserIdPrincipal(credential.payload.subject)
} else {
null
}
}
}
jwt(RoleManagement.SELLER.role) {
realm = authRealm
verifier(
jwtBuilder()
)
validate { credential ->
// Accepting multiple roles
val role = credential.payload.getClaim("role").asString()
if (role in listOf("SELLER")) {
UserIdPrincipal(credential.payload.subject)
} else {
null
}
}
}
jwt(RoleManagement.CUSTOMER.role) {
realm = authRealm
verifier(
jwtBuilder()
)
validate { credential ->
// Accepting multiple roles
val role = credential.payload.getClaim("role").asString()
if (role in listOf("CUSTOMER")) {
UserIdPrincipal(credential.payload.subject)
} else {
null
}
}
}
}
}

private const val SECRET = "zAP5MBA4B4Ijz0MZaS48"
private const val ISSUER = "piashcse"
fun jwtBuilder(): JWTVerifier {
return JWT.require(Algorithm.HMAC256(SECRET))
.withIssuer(ISSUER)
.build()
}

fun generateJwtToken(role: String): String {
return JWT.create()
.withIssuer(ISSUER)
.withClaim("role", role) // Pass appropriate role
.withSubject("CUSTOMER") // Set subject
.sign(Algorithm.HMAC256(SECRET))
}
Loading