<commit_before>package controllers

import play.api._
import play.api.mvc._
import scala.io.Source
import com.nimbusds.jose._
import com.nimbusds.jose.jwk._
import com.nimbusds.jose.crypto.RSASSASigner
import java.security.KeyPairGenerator
import java.security.interfaces.{RSAPrivateKey, RSAPublicKey}
import java.io.{BufferedWriter, FileWriter, File, FileNotFoundException}
import play.api.libs.json.Json
import scala.collection.JavaConversions._
import java.util.Calendar

class Application extends Controller {

  val containers = (1 to 9).map(i => s"test$i").toList

  def index = Action { request =>
    Ok(views.html.index(containers, request.host))
  }

  def login = Action { implicit request =>
    val cookies = Cookie("dit4c-jwt", jwt(containers))
    Redirect(request.headers.get("Referer").getOrElse("/"))
      .withCookies(cookies)
  }

  private def jwt(containers: List[String])(implicit request: RequestHeader) = {
    val privateKey = privateKeySet.getKeys.head
      .asInstanceOf[RSAKey].toRSAPrivateKey
    val tokenString = {
      val json = Json.obj(
          "iis" -> request.host,
          "iat" -> System.currentTimeMillis / 1000,
          "http://dit4c.github.io/authorized_containers" -> containers
        )
      json.toString
    }
    val header = new JWSHeader(JWSAlgorithm.RS256)
    val payload = new Payload(tokenString)
    val signer = new RSASSASigner(privateKey)
    val token = new JWSObject(header, payload)
    token.sign(signer)
    token.serialize
  }

  private def privateKeySet: JWKSet = {
    try {
      val content = Source.fromFile(privateKeysFile).mkString
      JWKSet.parse(content)
    } catch {
      case _: FileNotFoundException => createNewPrivateKeySet(privateKeysFile)
    }
  }

<commit_msg>Fixing domain handling, as redirect for each container isn't currently a good idea.<commit_after>package controllers

import play.api._
import play.api.mvc._
import scala.io.Source
import com.nimbusds.jose._
import com.nimbusds.jose.jwk._
import com.nimbusds.jose.crypto.RSASSASigner
import java.security.KeyPairGenerator
import java.security.interfaces.{RSAPrivateKey, RSAPublicKey}
import java.io.{BufferedWriter, FileWriter, File, FileNotFoundException}
import play.api.libs.json.Json
import scala.collection.JavaConversions._
import java.util.Calendar

class Application extends Controller {

  val containers = (1 to 9).map(i => s"test$i").toList

  def index = Action { request =>
    Ok(views.html.index(containers, request.host))
  }

  def login = Action { implicit request =>
    val cookies = Cookie("dit4c-jwt", jwt(containers),
        domain=getCookieDomain)
    Redirect(request.headers.get("Referer").getOrElse("/"))
      .withCookies(cookies)
  }

  def getCookieDomain(implicit request: RequestHeader): Option[String] =
    if (request.host.matches(".+\\..+")) Some("."+request.host)
    else None



  private def jwt(containers: List[String])(implicit request: RequestHeader) = {
    val privateKey = privateKeySet.getKeys.head
      .asInstanceOf[RSAKey].toRSAPrivateKey
    val tokenString = {
      val json = Json.obj(
          "iis" -> request.host,
          "iat" -> System.currentTimeMillis / 1000,
          "http://dit4c.github.io/authorized_containers" -> containers
        )
      json.toString
    }
    val header = new JWSHeader(JWSAlgorithm.RS256)
    val payload = new Payload(tokenString)
    val signer = new RSASSASigner(privateKey)
    val token = new JWSObject(header, payload)
    token.sign(signer)
    token.serialize
  }

  private def privateKeySet: JWKSet = {
    try {
      val content = Source.fromFile(privateKeysFile).mkString
      JWKSet.parse(content)
    } catch {
      case _: FileNotFoundException => createNewPrivateKeySet(privateKeysFile)
    }
  }


{'fraction_non_alphanumeric': 0.10969793322734499, 'fraction_numerical': 0.005829358770535241, 'mean_word_length': 3.570217917675545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3930999', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1174, 'n_words': 247}