Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is angular-jwt decoding my JWT without a secret?

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...

like image 343
G. Deward Avatar asked Aug 11 '15 22:08

G. Deward


People also ask

Can JWT be decoded without secret?

By design, anyone can decode a JWT and read the contents of the header and payload sections. But we need access to the secret key used to create the signature to verify a token's integrity.

How does JWT decryption work?

The token is created using a secret string that is stored on a server. Next, the server then sends that JWT back to the client which will store it either in a cookie or in local storage. Just like this, the user is authenticated and basically logged into our application without leaving any state on the server.

Can JWT be anything secret?

JWT is created with a secret key and that secret key is private to you which means you will never reveal that to the public or inject inside the JWT token. When you receive a JWT from the client, you can verify that JWT with this that secret key stored on the server.

How do I read JWT payload?

Each JWT contains a payload. The payload is a base64 encoded JSON object that sits between the two periods in the token. We can decode this payload by using atob() to decode the payload to a JSON string and use JSON. parse() to parse the string into an object.


1 Answers

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.

like image 116
Yuri Zarubin Avatar answered Sep 22 '22 01:09

Yuri Zarubin