Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/ex_saml/auth_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ defmodule ExSaml.AuthHandler do
%{
relay_state: relay_state,
session_id: session_id,
saml_nonce:
fetch_cookies(conn, encrypted: ~w(saml_nonce)).cookies["saml_nonce"] || UUID.uuid4(),
saml_nonce: resolve_saml_nonce(conn),
idp_id: idp_id,
user_token: get_session(conn, :user_token),
redirect_uri: get_session(conn, :redirect_uri),
Expand Down Expand Up @@ -99,8 +98,7 @@ defmodule ExSaml.AuthHandler do
%{
relay_state: relay_state,
session_id: session_id,
saml_nonce:
fetch_cookies(conn, encrypted: ~w(saml_nonce)).cookies["saml_nonce"] || UUID.uuid4(),
saml_nonce: resolve_saml_nonce(conn),
idp_id: idp_id,
target_url: target_url,
user_token: get_session(conn, :user_token),
Expand Down Expand Up @@ -180,4 +178,14 @@ defmodule ExSaml.AuthHandler do
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")
end

# Prefer a caller-assigned nonce so the SP can persist auxiliary state
# (e.g. redirect_uri) under the same key the IdP response will resolve to
# — `put_resp_cookie` does not populate `req_cookies`, so on the first
# round-trip the cookie fallback can't see what we just set.
defp resolve_saml_nonce(conn) do
conn.assigns[:saml_nonce] ||
fetch_cookies(conn, encrypted: ~w(saml_nonce)).cookies["saml_nonce"] ||
UUID.uuid4()
end
end
Loading