ruby-yadis 0.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/examples/openid.rb CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  require 'yadis'
4
4
 
5
+ # Want to verify server certificates for https?
6
+ # Visit http://curl.haxx.se/docs/caextract.html and uncomment:
7
+ # YADIS.ca_path = '/path/to/cacert.pem'
8
+
5
9
  yadis = YADIS.discover('http://brian.myopenid.com/')
6
10
  if yadis.nil? or yadis.openid_servers.length == 0
7
11
  p 'No XRDS found'
data/lib/yadis/fetcher.rb CHANGED
@@ -1,20 +1,14 @@
1
1
  require "uri"
2
-
3
- begin
4
- require "net/https"
5
- rescue LoadError # no openssl
6
- require "net/http"
7
- HAS_OPENSSL = false
8
- STDERR.puts("No openssl found, won't be able to fetch https URIs.")
9
- else
10
- HAS_OPENSSL = true
11
- end
2
+ require "net/https"
12
3
 
13
4
  class NetHTTPFetcher
14
5
 
6
+ attr_accessor :ca_path
7
+
15
8
  def initialize(read_timeout=20, open_timeout=20)
16
9
  @read_timeout = read_timeout
17
10
  @open_timeout = open_timeout
11
+ @ca_path = nil
18
12
  end
19
13
 
20
14
  def get(url, params = nil)
@@ -36,11 +30,13 @@ class NetHTTPFetcher
36
30
  http.open_timeout = @open_timeout
37
31
 
38
32
  if uri.scheme == 'https'
39
- if HAS_OPENSSL
40
- http.use_ssl = true
41
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
+ http.use_ssl = true
34
+ if @ca_path
35
+ http.ca_file = @ca_path
36
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
42
37
  else
43
- STDERR.puts("Cannot fetch https url without openssl installed: #{uri.to_s}")
38
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
39
+ STDERR.puts("Warning: fetching over https without verifying server certificate")
44
40
  end
45
41
  end
46
42
 
data/lib/yadis/yadis.rb CHANGED
@@ -6,29 +6,50 @@ class YADISParseError < StandardError; end
6
6
  class YADISHTTPError < StandardError; end
7
7
 
8
8
  class YADIS
9
-
10
- attr_reader :uri, :xrds_uri, :xrds
9
+
10
+ @@ca_path = nil
11
+ attr_accessor :uri, :xrds_uri, :xrds
11
12
 
12
13
  # Discover services for a given URI. Please note that no normalization
13
14
  # will be done to the passed in URI, it should be valid before calling
14
15
  # discover.
15
16
  #
16
- # Returns nil if no XRDS was found, or a YADIS object on success.
17
+ # Returns nil if no XRDS was found, or a YADIS object on success. This
18
+ # method is essentially the same as YADIS.new, but does not raise any
19
+ # exceptions.
17
20
  def YADIS.discover(uri)
21
+ return nil unless uri
18
22
  begin
19
- return YADIS.discover_verbose(uri)
23
+ return YADIS.new(uri)
20
24
  rescue
21
25
  return nil
22
26
  end
23
27
  end
24
28
 
25
- # same as YADIS.discover, but raises YADISParseError or YADISHTTPError
26
- # when bad things happen.
27
- def YADIS.discover_verbose(uri)
28
- return nil unless uri
29
+ # Set the path to a certificate authority pem file, for verifying
30
+ # server certificates of HTTPS pages. If you are interested in verifying
31
+ # certs like the mozilla web browser, have a look at the files here:
32
+ #
33
+ # http://curl.haxx.se/docs/caextract.html
34
+ def YADIS.ca_path=(ca_path)
35
+ ca_path = ca_path.to_s
36
+ if File.exists?(ca_path)
37
+ @@ca_path = ca_path
38
+ else
39
+ raise ArgumentError, "#{ca_path} is not a valid file path"
40
+ end
41
+ end
29
42
 
43
+ # Discover services for a URI using the Yadis protocol. +uri+ should
44
+ # be a valid URI represented as a string. This method may raise
45
+ # YADISParseError in the case of an invalid or unparsable XRDS file,
46
+ # or YADISHTTPError is the URI cannot be fetched.
47
+ def initialize(uri)
48
+ http = NetHTTPFetcher.new
49
+ http.ca_path = @@ca_path if @@ca_path
50
+
30
51
  headers = {'Accept' => 'application/xrds+xml'}
31
- response = NetHTTPFetcher.new.get(uri, headers)
52
+ response = http.get(uri, headers)
32
53
  raise YADISHTTPError, "Could not fetch #{uri}" if response.nil?
33
54
 
34
55
  uri, resp_payload = response
@@ -39,7 +60,7 @@ class YADIS
39
60
 
40
61
  if header
41
62
  xrds_uri = header
42
- response = NetHTTPFetcher.new.get(xrds_uri)
63
+ response = http.get(xrds_uri)
43
64
  raise YADISHTTPError, "Could not fetch XRDS #{xrds_uri}" if response.nil?
44
65
  resp_payload = response[1]
45
66
  end
@@ -47,17 +68,13 @@ class YADIS
47
68
  unless resp_payload['content-type'] == 'application/xrds+xml'
48
69
  loc = html_yadis_location(resp_payload.body)
49
70
  unless loc.nil?
50
- xrds_uri, resp_payload = NetHTTPFetcher.new.get(loc)
71
+ xrds_uri, resp_payload = http.get(loc)
51
72
  end
52
73
  end
53
74
 
54
75
  xrds = XRDS.parse(resp_payload.body)
55
76
  raise YADISParseError, "Bad XRDS" if xrds.nil?
56
77
 
57
- return new(uri, xrds_uri, xrds)
58
- end
59
-
60
- def initialize(uri, xrds_uri, xrds)
61
78
  @uri = uri
62
79
  @xrds_uri = xrds_uri
63
80
  @xrds = xrds
data/test/test_yadis.rb CHANGED
@@ -12,7 +12,10 @@ class YADISTestCase < Test::Unit::TestCase
12
12
 
13
13
  uri = 'http://brian.myopenid.com/'
14
14
  xrds_uri = 'http://brian.myopenid.com/xrds'
15
- y = YADIS.new(uri, xrds_uri, xrds)
15
+ y = YADIS.new(uri)
16
+ y.uri = uri
17
+ y.xrds_uri = xrds_uri
18
+ y.xrds_uri = xrds
16
19
 
17
20
  servers = y.openid_servers
18
21
  assert_equal(servers.length, 1)
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ruby-yadis
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2006-03-16 00:00:00 -08:00
6
+ version: 0.2.1
7
+ date: 2006-03-22 00:00:00 -08:00
8
8
  summary: A library for performing Yadis service discovery
9
9
  require_paths:
10
10
  - lib