Skip to content

Commit ac06dbe

Browse files
author
Lilli
committed
Added the following patch from the [email protected] mailing list:
http://lists.openidenabled.com/pipermail/dev/attachments/20090926/9e3e0f5b/attachment-0001.bin Original Message: pelle at stakeventures.com Sat Sep 26 15:30:46 PDT 2009 darcs patch: Added support for the new OpenID OAuth extension. This patch was in the form of a Darcs patch, not a normal patch. So solve this, I applied it to the Darcs repository found on openidenabled, then created a new diff file between the original Darcs repo and the new one (with the patch applied) so that I could apply it to this git repo. All hunks in patch succeeded.
1 parent 16e8cf1 commit ac06dbe

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

lib/openid/extensions/oauth.rb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# An implementation of the OpenID OAuth Extension
2+
# Extension 1.0
3+
# see: http://openid.net/specs/
4+
5+
require 'openid/extension'
6+
7+
module OpenID
8+
9+
module OAuth
10+
NS_URI = "http://specs.openid.net/extensions/oauth/1.0"
11+
# An OAuth token request, sent from a relying
12+
# party to a provider
13+
class Request < Extension
14+
attr_accessor :consumer, :scope, :ns_alias, :ns_uri
15+
def initialize(consumer=nil, scope=nil)
16+
@ns_alias = 'oauth'
17+
@ns_uri = NS_URI
18+
@consumer = consumer
19+
@scope = scope
20+
end
21+
22+
23+
def get_extension_args
24+
ns_args = {}
25+
ns_args['consumer'] = @consumer if @consumer
26+
ns_args['scope'] = @scope if @scope
27+
return ns_args
28+
end
29+
30+
# Instantiate a Request object from the arguments in a
31+
# checkid_* OpenID message
32+
# return nil if the extension was not requested.
33+
def self.from_openid_request(oid_req)
34+
oauth_req = new
35+
args = oid_req.message.get_args(NS_URI)
36+
if args == {}
37+
return nil
38+
end
39+
oauth_req.parse_extension_args(args)
40+
return oauth_req
41+
end
42+
43+
# Set the state of this request to be that expressed in these
44+
# OAuth arguments
45+
def parse_extension_args(args)
46+
@consumer = args["consumer"]
47+
@scope = args["scope"]
48+
end
49+
50+
end
51+
52+
# A OAuth request token response, sent from a provider
53+
# to a relying party
54+
class Response < Extension
55+
attr_accessor :request_token, :scope
56+
def initialize(request_token=nil, scope=nil)
57+
@ns_alias = 'oauth'
58+
@ns_uri = NS_URI
59+
@request_token = request_token
60+
@scope = scope
61+
end
62+
63+
# Create a Response object from an OpenID::Consumer::SuccessResponse
64+
def self.from_success_response(success_response)
65+
args = success_response.get_signed_ns(NS_URI)
66+
return nil if args.nil?
67+
oauth_resp = new
68+
oauth_resp.parse_extension_args(args)
69+
return oauth_resp
70+
end
71+
72+
# parse the oauth request arguments into the
73+
# internal state of this object
74+
# if strict is specified, raise an exception when bad data is
75+
# encountered
76+
def parse_extension_args(args, strict=false)
77+
@request_token = args["request_token"]
78+
@scope = args["scope"]
79+
end
80+
81+
def get_extension_args
82+
ns_args = {}
83+
ns_args['request_token'] = @request_token if @request_token
84+
ns_args['scope'] = @scope if @scope
85+
return ns_args
86+
end
87+
88+
end
89+
end
90+
91+
end

