ruby-saml 0.8.12
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ruby-saml might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +12 -0
- data/.travis.yml +11 -0
- data/Gemfile +37 -0
- data/LICENSE +19 -0
- data/README.md +160 -0
- data/Rakefile +27 -0
- data/changelog.md +24 -0
- data/lib/onelogin/ruby-saml/attributes.rb +147 -0
- data/lib/onelogin/ruby-saml/authrequest.rb +168 -0
- data/lib/onelogin/ruby-saml/logging.rb +26 -0
- data/lib/onelogin/ruby-saml/logoutrequest.rb +161 -0
- data/lib/onelogin/ruby-saml/logoutresponse.rb +153 -0
- data/lib/onelogin/ruby-saml/metadata.rb +66 -0
- data/lib/onelogin/ruby-saml/response.rb +426 -0
- data/lib/onelogin/ruby-saml/setting_error.rb +6 -0
- data/lib/onelogin/ruby-saml/settings.rb +166 -0
- data/lib/onelogin/ruby-saml/slo_logoutresponse.rb +158 -0
- data/lib/onelogin/ruby-saml/utils.rb +119 -0
- data/lib/onelogin/ruby-saml/validation_error.rb +7 -0
- data/lib/onelogin/ruby-saml/version.rb +5 -0
- data/lib/ruby-saml.rb +12 -0
- data/lib/schemas/saml20assertion_schema.xsd +283 -0
- data/lib/schemas/saml20protocol_schema.xsd +302 -0
- data/lib/schemas/xenc_schema.xsd +146 -0
- data/lib/schemas/xmldsig_schema.xsd +318 -0
- data/lib/xml_security.rb +292 -0
- data/ruby-saml.gemspec +28 -0
- data/test/certificates/certificate1 +12 -0
- data/test/certificates/r1_certificate2_base64 +1 -0
- data/test/certificates/ruby-saml.crt +14 -0
- data/test/certificates/ruby-saml.key +15 -0
- data/test/logoutrequest_test.rb +244 -0
- data/test/logoutresponse_test.rb +112 -0
- data/test/request_test.rb +229 -0
- data/test/response_test.rb +475 -0
- data/test/responses/adfs_response_sha1.xml +46 -0
- data/test/responses/adfs_response_sha256.xml +46 -0
- data/test/responses/adfs_response_sha384.xml +46 -0
- data/test/responses/adfs_response_sha512.xml +46 -0
- data/test/responses/encrypted_new_attack.xml.base64 +1 -0
- data/test/responses/logoutresponse_fixtures.rb +67 -0
- data/test/responses/no_signature_ns.xml +48 -0
- data/test/responses/open_saml_response.xml +56 -0
- data/test/responses/r1_response6.xml.base64 +1 -0
- data/test/responses/response1.xml.base64 +1 -0
- data/test/responses/response2.xml.base64 +79 -0
- data/test/responses/response3.xml.base64 +66 -0
- data/test/responses/response4.xml.base64 +93 -0
- data/test/responses/response5.xml.base64 +102 -0
- data/test/responses/response_eval.xml +7 -0
- data/test/responses/response_node_text_attack.xml.base64 +1 -0
- data/test/responses/response_with_ampersands.xml +139 -0
- data/test/responses/response_with_ampersands.xml.base64 +93 -0
- data/test/responses/response_with_concealed_signed_assertion.xml +51 -0
- data/test/responses/response_with_doubled_signed_assertion.xml +49 -0
- data/test/responses/response_with_multiple_attribute_statements.xml +72 -0
- data/test/responses/response_with_multiple_attribute_values.xml +67 -0
- data/test/responses/response_wrapped.xml.base64 +150 -0
- data/test/responses/simple_saml_php.xml +71 -0
- data/test/responses/starfield_response.xml.base64 +1 -0
- data/test/responses/valid_response.xml.base64 +1 -0
- data/test/responses/wrapped_response_2.xml.base64 +150 -0
- data/test/settings_test.rb +47 -0
- data/test/slo_logoutresponse_test.rb +226 -0
- data/test/test_helper.rb +155 -0
- data/test/utils_test.rb +41 -0
- data/test/xml_security_test.rb +158 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA512:
|
3
|
+
metadata.gz: 8a2479b6725a5a9e7fdc76a4bec612e2f0c66cf53cbb79ff7c1dc0343d1cc56c09e9fd3b2d3490bdacbb09b16b473702d42fa42fdb6fedff6e7fa5a44fa421a2
|
4
|
+
data.tar.gz: 43b1cfb12dc3fc14a2cbc139430e3ad15b975dca0d95d0d8c14363f362833de181a52ca10b8b607215c4b5aa23ffa62f2d8f3a3b2c9b5541e9387c635ca77150
|
5
|
+
SHA256:
|
6
|
+
metadata.gz: 694ade703ed05cc38aa2ca98cbfee57cc16223991ae6539422c136164cf29608
|
7
|
+
data.tar.gz: ee07b69a9391b26c9af95d0cfdbaa57c8991fa187b869660e15c549fcbbe47e3
|
data/.document
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# Please keep this file alphabetized and organized
|
3
|
+
#
|
4
|
+
source 'http://rubygems.org'
|
5
|
+
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
if RUBY_VERSION < '1.9'
|
9
|
+
gem 'nokogiri', '~> 1.5.0'
|
10
|
+
gem 'minitest', '~> 5.5', '<= 5.11.3'
|
11
|
+
elsif RUBY_VERSION < '2.1'
|
12
|
+
gem 'nokogiri', '>= 1.5.0', '<= 1.6.8.1'
|
13
|
+
gem 'minitest', '~> 5.5'
|
14
|
+
else
|
15
|
+
gem 'nokogiri', '>= 1.5.0'
|
16
|
+
gem 'minitest', '~> 5.5'
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
if RUBY_VERSION < '1.9'
|
21
|
+
gem 'ruby-debug', '~> 0.10.4'
|
22
|
+
elsif RUBY_VERSION < '2.0'
|
23
|
+
gem 'debugger-linecache', '~> 1.2.0'
|
24
|
+
gem 'debugger', '~> 1.6.4'
|
25
|
+
elsif RUBY_VERSION < '2.1'
|
26
|
+
gem 'byebug', '~> 2.1.1'
|
27
|
+
else
|
28
|
+
gem 'pry-byebug'
|
29
|
+
end
|
30
|
+
|
31
|
+
gem 'mocha', '~> 0.14', :require => false
|
32
|
+
gem 'rake', '~> 10'
|
33
|
+
gem 'shoulda', '~> 2.11'
|
34
|
+
gem 'systemu', '~> 2'
|
35
|
+
gem 'test-unit', '~> 3.0.9'
|
36
|
+
gem 'timecop', '<= 0.6.0'
|
37
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 OneLogin, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# Ruby SAML [![Build Status](https://secure.travis-ci.org/onelogin/ruby-saml.png)](http://travis-ci.org/onelogin/ruby-saml)
|
2
|
+
|
3
|
+
# Updating from 0.8.8 to 0.8.9
|
4
|
+
Version `0.8.9` deprecates the use of settings.issuer, use instead settings.sp_entity_id. Deprecates assertion_consumer_logout_service_url and assertion_consumer_logout_service_binding as well, use instead single_logout_service_url and single_logout_service_binding. Adds validate_audience.
|
5
|
+
|
6
|
+
# Updating from 0.8.7 to 0.8.8
|
7
|
+
Version `0.8.8` adds support for ForceAuthn and Subjects on AuthNRequests by the new name_identifier_value_requested setting
|
8
|
+
|
9
|
+
## Note on versions 0.8.6 and 0.8.7
|
10
|
+
Version `0.8.6` introduced an incompatibility with regards to manipulating the `OneLogin::RubySaml::Response#attributes` property; in this version
|
11
|
+
the `#attributes` property is a class (`OneLogin::RubySaml::Attributes`) which implements the `Enumerator` module, thus any non-overriden Hash method
|
12
|
+
will throw a NoMethodError.
|
13
|
+
Version `0.8.7` overrides the behavior of class `OneLogin::RubySaml::Attributes` by making any method not found on the class be tested on the
|
14
|
+
internal hash structure, thus all methods available in the `Hash` class are now available in `OneLogin::RubySaml::Response#attributes`.
|
15
|
+
|
16
|
+
## Updating from 0.7.x to 0.8.x
|
17
|
+
Version `0.8.x` changes the namespace of the gem from `OneLogin::Saml` to `OneLogin::RubySaml`. Please update your implementations of the gem accordingly.
|
18
|
+
|
19
|
+
## Overview
|
20
|
+
|
21
|
+
The Ruby SAML library is for implementing the client side of a SAML authorization, i.e. it provides a means for managing authorization initialization and confirmation requests from identity providers.
|
22
|
+
|
23
|
+
SAML authorization is a two step process and you are expected to implement support for both.
|
24
|
+
|
25
|
+
## The initialization phase
|
26
|
+
|
27
|
+
This is the first request you will get from the identity provider. It will hit your application at a specific URL (that you've announced as being your SAML initialization point). The response to this initialization, is a redirect back to the identity provider, which can look something like this (ignore the saml_settings method call for now):
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
def init
|
31
|
+
request = OneLogin::RubySaml::Authrequest.new
|
32
|
+
redirect_to(request.create(saml_settings))
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Once you've redirected back to the identity provider, it will ensure that the user has been authorized and redirect back to your application for final consumption, this is can look something like this (the authorize_success and authorize_failure methods are specific to your application):
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
def consume
|
40
|
+
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
|
41
|
+
response.settings = saml_settings
|
42
|
+
|
43
|
+
if response.is_valid? && user = current_account.users.find_by_email(response.name_id)
|
44
|
+
authorize_success(user)
|
45
|
+
else
|
46
|
+
authorize_failure(user)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
In the above there are a few assumptions in place, one being that the response.name_id is an email address. This is all handled with how you specify the settings that are in play via the saml_settings method. That could be implemented along the lines of this:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
def saml_settings
|
55
|
+
settings = OneLogin::RubySaml::Settings.new
|
56
|
+
|
57
|
+
settings.assertion_consumer_service_url = "http://#{request.host}/saml/finalize"
|
58
|
+
settings.sp_entity_id = request.host
|
59
|
+
settings.idp_sso_target_url = "https://app.onelogin.com/saml/signon/#{OneLoginAppId}"
|
60
|
+
settings.idp_cert_fingerprint = OneLoginAppCertFingerPrint
|
61
|
+
settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
|
62
|
+
# Optional for most SAML IdPs
|
63
|
+
settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
|
64
|
+
|
65
|
+
settings
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
What's left at this point, is to wrap it all up in a controller and point the initialization and consumption URLs in OneLogin at that. A full controller example could look like this:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# This controller expects you to use the URLs /saml/init and /saml/consume in your OneLogin application.
|
73
|
+
class SamlController < ApplicationController
|
74
|
+
def init
|
75
|
+
request = OneLogin::RubySaml::Authrequest.new
|
76
|
+
redirect_to(request.create(saml_settings))
|
77
|
+
end
|
78
|
+
|
79
|
+
def consume
|
80
|
+
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
|
81
|
+
response.settings = saml_settings
|
82
|
+
|
83
|
+
if response.is_valid? && user = current_account.users.find_by_email(response.name_id)
|
84
|
+
authorize_success(user)
|
85
|
+
else
|
86
|
+
authorize_failure(user)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def saml_settings
|
93
|
+
settings = OneLogin::RubySaml::Settings.new
|
94
|
+
|
95
|
+
settings.assertion_consumer_service_url = "http://#{request.host}/saml/consume"
|
96
|
+
settings.sp_entity_id = request.host
|
97
|
+
settings.idp_sso_target_url = "https://app.onelogin.com/saml/signon/#{OneLoginAppId}"
|
98
|
+
settings.idp_cert_fingerprint = OneLoginAppCertFingerPrint
|
99
|
+
settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
|
100
|
+
# Optional for most SAML IdPs
|
101
|
+
settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
|
102
|
+
|
103
|
+
settings
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
If are using saml:AttributeStatement to transfare metadata, like the user name, you can access all the attributes through response.attributes. It
|
109
|
+
contains all the saml:AttributeStatement with its 'Name' as a indifferent key and the one saml:AttributeValue as value.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
|
113
|
+
response.settings = saml_settings
|
114
|
+
|
115
|
+
response.attributes[:username]
|
116
|
+
```
|
117
|
+
|
118
|
+
## Service Provider Metadata
|
119
|
+
|
120
|
+
To form a trusted pair relationship with the IdP, the SP (you) need to provide metadata XML
|
121
|
+
to the IdP for various good reasons. (Caching, certificate lookups, relaying party permissions, etc)
|
122
|
+
|
123
|
+
The class OneLogin::RubySaml::Metadata takes care of this by reading the Settings and returning XML. All
|
124
|
+
you have to do is add a controller to return the data, then give this URL to the IdP administrator.
|
125
|
+
The metdata will be polled by the IdP every few minutes, so updating your settings should propagate
|
126
|
+
to the IdP settings.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class SamlController < ApplicationController
|
130
|
+
# ... the rest of your controller definitions ...
|
131
|
+
def metadata
|
132
|
+
settings = Account.get_saml_settings
|
133
|
+
meta = OneLogin::RubySaml::Metadata.new
|
134
|
+
render :xml => meta.generate(settings)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
## Clock Drift
|
140
|
+
|
141
|
+
Server clocks tend to drift naturally. If during validation of the response you get the error "Current time is earlier than NotBefore condition" then this may be due to clock differences between your system and that of the Identity Provider.
|
142
|
+
|
143
|
+
First, ensure that both systems synchronize their clocks, using for example the industry standard [Network Time Protocol (NTP)](http://en.wikipedia.org/wiki/Network_Time_Protocol).
|
144
|
+
|
145
|
+
Even then you may experience intermittent issues though, because the clock of the Identity Provider may drift slightly ahead of your system clocks. To allow for a small amount of clock drift you can initialize the response passing in an option named `:allowed_clock_drift`. Its value must be given in a number (and/or fraction) of seconds. The value given is added to the current time at which the response is validated before it's tested against the `NotBefore` assertion. For example:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], :allowed_clock_drift => 1)
|
149
|
+
```
|
150
|
+
|
151
|
+
Make sure to keep the value as comfortably small as possible to keep security risks to a minimum.
|
152
|
+
|
153
|
+
## Note on Patches/Pull Requests
|
154
|
+
|
155
|
+
* Fork the project.
|
156
|
+
* Make your feature addition or bug fix.
|
157
|
+
* Add tests for it. This is important so I don't break it in a
|
158
|
+
future version unintentionally.
|
159
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
160
|
+
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
#not being used yet.
|
5
|
+
require 'rake/testtask'
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.pattern = 'test/**/*_test.rb'
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'rcov/rcovtask'
|
14
|
+
Rcov::RcovTask.new do |test|
|
15
|
+
test.libs << 'test'
|
16
|
+
test.pattern = 'test/**/*_test.rb'
|
17
|
+
test.verbose = true
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
task :rcov do
|
21
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
task :test
|
26
|
+
|
27
|
+
task :default => :test
|
data/changelog.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# RubySaml Changelog
|
2
|
+
|
3
|
+
### 0.8.4 (March 5, 2018)
|
4
|
+
* Improve the fix for CVE-2017-11428 to parse CDATA properly
|
5
|
+
|
6
|
+
### 0.8.3 (Feb 27, 2018)
|
7
|
+
* Fix vulnerability CVE-2017-11428. Process text of nodes properly, ignoring comments
|
8
|
+
* Fix DigestMethod lookup bug #144
|
9
|
+
|
10
|
+
### 0.8.2 (Jan 26, 2014)
|
11
|
+
* [#183](https://github.com/onelogin/ruby-saml/pull/183) Resolved a security vulnerability where string interpolation in a `REXML::XPath.first()` method call allowed for arbitrary code execution.
|
12
|
+
|
13
|
+
### 0.8.0 (Feb 21, 2014)
|
14
|
+
**IMPORTANT**: This release changed namespace of the gem from `OneLogin::Saml` to `OneLogin::RubySaml`. Please update your implementations of the gem accordingly.
|
15
|
+
|
16
|
+
* [#111](https://github.com/onelogin/ruby-saml/pull/111) `Onelogin::` is `OneLogin::`
|
17
|
+
* [#108](https://github.com/onelogin/ruby-saml/pull/108) Change namespacing from `Onelogin::Saml` to `Onelogin::Rubysaml`
|
18
|
+
|
19
|
+
|
20
|
+
### 0.7.3 (Feb 20, 2014)
|
21
|
+
Updated gem dependencies to be compatible with Ruby 1.8.7-p374 and 1.9.3-p448. Removed unnecessary `canonix` gem dependency.
|
22
|
+
|
23
|
+
* [#107](https://github.com/onelogin/ruby-saml/pull/107) Relax nokogiri version requirement to >= 1.5.0
|
24
|
+
* [#105](https://github.com/onelogin/ruby-saml/pull/105) Lock Gem versions, fix to resolve possible namespace collision
|
@@ -0,0 +1,147 @@
|
|
1
|
+
module OneLogin
|
2
|
+
module RubySaml
|
3
|
+
|
4
|
+
# SAML2 Attributes. Parse the Attributes from the AttributeStatement of the SAML Response.
|
5
|
+
#
|
6
|
+
class Attributes
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
attr_reader :attributes
|
10
|
+
|
11
|
+
# By default Attributes#[] is backwards compatible and
|
12
|
+
# returns only the first value for the attribute
|
13
|
+
# Setting this to `false` returns all values for an attribute
|
14
|
+
@@single_value_compatibility = true
|
15
|
+
|
16
|
+
# @return [Boolean] Get current status of backwards compatibility mode.
|
17
|
+
#
|
18
|
+
def self.single_value_compatibility
|
19
|
+
@@single_value_compatibility
|
20
|
+
end
|
21
|
+
|
22
|
+
# Sets the backwards compatibility mode on/off.
|
23
|
+
# @param value [Boolean]
|
24
|
+
#
|
25
|
+
def self.single_value_compatibility=(value)
|
26
|
+
@@single_value_compatibility = value
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param attrs [Hash] The +attrs+ must be a Hash with attribute names as keys and **arrays** as values:
|
30
|
+
# Attributes.new({
|
31
|
+
# 'name' => ['value1', 'value2'],
|
32
|
+
# 'mail' => ['value1'],
|
33
|
+
# })
|
34
|
+
#
|
35
|
+
def initialize(attrs = {})
|
36
|
+
@attributes = attrs
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# Iterate over all attributes
|
41
|
+
#
|
42
|
+
def each
|
43
|
+
attributes.each{|name, values| yield name, values}
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# Test attribute presence by name
|
48
|
+
# @param name [String] The attribute name to be checked
|
49
|
+
#
|
50
|
+
def include?(name)
|
51
|
+
attributes.has_key?(canonize_name(name)) || attributes.has_key?(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Return first value for an attribute
|
55
|
+
# @param name [String] The attribute name
|
56
|
+
# @return [String] The value (First occurrence)
|
57
|
+
#
|
58
|
+
def single(name)
|
59
|
+
multi(name).first if include?(name)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return all values for an attribute
|
63
|
+
# @param name [String] The attribute name
|
64
|
+
# @return [Array] Values of the attribute
|
65
|
+
#
|
66
|
+
def multi(name)
|
67
|
+
values = attributes[canonize_name(name)] || attributes[name]
|
68
|
+
|
69
|
+
if values.is_a?(Array)
|
70
|
+
values
|
71
|
+
elsif !values.nil?
|
72
|
+
Array(values)
|
73
|
+
else
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Retrieve attribute value(s)
|
79
|
+
# @param name [String] The attribute name
|
80
|
+
# @return [String|Array] Depending on the single value compatibility status this returns:
|
81
|
+
# - First value if single_value_compatibility = true
|
82
|
+
# response.attributes['mail'] # => 'user@example.com'
|
83
|
+
# - All values if single_value_compatibility = false
|
84
|
+
# response.attributes['mail'] # => ['user@example.com','user@example.net']
|
85
|
+
#
|
86
|
+
def [](name)
|
87
|
+
self.class.single_value_compatibility ? single(name) : multi(name)
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Array] Return all attributes as an array
|
91
|
+
#
|
92
|
+
def all
|
93
|
+
attributes
|
94
|
+
end
|
95
|
+
|
96
|
+
# @param name [String] The attribute name
|
97
|
+
# @param values [Array] The values
|
98
|
+
#
|
99
|
+
def set(name, values)
|
100
|
+
attributes[canonize_name(name)] = values
|
101
|
+
end
|
102
|
+
alias_method :[]=, :set
|
103
|
+
|
104
|
+
# @param name [String] The attribute name
|
105
|
+
# @param values [Array] The values
|
106
|
+
#
|
107
|
+
def add(name, values = [])
|
108
|
+
attributes[canonize_name(name)] ||= []
|
109
|
+
attributes[canonize_name(name)] += Array(values)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Make comparable to another Attributes collection based on attributes
|
113
|
+
# @param other [Attributes] An Attributes object to compare with
|
114
|
+
# @return [Boolean] True if are contains the same attributes and values
|
115
|
+
#
|
116
|
+
def ==(other)
|
117
|
+
if other.is_a?(Attributes)
|
118
|
+
all == other.all
|
119
|
+
else
|
120
|
+
super
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def respond_to?(name)
|
125
|
+
attributes.respond_to?(name) || super
|
126
|
+
end
|
127
|
+
|
128
|
+
protected
|
129
|
+
|
130
|
+
# stringifies all names so both 'email' and :email return the same result
|
131
|
+
# @param name [String] The attribute name
|
132
|
+
# @return [String] stringified name
|
133
|
+
#
|
134
|
+
def canonize_name(name)
|
135
|
+
name.to_s
|
136
|
+
end
|
137
|
+
|
138
|
+
def method_missing(name, *args, &block)
|
139
|
+
if attributes.respond_to?(name)
|
140
|
+
attributes.send(name, *args, &block)
|
141
|
+
else
|
142
|
+
super
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|