C0 code coverage information
Generated on Fri Jul 11 15:55:29 -0700 2008 with rcov 0.7.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 require "openid/message"
2 require "openid/util"
3
4 module OpenID
5 class Consumer
6 # An object that holds the state necessary for generating an
7 # OpenID authentication request. This object holds the association
8 # with the server and the discovered information with which the
9 # request will be made.
10 #
11 # It is separate from the consumer because you may wish to add
12 # things to the request before sending it on its way to the
13 # server. It also has serialization options that let you encode
14 # the authentication request as a URL or as a form POST.
15 class CheckIDRequest
16 attr_accessor :return_to_args, :message
17 attr_reader :endpoint
18
19 # Users of this library should not create instances of this
20 # class. Instances of this class are created by the library
21 # when needed.
22 def initialize(assoc, endpoint)
23 @assoc = assoc
24 @endpoint = endpoint
25 @return_to_args = {}
26 @message = Message.new(endpoint.preferred_namespace)
27 @anonymous = false
28 end
29
30 attr_reader :anonymous
31
32 # Set whether this request should be made anonymously. If a
33 # request is anonymous, the identifier will not be sent in the
34 # request. This is only useful if you are making another kind of
35 # request with an extension in this request.
36 #
37 # Anonymous requests are not allowed when the request is made
38 # with OpenID 1.
39 def anonymous=(is_anonymous)
40 if is_anonymous && @message.is_openid1
41 raise ArgumentError, ("OpenID1 requests MUST include the "\
42 "identifier in the request")
43 end
44 @anonymous = is_anonymous
45 end
46
47 # Add an object that implements the extension interface for
48 # adding arguments to an OpenID message to this checkid request.
49 #
50 # extension_request: an OpenID::Extension object.
51 def add_extension(extension_request)
52 extension_request.to_message(@message)
53 end
54
55 # Add an extension argument to this OpenID authentication
56 # request. You probably want to use add_extension and the
57 # OpenID::Extension interface.
58 #
59 # Use caution when adding arguments, because they will be
60 # URL-escaped and appended to the redirect URL, which can easily
61 # get quite long.
62 def add_extension_arg(namespace, key, value)
63 @message.set_arg(namespace, key, value)
64 end
65
66 # Produce a OpenID::Message representing this request.
67 #
68 # Not specifying a return_to URL means that the user will not be
69 # returned to the site issuing the request upon its completion.
70 #
71 # If immediate mode is requested, the OpenID provider is to send
72 # back a response immediately, useful for behind-the-scenes
73 # authentication attempts. Otherwise the OpenID provider may
74 # engage the user before providing a response. This is the
75 # default case, as the user may need to provide credentials or
76 # approve the request before a positive response can be sent.
77 def get_message(realm, return_to=nil, immediate=false)
78 if !return_to.nil?
79 return_to = Util.append_args(return_to, @return_to_args)
80 elsif immediate
81 raise ArgumentError, ('"return_to" is mandatory when using '\
82 '"checkid_immediate"')
83 elsif @message.is_openid1
84 raise ArgumentError, ('"return_to" is mandatory for OpenID 1 '\
85 'requests')
86 elsif @return_to_args.empty?
87 raise ArgumentError, ('extra "return_to" arguments were specified, '\
88 'but no return_to was specified')
89 end
90
91
92 message = @message.copy
93
94 mode = immediate ? 'checkid_immediate' : 'checkid_setup'
95 message.set_arg(OPENID_NS, 'mode', mode)
96
97 realm_key = message.is_openid1 ? 'trust_root' : 'realm'
98 message.set_arg(OPENID_NS, realm_key, realm)
99
100 if !return_to.nil?
101 message.set_arg(OPENID_NS, 'return_to', return_to)
102 end
103
104 if not @anonymous
105 if @endpoint.is_op_identifier
106 # This will never happen when we're in OpenID 1
107 # compatibility mode, as long as is_op_identifier()
108 # returns false whenever preferred_namespace returns
109 # OPENID1_NS.
110 claimed_id = request_identity = IDENTIFIER_SELECT
111 else
112 request_identity = @endpoint.get_local_id
113 claimed_id = @endpoint.claimed_id
114 end
115
116 # This is true for both OpenID 1 and 2
117 message.set_arg(OPENID_NS, 'identity', request_identity)
118
119 if message.is_openid2
120 message.set_arg(OPENID2_NS, 'claimed_id', claimed_id)
121 end
122 end
123
124 if @assoc
125 message.set_arg(OPENID_NS, 'assoc_handle', @assoc.handle)
126 assoc_log_msg = "with assocication #{@assoc.handle}"
127 else
128 assoc_log_msg = 'using stateless mode.'
129 end
130
131 Util.log("Generated #{mode} request to #{@endpoint.server_url} "\
132 "#{assoc_log_msg}")
133 return message
134 end
135
136 # Returns a URL with an encoded OpenID request.
137 #
138 # The resulting URL is the OpenID provider's endpoint URL with
139 # parameters appended as query arguments. You should redirect
140 # the user agent to this URL.
141 #
142 # OpenID 2.0 endpoints also accept POST requests, see
143 # 'send_redirect?' and 'form_markup'.
144 def redirect_url(realm, return_to=nil, immediate=false)
145 message = get_message(realm, return_to, immediate)
146 return message.to_url(@endpoint.server_url)
147 end
148
149 # Get html for a form to submit this request to the IDP.
150 #
151 # form_tag_attrs is a hash of attributes to be added to the form
152 # tag. 'accept-charset' and 'enctype' have defaults that can be
153 # overridden. If a value is supplied for 'action' or 'method',
154 # it will be replaced.
155 def form_markup(realm, return_to=nil, immediate=false,
156 form_tag_attrs=nil)
157 message = get_message(realm, return_to, immediate)
158 return message.to_form_markup(@endpoint.server_url, form_tag_attrs)
159 end
160
161 # Get a complete HTML document that autosubmits the request to the IDP
162 # with javascript. This method wraps form_markup - see that method's
163 # documentation for help with the parameters.
164 def html_markup(realm, return_to=nil, immediate=false,
165 form_tag_attrs=nil)
166 Util.auto_submit_html(form_markup(realm,
167 return_to,
168 immediate,
169 form_tag_attrs))
170 end
171
172 # Should this OpenID authentication request be sent as a HTTP
173 # redirect or as a POST (form submission)?
174 #
175 # This takes the same parameters as redirect_url or form_markup
176 def send_redirect?(realm, return_to=nil, immediate=false)
177 if @endpoint.compatibility_mode
178 return true
179 else
180 url = redirect_url(realm, return_to, immediate)
181 return url.length <= OPENID1_URL_LIMIT
182 end
183 end
184 end
185 end
186 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.