-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from italia/feature/idp-metadata
Feature/idp metadata
- Loading branch information
Showing
10 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
113 changes: 113 additions & 0 deletions
113
spec/cassettes/Spid_IdentityProviders/_fetch_all/returns_an_array_of_identity_providers.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
spec/cassettes/Spid_IdpMetadata/_/returns_metadata_of_selected_provider.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.