Skip to content

Commit f130b9f

Browse files
nddelucasubfusc
authored andcommitted
remove uuid gem in favor of SecureRandom, and add util method for
creating ids
1 parent 5dcb7bb commit f130b9f

File tree

9 files changed

+33
-20
lines changed

9 files changed

+33
-20
lines changed

lib/onelogin/ruby-saml/authrequest.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
require "uuid"
21
require "rexml/document"
32

43
require "onelogin/ruby-saml/logging"
54
require "onelogin/ruby-saml/saml_message"
5+
require "onelogin/ruby-saml/utils"
66

77
# Only supports SAML 2.0
88
module OneLogin
@@ -20,7 +20,7 @@ class Authrequest < SamlMessage
2020
# Asigns an ID, a random uuid.
2121
#
2222
def initialize
23-
@uuid = "_" + UUID.new.generate
23+
@uuid = OneLogin::RubySaml::Utils.uuid
2424
end
2525

2626
# Creates the AuthNRequest string.

lib/onelogin/ruby-saml/idp_metadata_parser.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "base64"
2-
require "uuid"
32
require "zlib"
43
require "cgi"
54
require "net/http"

lib/onelogin/ruby-saml/logoutrequest.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
require "uuid"
2-
31
require "onelogin/ruby-saml/logging"
42
require "onelogin/ruby-saml/saml_message"
3+
require "onelogin/ruby-saml/utils"
54

65
# Only supports SAML 2.0
76
module OneLogin
@@ -18,7 +17,7 @@ class Logoutrequest < SamlMessage
1817
# Asigns an ID, a random uuid.
1918
#
2019
def initialize
21-
@uuid = "_" + UUID.new.generate
20+
@uuid = OneLogin::RubySaml::Utils.uuid
2221
end
2322

2423
# Creates the Logout Request string.
@@ -108,7 +107,7 @@ def create_logout_request_xml_doc(settings)
108107
nameid.text = settings.name_identifier_value
109108
else
110109
# If no NameID is present in the settings we generate one
111-
nameid.text = "_" + UUID.new.generate
110+
nameid.text = OneLogin::RubySaml::Utils.uuid
112111
nameid.attributes['Format'] = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'
113112
end
114113

lib/onelogin/ruby-saml/slo_logoutresponse.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require "uuid"
2-
31
require "onelogin/ruby-saml/logging"
2+
43
require "onelogin/ruby-saml/saml_message"
4+
require "onelogin/ruby-saml/utils"
55

66
# Only supports SAML 2.0
77
module OneLogin
@@ -18,7 +18,7 @@ class SloLogoutresponse < SamlMessage
1818
# Asigns an ID, a random uuid.
1919
#
2020
def initialize
21-
@uuid = "_" + UUID.new.generate
21+
@uuid = OneLogin::RubySaml::Utils.uuid
2222
end
2323

2424
# Creates the Logout Response string.

lib/onelogin/ruby-saml/utils.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module OneLogin
22
module RubySaml
33

44
# SAML2 Auxiliary class
5-
#
5+
#
66
class Utils
77

88
DSIG = "http://www.w3.org/2000/09/xmldsig#"
@@ -30,7 +30,7 @@ def self.format_cert(cert)
3030
# @return [String] The formatted private key
3131
#
3232
def self.format_private_key(key)
33-
# don't try to format an encoded private key or if is empty
33+
# don't try to format an encoded private key or if is empty
3434
return key if key.nil? || key.empty? || key.match(/\x0d/)
3535

3636
# is this an rsa key?
@@ -114,7 +114,7 @@ def self.decrypt_data(encrypted_node, private_key)
114114
{ 'xenc' => XENC }
115115
)
116116
algorithm = encrypt_method.attributes['Algorithm']
117-
retrieve_plaintext(node, symmetric_key, algorithm)
117+
retrieve_plaintext(node, symmetric_key, algorithm)
118118
end
119119

120120
# Obtains the symmetric key from the EncryptedData element
@@ -134,7 +134,7 @@ def self.retrieve_symmetric_key(encrypt_data, private_key)
134134
{"ds" => DSIG, "xenc" => XENC }
135135
)
136136
algorithm = encrypt_method.attributes['Algorithm']
137-
retrieve_plaintext(cipher_text, private_key, algorithm)
137+
retrieve_plaintext(cipher_text, private_key, algorithm)
138138
end
139139

140140
# Obtains the deciphered text
@@ -152,7 +152,7 @@ def self.retrieve_plaintext(cipher_text, symmetric_key, algorithm)
152152
when 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' then oaep = symmetric_key
153153
end
154154

155-
if cipher
155+
if cipher
156156
iv_len = cipher.iv_len
157157
data = cipher_text[iv_len..-1]
158158
cipher.padding, cipher.key, cipher.iv = 0, symmetric_key, cipher_text[0..iv_len-1]
@@ -167,6 +167,9 @@ def self.retrieve_plaintext(cipher_text, symmetric_key, algorithm)
167167
end
168168
end
169169

170+
def self.uuid
171+
"_#{SecureRandom.uuid}"
172+
end
170173
end
171174
end
172175
end

ruby-saml.gemspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ Gem::Specification.new do |s|
2525
s.summary = %q{SAML Ruby Tookit}
2626
s.test_files = `git ls-files test/*`.split("\n")
2727

28-
s.add_runtime_dependency('uuid', '~> 2.3')
29-
3028
# Because runtime dependencies are determined at build time, we cannot make
3129
# Nokogiri's version dependent on the Ruby version, even though we would
3230
# have liked to constrain Ruby 1.8.7 to install only the 1.5.x versions.

test/logoutrequest_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class RequestTest < Minitest::Test
2929
end
3030

3131
it "set sessionindex" do
32-
sessionidx = UUID.new.generate
32+
settings.idp_slo_target_url = "http://example.com"
33+
sessionidx = OneLogin::RubySaml::Utils.uuid
3334
settings.sessionindex = sessionidx
3435

3536
unauth_url = OneLogin::RubySaml::Logoutrequest.new.create(settings, { :nameid => "there" })

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def ruby_saml_key_text
203203
# logoutresponse fixtures
204204
#
205205
def random_id
206-
"_#{UUID.new.generate}"
206+
"_#{OneLogin::RubySaml::Utils.uuid}"
207207
end
208208

209209
#

test/utils_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,17 @@ class UtilsTest < Minitest::Test
142142
assert_equal = "The status code of the Logout Response was not Success", status_error_msg3
143143
end
144144
end
145-
end
145+
146+
describe "Utils" do
147+
148+
describe ".uuid" do
149+
it "returns a uuid starting with an underscore" do
150+
assert_match /^_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/, OneLogin::RubySaml::Utils.uuid
151+
end
152+
153+
it "doesn't return the same value twice" do
154+
refute_equal OneLogin::RubySaml::Utils.uuid, OneLogin::RubySaml::Utils.uuid
155+
end
156+
end
157+
end
158+
end

0 commit comments

Comments
 (0)