Skip to content

Commit 913eb1f

Browse files
committed
sync with samly v0.8.0
1 parent 204b7ca commit 913eb1f

File tree

7 files changed

+175
-36
lines changed

7 files changed

+175
-36
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
# SamlyHowto
22

3-
A Phoenix application that demonstrates how it is turned into a
4-
SAML Service Provider using [`Samly`](https://github.com/handnot2/samly)
5-
Elixir library.
3+
`SamlyHowto` is a Phoenix application that shows how to use the
4+
[`Samly`](https://hex.pm/packages/samly) Elixir Plug library for
5+
SAML 2.0 authentication. The Phoenix application becomes a
6+
SAML Service Provider.
7+
8+
This Web application shows the attributes in the SAML Assertion
9+
returned by the Identity Provider (IdP) upon successful user
10+
authentication.
11+
12+
It is fairly easy to enable SAML 2.0 authentication using the
13+
`samly` library. Often times, the initial hickups are related to
14+
configuring your application with proper Identity Provider settings.
15+
16+
Use this project as a test application to make sure that you are
17+
able to successfully authenticate with your IdP. Once confirmed,
18+
you can use those configuration settings with your own application.
19+
This help you in isolating and correcting any configuration issues.
20+
The changes required are confined to the `config/dev.exs` file.
621

722
```sh
823
# add the following to your /etc/hosts file:
924
# 127.0.0.1 samly.howto
25+
# 127.0.0.1 idp2.samly.howto
26+
# 127.0.0.1 idp3.samly.howto
1027

1128
git clone https://github.com/handnot2/samly_howto
1229
cd samly_howto
1330
./gencert.sh
31+
cd assets && npm install && cd ..
1432
mix deps.get
1533
mix compile
1634
```
@@ -29,4 +47,4 @@ Start this application by running
2947
```
3048

3149
This application can now be accessed from the browser by visiting the
32-
URL: `http://samly.howto:4003`
50+
URL: `http://samly.howto:4003`.

config/dev.exs

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,78 @@ config :logger, :console, format: "[$level] $message\n"
5050
config :phoenix, :stacktrace_depth, 20
5151

5252
config :samly, Samly.Provider,
53-
#base_url: "http://samly.howto:4003/sso",
54-
#entity_id: "urn:myhost-name:my-id",
55-
#use_redirect_for_idp_req: false,
56-
#sign_requests: true,
57-
#sign_metadata: true,
58-
#signed_assertion_in_idp_resp: true,
59-
#signed_envelopes_in_idp_resp: true,
60-
pre_session_create_pipeline: SamlyHowtoWeb.Plugs.SamlyPipeline,
61-
certfile: "samly.crt",
62-
keyfile: "samly.pem",
63-
idp_metadata_file: "idp_metadata.xml"
53+
idp_id_from: :path_segment,
54+
#idp_id_from: :subdomain,
55+
service_providers: [
56+
%{
57+
id: "sp1",
58+
entity_id: "urn:samly.howto:sp1",
59+
certfile: "samly.crt",
60+
keyfile: "samly.pem",
61+
contact_name: "Samly Howto SP1 Admin",
62+
contact_email: "[email protected]",
63+
org_name: "Samly Howto SP1",
64+
org_displayname: "Samly Howto SP1 Displayname",
65+
org_url: "http://samly.howto:4003"
66+
},
67+
%{
68+
id: "sp2",
69+
entity_id: "urn:idp2.samly.howto:sp2",
70+
certfile: "samly.crt",
71+
keyfile: "samly.pem",
72+
#contact_name: "Samly Howto SP2 Admin",
73+
#contact_email: "[email protected]",
74+
#org_name: "Samly Howto SP2",
75+
#org_displayname: "Samly Howto SP2 Displayname",
76+
#org_url: "http://idp2.samly.howto:4003"
77+
},
78+
%{
79+
id: "sp3",
80+
entity_id: "urn:idp3.samly.howto:sp3",
81+
certfile: "samly.crt",
82+
keyfile: "samly.pem",
83+
#contact_name: "Samly Howto SP3 Admin",
84+
#contact_email: "[email protected]",
85+
#org_name: "Samly Howto SP3",
86+
#org_displayname: "Samly Howto SP3 Displayname",
87+
#org_url: "http://idp3.samly.howto:4003"
88+
}
89+
],
90+
identity_providers: [
91+
%{
92+
id: "idp1",
93+
sp_id: "sp1",
94+
base_url: "http://samly.howto:4003/sso",
95+
metadata_file: "idp_metadata.xml",
96+
pre_session_create_pipeline: SamlyHowtoWeb.Plugs.SamlyPipeline,
97+
#use_redirect_for_req: false,
98+
#sign_requests: true,
99+
#sign_metadata: true,
100+
#signed_assertion_in_resp: true,
101+
#signed_envelopes_in_resp: true
102+
},
103+
%{
104+
id: "idp2",
105+
sp_id: "sp2",
106+
base_url: "http://idp2.samly.howto:4003/sso",
107+
metadata_file: "idp_metadata.xml",
108+
pre_session_create_pipeline: SamlyHowtoWeb.Plugs.SamlyPipeline,
109+
#use_redirect_for_req: false,
110+
#sign_requests: true,
111+
#sign_metadata: true,
112+
#signed_assertion_in_resp: true,
113+
#signed_envelopes_in_resp: true
114+
},
115+
%{
116+
id: "idp3",
117+
sp_id: "sp3",
118+
base_url: "http://idp3.samly.howto:4003/sso",
119+
metadata_file: "idp_metadata.xml",
120+
pre_session_create_pipeline: SamlyHowtoWeb.Plugs.SamlyPipeline,
121+
#use_redirect_for_req: false,
122+
#sign_requests: true,
123+
#sign_metadata: true,
124+
#signed_assertion_in_resp: true,
125+
#signed_envelopes_in_resp: true
126+
}
127+
]

lib/samly_howto_web/controllers/page_controller.ex

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,41 @@ defmodule SamlyHowtoWeb.PageController do
33

44
def index(conn, _params) do
55
assertion = Samly.get_active_assertion(conn)
6-
uid = Samly.get_attribute(assertion, :uid)
7-
{attributes, computed} = if assertion do
8-
{assertion.attributes, assertion.computed}
6+
uid = Samly.get_attribute(assertion, "uid")
7+
{idp_id, attributes, computed} = if assertion do
8+
{assertion.idp_id, assertion.attributes, assertion.computed}
99
else
10-
{nil, nil}
10+
{nil, nil, nil}
1111
end
1212

1313
target_url = "/?a=value1&b=value two" |> URI.encode_www_form()
14-
render conn, "index.html",
15-
uid: uid, attributes: attributes, computed: computed, target_url: target_url
14+
{metadata_uri, signin_uri, signout_uri} = get_samly_uris()
15+
render conn, "index.html", [
16+
idp_id: idp_id,
17+
uid: uid,
18+
attributes: attributes,
19+
computed: computed,
20+
target_url: target_url,
21+
metadata_uri: metadata_uri,
22+
signin_uri: signin_uri,
23+
signout_uri: signout_uri
24+
]
25+
end
26+
27+
defp get_samly_uris() do
28+
opts = Application.get_env(:samly, Samly.Provider, [])
29+
if opts[:idp_id_from] == :subdomain do
30+
{
31+
"/sso/sp/metadata",
32+
"/sso/auth/signin",
33+
"/sso/auth/signout"
34+
}
35+
else
36+
{
37+
"/sso/sp/metadata/idp1",
38+
"/sso/auth/signin/idp1",
39+
"/sso/auth/signout/idp1"
40+
}
41+
end
1642
end
1743
end

lib/samly_howto_web/plugs/samly_pipeline.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ defmodule SamlyHowtoWeb.Plugs.SamlyPipeline do
88
def compute_attributes(conn, _opts) do
99
assertion = conn.private[:samly_assertion]
1010

11-
first_name = Map.get(assertion.attributes, :first_name)
12-
last_name = Map.get(assertion.attributes, :last_name)
11+
first_name = Map.get(assertion.attributes, "first_name")
12+
last_name = Map.get(assertion.attributes, "last_name")
1313

14-
computed = %{full_name: "#{first_name} #{last_name}"}
14+
computed = %{"full_name" => "#{first_name} #{last_name}"}
1515

1616
assertion = %Assertion{assertion | computed: computed}
1717

lib/samly_howto_web/templates/page/index.html.eex

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,58 @@
77
</a>
88

99
<%= if @uid do %>
10-
<p class="nav navbar-text">Signed in as <%= @uid %></p>
10+
<p class="nav navbar-text"><kbd>Signed in as <%= @uid %></kbd></p>
1111
<% end %>
12+
1213
<div class="nav navbar-nav navbar-right" style="padding: 0 15px;">
13-
<a href="/sso/sp/metadata"
14+
<!--
15+
<a href="/sso/sp/metadata/idp1"
16+
class="btn btn-sm btn-default navbar-btn"
17+
role="button">
18+
SAML SP Metadata
19+
</a>
20+
-->
21+
22+
<!-- uncomment this section for: idp_id_from: :subdomain -->
23+
<a href="<%= @metadata_uri %>"
1424
class="btn btn-sm btn-default navbar-btn"
1525
role="button">
1626
SAML SP Metadata
17-
</a>
27+
</a>
28+
<!-- -->
1829

1930
<%= if @uid do %>
20-
<a href="/sso/auth/signout?target_url=<%= @target_url %>"
31+
<!--
32+
<a href="/sso/auth/signout/idp1?target_url=<%= @target_url %>"
33+
class="btn btn-sm btn-primary navbar-btn"
34+
role="button">
35+
Sign out
36+
</a>
37+
-->
38+
39+
<!-- uncomment this section for: idp_id_from: :subdomain -->
40+
<a href="<%= @signout_uri %>?target_url=<%= @target_url %>"
2141
class="btn btn-sm btn-primary navbar-btn"
2242
role="button">
2343
Sign out
2444
</a>
45+
<!-- -->
2546
<% else %>
26-
<a href="/sso/auth/signin?target_url=<%= @target_url %>"
47+
<!--
48+
<a href="/sso/auth/signin/idp1?target_url=<%= @target_url %>"
49+
class="btn btn-sm btn-primary navbar-btn"
50+
role="button">
51+
Sign in
52+
</a>
53+
-->
54+
55+
<!-- uncomment this section for: idp_id_from: :subdomain -->
56+
<a href="<%= @signin_uri %>?target_url=<%= @target_url %>"
2757
class="btn btn-sm btn-primary navbar-btn"
2858
role="button">
2959
Sign in
30-
</a>
60+
</a>
61+
<!-- -->
3162
<% end %>
3263
</div>
3364
</div>
@@ -37,7 +68,7 @@
3768
<div class="container-fluid">
3869
<table class="table table-striped">
3970
<thead>
40-
<tr><th>Attributes from IdP Sent SAML Assertion</th></tr>
71+
<tr><th>Attributes in IdP Sent SAML Assertion</th></tr>
4172
<tr><th>Attribute</th><th>Value</th></tr>
4273
</thead>
4374
<tbody>

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule SamlyHowto.Mixfile do
44
def project do
55
[
66
app: :samly_howto,
7-
version: "0.5.0",
7+
version: "0.6.0",
88
elixir: "~> 1.4",
99
elixirc_paths: elixirc_paths(Mix.env),
1010
compilers: [:phoenix, :gettext] ++ Mix.compilers,
@@ -38,7 +38,7 @@ defmodule SamlyHowto.Mixfile do
3838
{:phoenix_live_reload, "~> 1.0", only: :dev},
3939
{:gettext, "~> 0.11"},
4040
{:cowboy, "~> 1.0"},
41-
{:samly, "~> 0.7"},
41+
{:samly, "~> 0.8"},
4242
]
4343
end
4444
end

mix.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
%{"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
22
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], [], "hexpm"},
33
"esaml": {:hex, :esaml, "3.1.0", "76337f00b5953a6c249fa8c322905c7a069b7c20339ece3756072279e6dcb41c", [], [{:cowboy, "1.1.2", [hex: :cowboy, repo: "hexpm", optional: false]}], "hexpm"},
4-
"file_system": {:hex, :file_system, "0.2.1", "c4bec8f187d2aabace4beb890f0d4e468f65ca051593db768e533a274d0df587", [], [], "hexpm"},
4+
"file_system": {:hex, :file_system, "0.2.2", "7f1e9de4746f4eb8a4ca8f2fbab582d84a4e40fa394cce7bfcb068b988625b06", [], [], "hexpm"},
55
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [], [], "hexpm"},
66
"mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [], [], "hexpm"},
77
"phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
88
"phoenix_html": {:hex, :phoenix_html, "2.10.4", "d4f99c32d5dc4918b531fdf163e1fd7cf20acdd7703f16f5d02d4db36de803b7", [], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
9-
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.1.2", "dfd31cc1eb091533b4419bbdb67bec9767bb26c9fe09602e6cca313fab5302d0", [], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2 or ~> 1.3", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"},
9+
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.1.3", "1d178429fc8950b12457d09c6afec247bfe1fcb6f36209e18fbb0221bdfe4d41", [], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2 or ~> 1.3", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"},
1010
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [], [], "hexpm"},
1111
"plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
1212
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [], [], "hexpm"},
1313
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [], [], "hexpm"},
14-
"samly": {:hex, :samly, "0.7.2", "7048c1a12c960b563bd0d9be6ecc4196cdcb2b6069c533218546ef80b7077e26", [], [{:esaml, "~> 3.1", [hex: :esaml, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}}
14+
"samly": {:hex, :samly, "0.8.0", "c690b454b1d1e1d583961807824a84ff5a49b2c25ae5eb80e21dc31af5d335b7", [], [{:esaml, "~> 3.1", [hex: :esaml, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}}

0 commit comments

Comments
 (0)