ninjatronic / angular-base64

Base64 conversion for AngularJS
Other
185 stars 113 forks source link

Error when decoding Base64 with URL Safe #7

Closed ericmdantas closed 9 years ago

ericmdantas commented 9 years ago

When an application is using "Base64 URL Safe" to encode a string, angular-base64's decode will fail.

For example:

Just as a comparison, if you use window.atob(base64here), it'll work just fine;

ninjatronic commented 9 years ago

Hi @ericmdantas,

I'm not sure what you are referring to by "turning on URL Safe". $base64 certainly doesn't expose any url safe methods. Are you sure you aren't trying to use angular-utf8-base64's base64.urlencode?

FWIW, I think window.atob is a fine solution for most circumstances. angular-base64 is partly a homage to Nick's implementation, which I think is very fine indeed, and mostly exists to provide an easily mockable base64 algorithm for testing, and to extend base64 encoding support to browsers like IE10 and IE mobile that don't support window.atob.

ericmdantas commented 9 years ago

Hey @ninjatronic!

The thing is: sometimes the server side will expose some informations in the URL using base64. But since base64 will allow the string to have '+', '/' and '=', the browser would just make a big confusion out of it and not display it correctly - or not display it at all.

So, they'll do the so called "Base64 URL Safe", that makes the string safe to use in any URL.

The thing is, when I receive this kind of base64 modified, the decode will fail.

I think it'd be nice to have the decode to do this internally - so that other people would not have to treat this kind of situation in their code.

I can turn this into a PR, if you want to.

ninjatronic commented 9 years ago

Hi @ericmdantas,

Please see this PR for the feature you are requesting.

On the back of the conversation thread there I still don't see any need to grow the scope of this project, especially considering that

var base64EncodedString = $base64.encode('whatever');
var urlSafeBase64EncodedString = encodeURIComponent(base64EncodedString);
ninjatronic commented 9 years ago

See fe4a2499f4505ded31d3f196ffe923cc9e4c4a9a

ericmdantas commented 9 years ago

Hmm, I didn't know about this function at all. Thanks for pointing it out.