ruby-on-railsrubyopenidgoogle-openidxrds

Retrieving XRDS document in OpenID using Ruby


I am just playing around with the OpenID protocol. I am trying to send Discovery request and retrieve the XRDS document from google . When I try to do it from the terminal using the curl, I am getting the following output

curl --url "https://www.google.com/accounts/o8/id"
    <?xml version="1.0" encoding="UTF-8"?>
    <xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">
      <XRD>
      <Service priority="0">
      <Type>http://specs.openid.net/auth/2.0/server</Type>
      <Type>http://openid.net/srv/ax/1.0</Type>
      <Type>http://specs.openid.net/extensions/ui/1.0/mode/popup</Type>
      <Type>http://specs.openid.net/extensions/ui/1.0/icon</Type>
      <Type>http://specs.openid.net/extensions/pape/1.0</Type>
      <URI>https://www.google.com/accounts/o8/ud</URI>
      </Service>
      </XRD>
    </xrds:XRDS>

When I try to do the same from the ruby code, It gives me a 302 error and the url to which it has moved points to the same request url.

<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://www.google.com/accounts/o8/id">here</A>.
</BODY>
</HTML>

Code

  require 'net/http'
  require 'net/https'
  require 'uri'
  http = Net::HTTP.new(uri.host, uri.port)

  response =  Net::HTTP.get_response(URI.parse("http://www.google.com/accounts/o8/id"))
  puts "#{response.read_body}"

How to get the XRDS through the code and why is it showing different outputs. Can someone explain it?Thanks


Solution

  • Google expects the https protocol, though in your ruby example you use http, hence the 302 error. The following snippet should get you the xrds document:

    require 'net/http'
    require 'net/https'
    require 'uri'
    
    uri = URI.parse('https://www.google.com/accounts/o8/id')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
    puts "#{response.read_body}"