Giter VIP home page Giter VIP logo

Comments (6)

pauldijou avatar pauldijou commented on June 3, 2024 1

Sure, I can do that. I don't think the error is related to the project itself btw.

from jwt-scala.

pauldijou avatar pauldijou commented on June 3, 2024

I'm not 100% sure, but those functions are protected, so it should mean that if you extend the object, you will have access to them and then you can freely expose them.

The idea was to only publicly expose "safe" functions which include validation. And if someone wanted to do some dangerous stuff, it would need to kind of override the default feature to allow that. That's why they are protected rather than private.

Would that work for you?

from jwt-scala.

evbo avatar evbo commented on June 3, 2024

I like that idea, but my understanding is that Scala does not allow you to directly extend an object. But if it were a trait then I would be able to. Does that sound right to you?

https://stackoverflow.com/q/7625040/1080804

from jwt-scala.

pauldijou avatar pauldijou commented on June 3, 2024

Maybe? Sorry, it's been years since I have done any Scala. One way of solving it would be to make all current object as trait and then create an empty object extending it. This way, you could also freely extend those traits in your own code if needed.

The other solution is to just make them public et let people be responsible :-)

Do you have a preference? Would you consider doing a PR? I don't really have time to code and test Scala code right now.

from jwt-scala.

evbo avatar evbo commented on June 3, 2024

sorry I didn't get back to you on this. There is a very simple fix: just adding Option[String] variable to your JwtHeader case class. "kid" is an optional header claim, and so it's probably best to include it.

However, I was never able to run tests. I always hit this issue:

java.util.NoSuchElementException: head of empty stream at com.typesafe.sbt.SbtGit

The PR would be simple though:

case class JwtHeader(
  algorithm: Option[JwtAlgorithm] = None,
  typ: Option[String] = None,
  contentType: Option[String] = None,
  keyId: Option[String] = None
) {
  def toJson: String = JwtUtils.hashToJson(Seq(
    "typ" -> typ,
    "alg" -> algorithm.map(_.name).orElse(Option("none")),
    "cty" -> contentType
    "kid" -> keyId
  ).collect {
    case (key, Some(value)) => (key -> value)
  })

  /** Assign the kid to the header */
  def withKeyId(kid: String): JwtHeader = this.copy(kid = Option(kid))

/** Assign the type to the header */
  def withType(typ: String): JwtHeader = this.copy(typ = Option(typ))

  /** Assign the default type `JWT` to the header */
  def withType: JwtHeader = this.withType(JwtHeader.DEFAULT_TYPE)
}

object JwtHeader {
  val DEFAULT_TYPE = "JWT"

  def apply(algorithm: Option[JwtAlgorithm]): JwtHeader = algorithm match {
    case Some(algo) => JwtHeader(algo)
    case _ => new JwtHeader(None, None)
  }

  def apply(algorithm: JwtAlgorithm): JwtHeader = new JwtHeader(Option(algorithm), Option(DEFAULT_TYPE))

  def apply(algorithm: JwtAlgorithm, typ: String): JwtHeader = new JwtHeader(Option(algorithm), Option(typ))

  def apply(algorithm: JwtAlgorithm, typ: String, contentType: String): JwtHeader =
    new JwtHeader(Option(algorithm), Option(typ), Option(contentType))

def apply(algorithm: JwtAlgorithm, typ: String, contentType: String, keyId: String): JwtHeader =
    new JwtHeader(Option(algorithm), Option(typ), Option(contentType), Option(keyId))
}

from jwt-scala.

pauldijou avatar pauldijou commented on June 3, 2024

Done in 0.16.0

from jwt-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.