Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added new OpenID OAuth extension

  • Loading branch information...
commit 6e52ed41f04667e5bb2bc98956c536a18bc43342 1 parent 3714803
@pelle pelle authored
Showing with 266 additions and 0 deletions.
  1. +91 −0 lib/openid/extensions/oauth.rb
  2. +175 −0 test/test_oauth.rb
View
91 lib/openid/extensions/oauth.rb
@@ -0,0 +1,91 @@
+# An implementation of the OpenID OAuth Extension
+# Extension 1.0
+# see: http://openid.net/specs/
+
+require 'openid/extension'
+
+module OpenID
+
+ module OAuth
+ NS_URI = "http://specs.openid.net/extensions/oauth/1.0"
+ # An OAuth token request, sent from a relying
+ # party to a provider
+ class Request < Extension
+ attr_accessor :consumer, :scope, :ns_alias, :ns_uri
+ def initialize(consumer=nil, scope=nil)
+ @ns_alias = 'oauth'
+ @ns_uri = NS_URI
+ @consumer = consumer
+ @scope = scope
+ end
+
+
+ def get_extension_args
+ ns_args = {}
+ ns_args['consumer'] = @consumer if @consumer
+ ns_args['scope'] = @scope if @scope
+ return ns_args
+ 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 = args["consumer"]
+ @scope = args["scope"]
+ end
+
+ end
+
+ # A OAuth request token response, sent from a provider
+ # to a relying party
+ class Response < Extension
+ attr_accessor :request_token, :scope
+ def initialize(request_token=nil, scope=nil)
+ @ns_alias = 'oauth'
+ @ns_uri = NS_URI
+ @request_token = request_token
+ @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 request arguments into the
+ # internal state of this object
+ # if strict is specified, raise an exception when bad data is
+ # encountered
+ def parse_extension_args(args, strict=false)
+ @request_token = args["request_token"]
+ @scope = args["scope"]
+ end
+
+ def get_extension_args
+ ns_args = {}
+ ns_args['request_token'] = @request_token if @request_token
+ ns_args['scope'] = @scope if @scope
+ return ns_args
+ end
+
+ end
+ end
+
+end
View
175 test/test_oauth.rb
@@ -0,0 +1,175 @@
+require 'openid/extensions/oauth'
+require 'openid/message'
+require 'openid/server'
+require 'openid/consumer/responses'
+require 'openid/consumer/discovery'
+
+module OpenID
+ module OAuthTest
+ class OAuthRequestTestCase < Test::Unit::TestCase
+ def setup
+ @req = OAuth::Request.new
+ end
+
+ def test_construct
+ assert_nil(@req.consumer)
+ assert_nil(@req.scope)
+ assert_equal('oauth', @req.ns_alias)
+
+ req2 = OAuth::Request.new("CONSUMER","http://sample.com/some_scope")
+ assert_equal("CONSUMER",req2.consumer)
+ assert_equal("http://sample.com/some_scope",req2.scope)
+ end
+
+ def test_add_consumer
+ @req.consumer="CONSUMER"
+ assert_equal("CONSUMER",@req.consumer)
+ end
+
+ def test_add_scope
+ @req.scope="http://sample.com/some_scope"
+ assert_equal("http://sample.com/some_scope",@req.scope)
+ end
+
+ def test_get_extension_args
+ assert_equal({}, @req.get_extension_args)
+ @req.consumer="CONSUMER"
+ assert_equal({'consumer' => 'CONSUMER'}, @req.get_extension_args)
+ @req.scope="http://sample.com/some_scope"
+ assert_equal({'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
+ end
+
+ def test_parse_extension_args
+ args = {'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}
+ @req.parse_extension_args(args)
+ assert_equal("CONSUMER",@req.consumer)
+ assert_equal("http://sample.com/some_scope",@req.scope)
+ end
+
+ def test_parse_extension_args_empty
+ @req.parse_extension_args({})
+ assert_nil( @req.consumer )
+ assert_nil( @req.scope )
+ end
+
+ def test_from_openid_request
+ openid_req_msg = Message.from_openid_args({
+ 'mode' => 'checkid_setup',
+ 'ns' => OPENID2_NS,
+ 'ns.oauth' => OAuth::NS_URI,
+ 'oauth.consumer' => 'CONSUMER',
+ 'oauth.scope' => "http://sample.com/some_scope"
+ })
+ oid_req = Server::OpenIDRequest.new
+ oid_req.message = openid_req_msg
+ req = OAuth::Request.from_openid_request(oid_req)
+ assert_equal("CONSUMER",req.consumer)
+ assert_equal("http://sample.com/some_scope",req.scope)
+ end
+
+ def test_from_openid_request_no_oauth
+ message = Message.new
+ openid_req = Server::OpenIDRequest.new
+ openid_req.message = message
+ oauth_req = OAuth::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 = OAuth::Response.new
+ end
+
+ def test_construct
+ assert_nil(@req.request_token)
+ assert_nil(@req.scope)
+
+ req2 = OAuth::Response.new("REQUESTTOKEN","http://sample.com/some_scope")
+ assert_equal("REQUESTTOKEN",req2.request_token)
+ assert_equal("http://sample.com/some_scope",req2.scope)
+ end
+
+ def test_add_request_token
+ @req.request_token="REQUESTTOKEN"
+ assert_equal("REQUESTTOKEN",@req.request_token)
+ end
+
+ def test_add_scope
+ @req.scope="http://sample.com/some_scope"
+ assert_equal("http://sample.com/some_scope",@req.scope)
+ end
+
+ def test_get_extension_args
+ assert_equal({}, @req.get_extension_args)
+ @req.request_token="REQUESTTOKEN"
+ assert_equal({'request_token' => 'REQUESTTOKEN'}, @req.get_extension_args)
+ @req.scope="http://sample.com/some_scope"
+ assert_equal({'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
+ end
+
+ def test_parse_extension_args
+ args = {'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}
+ @req.parse_extension_args(args)
+ assert_equal("REQUESTTOKEN",@req.request_token)
+ assert_equal("http://sample.com/some_scope",@req.scope)
+ end
+
+ def test_parse_extension_args_empty
+ @req.parse_extension_args({})
+ assert_nil( @req.request_token )
+ assert_nil( @req.scope )
+ end
+
+ def test_from_success_response
+
+ openid_req_msg = Message.from_openid_args({
+ 'mode' => 'id_res',
+ 'ns' => OPENID2_NS,
+ 'ns.oauth' => OAuth::NS_URI,
+ 'ns.oauth' => OAuth::NS_URI,
+ 'oauth.request_token' => 'REQUESTTOKEN',
+ 'oauth.scope' => "http://sample.com/some_scope"
+ })
+ signed_stuff = {
+ 'request_token' => 'REQUESTTOKEN',
+ 'scope' => "http://sample.com/some_scope"
+ }
+ oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff)
+ req = OAuth::Response.from_success_response(oid_req)
+ assert_equal("REQUESTTOKEN",req.request_token)
+ assert_equal("http://sample.com/some_scope",req.scope)
+ end
+
+ def test_from_success_response_unsigned
+ openid_req_msg = Message.from_openid_args({
+ 'mode' => 'id_res',
+ 'ns' => OPENID2_NS,
+ 'ns.oauth' => OAuth::NS_URI,
+ 'oauth.request_token' => 'REQUESTTOKEN',
+ 'oauth.scope' => "http://sample.com/some_scope"
+ })
+ signed_stuff = {}
+ endpoint = OpenIDServiceEndpoint.new
+ oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff)
+ req = OAuth::Response.from_success_response(oid_req)
+ assert(req.nil?, req.inspect)
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.