Code Monkey home page Code Monkey logo

jsonwebtokens.cfc's Introduction

JSONWebTokens.cfc - A ColdFusion Gateway For JSON Web Tokens

by Ben Nadel (on Google+)

This is a small ColdFusion module to facilitate the encoding and decoding of JSON Web Tokens, or JWT. In order to start working with JSON Web Tokens, you can either use the .encode() and .decode() methods provided by the core gateway:

var jwt = new lib.JsonWebTokens();

var jwtToken = jwt.encode( somePayload, "HS256", "secretKey" );
var payload = jwt.decode( jwtToken, "HS256", "secretKey" );

Or, if you intend to use the same encoding and decoding methods over and over again in a particular application, it can be more efficient to instantiate and cache a JSON Web Token client:

var client = new lib.JsonWebTokens().createClient( "HS256", "secretKey" );

var jwtToken = client.encode( somePayload );
var payload = client.decode( jwtToken );

By doing this, you can pass around your JWT implementation without having to pass around your secret key or your signing algorithm.

Security

At this time, the ColdFusion JSON Web Token library supports both Hmac and RSA based signing:

  • HS256 ( uses HmacSHA256 as the underlying Java algorithm).
  • HS384 ( uses HmacSHA384 as the underlying Java algorithm).
  • HS512 ( uses HmacSHA512 as the underlying Java algorithm).
  • RS256 ( uses SHA256withRSA as the underlying Java algorithm).
  • RS384 ( uses SHA384withRSA as the underlying Java algorithm).
  • RS512 ( uses SHA512withRSA as the underlying Java algorithm).

However, since the round-trip RSA encryption algorithm requires both a public key (for verification) and a private key (for signing), the private key is required when using RSA-based methods or creating an RSA-based client:

// Create an Hmac-based client.
var client = new lib.JsonWebTokens().createClient( "HS256", "secret" );

// Create an RSA-based client.
var client = new lib.JsonWebTokens().createClient( "RS256", "publicKey", "privateKey" );

// Or, just use the .encode() and .decode() methods directly:
var jwt = new lib.JsonWebTokens();

// Encode a payload:
jwt.encode( somePayload, "HS256", "secretKey" );
jwt.encode( somePayload, "RS256", "publicKey", "privateKey" );

// Decode a payload:
jwt.decode( jwtToken, "HS256", "secretKey" );
jwt.decode( jwtToken, "RS256", "publicKey", "privateKey" );

When using the RSA encryption algorithm, the public and private keys are assumed to be provided in plain-text PEM format.

NOTE: PEM (Privacy Enhanced Mail) format is a Base64 encoded DER certificate commonly used on servers.

The JSON Web Token library prevent anonymous / pre-verified tokens, and for security purposes, never uses the algorithm presented in the header. All decoding / verifying is required to use the internal signer with a specific algorithm so as to prevent abuse.

jsonwebtokens.cfc's People

Contributors

bennadel avatar rayvarner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jsonwebtokens.cfc's Issues

Doesn't work in Lucee 5.x

I didn't spend a lot of time on this, but I got it working by scoping global variables in the CFCs.

Also it might be worth noting that real-world examples would likely require quotes around the payload properties.

Hit me up if you're interested in knowing what I did. Otherwise, if this project isn't really needed anymore and a different library would be better, adding that to the docs would be great.

Not working with certificate

I'm working with an authentication system that has given me a x509 certificate for the public key and when I try to decode the token using this library, I get this error: "Invalid RSA public key encoding."
I am able to successfully validate the signature on the jwt.io decoder so I'm pretty sure it's not a problem with the tokens or the certificate, but rather something in the Java being used by Coldfusion. I've tried tweaking it as much as I know how and I've gotten nowhere. Do you have any suggestions?

I'm pretty sure my code should work:
<cfset jwt = new lib.JsonWebTokens()>
<cfset payload = jwt.decode( token, "RS256", "#certString#" )>
Or am I missing something?
Thanks!

Url base 64 is broken

Hello,

I ported the code to an old version of CFML. I don't know if you face this problem, but the padding function was broken :

	function convertToBase64( input ) {
		input = replace( input, "-", "+", "all" );
		input = replace( input, "_", "/", "all" );
		var paddingLength = ( 4 - ( len( input ) mod 4 ) );
		return( input & repeatString( "=", paddingLength ) );
	}

When the input length is a multiple of 4, the modulo yields 0, so 4 - 0 yields 4, and we have a useless padding ==== added to the string.

If figured out with this simple payload : 'payload' (i.e. the simple word "payload" as a string. The JSON version ("payload") will be transformed to "payload when decoding (so there is a missing quote), and the JSON unserialize will just yield null.

I'm not 100% sure wether this behaviour is because of the unwanted padding, but the problem disappears with this dumb version of the function :

	function convertToBase64( input ) {
		input = replace( input, "-", "+", "all" );
		input = replace( input, "_", "/", "all" );
		switch(len( input ) mod 4) {
			case 1: return input & '===';
			case 2: return input & '==';
			case 3: return input & '=';
			default: return input;
		}
	}

I will not make a pull request because of the old version of the code we are using and because I'm not sure of what is the deep truth about this :)

Cheers.

Not working with google api JWT

I've tested this lib with google api JWT, but I got error:

can't decode the base64 input string [notasecret], because the input 
string has an invalid length

notasecret is the default password for google Jwt. The error is probably due to Base64url encoding.

Something like this:

reReplace(reReplace(reReplace(str, "\+", "-", "all"), "\/", "_", "all"),"=", "", "all")

could resolve the issue.

Here are some reference: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.