Skip to content

Commit

Permalink
Merge pull request #9 from italia/feature/idp-metadata
Browse files Browse the repository at this point in the history
Feature/idp metadata
  • Loading branch information
davidlibrera committed Jul 6, 2018
2 parents 0ee4941 + 04cc0d1 commit 96eb935
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## [Unreleased]
### Added
- Fetch all identity provider from https://registry.spid.gov.it
- Parse and store metadata from single Identity Provider

## [0.2.2] - 2018-07-02
### Fixed
Expand Down
2 changes: 2 additions & 0 deletions lib/spid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

require "spid/authn_request"
require "spid/generate_authn_request"
require "spid/identity_providers"
require "spid/metadata"
require "spid/idp_metadata"
require "spid/version"

module Spid # :nodoc:
Expand Down
44 changes: 44 additions & 0 deletions lib/spid/identity_providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require "faraday"
require "faraday_middleware"

module Spid
class IdentityProviders # :nodoc:
def self.fetch_all
new.fetch_all
end

def fetch_all
spid_idp_entities.map do |idp|
{
name: idp["entity_name"].gsub(/ ID$/, "").downcase,
metadata_url: idp["metadata_url"],
entity_id: idp["entity_id"]
}
end
end

private

def spid_idp_entities
return [] if response.body["spidFederationRegistry"].blank?
response.body["spidFederationRegistry"]["entities"]
end

def response
connection.get do |req|
req.url "/api/identity-providers"
req.headers["Accept"] = "application/json"
end
end

def connection
Faraday.new("https://registry.spid.gov.it") do |conn|
conn.response :json, content_type: /\bjson$/

conn.adapter Faraday.default_adapter
end
end
end
end
38 changes: 38 additions & 0 deletions lib/spid/idp_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "singleton"
require "onelogin/ruby-saml/idp_metadata_parser"

module Spid
class IdpMetadata # :nodoc:
include Singleton

def initialize
@identity_providers = Spid::IdentityProviders.fetch_all
@metadata = {}
end

def [](idp_name)
return @metadata[idp_name] if @metadata[idp_name].present?
idp_hash = identity_provider_hash(idp_name)

@metadata[idp_name] = parser.parse_remote_to_hash(
idp_hash[:metadata_url],
idp_hash[:metadata_url].start_with?("https://")
)
@metadata[idp_name]
end

def identity_provider_hash(idp_name)
@identity_providers.find do |idp|
idp[:name] == idp_name.to_s
end
end

private

def parser
@parser ||= ::OneLogin::RubySaml::IdpMetadataParser.new
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
require "bundler/setup"
require "spid"
require "nokogiri"
require "vcr"

Dir[File.join("./spec/support/**/*.rb")].each { |f| require f }

ENV["ruby-saml/testing"] = "true" # disable ruby-saml logging

VCR.configure do |c|
c.cassette_library_dir = "spec/cassettes"
c.hook_into :webmock
c.configure_rspec_metadata!
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
Loading

0 comments on commit 96eb935

Please sign in to comment.