summaryrefslogtreecommitdiff
path: root/bin/idp
blob: daa15cc6a44b3e9143810cb68c8b52002ea43b44 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env ruby

# Start the server by running:
#
# $ ruby main.rb

require "bundler/inline"
gemfile do
  source "https://rubygems.org"

  gem "erb", "~> 4.0"
  gem "rack", "~> 3.0"
  gem "rackup", "~> 2.0"
  gem "saml-kit", "~> 1.0"
  gem "webrick", "~> 1.0"
end

class User
  def initialize(attributes)
    @attributes = attributes
  end

  def name_id_for(name_id_format)
    @attributes[:email]
  end

  def assertion_attributes_for(request)
    {
      custom: 'custom attribute'
    }
  end
end

class OnDemandRegistry < Saml::Kit::DefaultRegistry
  def metadata_for(entity_id)
    found = super(entity_id)
    return found if found

    register_url(entity_id, verify_ssl: false)
    super(entity_id)
  end
end

Saml::Kit.configure do |x|
  x.entity_id = "http://localhost:8282/metadata.xml"
  x.registry = OnDemandRegistry.new
  x.logger = Logger.new("/dev/stderr")
end

class IdentityProvider
  def initialize
    @storage = {}
  end

  # Download IDP Metadata
  #
  # GET /metadata.xml
  def metadata
    xml = Saml::Kit::Metadata.build_xml do |builder|
      builder.contact_email = 'hi@example.com'
      builder.organization_name = "Acme, Inc"
      builder.organization_url = "https://example.com"
      builder.build_identity_provider do |x|
        x.add_single_sign_on_service("http://localhost:8282/sessions/new", binding: :http_post)
        x.name_id_formats = [Saml::Kit::Namespaces::EMAIL_ADDRESS]
        x.attributes << :Username
      end
    end

    [200, { 'Content-Type' => "application/samlmetadata+xml" }, [xml]]
  end

  def call(env)
    path = env['PATH_INFO']
    case env['REQUEST_METHOD']
    when 'GET'
      case path
      when "/metadata.xml"
        return metadata
      when "/sessions/new"
        return post_back(Rack::Request.new(env))
      else
        return not_found
      end
    when 'POST'
      case path
      when "/sessions/new"
        return post_back(Rack::Request.new(env))
      else
        return not_found
      end
    end
    not_found
  end

  private

  def post_back(request)
    params = saml_params_from(request)
    saml_request = binding_for(request).deserialize(params)
    @builder = nil
    url, saml_params = saml_request.response_for(
      User.new({ email: "example@example.com" }),
      binding: :http_post,
      relay_state: params[:RelayState]
    ) do |builder|
      builder.embed_signature = true
      @builder = builder
    end
    template = <<~ERB
      <!doctype html>
      <html>
        <head><title></title></head>
        <body>
          <h2>Recieved SAML Request</h2>
          <textarea readonly="readonly" disabled="disabled" cols=225 rows=6><%=- saml_request.to_xml(pretty: true) -%></textarea>

          <h2>Sending SAML Response (IdP -> SP)</h2>
          <textarea readonly="readonly" disabled="disabled" cols=225 rows=30><%=- @builder.build.to_xml(pretty: true) -%></textarea>
          <form action="<%= url %>" method="post">
            <%- saml_params.each do |(key, value)| -%>
              <input type="hidden" name="<%= key %>" value="<%= value %>" />
            <%- end -%>
            <input type="submit" value="Submit" />
          </form>
        </body>
      </html>
    ERB
    erb = ERB.new(template, nil, trim_mode: '-')
    html = erb.result(binding)
    [200, { 'Content-Type' => "text/html" }, [html]]
  end


  def not_found
    [404, {}, []]
  end

  def saml_params_from(request)
    if request.post?
      {
        "SAMLRequest" => request.params["SAMLRequest"],
        "RelayState" => request.params["RelayState"],
      }
    else
      query_string = request.query_string
      on = query_string.include?("&amp;") ? "&amp;" : "&"
      Hash[query_string.split(on).map { |x| x.split("=", 2) }].symbolize_keys
    end
  end

  def binding_for(request)
    location = "http://localhost:8282/sessions/new"
    if request.post?
      Saml::Kit::Bindings::HttpPost
        .new(location: location)
    else
      Saml::Kit::Bindings::HttpRedirect
        .new(location: location)
    end
  end
end

if __FILE__ == $0
  app = Rack::Builder.new do
    use Rack::Reloader
    run IdentityProvider.new
  end.to_app

  Rackup::Server.start(app: app, Port: 8282)
end