Skip to content

Commit aafc9b4

Browse files
committed
Pass optional prefix for UUID to Utils.uuid
1 parent 8e48c90 commit aafc9b4

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/onelogin/ruby-saml/authrequest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Authrequest < SamlMessage
2121
# Asigns an ID, a random uuid.
2222
#
2323
def initialize(**options)
24-
@uuid = "#{options[:id_prefix]}#{OneLogin::RubySaml::Utils.uuid}#{options[:id_suffix]}"
24+
@uuid = OneLogin::RubySaml::Utils.uuid(options[:id_prefix] || '_')
2525
end
2626

2727
def request_id

lib/onelogin/ruby-saml/utils.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,12 @@ def self.retrieve_plaintext(cipher_text, symmetric_key, algorithm)
333333
end
334334
end
335335

336-
def self.uuid
337-
RUBY_VERSION < '1.9' ? "_#{@@uuid_generator.generate}" : "_#{SecureRandom.uuid}"
336+
def self.uuid(prefix = '_')
337+
"#{prefix}#{generate_uuid}"
338+
end
339+
340+
def self.generate_uuid
341+
RUBY_VERSION < '1.9' ? @@uuid_generator.generate : SecureRandom.uuid
338342
end
339343

340344
# Given two strings, attempt to match them as URIs using Rails' parse method. If they can be parsed,

test/request_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ class RequestTest < Minitest::Test
161161
assert auth_url.include?('&RelayState=http%3A%2F%2Fexample.com')
162162
end
163163

164+
it "creates request with ID prefixed with default '_', when :id_prefix is not passed" do
165+
request = OneLogin::RubySaml::Authrequest.new
166+
167+
assert_match /^_/, request.uuid
168+
end
169+
170+
it "creates request with ID is prefixed, when :id_prefix is passed" do
171+
request = OneLogin::RubySaml::Authrequest.new(id_prefix: 'test')
172+
173+
assert_match /^test/, request.uuid
174+
end
175+
164176
describe "when the target url is not set" do
165177
before do
166178
settings.idp_sso_service_url = nil

test/utils_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ def result(duration, reference = 0)
205205
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
206206
end
207207

208+
it "returns a uuid starting with an prefix, when passed" do
209+
prefix = 'test'
210+
assert_match /^#{prefix}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/, OneLogin::RubySaml::Utils.uuid(prefix)
211+
end
212+
208213
it "doesn't return the same value twice" do
209214
refute_equal OneLogin::RubySaml::Utils.uuid, OneLogin::RubySaml::Utils.uuid
210215
end

0 commit comments

Comments
 (0)