├── testem_dev.yml ├── lib ├── json_parse.js ├── index.js └── base64_url_decode.js ├── bower.json ├── .gitignore ├── test └── tests.js ├── standalone.js ├── test_harness.html ├── package.json ├── testem.yml ├── Gruntfile.js └── README.md /testem_dev.yml: -------------------------------------------------------------------------------- 1 | test_page: test_harness.html -------------------------------------------------------------------------------- /lib/json_parse.js: -------------------------------------------------------------------------------- 1 | module.exports = function (str) { 2 | return window.JSON ? window.JSON.parse(str) : eval('(' + str + ')'); 3 | }; -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jwt-decode", 3 | "version": "1.0.0", 4 | "main": "build/jwt-decode.js", 5 | "dependencies": {} 6 | } 7 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var base64_url_decode = require('./base64_url_decode'); 2 | var json_parse = require('./json_parse'); 3 | 4 | module.exports = function (token) { 5 | return json_parse(base64_url_decode(token.split('.')[1])); 6 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by http://gitignore.io 2 | 3 | ### Node ### 4 | lib-cov 5 | *.seed 6 | *.log 7 | *.csv 8 | *.dat 9 | *.out 10 | *.pid 11 | *.gz 12 | 13 | pids 14 | logs 15 | results 16 | 17 | npm-debug.log 18 | node_modules 19 | bower_components -------------------------------------------------------------------------------- /lib/base64_url_decode.js: -------------------------------------------------------------------------------- 1 | var Base64 = require('Base64'); 2 | 3 | module.exports = function(str) { 4 | var output = str.replace("-", "+").replace("_", "/"); 5 | switch (output.length % 4) { 6 | case 0: 7 | break; 8 | case 2: 9 | output += "=="; 10 | break; 11 | case 3: 12 | output += "="; 13 | break; 14 | default: 15 | throw "Illegal base64url string!"; 16 | } 17 | return Base64.atob(output); 18 | }; -------------------------------------------------------------------------------- /test/tests.js: -------------------------------------------------------------------------------- 1 | var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJleHAiOjEzOTMyODY4OTMsImlhdCI6MTM5MzI2ODg5M30.4-iaDojEVl0pJQMjrbM1EzUIfAZgsbK_kgnVyVxFSVo'; 2 | 3 | describe('Auth0', function () { 4 | 5 | it('should fail to construct without a clientID', function () { 6 | var decoded = jwt_decode(token); 7 | expect(decoded.exp).to.equal(1393286893); 8 | expect(decoded.iat).to.equal(1393268893); 9 | expect(decoded.foo).to.equal('bar'); 10 | }); 11 | 12 | }); 13 | -------------------------------------------------------------------------------- /standalone.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * This is used to build the bundle with browserify. 4 | * 5 | * The bundle is used by people who doesn't use browserify. 6 | * Those who use browserify will install with npm and require the module, 7 | * the package.json file points to index.js. 8 | */ 9 | var jwt_decode = require('./lib/index'); 10 | 11 | //use amd or just throught to window object. 12 | if (typeof global.window.define == 'function' && global.window.define.amd) { 13 | global.window.define('jwt_decode', function () { return jwt_decode; }); 14 | } else if (global.window) { 15 | global.window.jwt_decode = jwt_decode; 16 | } -------------------------------------------------------------------------------- /test_harness.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Mocha Tests Harness 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jwt-decode", 3 | "version": "1.0.0", 4 | "description": "Decode JWT tokens, mostly useful for browser applications.", 5 | "main": "lib/index.js", 6 | "keywords": [ 7 | "jwt", 8 | "browser" 9 | ], 10 | "scripts": { 11 | "test": "grunt test" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/auth0/jwt-decode.js" 16 | }, 17 | "author": "Jose F. Romaniello (http://joseoncode.com)", 18 | "license": "MIT", 19 | "dependencies": { 20 | "Base64": "~0.1.3" 21 | }, 22 | "devDependencies": { 23 | "browserify": "~2.35.0", 24 | "uglify-js": "~2.4.0", 25 | "grunt": "~0.4.0", 26 | "grunt-cli": "~0.1.9", 27 | "grunt-exec": "~0.4.2", 28 | "grunt-contrib-connect": "~0.5.0", 29 | "grunt-contrib-watch": "~0.2.0", 30 | "grunt-browserify": "~1.2.8", 31 | "grunt-contrib-uglify": "~0.2.4", 32 | "grunt-contrib-clean": "~0.5.0", 33 | "grunt-contrib-copy": "~0.4.1", 34 | "rimraf": "~2.2.2", 35 | "expect.js": "~0.2.0", 36 | "mocha": "~1.13.0", 37 | "grunt-s3": "~0.2.0-alpha.3", 38 | "testem": "~0.5.8", 39 | "grunt-maxcdn": "https://github.com/siacomuzzi/grunt-maxcdn/tarball/0.0.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /testem.yml: -------------------------------------------------------------------------------- 1 | test_page: test_harness.html 2 | 3 | on_start: 4 | command: /usr/bin/java -jar $HOME/.browserstack/BrowserStackTunnel.jar $BROWSERSTACK_KEY localhost,,0 -force 5 | wait_for_text: Press Ctrl-C to exit 6 | wait_for_text_timeout: 9000 7 | 8 | launchers: 9 | 10 | bs_firefox: 11 | command: browserstack launch --timeout 120 --attach firefox:22.0 /test_harness.html 12 | protocol: browser 13 | 14 | bs_chrome: 15 | command: browserstack launch --timeout 120 --attach chrome:28.0 /test_harness.html 16 | protocol: browser 17 | 18 | bs_safari_51: 19 | command: browserstack launch --timeout 120 --attach safari:5.1 /test_harness.html 20 | protocol: browser 21 | 22 | bs_ie_8: 23 | command: browserstack launch --timeout 120 --attach ie:8.0 /test_harness.html 24 | protocol: browser 25 | 26 | bs_ie_9: 27 | command: browserstack launch --timeout 120 --attach ie:9.0 /test_harness.html 28 | protocol: browser 29 | 30 | bs_ie_10: 31 | command: browserstack launch --timeout 120 --attach ie:10.0 /test_harness.html 32 | protocol: browser 33 | 34 | bs_android_23: 35 | command: browserstack launch --timeout 240 --attach "Motorola Photon 4G" /test_harness.html 36 | protocol: browser 37 | 38 | bs_android_41: 39 | command: browserstack launch --timeout 240 --attach "Samsung Galaxy S III" /test_harness.html 40 | protocol: browser 41 | 42 | bs_iphone_5: 43 | command: browserstack launch --timeout 60 --attach "iPhone 5" /test_harness.html 44 | protocol: browser 45 | 46 | launch_in_ci: [bs_chrome, bs_firefox, bs_ie_8, bs_ie_9, bs_ie_10, bs_iphone_5, bs_android_41] -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var pkg = require('./package'); 3 | 4 | module.exports = function(grunt) { 5 | grunt.initConfig({ 6 | connect: { 7 | test: { 8 | options: { 9 | hostname: '0.0.0.0', 10 | port: 9999 11 | } 12 | } 13 | }, 14 | browserify: { 15 | dist: { 16 | files: { 17 | 'build/jwt-decode.js': ['standalone.js'], 18 | }, 19 | options: { 20 | debug: true 21 | } 22 | } 23 | }, 24 | uglify: { 25 | options: { 26 | ascii: true 27 | }, min: { 28 | files: { 29 | 'build/jwt-decode.min.js': ['build/jwt-decode.js'] 30 | } 31 | } 32 | }, 33 | clean: { 34 | build: ["build/"], 35 | }, 36 | watch: { 37 | another: { 38 | files: ['node_modules', 'standalone.js', 'lib/*.js'], 39 | tasks: ['build'] 40 | } 41 | }, 42 | exec: { 43 | 'test-phantom': { 44 | cmd: 'node_modules/testem/testem.js -f testem_dev.yml ci -l PhantomJS', 45 | stdout: true, 46 | stderr: true 47 | }, 48 | 'test-desktop': { 49 | cmd: 'node_modules/testem/testem.js ci -l bs_chrome,bs_firefox,bs_ie_8,bs_ie_9,bs_ie_10', 50 | stdout: true, 51 | stderr: true 52 | }, 53 | 'test-mobile': { 54 | cmd: 'node_modules/testem/testem.js ci -l bs_iphone_5', //disable ,bs_android_41: is not working 55 | stdout: true, 56 | stderr: true 57 | } 58 | } 59 | }); 60 | 61 | // Loading dependencies 62 | for (var key in grunt.file.readJSON("package.json").devDependencies) { 63 | if (key !== "grunt" && key.indexOf("grunt") === 0) grunt.loadNpmTasks(key); 64 | } 65 | 66 | grunt.registerTask("build", ["clean", "browserify:dist", "browserify:dist", "uglify:min"]); 67 | grunt.registerTask("dev", ["connect:test", "watch", "build"]); 68 | grunt.registerTask("test", ["exec:test-phantom"]); 69 | grunt.registerTask("integration", ["exec:test-desktop", "exec:test-mobile"]); 70 | }; 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | __jwt-decode__ is a small browser library that helps decoding JWTs token which are Base64Url encoded. 2 | 3 | __IMPORTANT:__ This library doesn't validate the token, any well formed JWT can be decoded. You should validate the token in your server-side logic by using something like [express-jwt](http://github.com/auth0/jwt), [koa-jwt](https://github.com/stiang/koa-jwt), [Owin Bearer JWT](https://github.com/michaelnoonan/Auth0-Owin-JwtBearerAuthentication), etc. 4 | 5 | ## Installation 6 | 7 | Install with npm, bower, or downloading from the build directory of this repository. 8 | 9 | Use with AMD, browserify or just include with an script tag. 10 | 11 | ## Usage 12 | 13 | ~~~javascript 14 | var token = 'eyJ0eXAiO.../// jwt token'; 15 | 16 | var decoded = jwt_decode(token); 17 | console.log(decoded); 18 | 19 | /* prints: 20 | * { foo: "bar", 21 | * exp: 1393286893, 22 | * iat: 1393268893 } 23 | */ 24 | ~~~ 25 | 26 | ## Develop 27 | 28 | Run `grunt dev` and fire a browser at http://localhost:9999/test_harness.html. 29 | 30 | ## License 31 | 32 | The MIT License (MIT) 33 | 34 | Copyright (c) 2014 AUTH10 LLC 35 | 36 | Permission is hereby granted, free of charge, to any person obtaining a copy 37 | of this software and associated documentation files (the "Software"), to deal 38 | in the Software without restriction, including without limitation the rights 39 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 40 | copies of the Software, and to permit persons to whom the Software is 41 | furnished to do so, subject to the following conditions: 42 | 43 | The above copyright notice and this permission notice shall be included in 44 | all copies or substantial portions of the Software. 45 | 46 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 47 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 48 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 49 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 50 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 51 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 52 | THE SOFTWARE. 53 | --------------------------------------------------------------------------------