class Rack::OAuth2::Client
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/rack/oauth2/client.rb, line 8 def initialize(attributes = {}) (required_attributes + optional_attributes).each do |key| self.send :"#{key}=", attributes[key] end @grant = Grant::ClientCredentials.new @authorization_endpoint ||= '/oauth2/authorize' @token_endpoint ||= '/oauth2/token' attr_missing! end
Public Instance Methods
access_token!(*args)
click to toggle source
# File lib/rack/oauth2/client.rb, line 60 def access_token!(*args) headers, params = {}, @grant.as_json # NOTE: # Using Array#estract_options! for backward compatibility. # Until v1.0.5, the first argument was 'client_auth_method' in scalar. options = args.extract_options! client_auth_method = args.first || options.delete(:client_auth_method) || :basic params[:scope] = Array(options.delete(:scope)).join(' ') if options[:scope].present? params.merge! options if secret && client_auth_method == :basic cred = ["#{identifier}:#{secret}"].pack('m').tr("\n", '') headers.merge!( 'Authorization' => "Basic #{cred}" ) else params.merge!( client_id: identifier, client_secret: secret ) end handle_response do Rack::OAuth2.http_client.post( absolute_uri_for(token_endpoint), Util.compact_hash(params), headers ) end end
jwt_bearer=(assertion)
click to toggle source
# File lib/rack/oauth2/client.rb, line 48 def jwt_bearer=(assertion) @grant = Grant::JWTBearer.new( assertion: assertion ) end
refresh_token=(token)
click to toggle source
# File lib/rack/oauth2/client.rb, line 42 def refresh_token=(token) @grant = Grant::RefreshToken.new( refresh_token: token ) end
resource_owner_credentials=(credentials)
click to toggle source
# File lib/rack/oauth2/client.rb, line 35 def resource_owner_credentials=(credentials) @grant = Grant::Password.new( username: credentials.first, password: credentials.last ) end
saml2_bearer=(assertion)
click to toggle source
# File lib/rack/oauth2/client.rb, line 54 def saml2_bearer=(assertion) @grant = Grant::SAML2Bearer.new( assertion: assertion ) end
Private Instance Methods
absolute_uri_for(endpoint)
click to toggle source
# File lib/rack/oauth2/client.rb, line 94 def absolute_uri_for(endpoint) _endpoint_ = Util.parse_uri endpoint _endpoint_.scheme ||= self.scheme || 'https' _endpoint_.host ||= self.host _endpoint_.port ||= self.port raise 'No Host Info' unless _endpoint_.host _endpoint_.to_s end
handle_error_response(response)
click to toggle source
# File lib/rack/oauth2/client.rb, line 130 def handle_error_response(response) error = parse_json response.body raise Error.new(response.status, error) rescue MultiJson::DecodeError raise Error.new(response.status, error: 'Unknown', error_description: response.body) end
handle_response() { || ... }
click to toggle source
# File lib/rack/oauth2/client.rb, line 103 def handle_response response = yield case response.status when 200..201 handle_success_response response else handle_error_response response end end
handle_success_response(response)
click to toggle source
# File lib/rack/oauth2/client.rb, line 113 def handle_success_response(response) token_hash = parse_json response.body case token_hash[:token_type].try(:downcase) when 'bearer' AccessToken::Bearer.new(token_hash) when 'mac' AccessToken::MAC.new(token_hash) when nil AccessToken::Legacy.new(token_hash) else raise 'Unknown Token Type' end rescue MultiJson::DecodeError # NOTE: Facebook support (They don't use JSON as token response) AccessToken::Legacy.new Rack::Utils.parse_nested_query(response.body).with_indifferent_access end
parse_json(raw_json)
click to toggle source
# File lib/rack/oauth2/client.rb, line 137 def parse_json(raw_json) # MultiJson.parse('') returns nil when using MultiJson::Adapters::JsonGem MultiJson.load(raw_json).try(:with_indifferent_access) || {} end