Skip to content

Commit

Permalink
add oauth hybrid extension + tests. updated gemspec with dependencies…
Browse files Browse the repository at this point in the history
… for testing (oauth/mocha)
  • Loading branch information
Tom Quackenbush committed Jan 21, 2010
1 parent 99d9e3a commit 68a970a
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ SPEC = Gem::Specification.new do |s|
s.has_rdoc = true
s.extra_rdoc_files = ['README','INSTALL','LICENSE','UPGRADE']
s.rdoc_options << '--main' << 'README'
s.add_dependency 'oauth'
s.add_dependency 'mocha'
end
120 changes: 120 additions & 0 deletions lib/openid/extensions/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# An implementation of the OpenID OAuth
# Extension Draft
# see: http://step2.googlecode.com/svn/spec/openid_oauth_extension/latest/openid_oauth_extension.html

require 'oauth'
require 'openid/extension'

module OpenID

module OAuthHybrid
NS_URI = "http://specs.openid.net/extensions/oauth/1.0"

# An OAuth request, sent from a relying
# party to a provider
class Request < Extension
attr_accessor :ns_alias, :ns_uri, :consumer_key, :scope
def initialize(consumer_key=nil, scope=nil)
@ns_alias = 'oauth'
@ns_uri = NS_URI
@consumer_key = consumer_key
@scope = scope
end

# Instantiate a Request object from the arguments in a
# checkid_* OpenID message
# return nil if the extension was not requested.
def self.from_openid_request(oid_req)
oauth_req = new
args = oid_req.message.get_args(NS_URI)
if args == {}
return nil
end
oauth_req.parse_extension_args(args)
return oauth_req
end

# Set the state of this request to be that expressed in these
# OAuth arguments
def parse_extension_args(args)
@consumer_key = args['consumer_key']
@scope = args['scope']
end

def get_extension_args
ns_args = {}
ns_args['consumer_key'] = @consumer_key
ns_args['scope'] = @scope
return ns_args
end
end

# A Provider Authentication Policy response, sent from a provider
# to a relying party
class Response < Extension
attr_accessor :ns_alias, :ns_uri, :request_token, :verifier, :scope
def initialize(request_token=nil, verifier=nil, scope=nil)
@ns_alias = 'oauth'
@ns_uri = NS_URI
@request_token = request_token
@verifier = verifier
@scope = scope
end

# Create a Response object from an OpenID::Consumer::SuccessResponse
def self.from_success_response(success_response)
args = success_response.get_signed_ns(NS_URI)
return nil if args.nil?
oauth_resp = new
oauth_resp.parse_extension_args(args)
return oauth_resp
end

# parse the oauth arguments into the
# internal state of this object
def parse_extension_args(args)
@request_token = args['request_token']
@verifier = args['verifier']
@scope = args['scope']
end

def get_extension_args
ns_args = {}
ns_args['request_token'] = @request_token
ns_args['verifier'] = @verifier
ns_args['scope'] = @scope
return ns_args
end
end

class Consumer

def initialize(key = "", secret = nil, options = {})

config = APP_CONFIG['openid']['oauth_hybrid']['consumer']

key = config['consumer_key'] if key.blank? && config && config['consumer_key']
secret = config['consumer_secret'] if secret.blank? && config && config['consumer_secret']
if options.empty? && config
options = {
:site => config['site'],
:scheme => :header,
:http_method => :post,
:request_token_path => config['request_token_url'],
:access_token_path => config['access_token_url'],
:authorize_path => config['authorize_url']
}
end
@consumer ||= OAuth::Consumer.new(key, secret, options)
end

def exchange_request_token(token = "", oauth_verifier = nil)
request_token = OAuth::RequestToken.new @consumer, token
options={}
options[:oauth_verifier]=oauth_verifier if oauth_verifier
access_token = request_token.get_access_token(options)
access_token
end
end
end
end
165 changes: 165 additions & 0 deletions test/test_oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
require 'openid/extensions/oauth'
require 'openid/message'
require 'openid/server'
require 'openid/consumer/responses'

require 'testutil'
require 'mocha'

module OpenID
module OAuthTest
class OAuthRequestTestCase < Test::Unit::TestCase
def setup
@req = OAuthHybrid::Request.new
end

def test_construct
assert_equal(nil, @req.consumer_key)
assert_equal(nil, @req.scope)
assert_equal('oauth', @req.ns_alias)

req2 = OAuthHybrid::Request.new("my_consumer_key", "my_scope")
assert_equal("my_consumer_key", req2.consumer_key)
assert_equal("my_scope", req2.scope)
end

