<commit_before>package com.identityblitz.jwt

import org.scalatest.{Matchers, FlatSpec}
import java.lang.IllegalArgumentException

object BaseJwtToolkit extends AlgorithmsKit with JwsToolkit with SimpleCryptoService with SimpleKidsRegisterService {
  override def extensionsNames: Set[String] = Set("exp")
}

class JwsTest extends FlatSpec with Matchers {

  import BaseJwtToolkit._

  behavior of "JWS toolkit"

  it should "deserialize JWS with [none] algorithm" in {
    val plainJwt = "<KEY>
    val jwt = JWT[ClaimsSet](plainJwt)

    jwt.header.alg.name should be (none.name)
    jwt.payload.iss should be (Some(StringOrUri("joe")))
    jwt.payload.exp should be (Some(new IntDate(1300819380)))
    jwt.payload.names should contain ("http://example.com/is_root")
  }

  import JWSNameKit._

  it should "build JWS with [none] algorithm" in {
    val noneJwt =  builder.alg(none)
      .header(typ % "JWT")
      .payload(cs(
      iss % StringOrUri("joe"),
      aud % Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3"))
    )).build

    noneJwt.header.alg.name should be (none.name)
    noneJwt.header.typ should be (Some("JWT"))
    noneJwt.payload.iss should be (Some(StringOrUri("joe")))
    noneJwt.payload.aud should be (Some(Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")).toList))
  }

  it should "build JWS with [HS256] algorithm" in {
    val hs256Jwt = builder.alg(HS256).
      header(typ % "JWT")
      .payload(cs(
      iss % StringOrUri("joe"),
      aud % Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")))).build

    hs256Jwt.header.alg.name should be (HS256.name)
    hs256Jwt.header.typ should be (Some("JWT"))
    hs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    hs256Jwt.payload.aud should be (Some(Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")).toList))
    hs256Jwt.asBase64 should be ("<KEY>")
  }

  it should "deserialize JWS with [HS256] algorithm" in {
    val strHs256Jwt = "<KEY>"
    val hs256Jwt = JWT[ClaimsSet](strHs256Jwt)

    hs256Jwt.header.alg.name should be (HS256.name)
    hs256Jwt.header.typ should be (Some("JWT"))
    hs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    hs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    hs256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "deserialize JWS with [RS256] algorithm" in {
    val strRs256Jwt = "<KEY>"
    val rs256Jwt = JWT[ClaimsSet](strRs256Jwt)

    rs256Jwt.header.alg.name should be (RS256.name)
    rs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    rs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    rs256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "build JWS with [RS256] algorithm" in {
    val rs256Jwt = builder.alg(RS256).
      header()
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build

    rs256Jwt.header.alg.name should be (RS256.name)
    rs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    rs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    rs256Jwt.payload.names should contain ("http://example.com/is_root")
    rs256Jwt.asBase64 should be ("<KEY>")
  }

  it should "deserialize JWS with [ES256] algorithm" in {
    val strEs256Jwt = "<KEY>"
    val es256Jwt = JWT[ClaimsSet](strEs256Jwt)

    es256Jwt.header.alg.name should be (ES256.name)
    es256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    es256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    es256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "build JWS with [ES256] algorithm" in {
    val es256Jwt = builder.alg(ES256).
      header()
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build

    es256Jwt.header.alg.name should be (ES256.name)
    es256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    es256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    es256Jwt.payload.names should contain ("http://example.com/is_root")
    val es256JwtCheck = JWT[ClaimsSet](es256Jwt asBase64)
  }

  it should "deserialize JWS with [ES512] algorithm" in {
    val strEs512Jwt = "<KEY>"
    implicit object stringPConverter extends PayloadConverter[String]{
      def toBytes(orig: String): Array[Byte] = orig.getBytes("UTF-8")
      def fromBytes(a: Array[Byte]): String = new String(a, "UTF-8")
    }

    val es512Jwt = JWT[String](strEs512Jwt)
    es512Jwt.header.alg.name should be (ES512.name)
    es512Jwt.payload should be ("Payload")
  }

  it should "process [crit] Header parameter correctly" in {
    builder.alg(ES256).
      header(
        typ % "JWT",
        crit % Array("exp"),
        exp % new IntDate(1300819380)
      )
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build
  }

  it should "reject JWS with [crit] Header parameter containing name that is absent in a header" in {
    a [IllegalStateException] should be thrownBy {
      builder.alg(ES256).
        header(
          typ % "JWT",
          crit % Array("exp")
        )
        .payload(cs(
        iss % StringOrUri("joe"),
        exp % IntDate(1300819380),
        "http://example.com/is_root" % true))
        .build
    }
  }

  it should "reject JWS with [crit] Header parameter containing unsupported extension" in {
    a [IllegalArgumentException] should be thrownBy {
      builder.alg(ES256).
        header(
          typ % "JWT",
          crit % Array("iss"),
          iss % StringOrUri("joe")
        )
        .payload(cs(
        iss % StringOrUri("joe"),
        exp % IntDate(1300819380),
        "http://example.com/is_root" % true))
        .build
    }
  }
  
}<commit_msg>added JWT<commit_after>package com.identityblitz.jwt

import org.scalatest.{Matchers, FlatSpec}
import java.lang.IllegalArgumentException

object BaseJwtToolkit extends AlgorithmsKit with JwsToolkit with SimpleCryptoService with SimpleKidsRegisterService {
  override def extensionsNames: Set[String] = Set("exp")
}

class JwsTest extends FlatSpec with Matchers {

  import BaseJwtToolkit._

  behavior of "JWS toolkit"

  it should "deserialize JWS with [none] algorithm" in {
    val plainJwt = "<KEY>
    val jwt = JWT[ClaimsSet](plainJwt)

    jwt.header.alg.name should be (none.name)
    jwt.payload.iss should be (Some(StringOrUri("joe")))
    jwt.payload.exp should be (Some(new IntDate(1300819380)))
    jwt.payload.names should contain ("http://example.com/is_root")
  }

  import JWSNameKit._

  it should "build JWS with [none] algorithm" in {
    val noneJwt =  builder.alg(none)
      .header(typ % "JWT")
      .payload(cs(
      iss % StringOrUri("joe"),
      aud % Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3"))
    )).build

    noneJwt.header.alg.name should be (none.name)
    noneJwt.header.typ should be (Some("JWT"))
    noneJwt.payload.iss should be (Some(StringOrUri("joe")))
    noneJwt.payload.aud should be (Some(Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")).toList))
  }

  it should "build JWS with [HS256] algorithm" in {
    val hs256Jwt = builder.alg(HS256).
      header(typ % "JWT")
      .payload(cs(
      iss % StringOrUri("joe"),
      aud % Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")))).build

    hs256Jwt.header.alg.name should be (HS256.name)
    hs256Jwt.header.typ should be (Some("JWT"))
    hs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    hs256Jwt.payload.aud should be (Some(Array(StringOrUri("aud1"), StringOrUri("aud2"), StringOrUri("aud3")).toList))
    hs256Jwt.asBase64 should be ("<KEY>")
  }

  it should "deserialize JWS with [HS256] algorithm" in {
    val strHs256Jwt = "<KEY>"
    val hs256Jwt = JWT[ClaimsSet](strHs256Jwt)

    hs256Jwt.header.alg.name should be (HS256.name)
    hs256Jwt.header.typ should be (Some("JWT"))
    hs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    hs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    hs256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "deserialize JWS with [RS256] algorithm" in {
    val strRs256Jwt = "<KEY>"
    val rs256Jwt = JWT[ClaimsSet](strRs256Jwt)

    rs256Jwt.header.alg.name should be (RS256.name)
    rs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    rs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    rs256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "build JWS with [RS256] algorithm" in {
    val rs256Jwt = builder.alg(RS256).
      header()
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build

    rs256Jwt.header.alg.name should be (RS256.name)
    rs256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    rs256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    rs256Jwt.payload.names should contain ("http://example.com/is_root")
    rs256Jwt.asBase64 should be ("<KEY>gY<KEY>")
  }

  it should "deserialize JWS with [ES256] algorithm" in {
    val strEs256Jwt = "<KEY>"
    val es256Jwt = JWT[ClaimsSet](strEs256Jwt)

    es256Jwt.header.alg.name should be (ES256.name)
    es256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    es256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    es256Jwt.payload.names should contain ("http://example.com/is_root")
  }

  it should "build JWS with [ES256] algorithm" in {
    val es256Jwt = builder.alg(ES256).
      header()
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build

    es256Jwt.header.alg.name should be (ES256.name)
    es256Jwt.payload.iss should be (Some(StringOrUri("joe")))
    es256Jwt.payload.exp should be (Some(new IntDate(1300819380)))
    es256Jwt.payload.names should contain ("http://example.com/is_root")
    val es256JwtCheck = JWT[ClaimsSet](es256Jwt asBase64)
  }

  it should "deserialize JWS with [ES512] algorithm" in {
    val strEs512Jwt = "<KEY>"
    implicit object stringPConverter extends PayloadConverter[String]{
      def toBytes(orig: String): Array[Byte] = orig.getBytes("UTF-8")
      def fromBytes(a: Array[Byte]): String = new String(a, "UTF-8")
    }

    val es512Jwt = JWT[String](strEs512Jwt)
    es512Jwt.header.alg.name should be (ES512.name)
    es512Jwt.payload should be ("Payload")
  }

  it should "process [crit] Header parameter correctly" in {
    builder.alg(ES256).
      header(
        typ % "JWT",
        crit % Array("exp"),
        exp % new IntDate(1300819380)
      )
      .payload(cs(
      iss % StringOrUri("joe"),
      exp % IntDate(1300819380),
      "http://example.com/is_root" % true))
      .build
  }

  it should "reject JWS with [crit] Header parameter containing name that is absent in a header" in {
    a [IllegalStateException] should be thrownBy {
      builder.alg(ES256).
        header(
          typ % "JWT",
          crit % Array("exp")
        )
        .payload(cs(
        iss % StringOrUri("joe"),
        exp % IntDate(1300819380),
        "http://example.com/is_root" % true))
        .build
    }
  }

  it should "reject JWS with [crit] Header parameter containing unsupported extension" in {
    a [IllegalArgumentException] should be thrownBy {
      builder.alg(ES256).
        header(
          typ % "JWT",
          crit % Array("iss"),
          iss % StringOrUri("joe")
        )
        .payload(cs(
        iss % StringOrUri("joe"),
        exp % IntDate(1300819380),
        "http://example.com/is_root" % true))
        .build
    }
  }

}
{'fraction_non_alphanumeric': 0.1340284200790381, 'fraction_numerical': 0.056840158076179266, 'mean_word_length': 3.202826855123675, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '973403', 'n_tokens_mistral': 4571, 'n_tokens_neox': 4077, 'n_words': 921}