diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6b9d58f..bcdd4b9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,69 +6,11 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: IndentationWidth. -# SupportedStyles: outdent, indent -Layout/AccessModifierIndentation: - EnforcedStyle: outdent - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: with_first_argument, with_fixed_indentation -Layout/ArgumentAlignment: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterGuardClause: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - - 'lib/omniauth/strategies/cas/service_ticket_validator.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: special_inside_parentheses, consistent, align_braces -Layout/FirstHashElementIndentation: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -Layout/SpaceAfterColon: - Exclude: - - 'lib/omniauth/strategies/cas/logout_request.rb' - -# This cop supports safe autocorrection (--autocorrect). -Layout/SpaceAfterComma: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - - 'lib/omniauth/strategies/cas/logout_request.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceBeforeBlockBraces: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: final_newline, final_blank_line -Layout/TrailingEmptyLines: - Exclude: - - 'lib/omniauth/cas.rb' - # This cop supports safe autocorrection (--autocorrect). Lint/EnsureReturn: Exclude: - 'lib/omniauth/strategies/cas/logout_request.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. -Lint/UnusedBlockArgument: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Lint/UselessAssignment: Exclude: @@ -88,7 +30,7 @@ Metrics/BlockNesting: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 149 + Max: 148 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: @@ -117,12 +59,6 @@ Naming/FileName: Exclude: - 'lib/omniauth-cas.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: PreferredName. -Naming/RescuedExceptionsVariableName: - Exclude: - - 'lib/omniauth/strategies/cas/logout_request.rb' - RSpec/AnyInstance: Exclude: - 'spec/omniauth/strategies/cas_spec.rb' @@ -177,20 +113,6 @@ RSpec/VerifiedDoubles: - 'spec/omniauth/strategies/cas/service_ticket_validator_spec.rb' - 'spec/omniauth/strategies/cas_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: prefer_alias, prefer_alias_method -Style/Alias: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: is_a?, kind_of? -Style/ClassCheck: - Exclude: - - 'lib/omniauth/strategies/cas/service_ticket_validator.rb' - # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -200,11 +122,6 @@ Style/Documentation: - 'lib/omniauth/strategies/cas/logout_request.rb' - 'lib/omniauth/strategies/cas/service_ticket_validator.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/EmptyLiteral: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never @@ -217,12 +134,6 @@ Style/FrozenStringLiteralComment: - 'lib/omniauth/strategies/cas/logout_request.rb' - 'lib/omniauth/strategies/cas/service_ticket_validator.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: MinBodyLength, AllowConsecutiveConditionals. -Style/GuardClause: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowedReceivers. # AllowedReceivers: Thread.current @@ -230,11 +141,6 @@ Style/HashEachMethods: Exclude: - 'lib/omniauth/strategies/cas.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/IfUnlessModifier: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: literals, strict @@ -252,17 +158,6 @@ Style/NumericPredicate: - 'spec/**/*' - 'lib/omniauth/strategies/cas/service_ticket_validator.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/ParallelAssignment: - Exclude: - - 'lib/omniauth/strategies/cas/logout_request.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: short, verbose @@ -270,38 +165,6 @@ Style/PreferredHashMethods: Exclude: - 'lib/omniauth/strategies/cas.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/Proc: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, AllowedCompactTypes. -# SupportedStyles: compact, exploded -Style/RaiseArgs: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowMultipleReturnValues. -Style/RedundantReturn: - Exclude: - - 'lib/omniauth/strategies/cas/logout_request.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: implicit, explicit -Style/RescueStandardError: - Exclude: - - 'lib/omniauth/strategies/cas/logout_request.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiterals: - Exclude: - - 'lib/omniauth/strategies/cas.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https diff --git a/lib/omniauth/cas.rb b/lib/omniauth/cas.rb index dab016a..2fe34bb 100644 --- a/lib/omniauth/cas.rb +++ b/lib/omniauth/cas.rb @@ -1,2 +1,2 @@ require 'omniauth/cas/version' -require 'omniauth/strategies/cas' \ No newline at end of file +require 'omniauth/strategies/cas' diff --git a/lib/omniauth/strategies/cas.rb b/lib/omniauth/strategies/cas.rb index 3b00a0b..e1db34a 100644 --- a/lib/omniauth/strategies/cas.rb +++ b/lib/omniauth/strategies/cas.rb @@ -14,7 +14,7 @@ class InvalidCASTicket < StandardError; end autoload :LogoutRequest, 'omniauth/strategies/cas/logout_request' attr_accessor :raw_info - alias_method :user_info, :raw_info + alias user_info raw_info option :name, :cas # Required property by OmniAuth::Strategy @@ -26,7 +26,7 @@ class InvalidCASTicket < StandardError; end option :service_validate_url, '/serviceValidate' option :login_url, '/login' option :logout_url, '/logout' - option :on_single_sign_out, Proc.new {} + option :on_single_sign_out, proc {} # A Proc or lambda that returns a Hash of additional user info to be # merged with the info returned by the CAS server. # @@ -35,7 +35,7 @@ class InvalidCASTicket < StandardError; end # @param [Hash] The user info for the Service Ticket returned by the CAS server # # @return [Hash] Extra user info - option :fetch_raw_info, Proc.new { Hash.new } + option :fetch_raw_info, proc { {} } # Make all the keys configurable with some defaults set here option :uid_field, 'user' option :name_key, 'name' @@ -48,23 +48,23 @@ class InvalidCASTicket < StandardError; end option :phone_key, 'phone' # As required by https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema - AuthHashSchemaKeys = %w{name email nickname first_name last_name location image phone} + AuthHashSchemaKeys = %w[name email nickname first_name last_name location image phone] info do prune!({ - name: raw_info[options[:name_key].to_s], - email: raw_info[options[:email_key].to_s], - nickname: raw_info[options[:nickname_key].to_s], - first_name: raw_info[options[:first_name_key].to_s], - last_name: raw_info[options[:last_name_key].to_s], - location: raw_info[options[:location_key].to_s], - image: raw_info[options[:image_key].to_s], - phone: raw_info[options[:phone_key].to_s] - }) + name: raw_info[options[:name_key].to_s], + email: raw_info[options[:email_key].to_s], + nickname: raw_info[options[:nickname_key].to_s], + first_name: raw_info[options[:first_name_key].to_s], + last_name: raw_info[options[:last_name_key].to_s], + location: raw_info[options[:location_key].to_s], + image: raw_info[options[:image_key].to_s], + phone: raw_info[options[:phone_key].to_s] + }) end extra do prune!( - raw_info.delete_if{ |k,v| AuthHashSchemaKeys.include?(k) } + raw_info.delete_if { |k, _v| AuthHashSchemaKeys.include?(k) } ) end @@ -82,8 +82,10 @@ def callback_phase else @ticket = request.params['ticket'] return fail!(:no_ticket, MissingCASTicket.new('No CAS Ticket')) unless @ticket + fetch_raw_info(@ticket) return fail!(:invalid_ticket, InvalidCASTicket.new('Invalid CAS Ticket')) if raw_info.empty? + super end end @@ -97,7 +99,7 @@ def request_phase 'Location' => login_url(service_url), 'Content-Type' => 'text/plain' }, - ["You are being redirected to CAS for sign-in."] + ['You are being redirected to CAS for sign-in.'] ] end @@ -136,9 +138,9 @@ def extract_url end def validate_cas_setup - if options.host.nil? || options.login_url.nil? - raise ArgumentError.new(":host and :login_url MUST be provided") - end + return unless options.host.nil? || options.login_url.nil? + + raise ArgumentError, ':host and :login_url MUST be provided' end # Build a service-validation URL from +service+ and +ticket+. @@ -154,9 +156,9 @@ def service_validate_url(service_url, ticket) service_url = Addressable::URI.parse(service_url) service_url.query_values = service_url.query_values.tap { |qs| qs.delete('ticket') } cas_url + append_params(options.service_validate_url, { - service: service_url.to_s, - ticket: ticket - }) + service: service_url.to_s, + ticket: ticket + }) end # Build a CAS login URL from +service+. @@ -175,7 +177,7 @@ def login_url(service) # # @return [String] the new joined URL. def append_params(base, params) - params = params.each { |k,v| v = Rack::Utils.escape(v) } + params = params.each { |_k, v| v = Rack::Utils.escape(v) } Addressable::URI.parse(base).tap do |base_uri| base_uri.query_values = (base_uri.query_values || {}).merge(params) end.to_s @@ -187,14 +189,14 @@ def validate_service_ticket(ticket) ServiceTicketValidator.new(self, options, callback_url, ticket).call end - private + private def fetch_raw_info(ticket) validator = validate_service_ticket(ticket) ticket_user_info = validator.user_info ticket_success_body = validator.success_body custom_user_info = options.fetch_raw_info.call(self, - options, ticket, ticket_user_info, ticket_success_body) + options, ticket, ticket_user_info, ticket_success_body) self.raw_info = ticket_user_info.merge(custom_user_info) end diff --git a/lib/omniauth/strategies/cas/logout_request.rb b/lib/omniauth/strategies/cas/logout_request.rb index eba676c..3448208 100644 --- a/lib/omniauth/strategies/cas/logout_request.rb +++ b/lib/omniauth/strategies/cas/logout_request.rb @@ -3,7 +3,8 @@ module Strategies class CAS class LogoutRequest def initialize(strategy, request) - @strategy, @request = strategy, request + @strategy = strategy + @request = request end def call(options = {}) @@ -11,10 +12,10 @@ def call(options = {}) begin result = single_sign_out_callback.call(*logout_request) - rescue StandardError => err - return @strategy.fail! :logout_request, err + rescue StandardError => e + @strategy.fail! :logout_request, e else - result = [200,{},'OK'] if result == true || result.nil? + result = [200, {}, 'OK'] if result == true || result.nil? ensure return unless result @@ -22,18 +23,18 @@ def call(options = {}) # when Rack::Response#new wants [body,status,headers]? Additionally, # why does Rack::Response differ in argument order from the usual # Rack-like [status,headers,body] array? - return Rack::Response.new(result[2],result[0],result[1]).finish + return Rack::Response.new(result[2], result[0], result[1]).finish end end - private + private def logout_request @logout_request ||= begin saml = Nokogiri.parse(@request.params['logoutRequest']) name_id = saml.xpath('//saml:NameID').text sess_idx = saml.xpath('//samlp:SessionIndex').text - inject_params(name_id:name_id, session_index:sess_idx) + inject_params(name_id: name_id, session_index: sess_idx) @request end end @@ -42,7 +43,7 @@ def inject_params(new_params) rack_input = @request.env['rack.input'].read params = Rack::Utils.parse_query(rack_input, '&').merge new_params @request.env['rack.input'] = StringIO.new(Rack::Utils.build_query(params)) - rescue + rescue StandardError # A no-op intended to ensure that the ensure block is run raise ensure diff --git a/lib/omniauth/strategies/cas/service_ticket_validator.rb b/lib/omniauth/strategies/cas/service_ticket_validator.rb index c5d8d74..6b813fa 100644 --- a/lib/omniauth/strategies/cas/service_ticket_validator.rb +++ b/lib/omniauth/strategies/cas/service_ticket_validator.rb @@ -40,7 +40,7 @@ def user_info parse_user_info(@success_body) end - private + private # Merges attributes with multiple values into an array if support is # enabled (disabled by default) @@ -56,10 +56,11 @@ def attribute_value(user_info, attribute, value) # returns nil if given nil def parse_user_info(node) return nil if node.nil? + {}.tap do |hash| node.children.each do |e| node_name = e.name.sub(/^cas:/, '') - unless e.kind_of?(Nokogiri::XML::Text) || node_name == 'proxies' + unless e.is_a?(Nokogiri::XML::Text) || node_name == 'proxies' # There are no child elements if e.element_children.count == 0 hash[node_name] = attribute_value(hash, node_name, e.content) @@ -82,6 +83,7 @@ def parse_user_info(node) # if the passed body is nil or if there is no such node. def find_authentication_success(body) return nil if body.nil? || body == '' + begin doc = Nokogiri::XML(body) begin