summaryrefslogtreecommitdiff
path: root/lib/saml/kit/cli/commands/decode.rb
blob: 642684f51f9eb5a6cc871dc779713c86647f4885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Saml
  module Kit
    module Cli
      module Commands
        class Decode < Thor
          desc 'redirect uri', 'Decodes the uri using the HTTP Redirect binding'
          method_option :export, default: nil, required: false
          def redirect(uri)
            print_report_for(redirect_binding.deserialize(uri))
          rescue StandardError => error
            say error.message, :red
          end

          desc(
            'post saml',
            'Decodes the SAMLRequest/SAMLResponse using the HTTP Post binding'
          )
          method_option :export, default: nil, required: false
          def post(saml)
            print_report_for(post_binding.deserialize('SAMLRequest' => saml))
          rescue StandardError => error
            say error.message, :red
          end

          desc 'raw <file>', 'Decode the contents of a decoded file'
          def raw(file)
            content = IO.read(File.expand_path(file))
            print_report_for(Document.to_saml_document(content))
          rescue StandardError => error
            say error.message, :red
          end

          private

          def print_report_for(document, export = options[:export])
            IO.write(export, document.to_xml) if export
            2.times { say '' }
            Report.new(document).print(self)
          end

          def post_binding(location = '')
            Saml::Kit::Bindings::HttpPost.new(location: location)
          end

          def redirect_binding(location = '')
            Saml::Kit::Bindings::HttpRedirect.new(location: location)
          end
        end
      end
    end
  end
end