def test_get_extension_args
assert_equal({'consumer_key' => nil, 'scope' => nil}, @req.get_extension_args)
@req.scope = "my_scope"
assert_equal({'consumer_key' => nil, 'scope' => 'my_scope'}, @req.get_extension_args)
@req.consumer_key = "my_consumer_key"
assert_equal({'consumer_key' => 'my_consumer_key', 'scope' => 'my_scope'}, @req.get_extension_args)
end

def test_parse_extension_args
args = {'consumer_key' => 'my_consumer_key',
'scope' => 'my_scope'}
@req.parse_extension_args(args)
assert_equal('my_consumer_key', @req.consumer_key)
assert_equal('my_scope', @req.scope)
end

def test_parse_extension_args_empty
@req.parse_extension_args({})
assert_equal(nil, @req.consumer_key)
assert_equal(nil, @req.scope)
end

def test_from_openid_request
openid_req_msg = Message.from_openid_args({
'mode' => 'checkid_setup',
'ns' => OPENID2_NS,
'ns.oauth' => OAuthHybrid::NS_URI,
'oauth.consumer_key' => 'oauth_consumer_key',
'oauth.scope' => 'for_testing_only'
})
oid_req = Server::OpenIDRequest.new
oid_req.message = openid_req_msg
req = OAuthHybrid::Request.from_openid_request(oid_req)
assert_equal('oauth_consumer_key', req.consumer_key)
assert_equal('for_testing_only', req.scope)
end

def test_from_openid_request_no_oauth
message = Message.new
openid_req = Server::OpenIDRequest.new
openid_req.message = message
oauth_req = OAuthHybrid::Request.from_openid_request(openid_req)
assert(oauth_req.nil?)
end
end

class DummySuccessResponse
attr_accessor :message

def initialize(message, signed_stuff)
@message = message
@signed_stuff = signed_stuff
end

def get_signed_ns(ns_uri)
return @signed_stuff
end

end

class OAuthResponseTestCase < Test::Unit::TestCase
def setup
@req = OAuthHybrid::Response.new

@oauth_consumer = mock()
@oauth_consumer.stubs(:key => "key", :secret => "secret")
@request_token = mock()
@oauth_consumer.stubs(:token => "my_key", :secret => "my_secret", :consumer => @oauth_consumer)
end

def test_construct
assert_equal(nil, @req.request_token)
assert_equal(nil, @req.scope)
assert_equal('oauth', @req.ns_alias)

req2 = OAuthHybrid::Response.new(@request_token, 'my_verifier', 'my_scope')
assert_equal(@request_token, req2.request_token)
assert_equal('my_verifier', req2.verifier)
assert_equal('my_scope', req2.scope)
end

def test_get_extension_args
assert_equal({'verifier'=>nil, 'request_token' => nil, 'scope' => nil}, @req.get_extension_args)
@req.request_token = @request_token
assert_equal({'verifier'=> nil, 'request_token' => @request_token, 'scope' => nil}, @req.get_extension_args)
@req.scope = 'my_scope'
assert_equal({'verifier'=> nil, 'request_token' => @request_token, 'scope' => 'my_scope'}, @req.get_extension_args)
@req.verifier = 'my_verifier'
assert_equal({'verifier'=> 'my_verifier', 'request_token' => @request_token, 'scope' => 'my_scope'}, @req.get_extension_args)
end

def test_parse_extension_args
args = {'request_token' => @request_token,
'scope' => 'my_scope'}
@req.parse_extension_args(args)
assert_equal(@request_token, @req.request_token)
assert_equal('my_scope', @req.scope)
end

def test_parse_extension_args_empty
@req.parse_extension_args({})
assert_equal(nil, @req.request_token)
assert_equal(nil, @req.scope)
end

def test_from_success_response
openid_req_msg = Message.from_openid_args({
'mode' => 'id_res',
'ns' => OPENID2_NS,
'ns.oauth' => OAuthHybrid::NS_URI,
'oauth.request_token' => @request_token,
'oauth.scope' => 'for_testng_only'
})
signed_stuff = {
'request_token' => @request_token,
'scope' => 'for_testing_only'
}
oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff)
req = OAuthHybrid::Response.from_success_response(oid_req)
assert_equal(@request_token, req.request_token)
assert_equal('for_testing_only', req.scope)
end

def test_from_success_response_unsigned
openid_req_msg = Message.from_openid_args({
'mode' => 'id_res',
'ns' => OPENID2_NS,
'ns.oauth' => OAuthHybrid::NS_URI,
'oauth.request_token' => @request_token,
'oauth.scope' => 'for_testng_only'
})
signed_stuff = {}
endpoint = OpenIDServiceEndpoint.new
oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff)
req = OAuthHybrid::Response.from_success_response(oid_req)
assert(req.nil?, req.inspect)
end
end
end
end

0 comments on commit 68a970a

Please sign in to comment.