Skip to content
This repository has been archived by the owner on Dec 11, 2021. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Mar 31, 2016
2 parents 13753d6 + e0006cb commit d9f6fc3
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Expand Up @@ -20,6 +20,9 @@ Lint/UnusedBlockArgument:
Style/AccessModifierIndentation:
EnforcedStyle: outdent

Style/ClassVars:
Enabled: false

Style/CollectionMethods:
PreferredMethods:
map: 'collect'
Expand Down Expand Up @@ -51,5 +54,5 @@ Style/Lambda:
Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/TrailingComma:
Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: 'comma'
12 changes: 8 additions & 4 deletions .travis.yml
@@ -1,16 +1,20 @@
language: ruby
sudo: false
rvm:
- 2.2
- 2.0.0
- 2.1
- 2.0
- 2.2
- 2.3.0
- jruby-9.0
- ruby-head
- jruby-19mode
- jruby-head
- rbx-2
- ruby-head
matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-2
- rvm: ruby-head
env:
global:
- JRUBY_OPTS="$JRUBY_OPTS --debug"
Expand Down
10 changes: 6 additions & 4 deletions Gemfile
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'jwt', '< 1.5.2', :platforms => [:jruby_18, :ruby_18]
gem 'rake'
gem 'rdoc'

Expand All @@ -8,13 +9,14 @@ group :development do
end

group :test do
gem 'addressable'
gem 'addressable', '~> 2.3.8'
gem 'backports'
gem 'coveralls'
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
gem 'rest-client', '~> 1.6.0', :platforms => [:jruby, :ruby_18]
gem 'mime-types', '~> 1.25', :platforms => [:jruby_18, :ruby_18]
gem 'rack', '~> 1.2', :platforms => [:jruby_18, :jruby_19, :ruby_18, :ruby_19, :ruby_20, :ruby_21]
gem 'rest-client', '~> 1.6.0', :platforms => [:jruby_18, :ruby_18]
gem 'rspec', '>= 3'
gem 'rubocop', '>= 0.25', :platforms => [:ruby_19, :ruby_20, :ruby_21]
gem 'rubocop', '>= 0.37', :platforms => [:ruby_19, :ruby_20, :ruby_21]
gem 'simplecov', '>= 0.9'
gem 'yardstick'
end
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -101,6 +101,7 @@ implementations:
* Ruby 2.0
* Ruby 2.1
* Ruby 2.2
* Ruby 2.3
* JRuby 9.0

If something doesn't work on one of these interpreters, it's a bug.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -33,7 +33,7 @@ end

require 'yardstick/rake/verify'
Yardstick::Rake::Verify.new do |verify|
verify.threshold = 58.8
verify.threshold = 59.7
end

task :default => [:spec, :verify_measurements]
18 changes: 9 additions & 9 deletions lib/oauth2/access_token.rb
Expand Up @@ -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 = {}) # rubocop:disable Metrics/AbcSize
@client = client
@token = token.to_s
[:refresh_token, :expires_in, :expires_at].each do |arg|
Expand All @@ -63,7 +63,7 @@ def [](key)
#
# @return [Boolean]
def expires?
!!@expires_at # rubocop:disable DoubleNegation
!!@expires_at
end

# Whether or not the token is expired
Expand All @@ -78,11 +78,11 @@ def expired?
# @return [AccessToken] a new AccessToken
# @note options should be carried over to the new AccessToken
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',
:refresh_token => refresh_token)
raise('A refresh_token is not available') unless refresh_token
params[:client_id] = @client.id
params[:client_secret] = @client.secret
params[:grant_type] = 'refresh_token'
params[:refresh_token] = refresh_token
new_token = @client.get_token(params)
new_token.options = options
new_token.refresh_token = refresh_token unless new_token.refresh_token
Expand Down Expand Up @@ -149,7 +149,7 @@ def headers

private

def token=(opts) # rubocop:disable MethodLength
def token=(opts) # rubocop:disable MethodLength, Metrics/AbcSize
case options[:mode]
when :header
opts[:headers] ||= {}
Expand All @@ -166,7 +166,7 @@ def token=(opts) # rubocop:disable MethodLength
end
# @todo support for multi-part (file uploads)
else
fail("invalid :mode option of #{options[:mode]}")
raise("invalid :mode option of #{options[:mode]}")
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/oauth2/client.rb
Expand Up @@ -87,7 +87,7 @@ 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 = {}) # rubocop:disable CyclomaticComplexity, MethodLength
def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, MethodLength, Metrics/AbcSize
connection.response :logger, ::Logger.new($stdout) if ENV['OAUTH_DEBUG'] == 'true'

url = connection.build_url(url, opts[:params]).to_s
Expand All @@ -112,12 +112,12 @@ def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, Method
response
when 400..599
error = Error.new(response)
fail(error) if opts.fetch(:raise_errors, options[:raise_errors])
raise(error) if opts.fetch(:raise_errors, options[:raise_errors])
response.error = error
response
else
error = Error.new(response)
fail(error, "Unhandled status code value of #{response.status}")
raise(error, "Unhandled status code value of #{response.status}")
end
end

