13

The Auth0 team created something called "angular-jwt" which has a jwtHelper class. This thing successfully decodes a local JWT without the secret I used on the server. How did this happen? If they are not secure, then what is the point of using a secret to sign/encrypt them?

Function on the server that encrypts the token (using "jsonwebtoken"):

function createToken (user) {
    return jwt.sign(_.omit(user, 'password'), config.secret, { expiresInMinutes: 60*5 });
}

Code from the client:

angular
    .module('sample.home', [
        'ui.router',
        'angular-storage',
        'angular-jwt'
    ])
    .config(function ($stateProvider) {
        $stateProvider
            .state('home', {
                url: '/',
                controller: 'HomeCtrl',
                templateUrl: 'modules/home/home.html',
                data: { requiresLogin: true }
            })
    })
    .controller('HomeCtrl', function homeController ($scope, $http, store, jwtHelper) {

        $scope.jwt = store.get('jwt');
        $scope.decodedJwt = $scope.jwt && jwtHelper.decodeToken($scope.jwt);

    });

Here's a link to the full example: http://github.com/auth0/ang...

G. Deward
  • 1,542
  • 3
  • 17
  • 30

1 Answers1

13

A JWT uses encoding, not encryption. The data that the token contains is not a secret, anyone can decode it and view. What the server does, is it signs the token using a secret (in your case, config.secret), which effectively makes it impossible to modify the token without knowing the secret. Hence, only the server will be able to change the contents of the token, but anyone can read it.

Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33
  • 1
    Aaaah... they're signed! What was I thinking!?!? Thanks, Yuri. – G. Deward Aug 11 '15 at 23:10
  • 2
    For reference, this answer applies only when using symmetric keys (e.g. HMAC-SHA256). It's also possible to use asymmetric keys (e.g. RSA-SHA256), which lets you sign tokens with the private key and validate them with the public key. It's also possible to have encrypted claims in a token if they need to be hidden from the client, or have fully encrypted tokens using JWE: https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-40 – Rodrigo López Dato Aug 14 '15 at 18:46
  • This made my day... Thank you – SoliQuiD Nov 01 '16 at 11:56