main
 1# frozen_string_literal: true
 2
 3module Saml
 4  module Kit
 5    # This module is responsible for serializing/deserialing values.
 6    module Serializable
 7      # Base 64 decodes the value.
 8      #
 9      # @param value [String] the string to base 64 decode.
10      def decode(value)
11        Base64.decode64(value)
12      end
13
14      # Base 64 encodes the value.
15      #
16      # @param value [String] the string to base 64 encode.
17      def encode(value)
18        Base64.strict_encode64(value)
19      end
20
21      # Inflates the value using zlib decompression.
22      #
23      # @param value [String] the value to inflate.
24      def inflate(value)
25        inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
26        inflater.inflate(value)
27      end
28
29      # Deflate the value and drop the header and checksum as per the SAML spec.
30      # https://en.wikipedia.org/wiki/SAML_2.0#HTTP_Redirect_Binding
31      #
32      # @param value [String] the value to deflate.
33      # @param level [Integer] the level of compression.
34      def deflate(value, level: Zlib::BEST_COMPRESSION)
35        Zlib::Deflate.deflate(value, level)[2..-5]
36      end
37
38      # URL unescape a value
39      #
40      # @param value [String] the value to unescape.
41      def unescape(value)
42        CGI.unescape(value)
43      end
44
45      # URL escape a value
46      #
47      # @param value [String] the value to escape.
48      def escape(value)
49        CGI.escape(value)
50      end
51    end
52  end
53end