Class: Shibkit::MetaMeta::Source
- Inherits:
-
Object
- Object
- Shibkit::MetaMeta::Source
- Includes:
- Mixin::CachedDownloads
- Defined in:
- lib/shibkit/meta_meta/source.rb
Constant Summary collapse
- REAL_SOURCES_FILE =
Location of default real sources list (contains real-world federation details) # REMOVE
"#{::File.dirname(__FILE__)}/data/real_sources.yml"
- DEV_SOURCES_FILE =
Location of default mock sources list (contains small fictional federations) # REMOVE
"#{::File.dirname(__FILE__)}/data/dev_sources.yml"
- PERCENTAGE_PATTERN =
/(\d+)\s*%/
- NAMESPACES =
Additional namespaces that Nokogiri needs to know about # TODO Move to Config?
{ 'ukfedlabel' => 'http://ukfederation.org.uk/2006/11/label', 'elab' => 'http://eduserv.org.uk/labels', 'wayf' => 'http://sdss.ac.uk/2006/06/WAYF', 'mdui' => 'urn:oasis:names:tc:SAML:metadata:ui', 'saml' => 'urn:oasis:names:tc:SAML:2.0:assertion', 'shibmd' => 'urn:mace:shibboleth:metadata:1.0' }
Instance Attribute Summary collapse
-
#active ⇒ Object
(also: #active?)
Returns the value of attribute active.
-
#certificate_source ⇒ String
URL or filesystem path of the metadata certificate to be used.
-
#countries ⇒ Array
Country codes for areas served by the federation.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#description ⇒ String
Brief description of the federation or collection.
-
#display_name ⇒ String
The common, friendler name of the federation or collection.
-
#fetched_at ⇒ String
readonly
Time the metadata for this federation was last fetched.
-
#fingerprint ⇒ String?
Fingerprint of the federation certificate.
-
#homepage ⇒ String
(also: #homepage_url)
URL of the federation or collection’s home page.
-
#languages ⇒ Array
Array of languages supported by the federation or collection.
-
#messages ⇒ String
readonly
deprecated
Deprecated.
Not actually used at present, not sure if this is needed…
-
#metadata_source ⇒ String
(also: #url)
URL or filesystem path of the metadata file to be used.
-
#name ⇒ String
The full name of the federation or collection.
-
#name_uri ⇒ String
(also: #uri)
The URI identifier for the federation or collection.
-
#refeds_url ⇒ String?
(also: #refeds_info)
URL of the federation’s Refeds wiki entry.
-
#refresh_delay ⇒ Fixednum
The recommended refresh period for the federation, in seconds.
-
#status ⇒ String
readonly
Status of the source: indicates success of last operation.
-
#support_email ⇒ String
Main contact email address for the federation.
-
#type ⇒ String
:federation for proper federations, :collection for simple collections of entities.
Class Method Summary collapse
-
.from_hash(data, uri = nil) ⇒ Object
Create a new source from a hash.
-
.load(source_list = :auto, *selected_groups) ⇒ Array
Load a metadata source list from a YAML file.
-
.locate_sources_file(source_list) ⇒ Object
Return appropriate file path for.
Instance Method Summary collapse
-
#certificate_pem ⇒ String?
The content of the certificate associated with the metadata.
-
#content ⇒ String
Return raw source string from the file.
-
#fetch_certificate ⇒ File
Fetch remote file and store locally.
-
#fetch_metadata ⇒ File
Fetch remote file and store locally without validation.
- #groups ⇒ Object
- #groups=(group_names) ⇒ Object
-
#initialize(&block) ⇒ Source
constructor
New Source object with default values.
-
#ok? ⇒ TrueClass, FalseClass
Does the source object look sensible?.
-
#parse_xml ⇒ Nokogiri::XML::Document
Return Nokogiri object tree for the metadata.
- #read ⇒ Object
-
#refresh ⇒ TrueClass, FalseClass
Redownload and revalidate remote files for the source.
-
#selected? ⇒ Boolean
Has this federation/source been selected by the user?.
- #tags ⇒ Object
- #tags=(tag_names) ⇒ Object
-
#to_federation ⇒ Shibkit::MetaMeta::Federation
Build a parsed Federation object containing Entitiess.
-
#to_hash ⇒ Object
Create a new hash from a source object.
- #to_s ⇒ Object
- #trustiness ⇒ Object
- #trustiness=(level) ⇒ Object
-
#valid? ⇒ TrueClass, FalseClass
Checks validity of metadata and certificate without raising exceptions.
-
#validate ⇒ TrueClass, FalseClass
Validates metadata and certificate or raises an exception.
Methods included from Mixin::CachedDownloads
#fetch_local, #fetch_remote, included
Constructor Details
#initialize(&block) ⇒ Source
New Source object with default values
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/shibkit/meta_meta/source.rb', line 133 def initialize(&block) @name_uri = "" @created_at = Time.new @name = "Unnown" @refresh_delay = 86400 @display_name = "Unknown" @type = "federation" @countries = [] @metadata_source = nil @certificate_source = nil @fingerprint = nil @refeds_info = nil @homepage = nil @languages = [] @support_email = nil @description = "" @certificate_tmpfile = nil @metadata_tmpfile = nil @active = true @trustiness = 1 @groups = [] @tags = [] self.instance_eval(&block) if block end |
Instance Attribute Details
#active ⇒ Object Also known as: active?
Returns the value of attribute active.
119 120 121 |
# File 'lib/shibkit/meta_meta/source.rb', line 119 def active @active end |
#certificate_source ⇒ String
Returns URL or filesystem path of the metadata certificate to be used.
86 87 88 |
# File 'lib/shibkit/meta_meta/source.rb', line 86 def certificate_source @certificate_source end |
#countries ⇒ Array
Returns country codes for areas served by the federation.
79 80 81 |
# File 'lib/shibkit/meta_meta/source.rb', line 79 def countries @countries end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
122 123 124 |
# File 'lib/shibkit/meta_meta/source.rb', line 122 def created_at @created_at end |
#description ⇒ String
Returns Brief description of the federation or collection.
106 107 108 |
# File 'lib/shibkit/meta_meta/source.rb', line 106 def description @description end |
#display_name ⇒ String
Returns the common, friendler name of the federation or collection.
69 70 71 |
# File 'lib/shibkit/meta_meta/source.rb', line 69 def display_name @display_name end |
#fetched_at ⇒ String (readonly)
This is not persistent between uses of this class
Returns Time the metadata for this federation was last fetched.
110 111 112 |
# File 'lib/shibkit/meta_meta/source.rb', line 110 def fetched_at @fetched_at end |
#fingerprint ⇒ String?
Returns Fingerprint of the federation certificate.
89 90 91 |
# File 'lib/shibkit/meta_meta/source.rb', line 89 def fingerprint @fingerprint end |
#homepage ⇒ String Also known as: homepage_url
Returns URL of the federation or collection’s home page.
96 97 98 |
# File 'lib/shibkit/meta_meta/source.rb', line 96 def homepage @homepage end |
#languages ⇒ Array
Returns Array of languages supported by the federation or collection.
100 101 102 |
# File 'lib/shibkit/meta_meta/source.rb', line 100 def languages @languages end |
#messages ⇒ String (readonly)
Not actually used at present, not sure if this is needed…
Returns Message returned during processing.
114 115 116 |
# File 'lib/shibkit/meta_meta/source.rb', line 114 def @messages end |
#metadata_source ⇒ String Also known as: url
Returns URL or filesystem path of the metadata file to be used.
82 83 84 |
# File 'lib/shibkit/meta_meta/source.rb', line 82 def @metadata_source end |
#name ⇒ String
Returns the full name of the federation or collection.
66 67 68 |
# File 'lib/shibkit/meta_meta/source.rb', line 66 def name @name end |
#name_uri ⇒ String Also known as: uri
Returns the URI identifier for the federation or collection.
62 63 64 |
# File 'lib/shibkit/meta_meta/source.rb', line 62 def name_uri @name_uri end |
#refeds_url ⇒ String? Also known as: refeds_info
Returns URL of the federation’s Refeds wiki entry.
92 93 94 |
# File 'lib/shibkit/meta_meta/source.rb', line 92 def refeds_url @refeds_url end |
#refresh_delay ⇒ Fixednum
Returns the recommended refresh period for the federation, in seconds.
76 77 78 |
# File 'lib/shibkit/meta_meta/source.rb', line 76 def refresh_delay @refresh_delay end |
#status ⇒ String (readonly)
Returns Status of the source: indicates success of last operation.
117 118 119 |
# File 'lib/shibkit/meta_meta/source.rb', line 117 def status @status end |
#support_email ⇒ String
Returns Main contact email address for the federation.
103 104 105 |
# File 'lib/shibkit/meta_meta/source.rb', line 103 def support_email @support_email end |
#type ⇒ String
Returns :federation for proper federations, :collection for simple collections of entities.
73 74 75 |
# File 'lib/shibkit/meta_meta/source.rb', line 73 def type @type end |
Class Method Details
.from_hash(data, uri = nil) ⇒ Object
Create a new source from a hash
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/shibkit/meta_meta/source.rb', line 168 def self.from_hash(data, uri=nil) raise "#{data.class} is not a hash" if not data.instance_of? Hash data = data.inject({}){|m,(k,v)| m[k.to_sym] = v; m} new_source = self.new do |source| source.name_uri = data[:uri] || uri source.name = data[:name] || uri source.refresh_delay = data[:refresh].to_i || 86400 source.display_name = data[:display_name] || data['name'] || uri source.type = data[:type].to_sym || :collection source. = data[:metadata] source.certificate_source = data[:certificate] source.fingerprint = data[:fingerprint] source.refeds_url = data[:refeds_info] source.homepage = data[:homepage] source.support_email = data[:support_email] || nil source.description = data[:description] || "" source.trustiness = data[:trustiness].to_f || 1 source.languages = data[:languages].inject([]){|m,v| m << v.to_s.downcase.to_sym } || [:en] source.countries = data[:countries].inject([]){|m,v| m << v.to_s.downcase.to_sym } || [] source.groups = data[:groups] || [] source. = data[:tags] || [] end return new_source end |
.load(source_list = :auto, *selected_groups) ⇒ Array
Load a metadata source list from a YAML file
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/shibkit/meta_meta/source.rb', line 493 def self.load(source_list=:auto, *selected_groups) selected_groups = selected_groups.empty? ? ::Shibkit::MetaMeta.config.selected_groups : [] Shibkit::MetaMeta.log.debug "Load sources from #{source_list.to_s}" file = self.locate_sources_file(source_list) sources = Array.new source_data = YAML::load(File.open(file)) Shibkit::MetaMeta.log.debug "Source YAML:\n#{source_data.inspect}" ## Load records from the YAML-derived hash rather than directly to process them first source_data.each_pair do |id, data| case data when Source sources << data when Hash sources << Source.from_hash(data,id) else raise "Don't know how to convert #{source_data.class} to Source" end end ## If groups are specified then trim off any non-matching sources unless selected_groups.empty? or selected_groups.include? :all Shibkit::MetaMeta.log.info "Filtering source/federations by selected groups" selected_groups.inject([]){|m,v| m << v.to_s.downcase.to_sym } group_set = Set.new selected_groups sources = sources.delete_if { |s| group_set.intersection(s.groups).empty? } Shibkit::MetaMeta.log.debug "Rejected sources that aren't in #{selected_groups.join(', ')}" end return sources end |
.locate_sources_file(source_list) ⇒ Object
Return appropriate file path for
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/shibkit/meta_meta/source.rb', line 537 def self.locate_sources_file(source_list) config = ::Shibkit::MetaMeta.config case source_list when :auto file_path = config.in_production? ? REAL_SOURCES_FILE : DEV_SOURCES_FILE when :dev, :test file_path = DEV_SOURCES_FILE when :real, :prod, :production file_path = REAL_SOURCES_FILE else file_path = source_list end return file_path end |
Instance Method Details
#certificate_pem ⇒ String?
The content of the certificate associated with the metadata
413 414 415 416 417 418 419 420 |
# File 'lib/shibkit/meta_meta/source.rb', line 413 def certificate_pem ## Deal with caching locally, downloading, etc refresh if ::Shibkit::MetaMeta.config.auto_refresh? and @certificate_tmpfile == nil return IO.read(certificate_tmpfile.path) end |
#content ⇒ String
Return raw source string from the file
424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/shibkit/meta_meta/source.rb', line 424 def content ## Deal with caching locally, downloading, etc refresh if ::Shibkit::MetaMeta.config.auto_refresh? and @metadata_tmpfile == nil raise "No content is available, source has not been downloaded" unless .path return IO.read(.path) end |
#fetch_certificate ⇒ File
Fetch remote file and store locally
356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/shibkit/meta_meta/source.rb', line 356 def fetch_certificate @certificate_tmpfile = case certificate_source when /^http/ fetch_remote(certificate_source) else fetch_local(certificate_source) end return @certificate_tmpfile end |
#fetch_metadata ⇒ File
Fetch remote file and store locally without validation
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/shibkit/meta_meta/source.rb', line 339 def @metadata_tmpfile = case when /^http/ fetch_remote() else fetch_local() end @fetched_at = Time.new return @metadata_tmpfile end |
#groups ⇒ Object
265 266 267 268 269 |
# File 'lib/shibkit/meta_meta/source.rb', line 265 def groups return @groups end |
#groups=(group_names) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/shibkit/meta_meta/source.rb', line 258 def groups=(group_names) @groups ||= Array.new @groups = [group_names].flatten.inject([]) {|m,v| m << v.to_s.downcase.to_sym } end |
#ok? ⇒ TrueClass, FalseClass
Does the source object look sensible?
463 464 465 466 467 468 469 |
# File 'lib/shibkit/meta_meta/source.rb', line 463 def ok? return false unless and .size > 1 return true end |
#parse_xml ⇒ Nokogiri::XML::Document
Return Nokogiri object tree for the metadata
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/shibkit/meta_meta/source.rb', line 438 def parse_xml ## Parse the entire file as an XML document doc = Nokogiri::XML.parse(content) do |config| #config.strict.noent.dtdvalid end ## Select the top node xml = doc.root ## Add exotic namespaces to make sure we can deal with all metadata # TODO NAMESPACES.each_pair { |label, uri| xml.add_namespace_definition(label,uri) } return xml end |
#read ⇒ Object
455 456 457 458 459 |
# File 'lib/shibkit/meta_meta/source.rb', line 455 def read Nokogiri::XML::Reader(content) end |
#refresh ⇒ TrueClass, FalseClass
Redownload and revalidate remote files for the source
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/shibkit/meta_meta/source.rb', line 317 def refresh unless selected? log.info "Content for #{ uri} - skipping refresh as not selected." return false end log.info "Content for #{ uri} is being refreshed..." fetch_certificate raise "Validation error" unless valid? return true end |
#selected? ⇒ Boolean
Has this federation/source been selected by the user?
399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/shibkit/meta_meta/source.rb', line 399 def selected? ## If nothing has been specified then everything has been selected. return true if ::Shibkit::MetaMeta.config.selected_federation_uris.empty? ## If this source's uri is present in the list, then yup. return true if ::Shibkit::MetaMeta.config.selected_federation_uris.include?(uri) return false end |
#tags ⇒ Object
278 279 280 281 282 |
# File 'lib/shibkit/meta_meta/source.rb', line 278 def return @tags end |
#tags=(tag_names) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/shibkit/meta_meta/source.rb', line 271 def (tag_names) @tags ||= Array.new @tags = [tag_names].flatten.inject([]) {|m,v| m << v.to_s.downcase.to_sym } end |
#to_federation ⇒ Shibkit::MetaMeta::Federation
Build a parsed Federation object containing Entitiess
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/shibkit/meta_meta/source.rb', line 232 def to_federation fx = self.parse_xml federation = ::Shibkit::MetaMeta::Federation.new(fx) ## Pass additional information from source into federation object federation.name = name || uri federation.display_name = display_name || name federation.type = type federation.refeds_url = refeds_info federation.countries = countries federation.languages = languages federation.support_email = support_email federation.homepage_url = homepage federation.description = description federation.groups = groups federation. = federation.trustiness = trustiness #federation.from_xml(fx) return federation end |
#to_hash ⇒ Object
Create a new hash from a source object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/shibkit/meta_meta/source.rb', line 204 def to_hash data = Hash.new data['uri'] = name_uri.strip data['name'] = name data['refresh'] = refresh_delay.to_i data['display_name'] = display_name data['type'] = type.to_s data['countries'] = countries data['metadata'] = data['certificate'] = certificate_source data['fingerprint'] = fingerprint data['refeds_info'] = refeds_url data['homepage'] = homepage data['languages'] = languages data['support_email'] = support_email data['description'] = description.strip data['trustiness'] = trustiness data['groups'] = groups data['tags'] = return data end |
#to_s ⇒ Object
161 162 163 164 165 |
# File 'lib/shibkit/meta_meta/source.rb', line 161 def to_s return || nil end |
#trustiness ⇒ Object
309 310 311 312 313 |
# File 'lib/shibkit/meta_meta/source.rb', line 309 def trustiness return @trustiness || 1.0 # TODO should be able to configure default trustiness end |
#trustiness=(level) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/shibkit/meta_meta/source.rb', line 284 def trustiness=(level) ## Convert strings if level.kind_of? String ## Percentages as strings become decimal fractions, otherwise directly converted. level = level.match(PERCENTAGE_PATTERN) ? level.to_f / 100 : level.to_f end case when level > 1 log.warn "Setting trustiness greater than 1 is ambiguous, so storing as 1. Use decimal fraction or percentage." @trustiness = 1.0 when level < 0 log.warn "Setting trustiness less than 1 is ambiguous, so storing as 0. Use decimal fraction or percentage." @trustiness = 0.0 else @trustiness = level.to_f end return @trustiness end |
#valid? ⇒ TrueClass, FalseClass
Checks validity of metadata and certificate without raising exceptions
388 389 390 391 392 393 394 395 396 |
# File 'lib/shibkit/meta_meta/source.rb', line 388 def valid? begin return true if validate rescue return false end end |
#validate ⇒ TrueClass, FalseClass
Validates metadata and certificate or raises an exception
371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/shibkit/meta_meta/source.rb', line 371 def validate ## Check that XML is valid # ... ## Check that certificate is OK # ... ## Check that metadata has been signed OK, prob. Using XMLSecTool? # ... return true end |