Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL-safe Base64 for use in routes and params #11

Merged
merged 1 commit into from
Aug 18, 2014

Conversation

bencrouse
Copy link
Contributor

Two questions:

  1. I used Base64.urlsafe_encode64 instead because that seemed to make sense, that ok?
  2. Should it locate with a base64 encoded signed gid? If so, I can add another commit for that.

# Not a base64 string
end

parse(decoded)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline the whole thing: parse Base64.urlsafe_decode64(gid) rescue ''

@bencrouse
Copy link
Contributor Author

Added style clean up from @dhh. Apologies for forgetting to put it in a feature branch.

@dhh
Copy link
Member

dhh commented Aug 18, 2014

Would be great if we could map == to something else ala https://github.com/cheerful/URLcrypt/blob/master/lib/URLcrypt.rb#L18 to get a prettier token.

@dhh
Copy link
Member

dhh commented Aug 18, 2014

Also, no, we don't need to base64 a sgid. It's already ready to go as a token.

end

def parse_base64(gid)
parse Base64.urlsafe_decode64(gid) rescue ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Base64 decoding fails, can just return nil rather than trying to parse an empty string.

@bencrouse
Copy link
Contributor Author

I made changes based on the comments. @dhh, I took a simpler route to cleaning up the padding equals signs. Let me know if there was specific reason you linked to that URLcrypt method.

def to_param
Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are valid chars in the base64 encoding, so removing them will munge the data. If we don't want them in the output, we'll need to tweak the encoding or use something else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I looked at http://en.wikipedia.org/wiki/Base64 and it seemed ok since these ending equals signs are always padding to make blocks of 4 chars. I'll look at other solutions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it does look like = is the only unwanted char, and it is safe to remove if we manage the padding ourselves, as you do. Nice. I thought ? and & were present as well, but it appears not. So we should be good to go with urlsafe_encode64!

@bencrouse
Copy link
Contributor Author

Think I got everything, awaiting review.


test 'to param' do
assert_equal 'Z2lkOi8vYmN4L1BlcnNvbi81--bd2dab1418d8577e10cf93f8ec055b4b61690755', @person_sgid.to_param
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think a little clearer to test assert_equal @person_sgid.to_s, @person_sgid.to_param

We just care that to_param is the string representation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change

@jeremy
Copy link
Member

jeremy commented Aug 18, 2014

Nice! Could you rebase & squash your commits?

@bencrouse
Copy link
Contributor Author

Cool, I've squashed to a single commit.

jeremy added a commit that referenced this pull request Aug 18, 2014
URL-safe Base64 for use in routes and params
@jeremy jeremy merged commit c71338b into rails:master Aug 18, 2014
@jeremy
Copy link
Member

jeremy commented Aug 18, 2014

Thank you @bencrouse !

@bencrouse
Copy link
Contributor Author

Glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants