j3d
j3d

Reputation: 9724

Encoding/Decoding URL between Play Backend and JavaScript Client

I've I Play REST API and a JavaScript client that sends a token:signature pair in the URL like this:

http://localhost:9000/auth/users/all/0/11?auth=eyJhbGciOiJIUzI1N...ciOiJIU:8so/gRFwOoPXp2x6RfyUpMYIMD4=

In the JavaScript client I encode the URL like this:

signature = CryptoJS.HmacSHA1(token + method + obj.url.slice(baseUrl.length) + body, apiKey).toString(CryptoJS.enc.Base64);
...
obj.url = obj.url + (obj.url.indexOf("?") > 0 ? "&" : "?") + "auth=" + encodeURIComponent(token + ":" + signature);

Then, in the Play backend I decode the URL like this:

import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding

// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePathSegment(jwt, SC.US_ASCII.name).split(":")

The problem is that UriEncoding crashes because of the slash:

play.api.Application$$anon$1:
Execution exception[[InvalidUriEncodingException: Cannot decode eyJhbGciOiJIUzI1N...vZ5v18d2EZik1ki5W9_6XABi-JA:8so/gRFwOoPXp2x6RfyUpMYIMD4=: illegal character at position 715.]]

Now my questions are:

  1. In the JavaScript client should I base64 before encoding the URL with encodeURIComponent?
  2. In the Play backend, what encoding should I use, US_ASCII or UTF_8?

Upvotes: 2

Views: 3021

Answers (1)

j3d
j3d

Reputation: 9724

Just replaced UriEncoding.decodePathSegment with UriEncoding.decodePath and it works:

import java.nio.charset.{StandardCharsets => SC}
import play.utils.UriEncoding

// auth(0) => token, auth(1) => signature
val auth = UriEncoding.decodePath(jwt, SC.US_ASCII.name).split(":")

I hope it helps.

Upvotes: 4

Related Questions