Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jwt eli class exercise #16

Merged
merged 16 commits into from
Sep 17, 2024
Merged
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
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ dependencies {
implementation("org.jooq:jooq:3.16.9")
implementation("org.slf4j:slf4j-api")

//JWT
implementation("io.jsonwebtoken:jjwt-api:0.11.5")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")

implementation("org.postgresql:postgresql")

testImplementation("org.junit.jupiter:junit-jupiter")
Expand Down
44 changes: 44 additions & 0 deletions src/main/kotlin/com/hibob/academy/filters/AuthenticationFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.hibob.academy.filters
import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jws
import io.jsonwebtoken.Jwts
import jakarta.ws.rs.container.ContainerRequestContext
import jakarta.ws.rs.container.ContainerRequestFilter
import org.springframework.stereotype.Component
import com.hibob.academy.service.SessionService.Companion.SECRET_KEY
import jakarta.ws.rs.core.Response
import jakarta.ws.rs.ext.Provider


@Component
@Provider
class AuthenticationFilter : ContainerRequestFilter {

companion object {
private const val LOGIN_PATH = "jwt/users/login"
const val COOKIE_NAME = "ron_cookie_name" // Replace with actual cookie name
}

override fun filter(requestContext: ContainerRequestContext) {
if (requestContext.uriInfo.path == LOGIN_PATH) return

val cookie = requestContext.cookies[COOKIE_NAME]?.value

if (!verify(cookie)) {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("Invalid or expired token").build())
}
}

private val jwtParser = Jwts.parserBuilder().setSigningKey(SECRET_KEY).build()

fun verify(cookie: String?): Boolean {
return cookie?.let {
try {
jwtParser.parseClaimsJws(it)
true
} catch (ex: Exception) {
false
}
} ?: false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.hibob.academy.resource

import jakarta.ws.rs.POST
import jakarta.ws.rs.Path
import jakarta.ws.rs.core.Response
import com.hibob.academy.service.SessionService
import jakarta.ws.rs.Consumes
import jakarta.ws.rs.GET
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType
import jakarta.ws.rs.core.NewCookie
import org.springframework.stereotype.Controller
import com.hibob.academy.filters.AuthenticationFilter.Companion.COOKIE_NAME

data class User(val email: String,val name: String, val isAdmin: Boolean)

@Controller
@Produces(MediaType.APPLICATION_JSON)
@Path("/jwt/users")
class AuthenticationResourse(private val service: SessionService) {

@Path("/login")
@POST
@Consumes(MediaType.APPLICATION_JSON)
fun addNewUser(newUser: User): Response {
val tokenJwt = service.createJWTToken(newUser) // Assuming createJWTToken returns a JWT
val cookie = NewCookie.Builder(COOKIE_NAME).value(tokenJwt).build()//Creating new cookie
return Response.ok().cookie(cookie).build()
}

@Path("/getAllUsers")
@GET
@Consumes(MediaType.APPLICATION_JSON)
fun getAllUsers(): Response {
return Response.ok().entity("Yessssss").build()
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/com/hibob/academy/service/SessionService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hibob.academy.service
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.springframework.stereotype.Component
import java.util.Date
import com.hibob.academy.resource.User

@Component
class SessionService {
companion object {
const val SECRET_KEY =
"secretsdfghjkjhghjhghjhjkjhghjkjhgfghjhg21243e5wredwedywe5te4343tewqawsertyusdfghjkerftgyhujdfgxdewefcvhj"
}

fun createJWTToken(user: User): String {
return Jwts.builder()
.setHeaderParam("typ", "JWT")
.claim("email", user.email)
.claim("username", user.name)
.claim("isAdmin", user.isAdmin)
.setExpiration(Date(Date().time + 24 * 60 * 60 * 1000))
.signWith(SignatureAlgorithm.HS512, SECRET_KEY)
.compact()
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/hibob/jwt/jwtClassExercise.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.hibob.jwt

Loading