Expand All @@ -127,7 +127,7 @@ def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, Method
# @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) # rubocop:disable Metrics/AbcSize
opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
headers = params.delete(:headers)
if options[:token_method] == :post
Expand All @@ -140,7 +140,7 @@ def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
opts[:headers].merge!(headers) if headers
response = request(options[:token_method], token_url, opts)
error = Error.new(response)
fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
raise(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
8 changes: 4 additions & 4 deletions lib/oauth2/mac_token.rb
Expand Up @@ -44,7 +44,7 @@ def request(verb, path, opts = {}, &block)
url = client.connection.build_url(path, opts[:params]).to_s

opts[:headers] ||= {}
opts[:headers].merge!('Authorization' => header(verb, url))
opts[:headers]['Authorization'] = header(verb, url)

@client.request(verb, path, opts, &block)
end
Expand All @@ -64,7 +64,7 @@ def header(verb, url)

uri = URI.parse(url)

fail(ArgumentError, "could not parse \"#{url}\" into URI") unless uri.is_a?(URI::HTTP)
raise(ArgumentError, "could not parse \"#{url}\" into URI") unless uri.is_a?(URI::HTTP)

mac = signature(timestamp, nonce, verb, uri)

Expand Down Expand Up @@ -102,7 +102,7 @@ def algorithm=(alg)
when 'hmac-sha-256'
OpenSSL::Digest::SHA256.new
else
fail(ArgumentError, 'Unsupported algorithm')
raise(ArgumentError, 'Unsupported algorithm')
end
end
end
Expand All @@ -116,7 +116,7 @@ def token=(_)

# Base64.strict_encode64 is not available on Ruby 1.8.7
def strict_encode64(str)
Base64.encode64(str).gsub("\n", '')
Base64.encode64(str).delete("\n")
end
end
end
44 changes: 22 additions & 22 deletions lib/oauth2/response.rb
Expand Up @@ -8,16 +8,32 @@ class Response
attr_reader :response
attr_accessor :error, :options

# Procs that, when called, will parse a response body according
# to the specified format.
@@parsers = {
: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.
@@content_types = {
'application/json' => :json,
'text/javascript' => :json,
'application/x-www-form-urlencoded' => :query,
'text/plain' => :text,
}

# Adds a new content type parser.
#
# @param [Symbol] key A descriptive symbol key such as :json or :query.
# @param [Array] One or more mime types to which this parser applies.
# @yield [String] A block returning parsed content.
def self.register_parser(key, mime_types, &block)
key = key.to_sym
PARSERS[key] = block
@@parsers[key] = block
Array(mime_types).each do |mime_type|
CONTENT_TYPES[mime_type] = key
@@content_types[mime_type] = key
end
end

Expand Down Expand Up @@ -47,28 +63,12 @@ def body
response.body || ''
end

# Procs that, when called, will parse a response body according
# to the specified format.
PARSERS = {
: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.
CONTENT_TYPES = {
'application/json' => :json,
'text/javascript' => :json,
'application/x-www-form-urlencoded' => :query,
'text/plain' => :text,
}

# The parsed response body.
# Will attempt to parse application/x-www-form-urlencoded and
# application/json Content-Type response bodies
def parsed
return nil unless PARSERS.key?(parser)
@parsed ||= PARSERS[parser].call(body)
return nil unless @@parsers.key?(parser)
@parsed ||= @@parsers[parser].call(body)
end

# Attempts to determine the content type of the response.
Expand All @@ -78,8 +78,8 @@ def content_type

# Determines the parser that will be used to supply the content of #parsed
def parser
return options[:parse].to_sym if PARSERS.key?(options[:parse])
CONTENT_TYPES[content_type]
return options[:parse].to_sym if @@parsers.key?(options[:parse])
@@content_types[content_type]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth2/strategy/assertion.rb
Expand Up @@ -25,7 +25,7 @@ class Assertion < Base
#
# @raise [NotImplementedError]
def authorize_url
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
raise(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified client.
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth2/strategy/client_credentials.rb
Expand Up @@ -10,7 +10,7 @@ class ClientCredentials < Base
#
# @raise [NotImplementedError]
def authorize_url
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
raise(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified client.
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth2/strategy/implicit.rb
Expand Up @@ -22,7 +22,7 @@ def authorize_url(params = {})
#
# @raise [NotImplementedError]
def get_token(*)
fail(NotImplementedError, 'The token is accessed differently in this strategy')
raise(NotImplementedError, 'The token is accessed differently in this strategy')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth2/strategy/password.rb
Expand Up @@ -8,7 +8,7 @@ class Password < Base
#
# @raise [NotImplementedError]
def authorize_url
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
raise(NotImplementedError, 'The authorization endpoint is not used in this strategy')
end

# Retrieve an access token given the specified End User username and password.
Expand Down
66 changes: 55 additions & 11 deletions lib/oauth2/version.rb
@@ -1,15 +1,59 @@
module OAuth2
class Version
MAJOR = 1
MINOR = 0
PATCH = 0
PRE = nil

class << self
# @return [String]
def to_s
[MAJOR, MINOR, PATCH, PRE].compact.join('.')
end
module Version
module_function

# The major version
#
# @return [Integer]
def major
1
end

# The minor version
#
# @return [Integer]
def minor
1
end

# The patch version
#
# @return [Integer]
def patch
0
end

# The pre-release version, if any
#
# @return [Integer, NilClass]
def pre
nil
end

# The version number as a hash
#
# @return [Hash]
def to_h
{
:major => major,
:minor => minor,
:patch => patch,
:pre => pre,
}
end

# The version number as an array
#
# @return [Array]
def to_a
[major, minor, patch, pre].compact
end

# The version number as a string
#
# @return [String]
def to_s
to_a.join('.')
end
end
end

0 comments on commit d9f6fc3

Please sign in to comment.