Skip to content

Commit 4a1cea8

Browse files
committed
Allow receiving raw XML metadadata with IDP config
1 parent 20c9887 commit 4a1cea8

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ config :samly, Samly.Provider,
190190
| `id` | _(mandatory)_ This will be the idp_id in the URLs |
191191
| `sp_id` | _(mandatory)_ The service provider definition to be used with this Identity Provider definition |
192192
| `base_url` | _(optional)_ If missing `Samly` will use the current URL to derive this. It is better to define this in production deployment. |
193-
| `metadata_file` | _(mandatory)_ Path to the IdP metadata XML file obtained from the Identity Provider. |
193+
| `metadata` | _(optional)_ IdP metadata XML obtained from the Identity Provider. |
194+
| `metadata_file` | _(optional)_ Path to the IdP metadata XML file obtained from the Identity Provider. Must be set if `metadata` is not. |
194195
| `pre_session_create_pipeline` | _(optional)_ Check the customization section. |
195196
| `use_redirect_for_req` | _(optional)_ Default is `false`. When this is `false`, `Samly` will POST to the IdP SAML endpoints. |
196197
| `sign_requests`, `sign_metadata` | _(optional)_ Default is `true`. |
@@ -280,7 +281,7 @@ config :samly, Samly.Provider,
280281
%{
281282
# ...
282283
pre_session_create_pipeline: MySamlyPipeline,
283-
# ...
284+
# ...
284285
}
285286
]
286287
```

lib/samly/idp_data.ex

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule Samly.IdpData do
1313
defstruct id: "",
1414
sp_id: "",
1515
base_url: nil,
16+
metadata: nil,
1617
metadata_file: nil,
1718
pre_session_create_pipeline: nil,
1819
use_redirect_for_req: false,
@@ -39,6 +40,7 @@ defmodule Samly.IdpData do
3940
id: binary(),
4041
sp_id: binary(),
4142
base_url: nil | binary(),
43+
metadata: nil | binary(),
4244
metadata_file: nil | binary(),
4345
pre_session_create_pipeline: nil | module(),
4446
use_redirect_for_req: boolean(),
@@ -106,7 +108,13 @@ defmodule Samly.IdpData do
106108
@spec save_idp_config(%IdpData{}, map()) :: %IdpData{}
107109
defp save_idp_config(idp_data, %{id: id, sp_id: sp_id} = opts_map)
108110
when is_binary(id) and is_binary(sp_id) do
109-
%IdpData{idp_data | id: id, sp_id: sp_id, base_url: Map.get(opts_map, :base_url)}
111+
%IdpData{
112+
idp_data
113+
| id: id,
114+
sp_id: sp_id,
115+
base_url: Map.get(opts_map, :base_url),
116+
metadata: Map.get(opts_map, :metadata)
117+
}
110118
|> set_metadata_file(opts_map)
111119
|> set_pipeline(opts_map)
112120
|> set_allowed_target_urls(opts_map)
@@ -119,21 +127,27 @@ defmodule Samly.IdpData do
119127
end
120128

121129
@spec load_metadata(%IdpData{}, map()) :: %IdpData{}
122-
defp load_metadata(idp_data, _opts_map) do
123-
with {:reading, {:ok, raw_xml}} <- {:reading, File.read(idp_data.metadata_file)},
124-
{:parsing, {:ok, idp_data}} <- {:parsing, from_xml(raw_xml, idp_data)} do
130+
defp load_metadata(%IdpData{metadata: raw_xml} = idp_data, _opts_map) when is_binary(raw_xml) do
131+
with {:parsing, {:ok, idp_data}} <- {:parsing, from_xml(raw_xml, idp_data)} do
125132
idp_data
126133
else
127-
{:reading, {:error, reason}} ->
128-
Logger.error("[Samly] Failed to read metadata_file: #{inspect(reason)}")
129-
idp_data
130-
131134
{:parsing, {:error, reason}} ->
132135
Logger.error("[Samly] Invalid metadata_file content: #{inspect(reason)}")
133136
idp_data
134137
end
135138
end
136139

140+
@spec load_metadata(%IdpData{}, map()) :: %IdpData{}
141+
defp load_metadata(idp_data, opts_map) do
142+
with {:reading, {:ok, raw_xml}} <- {:reading, File.read(idp_data.metadata_file)} do
143+
%IdpData{idp_data | metadata: raw_xml} |> load_metadata(opts_map)
144+
else
145+
{:reading, {:error, reason}} ->
146+
Logger.error("[Samly] Failed to read metadata_file: #{inspect(reason)}")
147+
idp_data
148+
end
149+
end
150+
137151
@spec update_esaml_recs(%IdpData{}, %{required(id()) => %SpData{}}, map()) :: %IdpData{}
138152
defp update_esaml_recs(idp_data, service_providers, opts_map) do
139153
case Map.get(service_providers, idp_data.sp_id) do
@@ -162,6 +176,12 @@ defmodule Samly.IdpData do
162176

163177
@default_metadata_file "idp_metadata.xml"
164178

179+
@spec set_metadata_file(%IdpData{}, map()) :: %IdpData{}
180+
defp set_metadata_file(%IdpData{metadata: metadata} = idp_data, _opts_map)
181+
when is_binary(metadata) do
182+
%IdpData{idp_data | metadata_file: nil}
183+
end
184+
165185
@spec set_metadata_file(%IdpData{}, map()) :: %IdpData{}
166186
defp set_metadata_file(%IdpData{} = idp_data, %{} = opts_map) do
167187
%IdpData{idp_data | metadata_file: Map.get(opts_map, :metadata_file, @default_metadata_file)}

0 commit comments

Comments
 (0)