Skip to content

Commit

Permalink
Fix RuboCop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 31, 2013
1 parent e360e4d commit 3d0ad2d
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 241 deletions.
31 changes: 16 additions & 15 deletions lib/oauth2/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self
# @param [Hash] a hash of AccessToken property values
# @return [AccessToken] the initalized AccessToken
def from_hash(client, hash)
self.new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
end

# Initializes an AccessToken from a key/value application/x-www-form-urlencoded string
Expand All @@ -36,7 +36,7 @@ def from_kvform(client, kvform)
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
# Access Token value in :body or :query transmission mode
def initialize(client, token, opts={})
def initialize(client, token, opts = {})
@client = client
@token = token.to_s
[:refresh_token, :expires_in, :expires_at].each do |arg|
Expand Down Expand Up @@ -77,8 +77,8 @@ def expired?
#
# @return [AccessToken] a new AccessToken
# @note options should be carried over to the new AccessToken
def refresh!(params={})
raise "A refresh_token is not available" unless refresh_token
def refresh!(params = {})
fail('A refresh_token is not available') unless refresh_token
params.merge!(:client_id => @client.id,
:client_secret => @client.secret,
:grant_type => 'refresh_token',
Expand All @@ -93,7 +93,7 @@ def refresh!(params={})
#
# @return [Hash] a hash of AccessToken property values
def to_hash
params.merge({:access_token => token, :refresh_token => refresh_token, :expires_at => expires_at})
params.merge(:access_token => token, :refresh_token => refresh_token, :expires_at => expires_at)
end

# Make a request with the Access Token
Expand All @@ -102,53 +102,54 @@ def to_hash
# @param [String] path the HTTP URL path of the request
# @param [Hash] opts the options to make the request with
# @see Client#request
def request(verb, path, opts={}, &block)
set_token(opts)
def request(verb, path, opts = {}, &block)
self.token = opts
@client.request(verb, path, opts, &block)
end

# Make a GET request with the Access Token
#
# @see AccessToken#request
def get(path, opts={}, &block)
def get(path, opts = {}, &block)
request(:get, path, opts, &block)
end

# Make a POST request with the Access Token
#
# @see AccessToken#request
def post(path, opts={}, &block)
def post(path, opts = {}, &block)
request(:post, path, opts, &block)
end

# Make a PUT request with the Access Token
#
# @see AccessToken#request
def put(path, opts={}, &block)
def put(path, opts = {}, &block)
request(:put, path, opts, &block)
end

# Make a PATCH request with the Access Token
#
# @see AccessToken#request
def patch(path, opts={}, &block)
def patch(path, opts = {}, &block)
request(:patch, path, opts, &block)
end

# Make a DELETE request with the Access Token
#
# @see AccessToken#request
def delete(path, opts={}, &block)
def delete(path, opts = {}, &block)
request(:delete, path, opts, &block)
end

# Get the headers hash (includes Authorization token)
def headers
{ 'Authorization' => options[:header_format] % token }
{'Authorization' => options[:header_format] % token}
end

private
def set_token(opts)

def token=(opts) # rubocop:disable MethodLength
case options[:mode]
when :header
opts[:headers] ||= {}
Expand All @@ -165,7 +166,7 @@ def set_token(opts)
end
# @todo support for multi-part (file uploads)
else
raise "invalid :mode option of #{options[:mode]}"
fail("invalid :mode option of #{options[:mode]}")
end
end
end
Expand Down
24 changes: 13 additions & 11 deletions lib/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Client
# @option opts [Boolean] :raise_errors (true) whether or not to raise an OAuth2::Error
# on responses with 400+ status codes
# @yield [builder] The Faraday connection builder
def initialize(client_id, client_secret, opts={}, &block)
def initialize(client_id, client_secret, opts = {}, &block)
_opts = opts.dup
@id = client_id
@secret = client_secret
Expand Down Expand Up @@ -61,14 +61,14 @@ def connection
# The authorize endpoint URL of the OAuth2 provider
#
# @param [Hash] params additional query parameters
def authorize_url(params=nil)
def authorize_url(params = nil)
connection.build_url(options[:authorize_url], params).to_s
end

# The token endpoint URL of the OAuth2 provider
#
# @param [Hash] params additional query parameters
def token_url(params=nil)
def token_url(params = nil)
connection.build_url(options[:token_url], params).to_s
end

Expand All @@ -84,8 +84,8 @@ def token_url(params=nil)
# code response for this request. Will default to client option
# @option opts [Symbol] :parse @see Response::initialize
# @yield [req] The Faraday request
def request(verb, url, opts={})
url = self.connection.build_url(url, opts[:params]).to_s
def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, MethodLength
url = connection.build_url(url, opts[:params]).to_s

response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
yield(req) if block_given?
Expand All @@ -106,12 +106,13 @@ def request(verb, url, opts={})
# on non-redirecting 3xx statuses, just return the response
response
when 400..599
e = Error.new(response)
raise e if opts.fetch(:raise_errors, options[:raise_errors])
response.error = e
error = Error.new(response)
fail(error) if opts.fetch(:raise_errors, options[:raise_errors])
response.error = error
response
else
raise Error.new(response), "Unhandled status code value of #{response.status}"
error = Error.new(response)
fail(error, "Unhandled status code value of #{response.status}")
end
end

Expand All @@ -121,7 +122,7 @@ def request(verb, url, opts={})
# @param [Hash] access token options, to pass to the AccessToken object
# @param [Class] class of access token for easier subclassing OAuth2::AccessToken
# @return [AccessToken] the initalized AccessToken
def get_token(params, access_token_opts={}, access_token_class = AccessToken)
def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
if options[:token_method] == :post
headers = params.delete(:headers)
Expand All @@ -132,7 +133,8 @@ def get_token(params, access_token_opts={}, access_token_class = AccessToken)
opts[:params] = params
end
response = request(options[:token_method], token_url, opts)
raise Error.new(response) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
error = Error.new(response)
fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
access_token_class.from_hash(self, response.parsed.merge(access_token_opts))
end

Expand Down
18 changes: 8 additions & 10 deletions lib/oauth2/response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'multi_json'
require 'multi_xml'
require 'rack'

module OAuth2
Expand Down Expand Up @@ -26,7 +27,7 @@ def self.register_parser(key, mime_types, &block)
# @param [Hash] opts options in which to initialize the instance
# @option opts [Symbol] :parse (:automatic) how to parse the response body. one of :query (for x-www-form-urlencoded),
# :json, or :automatic (determined by Content-Type response header)
def initialize(response, opts={})
def initialize(response, opts = {})
@response = response
@options = {:parse => :automatic}.merge(opts)
end
Expand All @@ -49,9 +50,9 @@ def body
# Procs that, when called, will parse a response body according
# to the specified format.
PARSERS = {
:json => lambda{ |body| MultiJson.load(body) rescue body },
:query => lambda{ |body| Rack::Utils.parse_query(body) },
:text => lambda{ |body| body }
:json => lambda { |body| MultiJson.load(body) rescue body }, # rubocop:disable RescueModifier
:query => lambda { |body| Rack::Utils.parse_query(body) },
:text => lambda { |body| body }
}

# Content type assignments for various potential HTTP content types.
Expand Down Expand Up @@ -83,9 +84,6 @@ def parser
end
end

begin
require 'multi_xml'
OAuth2::Response.register_parser(:xml, ['text/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml']) do |body|
MultiXml.parse(body) rescue body
end
rescue LoadError; end
OAuth2::Response.register_parser(:xml, ['text/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml']) do |body|
MultiXml.parse(body) rescue body # rubocop:disable RescueModifier
end
13 changes: 6 additions & 7 deletions lib/oauth2/strategy/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Assertion < Base
#
# @raise [NotImplementedError]
def authorize_url
raise NotImplementedError, "The authorization endpoint is not used in this strategy"
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified client.
Expand All @@ -42,15 +42,15 @@ def authorize_url
# params :exp, expired at, in seconds, like Time.now.utc.to_i + 3600
#
# @param [Hash] opts options
def get_token(params={}, opts={})
def get_token(params = {}, opts = {})
hash = build_request(params)
@client.get_token(hash, opts.merge('refresh_token' => nil))
end

def build_request(params)
assertion = build_assertion(params)
{:grant_type => "assertion",
:assertion_type => "urn:ietf:params:oauth:grant-type:jwt-bearer",
{:grant_type => 'assertion',
:assertion_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
:assertion => assertion,
:scope => params[:scope]
}.merge(client_params)
Expand All @@ -63,12 +63,11 @@ def build_assertion(params)
:exp => params[:exp]
}
if params[:hmac_secret]
jwt_assertion = JWT.encode(claims, params[:hmac_secret], "HS256")
JWT.encode(claims, params[:hmac_secret], 'HS256')
elsif params[:private_key]
jwt_assertion = JWT.encode(claims, params[:private_key], "RS256")
JWT.encode(claims, params[:private_key], 'RS256')
end
end
end
end
end

6 changes: 3 additions & 3 deletions lib/oauth2/strategy/auth_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class AuthCode < Base
# The required query parameters for the authorize URL
#
# @param [Hash] params additional query parameters
def authorize_params(params={})
def authorize_params(params = {})
params.merge('response_type' => 'code', 'client_id' => @client.id)
end

# The authorization URL endpoint of the provider
#
# @param [Hash] params additional query parameters for the URL
def authorize_url(params={})
def authorize_url(params = {})
@client.authorize_url(authorize_params.merge(params))
end

Expand All @@ -24,7 +24,7 @@ def authorize_url(params={})
# @param [Hash] params additional params
# @param [Hash] opts options
# @note that you must also provide a :redirect_uri with most OAuth 2.0 providers
def get_token(code, params={}, opts={})
def get_token(code, params = {}, opts = {})
params = {'grant_type' => 'authorization_code', 'code' => code}.merge(client_params).merge(params)
@client.get_token(params, opts)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/oauth2/strategy/client_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class ClientCredentials < Base
#
# @raise [NotImplementedError]
def authorize_url
raise NotImplementedError, "The authorization endpoint is not used in this strategy"
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified client.
#
# @param [Hash] params additional params
# @param [Hash] opts options
def get_token(params={}, opts={})
def get_token(params = {}, opts = {})
request_body = opts.delete('auth_scheme') == 'request_body'
params.merge!('grant_type' => 'client_credentials')
params.merge!(request_body ? client_params : {:headers => {'Authorization' => authorization(client_params['client_id'], client_params['client_secret'])}})
Expand Down
6 changes: 3 additions & 3 deletions lib/oauth2/strategy/implicit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class Implicit < Base
# The required query parameters for the authorize URL
#
# @param [Hash] params additional query parameters
def authorize_params(params={})
def authorize_params(params = {})
params.merge('response_type' => 'token', 'client_id' => @client.id)
end

# The authorization URL endpoint of the provider
#
# @param [Hash] params additional query parameters for the URL
def authorize_url(params={})
def authorize_url(params = {})
@client.authorize_url(authorize_params.merge(params))
end

# Not used for this strategy
#
# @raise [NotImplementedError]
def get_token(*)
raise NotImplementedError, "The token is accessed differently in this strategy"
fail(NotImplementedError, 'The token is accessed differently in this strategy')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/oauth2/strategy/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class Password < Base
#
# @raise [NotImplementedError]
def authorize_url
raise NotImplementedError, "The authorization endpoint is not used in this strategy"
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified End User username and password.
#
# @param [String] username the End User username
# @param [String] password the End User password
# @param [Hash] params additional params
def get_token(username, password, params={}, opts={})
def get_token(username, password, params = {}, opts = {})
params = {'grant_type' => 'password',
'username' => username,
'password' => password}.merge(client_params).merge(params)
Expand Down
3 changes: 0 additions & 3 deletions lib/oauth2/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ class Version
PRE = nil unless defined? PRE

class << self

# @return [String]
def to_s
[MAJOR, MINOR, PATCH, PRE].compact.join('.')
end

end

end
end
10 changes: 5 additions & 5 deletions oauth2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ Gem::Specification.new do |spec|
spec.add_dependency 'multi_xml', '~> 0.5'
spec.add_dependency 'rack', '~> 1.2'
spec.add_dependency 'jwt', '~> 0.1.8'
spec.authors = ["Michael Bleigh", "Erik Michaels-Ober"]
spec.authors = ['Michael Bleigh', 'Erik Michaels-Ober']
spec.cert_chain = %w(certs/sferik.pem)
spec.description = %q{A Ruby wrapper for the OAuth 2.0 protocol built with a similar style to the original OAuth spec.}
spec.email = ['michael@intridea.com', 'sferik@gmail.com']
spec.files = %w(.document CONTRIBUTING.md LICENSE.md README.md Rakefile oauth2.gemspec)
spec.files += Dir.glob("lib/**/*.rb")
spec.files += Dir.glob("spec/**/*")
spec.files += Dir.glob('lib/**/*.rb')
spec.files += Dir.glob('spec/**/*')
spec.homepage = 'http://github.com/intridea/oauth2'
spec.licenses = ['MIT']
spec.name = 'oauth2'
spec.require_paths = ['lib']
spec.required_rubygems_version = '>= 1.3.5'
spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/
spec.signing_key = File.expand_path('~/.gem/private_key.pem') if $PROGRAM_NAME =~ /gem\z/
spec.summary = %q{A Ruby wrapper for the OAuth 2.0 protocol.}
spec.test_files = Dir.glob("spec/**/*")
spec.test_files = Dir.glob('spec/**/*')
spec.version = OAuth2::Version
end
Loading

0 comments on commit 3d0ad2d

Please sign in to comment.