Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto format #31

Merged
merged 1 commit into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Expand Up @@ -10,10 +10,10 @@ gemspec
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

gem "rails", ">= 5.2.4.6", "< 6.2"
gem 'rails', '>= 5.2.4.6', '< 6.2'

group :development, :test do
gem 'rubocop'
gem 'pry'
gem 'pry-nav'
gem 'rubocop'
end
12 changes: 5 additions & 7 deletions Rakefile
Expand Up @@ -14,21 +14,19 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
load 'rails/tasks/engine.rake'


load 'rails/tasks/statistics.rake'


Bundler::GemHelper.install_tasks

Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each { |f| load f }

require 'rspec/core'
require 'rspec/core/rake_task'

desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
desc 'Run all specs in spec directory (excluding plugin specs)'
RSpec::Core::RakeTask.new(spec: 'app:db:test:prepare')

task :default => :spec
task default: :spec
116 changes: 58 additions & 58 deletions app/controllers/concerns/scim_rails/response.rb
Expand Up @@ -2,7 +2,7 @@

module ScimRails
module Response
CONTENT_TYPE = "application/scim+json"
CONTENT_TYPE = 'application/scim+json'

def json_response(object, status = :ok)
render \
Expand All @@ -13,12 +13,12 @@ def json_response(object, status = :ok)

def json_scim_response(object:, status: :ok, counts: nil)
case params[:action]
when "index"
when 'index'
render \
json: list_response(object, counts),
status: status,
content_type: CONTENT_TYPE
when "show", "create", "put_update", "patch_update"
when 'show', 'create', 'put_update', 'patch_update'
render \
json: object_response(object),
status: status,
Expand All @@ -28,67 +28,67 @@ def json_scim_response(object:, status: :ok, counts: nil)

private

