I have a REST API that is HTTP Basic Auth enabled.
My user name is an email address: "email@domain.com" and my password
is "asdf"
Logging in and everything work fine in the browser or curl. However,
I'm lost at how to use an email for a username with ActiveResource. If
I don't escape the '@' in the email I get a bad uri error:
and If I escape the '@' with '%40' it's taken literally and the
username is passed to rails as 'email%40domain.com' and subsequently
fails authentication.
I have the same problem and don't yet have a solution for you but
observe that another REST client, the gem rally_rest_api (see
rest_builder.rb #send_request), handles this by not relying on URI
parsing. Instead they create a Net::HTTP::Get and then call
#basic_auth username, password which set the credentials directly in
the HTTP header. I haven't been successful in escaping the @ sign and
suspect that it may be necessary to patch ActiveResource to behave
like rest_builder.rb.
John-Mason Shackelford
Software Development Coach
Pearson
2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
john-mason.shackelford@pearson.com
I don't really have this issue anymore 'cause I moved to a token based
auth system for my API. Regardless, I'm still thinking about filing a
bug report in the rails trac, I'm just not sure if this is exceptional
behavior or not...
class ActiveResource::Base
# Pass username and password directly to the connection
# rather than on the URI so that we can add Basic Authorization
headers
# for usernames which would not be permitted on the URI such as
those in the
# of email addresses.
def self.auth_as(username,password)
self.connection.basic_auth_user = username
self.connection.basic_auth_pass = password
end
end
A cop out? We're not out to win any awards... I personally think
encoding it in the url is a bit ugly. You can use the #site attribute
to set the userinfo (site is an instance of URI), but it falls for the
same encoding problem. If someone wants to write a quick patch for
URI, that'd be fine I suppose. But in the end, I went with something
similar to what John proposed.