How to read http_userid's Base64 encoded userid from Ruby?

Hey,

I am using the http user id module and have not yet figured out how I
can
read the base64 encoded cookie from Ruby. It seems like nginx uses a
different form of base64 than Ruby does…

Example:
| uid value = E2F32BD0F154704B0C176D1D02050303

In Ruby I see the cookie encoded like this:

| {“uid”=>“0Cvz4ktwVPEdbRcMAwMFAg==”}
When I try to decode this value with Base64 in Ruby I get:

| require “base64”
| enc=“0Cvz4ktwVPEdbRcMAwMFAg==”
| => “0Cvz4ktwVPEdbRcMAwMFAg==”
| Base64.decode64(enc)
| => “\320+\363\342KpT\361\035m\027\f\003\003\005\002”

Has anyone an idea about what I am doing wrong here?

-S.

[nginx.conf entries regarding http userid]
| userid on;
| userid_name uid;
| userid_domain foo.com;
| userid_path /;
| userid_expires 720d;
| userid_p3p ‘policyref=“/w3c/p3p.xml”, CP=“ADMa DEVa PSAa PSDa OUR IND DSP NON COR”’;

On Tue, Feb 9, 2010 at 1:20 PM, Sven C. Koehler [email protected]
wrote:

| {“uid”=>“0Cvz4ktwVPEdbRcMAwMFAg==”}
When I try to decode this value with Base64 in Ruby I get:

Hi, Don’t know the details, but the E2F3… thing is not base64. It
looks
more like hex encoding (should read as E2 F3 2B)… see ruby’s
String#unpack
to munge it.

Ok, I’ve found a way to get to the original value, seems like nginx uses
a different byte order than ruby does…

cookie_uid = “0Cvz4ktwVPEdbRcMAwMFAg==”; cc = cookie_uid.unpack(‘m*’).first; rr = cc.split("").map{|c| c[0].to_i}.inject([]) {|v,s| v.push sprintf("%02X", s); v; }.values_at(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12).join("")
=> “E2F32BD0F154704B0C176D1D02050303”

(One could probably write this more elegantly by using the right flags
for
unpack.)

-S.

Hello Sven,
I came across the same problem. But the solution you
mentioned does not work with my program. When I run this code
cookie = CqCx7E5qMNUxrmAUAwMJAg==

cc = cookie.unpack(‘m*’).first; rr = cc.split("").map{|c|
c[0].to_i}.inject([]) {|v,s| v.push sprintf("%02X", s); v;
}.values_at(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13,
12).join("")

I get back the result as “00000000000000000000000100000000” but actually
it should be ECB1A00AD5306A4E1460AE3102090303

Do you know what am i doing wrong?