lib/spark_api/authentication/oauth2.rb in spark_api-1.0.2 vs lib/spark_api/authentication/oauth2.rb in spark_api-1.0.4

- old
+ new

@@ -65,11 +65,10 @@ "response_type" => "code", "redirect_uri" => @provider.redirect_uri } "#{@provider.authorization_uri}?#{build_url_parameters(params)}" end - protected def auth_header {"Authorization"=> "OAuth #{session.access_token}"} @@ -81,15 +80,57 @@ def client @client end end + + #==OpenId/OAuth 2 Hybrid + # Uses OpenId for Authentication, but also uses OAuth2 for authorization. + class OpenIdOAuth2Hybrid < OAuth2 + def authorization_url(parameters={}) + params = openid_parameters.merge(parameters) + params["openid.spark.combined_flow"] = true + build_openid_uri(params) + end + + protected + + def build_openid_uri(params) + "#{@provider.authorization_uri}?#{build_url_parameters(params)}" + end + + def openid_parameters + { + "openid.mode" => "checkid_setup", + "openid.spark.client_id" => @provider.client_id, + "openid.return_to" => @provider.redirect_uri + } + end + end + + class OpenId < OpenIdOAuth2Hybrid + def authorization_url(parameters={}) + params = openid_parameters.merge(parameters) + build_openid_uri(params) + end + + def authenticate + raise RuntimeError, "API Authorization not available with an OpenId-only Auth instance" + end + + def request(method, path, body, options={}) + raise RuntimeError, "API Data not available with an OpenId-only Auth instance" + end + end # Representation of a session with the api using oauth2 class OAuthSession SESSION_ATTRIBUTES = [:access_token, :expires_in, :scope, :refresh_token, :refresh_timeout, :start_time] attr_accessor *SESSION_ATTRIBUTES - def initialize(options={}) + def initialize(o={}) + + options = OptionsHash.new(o) + @access_token = options["access_token"] @expires_in = options["expires_in"] @scope = options["scope"] @refresh_token = options["refresh_token"] @start_time = options.fetch("start_time", DateTime.now)