Skip to content

Commit

Permalink
Fix #30 - bug rendering the library unusable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongoodwin committed Jan 11, 2017
1 parent a007685 commit fd964cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/main/scala/authentikat/jwt/JwtHeader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ package authentikat.jwt

import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization

import util.control.Exception.allCatch
import scala.util.control.Exception.allCatch

case class JwtHeader(algorithm: Option[String],
contentType: Option[String],
typ: Option[String]) {
case class JwtHeader(
algorithm: Option[String],
contentType: Option[String],
typ: Option[String]
) {

def asJsonString: String = {
val toSerialize =
algorithm.map(x ("alg", x)).toSeq ++
contentType.map(x ("cty", x)).toSeq ++
typ.map(x ("typ", x)).toSeq
algorithm.map(x => ("alg" -> x)).toSeq ++
contentType.map(x => ("cty", x)).toSeq ++
typ.map(x => ("typ", x)).toSeq

val map = toSerialize.toMap

import org.json4s.native.Serialization.write
implicit val formats = org.json4s.DefaultFormats
org.json4s.jackson.Serialization.write(toSerialize)

write(map)
}
}

Expand All @@ -27,7 +32,7 @@ object JwtHeader {

implicit val formats = DefaultFormats

def apply(algorithm: String = null, contentType: String = null, typ: String = "JWT"): JwtHeader = {
def apply(algorithm: String, contentType: String = null, typ: String = "JWT"): JwtHeader = {
JwtHeader(Option(algorithm), Option(contentType), Option(typ))
}

Expand All @@ -44,4 +49,4 @@ object JwtHeader {
def fromJsonStringOpt(jsonString: String): Option[JwtHeader] = allCatch opt {
fromJsonString(jsonString)
}
}
}
6 changes: 5 additions & 1 deletion src/test/scala/authentikat/jwt/JsonWebTokenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class JsonWebTokenSpec extends FunSpec with Matchers {

it("should have three parts for a token created with claims map claims") {
val result = JsonWebToken.apply(header, claims, "secretkey")
println(result)
println(result)
println(result)
result.split("\\.").length should equal(3)
}

Expand Down Expand Up @@ -88,6 +91,7 @@ class JsonWebTokenSpec extends FunSpec with Matchers {
describe("JwtHeader") {
it("should render to json as per spec") {
val header = JwtHeader("algorithm", "contentType")
println(header.asJsonString)
val expectedJson = "{\"alg\":\"algorithm\",\"cty\":\"contentType\",\"typ\":\"JWT\"}"

header.asJsonString should equal(expectedJson)
Expand All @@ -97,7 +101,7 @@ class JsonWebTokenSpec extends FunSpec with Matchers {
describe("JwtClaimsSet") {

val date = new Date
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
val dateIso8601 = dateFormat.format(date)

Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version := "0.4.4"
version := "0.4.5"

0 comments on commit fd964cf

Please # to comment.