test/test_oauth.rb

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
require 'openid/extensions/oauth'
2+
require 'openid/message'
3+
require 'openid/server'
4+
require 'openid/consumer/responses'
5+
require 'openid/consumer/discovery'
6+
7+
module OpenID
8+
module OAuthTest
9+
class OAuthRequestTestCase < Test::Unit::TestCase
10+
def setup
11+
@req = OAuth::Request.new
12+
end
13+
14+
def test_construct
15+
assert_nil(@req.consumer)
16+
assert_nil(@req.scope)
17+
assert_equal('oauth', @req.ns_alias)
18+
19+
req2 = OAuth::Request.new("CONSUMER","http://sample.com/some_scope")
20+
assert_equal("CONSUMER",req2.consumer)
21+
assert_equal("http://sample.com/some_scope",req2.scope)
22+
end
23+
24+
def test_add_consumer
25+
@req.consumer="CONSUMER"
26+
assert_equal("CONSUMER",@req.consumer)
27+
end
28+
29+
def test_add_scope
30+
@req.scope="http://sample.com/some_scope"
31+
assert_equal("http://sample.com/some_scope",@req.scope)
32+
end
33+
34+
def test_get_extension_args
35+
assert_equal({}, @req.get_extension_args)
36+
@req.consumer="CONSUMER"
37+
assert_equal({'consumer' => 'CONSUMER'}, @req.get_extension_args)
38+
@req.scope="http://sample.com/some_scope"
39+
assert_equal({'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
40+
end
41+
42+
def test_parse_extension_args
43+
args = {'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}
44+
@req.parse_extension_args(args)
45+
assert_equal("CONSUMER",@req.consumer)
46+
assert_equal("http://sample.com/some_scope",@req.scope)
47+
end
48+
49+
def test_parse_extension_args_empty
50+
@req.parse_extension_args({})
51+
assert_nil( @req.consumer )
52+
assert_nil( @req.scope )
53+
end
54+
55+
def test_from_openid_request
56+
openid_req_msg = Message.from_openid_args({
57+
'mode' => 'checkid_setup',
58+
'ns' => OPENID2_NS,
59+
'ns.oauth' => OAuth::NS_URI,
60+
'oauth.consumer' => 'CONSUMER',
61+
'oauth.scope' => "http://sample.com/some_scope"
62+
})
63+
oid_req = Server::OpenIDRequest.new
64+
oid_req.message = openid_req_msg
65+
req = OAuth::Request.from_openid_request(oid_req)
66+
assert_equal("CONSUMER",req.consumer)
67+
assert_equal("http://sample.com/some_scope",req.scope)
68+
end
69+
70+
def test_from_openid_request_no_oauth
71+
message = Message.new
72+
openid_req = Server::OpenIDRequest.new
73+
openid_req.message = message
74+
oauth_req = OAuth::Request.from_openid_request(openid_req)
75+
assert(oauth_req.nil?)
76+
end
77+
78+
end
79+
80+
class DummySuccessResponse
81+
attr_accessor :message
82+
83+
def initialize(message, signed_stuff)
84+
@message = message
85+
@signed_stuff = signed_stuff
86+
end
87+
88+
def get_signed_ns(ns_uri)
89+
return @signed_stuff
90+
end
91+
92+
end
93+
94+
class OAuthResponseTestCase < Test::Unit::TestCase
95+
def setup
96+
@req = OAuth::Response.new
97+
end
98+
99+
def test_construct
100+
assert_nil(@req.request_token)
101+
assert_nil(@req.scope)
102+
103+
req2 = OAuth::Response.new("REQUESTTOKEN","http://sample.com/some_scope")
104+
assert_equal("REQUESTTOKEN",req2.request_token)
105+
assert_equal("http://sample.com/some_scope",req2.scope)
106+
end
107+
108+
def test_add_request_token
109+
@req.request_token="REQUESTTOKEN"
110+
assert_equal("REQUESTTOKEN",@req.request_token)
111+
end
112+
113+
def test_add_scope
114+
@req.scope="http://sample.com/some_scope"
115+
assert_equal("http://sample.com/some_scope",@req.scope)
116+
end
117+
118+
def test_get_extension_args
119+
assert_equal({}, @req.get_extension_args)
120+
@req.request_token="REQUESTTOKEN"
121+
assert_equal({'request_token' => 'REQUESTTOKEN'}, @req.get_extension_args)
122+
@req.scope="http://sample.com/some_scope"
123+
assert_equal({'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
124+
end
125+
126+
def test_parse_extension_args
127+
args = {'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}
128+
@req.parse_extension_args(args)
129+
assert_equal("REQUESTTOKEN",@req.request_token)
130+
assert_equal("http://sample.com/some_scope",@req.scope)
131+
end
132+
133+
def test_parse_extension_args_empty
134+
@req.parse_extension_args({})
135+
assert_nil( @req.request_token )
136+
assert_nil( @req.scope )
137+
end
138+
139+
def test_from_success_response
140+
141+
openid_req_msg = Message.from_openid_args({
142+
'mode' => 'id_res',
143+
'ns' => OPENID2_NS,
144+
'ns.oauth' => OAuth::NS_URI,
145+
'ns.oauth' => OAuth::NS_URI,
146+
'oauth.request_token' => 'REQUESTTOKEN',
147+
'oauth.scope' => "http://sample.com/some_scope"
148+
})
149+
signed_stuff = {
150+
'request_token' => 'REQUESTTOKEN',
151+
'scope' => "http://sample.com/some_scope"
152+
}
153+
oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff)
154+
req = OAuth::Response.from_success_response(oid_req)
155+
assert_equal("REQUESTTOKEN",req.request_token)
156+
assert_equal("http://sample.com/some_scope",req.scope)
157+
end
158+
159+
def test_from_success_response_unsigned
160+
openid_req_msg = Message.from_openid_args({
161+
'mode' => 'id_res',
162+
'ns' => OPENID2_NS,
163+
'ns.oauth' => OAuth::NS_URI,
164+
'oauth.request_token' => 'REQUESTTOKEN',
165+
'oauth.scope' => "http://sample.com/some_scope"
166+
})
167+
signed_stuff = {}
168+
endpoint = OpenIDServiceEndpoint.new
169+
oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff)
170+
req = OAuth::Response.from_success_response(oid_req)
171+
assert(req.nil?, req.inspect)
172+
end
173+
end
174+
end
175+
end

0 commit comments

Comments
 (0)