11require 'cgi'
22require 'zlib'
33require '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
99module 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
10192end
0 commit comments