def list_response(object, counts)
object = object
.order(:id)
.offset(counts.offset)
.limit(counts.limit)
{
schemas: [
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
totalResults: counts.total,
startIndex: counts.start_index,
itemsPerPage: counts.limit,
Resources: list_objects(object)
}
end
def list_response(object, counts)
object = object
.order(:id)
.offset(counts.offset)
.limit(counts.limit)
{
schemas: [
'urn:ietf:params:scim:api:messages:2.0:ListResponse'
],
totalResults: counts.total,
startIndex: counts.start_index,
itemsPerPage: counts.limit,
Resources: list_objects(object),
}
end

def list_objects(objects)
objects.map do |object|
object_response(object)
def list_objects(objects)
objects.map do |object|
object_response(object)
end
end
end

def object_response(object)
schema = case object
when ScimRails.config.scim_users_model
ScimRails.config.user_schema
when ScimRails.config.scim_groups_model
ScimRails.config.group_schema
else
raise ScimRails::ExceptionHandler::InvalidQuery,
"Unknown model: #{object}"
end
find_value(object, schema)
end
def object_response(object)
schema = case object
when ScimRails.config.scim_users_model
ScimRails.config.user_schema
when ScimRails.config.scim_groups_model
ScimRails.config.group_schema
else
raise ScimRails::ExceptionHandler::InvalidQuery,
"Unknown model: #{object}"
end
find_value(object, schema)
end

# `find_value` is a recursive method that takes a "user" and a
# "user schema" and replaces any symbols in the schema with the
# corresponding value from the user. Given a schema with symbols,
# `find_value` will search through the object for the symbols,
# send those symbols to the model, and replace the symbol with
# the return value.
# `find_value` is a recursive method that takes a "user" and a
# "user schema" and replaces any symbols in the schema with the
# corresponding value from the user. Given a schema with symbols,
# `find_value` will search through the object for the symbols,
# send those symbols to the model, and replace the symbol with
# the return value.

def find_value(object, schema)
case schema
when Hash
schema.each.with_object({}) do |(key, value), hash|
hash[key] = find_value(object, value)
def find_value(object, schema)
case schema
when Hash
schema.each.with_object({}) do |(key, value), hash|
hash[key] = find_value(object, value)
end
when Array, ActiveRecord::Associations::CollectionProxy
schema.map do |value|
find_value(object, value)
end
when ScimRails.config.scim_users_model
find_value(schema, ScimRails.config.user_abbreviated_schema)
when ScimRails.config.scim_groups_model
find_value(schema, ScimRails.config.group_abbreviated_schema)
when Symbol
find_value(object, object.public_send(schema))
else
schema
end
when Array, ActiveRecord::Associations::CollectionProxy
schema.map do |value|
find_value(object, value)
end
when ScimRails.config.scim_users_model
find_value(schema, ScimRails.config.user_abbreviated_schema)
when ScimRails.config.scim_groups_model
find_value(schema, ScimRails.config.group_abbreviated_schema)
when Symbol
find_value(object, object.public_send(schema))
else
schema
end
end
end
end
94 changes: 47 additions & 47 deletions app/controllers/scim_rails/application_controller.rb
Expand Up @@ -10,63 +10,63 @@ class ApplicationController < ActionController::API

private

def authorize_request
send(authentication_strategy) do |searchable_attribute, authentication_attribute|
authorization = AuthorizeApiRequest.new(
searchable_attribute: searchable_attribute,
authentication_attribute: authentication_attribute
)
@company = authorization.company
def authorize_request
send(authentication_strategy) do |searchable_attribute, authentication_attribute|
authorization = AuthorizeApiRequest.new(
searchable_attribute: searchable_attribute,
authentication_attribute: authentication_attribute
)
@company = authorization.company
end
raise ScimRails::ExceptionHandler::InvalidCredentials if @company.blank?
end
raise ScimRails::ExceptionHandler::InvalidCredentials if @company.blank?
end

def authentication_strategy
if request.headers["Authorization"]&.include?("Bearer")
:authenticate_with_oauth_bearer
else
:authenticate_with_http_basic
def authentication_strategy
if request.headers['Authorization']&.include?('Bearer')
:authenticate_with_oauth_bearer
else
:authenticate_with_http_basic
end
end
end

def authenticate_with_oauth_bearer
authentication_attribute = request.headers["Authorization"].split.last
payload = ScimRails::Encoder.decode(authentication_attribute).with_indifferent_access
searchable_attribute = payload[ScimRails.config.basic_auth_model_searchable_attribute]
def authenticate_with_oauth_bearer
authentication_attribute = request.headers['Authorization'].split.last
payload = ScimRails::Encoder.decode(authentication_attribute).with_indifferent_access
searchable_attribute = payload[ScimRails.config.basic_auth_model_searchable_attribute]

yield searchable_attribute, authentication_attribute
end
yield searchable_attribute, authentication_attribute
end

def find_value_for(attribute)
params.dig(*path_for(attribute))
end
def find_value_for(attribute)
params.dig(*path_for(attribute))
end

# `path_for` is a recursive method used to find the "path" for
# `.dig` to take when looking for a given attribute in the
# params.
#
# Example: `path_for(:name)` should return an array that looks
# like [:names, 0, :givenName]. `.dig` can then use that path
# against the params to translate the :name attribute to "John".
# `path_for` is a recursive method used to find the "path" for
# `.dig` to take when looking for a given attribute in the
# params.
#
# Example: `path_for(:name)` should return an array that looks
# like [:names, 0, :givenName]. `.dig` can then use that path
# against the params to translate the :name attribute to "John".

def path_for(attribute, object = controller_schema, path = [])
at_path = path.empty? ? object : object.dig(*path)
return path if at_path == attribute
def path_for(attribute, object = controller_schema, path = [])
at_path = path.empty? ? object : object.dig(*path)
return path if at_path == attribute

case at_path
when Hash
at_path.each do |key, _value|
found_path = path_for(attribute, object, [*path, key])
return found_path if found_path
end
nil
when Array
at_path.each_with_index do |_value, index|
found_path = path_for(attribute, object, [*path, index])
return found_path if found_path
case at_path
when Hash
at_path.each do |key, _value|
found_path = path_for(attribute, object, [*path, key])
return found_path if found_path
end
nil
when Array
at_path.each_with_index do |_value, index|
found_path = path_for(attribute, object, [*path, index])
return found_path if found_path
end
nil
end
nil
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/scim_rails/scim_groups_controller.rb
Expand Up @@ -82,7 +82,7 @@ def destroy
raise ScimRails::ExceptionHandler::InvalidConfiguration, e.message
rescue ActiveRecord::RecordNotDestroyed => e
raise ScimRails::ExceptionHandler::InvalidRequest, e.message
rescue => e
rescue StandardError => e
raise ScimRails::ExceptionHandler::UnexpectedError, e.message
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/scim_rails/scim_users_controller.rb
Expand Up @@ -82,7 +82,7 @@ def destroy
raise ScimRails::ExceptionHandler::InvalidConfiguration, e.message
rescue ActiveRecord::RecordNotDestroyed => e
raise ScimRails::ExceptionHandler::InvalidRequest, e.message
rescue => e
rescue StandardError => e
raise ScimRails::ExceptionHandler::UnexpectedError, e.message
end

Expand Down
37 changes: 18 additions & 19 deletions app/models/scim_rails/authorize_api_request.rb
Expand Up @@ -5,7 +5,9 @@ def initialize(searchable_attribute:, authentication_attribute:)
@searchable_attribute = searchable_attribute
@authentication_attribute = authentication_attribute

raise ScimRails::ExceptionHandler::InvalidCredentials if searchable_attribute.blank? || authentication_attribute.blank?
if searchable_attribute.blank? || authentication_attribute.blank?
raise ScimRails::ExceptionHandler::InvalidCredentials
end

@search_parameter = { ScimRails.config.basic_auth_model_searchable_attribute => @searchable_attribute }
end
Expand All @@ -18,23 +20,20 @@ def company

private

attr_reader :authentication_attribute
attr_reader :search_parameter
attr_reader :searchable_attribute

def find_company
@company ||= ScimRails.config.basic_auth_model.find_by!(search_parameter)

rescue ActiveRecord::RecordNotFound
raise ScimRails::ExceptionHandler::InvalidCredentials
end

def authorize(authentication_model)
authorized = ActiveSupport::SecurityUtils.secure_compare(
authentication_model.public_send(ScimRails.config.basic_auth_model_authenticatable_attribute),
authentication_attribute
)
raise ScimRails::ExceptionHandler::InvalidCredentials unless authorized
end
attr_reader :authentication_attribute, :search_parameter, :searchable_attribute

def find_company
@company ||= ScimRails.config.basic_auth_model.find_by!(search_parameter)
rescue ActiveRecord::RecordNotFound
raise ScimRails::ExceptionHandler::InvalidCredentials
end

def authorize(authentication_model)
authorized = ActiveSupport::SecurityUtils.secure_compare(
authentication_model.public_send(ScimRails.config.basic_auth_model_authenticatable_attribute),
authentication_attribute
)
raise ScimRails::ExceptionHandler::InvalidCredentials unless authorized
end
end
end