<commit_before>package util

import javax.inject.Inject

import play.api.Configuration
import play.api.libs.json.Json._
import scala.util._

import pdi.jwt.{Jwt, JwtAlgorithm, JwtHeader, JwtClaim, JwtOptions}
import com.trifectalabs.roadquality.v0.models.{ User, UserRole }
import com.trifectalabs.roadquality.v0.models.json._

class JwtUtil @Inject() (configuration: Configuration) {
  lazy val secret = configuration.getString("play.crypto.secret").get

  def isTokenValid(token: String): Boolean = {
    Jwt.isValid(token, secret, Seq(JwtAlgorithm.HS256))
    // TODO check if token exists
  }

  def decodeToken(token: String): Option[User] = {
    Jwt.decode(token, secret, Seq(JwtAlgorithm.HS256)) match {
      case Success(t) => parse(t).asOpt[User]
      case Failure(e) => None
    }
  }

	def createToken(user: User): String = {
    Jwt.encode(stringify(toJson(user)), secret, JwtAlgorithm.HS256)
	}

}<commit_msg>JWT expiration (1 month default)<commit_after>package util

import javax.inject.Inject

import play.api.Configuration
import play.api.libs.json.Json._
import scala.util._

import pdi.jwt.{Jwt, JwtAlgorithm, JwtHeader, JwtClaim, JwtOptions}
import com.trifectalabs.roadquality.v0.models.{ User, UserRole }
import com.trifectalabs.roadquality.v0.models.json._

class JwtUtil @Inject() (configuration: Configuration) {
  lazy val secret = configuration.getString("play.crypto.secret").get

  def isTokenValid(token: String): Boolean = {
    Jwt.isValid(token, secret, Seq(JwtAlgorithm.HS256))
    // TODO check if token exists
  }

  def decodeToken(token: String): Option[User] = {
    Jwt.decode(token, secret, Seq(JwtAlgorithm.HS256)) match {
      case Success(t) => parse(t).asOpt[User]
      case Failure(e) => None
    }
  }

	def createToken(user: User, durationSec: Long = 2629746000l): String = {
    Jwt.encode(JwtClaim({stringify(toJson(user))}).issuedNow.expiresIn(durationSec), secret, JwtAlgorithm.HS256)
	}

}
{'fraction_non_alphanumeric': 0.12891628145865433, 'fraction_numerical': 0.01694915254237288, 'mean_word_length': 4.54985754985755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5598942', 'n_tokens_mistral': 683, 'n_tokens_neox': 657, 'n_words': 135}