Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Added character encoding option for JWT decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
iain-logan committed Jun 11, 2016
1 parent d3d0363 commit 652e67d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -2,7 +2,7 @@ name := "jwt"

organization := "io.igl"

version := "1.2.0"
version := "1.2.1"

scalaVersion := "2.11.7"

Expand Down
9 changes: 5 additions & 4 deletions src/main/scala/io/igl/jwt/DecodedJwt.scala
Expand Up @@ -76,7 +76,7 @@ class DecodedJwt(headers_ : Seq[HeaderValue], claims_ : Seq[ClaimValue]) extends
object DecodedJwt {

/** Returns the Base64 decoded version of provided string **/
private def decodeBase64(subject: String): String = new String(Base64.decodeBase64(subject))
private def decodeBase64(subject: String, charset: String): String = new String(Base64.decodeBase64(subject), charset)

/** Returns the Base64 url safe encoding of a byte array **/
private def encodeBase64Url(subject: Array[Byte]): String = Base64.encodeBase64URLSafeString(subject)
Expand Down Expand Up @@ -148,7 +148,8 @@ object DecodedJwt {
aud: Option[Aud] = None,
iat: Option[Iat] = None,
sub: Option[Sub] = None,
jti: Option[Jti] = None): Try[Jwt] = Try {
jti: Option[Jti] = None,
charset: String = "UTF-8"): Try[Jwt] = Try {

require(requiredHeaders.map(_.name).size == requiredHeaders.size, "Required headers contains field name collisions")
require(requiredClaims.map(_.name).size == requiredClaims.size, "Required claims contains field name collisions")
Expand All @@ -167,7 +168,7 @@ object DecodedJwt {

// Validate headers
val headerJson = Try {
Json.parse(decodeBase64(header)) match {
Json.parse(decodeBase64(header, charset)) match {
case header: JsObject => header
case _ => throw new IllegalArgumentException()
}
Expand All @@ -193,7 +194,7 @@ object DecodedJwt {

// Validate payload
val payloadJson = Try {
Json.parse(decodeBase64(payload)) match {
Json.parse(decodeBase64(payload, charset)) match {
case header: JsObject => header
case _ => throw new IllegalArgumentException()
}
Expand Down

0 comments on commit 652e67d

Please sign in to comment.