Search code examples
angularjsjax-rsjwtsatellizer

SyntaxError: Unexpected token t when using sattelizer and JAX-RS


Trying to return JWT from JAX-RS webservice but get the following error:

SyntaxError: Unexpected token t
    at Object.parse (native)
    at fromJson (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:1250:14)
    at defaultHttpResponseTransform (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9371:16)
    at http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9462:12
    at forEach (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:336:20)
    at transformData (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:9461:3)
    at transformResponse (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:10241:23)
    at processQueue (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:14634:28)
    at http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:14650:27
    at Scope.$get.Scope.$eval (http://localhost:9081/FoodView/resources/bower_components/angular/angular.js:15916:28)

This is my LoginController code from which I tries to perform login:

app.controller('LoginController', function($scope, $log, $auth){
    $scope.login = function() {
          $auth.login($scope.user)
            .then(function() {
              $log.info('You have successfully signed in');
              $location.path('/');
            })
            .catch(function(response) {
                $log.info(response.data, response.status);
            });
        };

});

And this is JAX-RS webservice code which issues JWT tokens, to generate JWT I'm using jose.4.j library:

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response mergeInfo(String json){
        Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
        .setPrettyPrinting().create();

        System.out.println(json);

        String serializedJwe = null;
        try {
             Key key = new AesKey(ByteUtil.randomBytes(16));
             JsonWebEncryption jwe = new JsonWebEncryption();
             jwe.setPayload("Hello World!");
             jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW);
             jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
             jwe.setKey(key);
             serializedJwe = jwe.getCompactSerialization();
             System.out.println("Serialized Encrypted JWE: " + serializedJwe);
             jwe = new JsonWebEncryption();
             jwe.setKey(key);
             jwe.setCompactSerialization(serializedJwe);
             System.out.println("Payload: " + jwe.getPayload());
        } catch (Exception e) {
            e.printStackTrace();
        }

        String token = "{token: " + serializedJwe + "}";

        return Response.ok(token).build();

    }

I think that format/way in which I return generated JWT is wrong, but can't realize how to fix it. Thank you.


Solution

  • Ok, I get the reason why it didn't work, my JSON string was misformatted.

    This is correct string to parse it to json: var test = '{"token":"test"}' So if you will perform: JSON.parse(test) you will get: Object {token: "test"}

    But! If you will use string like this var test = "{'token':'test'}" to parse to javascript object, you will get Uncaught SyntaxError: Unexpected token '

    So I've created in my Java application the following class:

    public class Token {
        private String token;
    
        public String getToken() {
            return token;
        }
    
        public void setToken(String token) {
            this.token = token;
        }
    }
    

    And then changed the last 3 lines of my code to following:

            Token token = new Token();
            token.setToken(serializedJwe);
    
            return Response.ok(gson.toJson(token)).build();
    

    Now everything works like expected.