-
-
Notifications
You must be signed in to change notification settings - Fork 566
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
Some features from the PR #197 (PR splitted) #225
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
module OneLogin | ||
module RubySaml | ||
|
||
# SAML2 AttributeService. Auxiliary class to build the AttributeService of the SP Metadata | ||
# | ||
class AttributeService | ||
attr_reader :attributes | ||
attr_reader :name | ||
attr_reader :index | ||
|
||
# Initializes the AttributeService, set the index value as 1 and an empty array as attributes | ||
# | ||
def initialize | ||
@index = "1" | ||
@attributes = [] | ||
|
@@ -14,18 +19,29 @@ def configure(&block) | |
instance_eval &block | ||
end | ||
|
||
# @return [Boolean] True if the AttributeService object has been initialized and set with the required values | ||
# (has attributes and a name) | ||
def configured? | ||
@attributes.length > 0 && !@name.nil? | ||
end | ||
|
||
# Set a name | ||
# @param name [String] The service name | ||
# | ||
def service_name(name) | ||
@name = name | ||
end | ||
|
||
# Set an index | ||
# @param name [Integer] An index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# | ||
def service_index(index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not service_index= ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was only adding comments on this PR, the code on master is without the =. Notice that the attribute is index, and we named the setter service_index. |
||
@index = index | ||
end | ||
|
||
|
||
# Add an attribute | ||
# @param name [Hash] Attribute for the AttributeService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# | ||
def add_attribute(options={}) | ||
attributes << options | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,110 @@ | ||
module OneLogin | ||
module RubySaml | ||
# Wraps all attributes and provides means to query them for single or multiple values. | ||
# | ||
# For backwards compatibility Attributes#[] returns *first* value for the attribute. | ||
# Turn off compatibility to make it return all values as an array: | ||
# Attributes.single_value_compatibility = false | ||
|
||
# SAML2 Attributes. Parse the Attributes from the AttributeStatement of the SAML Response. | ||
# | ||
class Attributes | ||
include Enumerable | ||
|
||
attr_reader :attributes | ||
|
||
# By default Attributes#[] is backwards compatible and | ||
# returns only the first value for the attribute | ||
# Setting this to `false` returns all values for an attribute | ||
@@single_value_compatibility = true | ||
|
||
# Get current status of backwards compatibility mode. | ||
# @return [Boolean] Get current status of backwards compatibility mode. | ||
# | ||
def self.single_value_compatibility | ||
@@single_value_compatibility | ||
end | ||
|
||
# Sets the backwards compatibility mode on/off. | ||
# @param value [Boolean] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The line describe the behavior: Turn the backwards compatibility mode on/off. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kk, we can dismiss this one |
||
# | ||
def self.single_value_compatibility=(value) | ||
@@single_value_compatibility = value | ||
end | ||
|
||
# Initialize Attributes collection, optionally taking a Hash of attribute names and values. | ||
# | ||
# The +attrs+ must be a Hash with attribute names as keys and **arrays** as values: | ||
# @param attrs [Hash] The +attrs+ must be a Hash with attribute names as keys and **arrays** as values: | ||
# Attributes.new({ | ||
# 'name' => ['value1', 'value2'], | ||
# 'mail' => ['value1'], | ||
# }) | ||
# | ||
def initialize(attrs = {}) | ||
@attributes = attrs | ||
end | ||
|
||
|
||
# Iterate over all attributes | ||
# | ||
def each | ||
attributes.each{|name, values| yield name, values} | ||
end | ||
|
||
|
||
# Test attribute presence by name | ||
# @param name [String] The attribute name to be checked | ||
# | ||
def include?(name) | ||
attributes.has_key?(canonize_name(name)) | ||
end | ||
|
||
# Return first value for an attribute | ||
# @param name [String] The attribute name | ||
# return [String] The value (First occurrence) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return [NilClass] If the value was not found |
||
# | ||
def single(name) | ||
attributes[canonize_name(name)].first if include?(name) | ||
end | ||
|
||
# Return all values for an attribute | ||
# @param name [String] The attribute name | ||
# return [Array] Values of the attribute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it has to be always an array, wrap the response. Be careful:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daniel-g PR for that is welcome since you are the Ruby Master ;) |
||
# | ||
def multi(name) | ||
attributes[canonize_name(name)] | ||
end | ||
|
||
# By default returns first value for an attribute. | ||
# | ||
# Depending on the single value compatibility status this returns first value | ||
# Attributes.single_value_compatibility = true # Default | ||
# response.attributes['mail'] # => 'user@example.com' | ||
# Retrieve attribute value(s) | ||
# @param name [String] The attribute name | ||
# @return [String|Array] Depending on the single value compatibility status this returns: | ||
# - First value if single_value_compatibility = true | ||
# response.attributes['mail'] # => 'user@example.com' | ||
# - All values if single_value_compatibility = false | ||
# response.attributes['mail'] # => ['user@example.com','user@example.net'] | ||
# | ||
# Or all values: | ||
# Attributes.single_value_compatibility = false | ||
# response.attributes['mail'] # => ['user@example.com','user@example.net'] | ||
def [](name) | ||
self.class.single_value_compatibility ? single(canonize_name(name)) : multi(canonize_name(name)) | ||
end | ||
|
||
# Return all attributes as an array | ||
# @return [Array] Return all attributes as an array | ||
# | ||
def all | ||
attributes | ||
end | ||
|
||
# Set values for an attribute, overwriting all existing values | ||
# @param name [String] The attribute name | ||
# @param values [Array] The values | ||
# | ||
def set(name, values) | ||
attributes[canonize_name(name)] = values | ||
end | ||
alias_method :[]=, :set | ||
|
||
# Add new attribute or new value(s) to an existing attribute | ||
# @param name [String] The attribute name | ||
# @param values [Array] The values | ||
# | ||
def add(name, values = []) | ||
attributes[canonize_name(name)] ||= [] | ||
attributes[canonize_name(name)] += Array(values) | ||
end | ||
|
||
# Make comparable to another Attributes collection based on attributes | ||
# @param other [Attributes] An Attributes object to compare with | ||
# @return [Boolean] True if are contains the same attributes and values | ||
# | ||
def ==(other) | ||
if other.is_a?(Attributes) | ||
all == other.all | ||
|
@@ -97,13 +116,13 @@ def ==(other) | |
protected | ||
|
||
# stringifies all names so both 'email' and :email return the same result | ||
# @param name [String] The attribute name | ||
# @return [String] stringified name | ||
# | ||
def canonize_name(name) | ||
name.to_s | ||
end | ||
|
||
def attributes | ||
@attributes | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not service_name= ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1