Skip to content

Commit d788072

Browse files
committed
Merge pull request #214 from phlipper/cleanup-saml-message
Cleanup `SamlMessage` class
2 parents 0552dc1 + db7505b commit d788072

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

lib/onelogin/ruby-saml/saml_message.rb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require 'cgi'
22
require 'zlib'
33
require 'base64'
4-
require "nokogiri"
5-
require "rexml/document"
6-
require "rexml/xpath"
7-
require "thread"
4+
require 'nokogiri'
5+
require 'rexml/document'
6+
require 'rexml/xpath'
7+
require 'thread'
88

99
module OneLogin
1010
module RubySaml
@@ -14,7 +14,7 @@ class SamlMessage
1414
ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
1515
PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
1616

17-
BASE64_FORMAT_REGEXP = %r{\A(([A-Za-z0-9+/]{4}))*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)\Z}
17+
BASE64_FORMAT = %r(\A[A-Za-z0-9+/]{4}*[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=?\Z)
1818

1919
def self.schema
2020
@schema ||= Mutex.new.synchronize do
@@ -48,7 +48,7 @@ def validation_error(message)
4848
# is to try and inflate it and fall back to the base64 decoded string if
4949
# the stream contains errors.
5050
def decode_raw_saml(saml)
51-
return saml unless base64_formatted?(saml)
51+
return saml unless base64_encoded?(saml)
5252

5353
decoded = decode(saml)
5454
begin
@@ -59,9 +59,9 @@ def decode_raw_saml(saml)
5959
end
6060

6161
def encode_raw_saml(saml, settings)
62-
saml = Zlib::Deflate.deflate(saml, 9)[2..-5] if settings.compress_request
63-
base64_saml = Base64.encode64(saml)
64-
return CGI.escape(base64_saml)
62+
saml = deflate(saml) if settings.compress_request
63+
64+
CGI.escape(Base64.encode64(saml))
6565
end
6666

6767
def decode(encoded)
@@ -72,30 +72,21 @@ def encode(encoded)
7272
Base64.encode64(encoded).gsub(/\n/, "")
7373
end
7474

75-
# Check if the provided string is base64 encoded.
76-
# @param message [String] The value to be checked.
77-
# @return [Boolean] True if the value is a base64 encoded string.
78-
def base64_formatted?(string)
79-
string.gsub(/[\r\n]|\\r|\\n/, "").match(BASE64_FORMAT_REGEXP)
80-
end
81-
82-
def escape(unescaped)
83-
CGI.escape(unescaped)
84-
end
85-
86-
def unescape(escaped)
87-
CGI.unescape(escaped)
75+
# Check if a string is base64 encoded
76+
#
77+
# @param string [String] string to check the encoding of
78+
# @return [true, false] whether or not the string is base64 encoded
79+
def base64_encoded?(string)
80+
!!string.gsub(/[\r\n]|\\r|\\n/, "").match(BASE64_FORMAT)
8881
end
8982

9083
def inflate(deflated)
91-
zlib = Zlib::Inflate.new(-Zlib::MAX_WBITS)
92-
zlib.inflate(deflated)
84+
Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(deflated)
9385
end
9486

9587
def deflate(inflated)
9688
Zlib::Deflate.deflate(inflated, 9)[2..-5]
9789
end
98-
9990
end
10091
end
10192
end

0 commit comments

Comments
 (0)