Skip to content

Commit 49a7e96

Browse files
committed
Multi-valued attributes - fixes #43
1 parent 21de16c commit 49a7e96

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

lib/samly.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Samly do
2222
# When there is an authenticated SAML assertion
2323
%Assertion{} = Samly.get_active_assertion()
2424
"""
25-
@spec get_active_assertion(Conn.t()) :: Assertion.t() | nil
25+
@spec get_active_assertion(Conn.t()) :: nil | Assertion.t()
2626
def get_active_assertion(conn) do
2727
case Conn.get_session(conn, "samly_assertion_key") do
2828
{_idp_id, _nameid} = assertion_key ->
@@ -37,7 +37,8 @@ defmodule Samly do
3737
Returns value of the specified attribute name in the given SAML Assertion.
3838
3939
Checks for the attribute in `computed` map first and `attributes` map next.
40-
Returns `nil` if attribute is not present.
40+
Returns a UTF-8 binary or a list of UTF-8 binaries (in case of multi-valued)
41+
if the given attribute is present. Returns `nil` if attribute is not present.
4142
4243
## Parameters
4344
@@ -47,14 +48,15 @@ defmodule Samly do
4748
## Examples
4849
4950
assertion = Samly.get_active_assertion()
51+
# returns a list if the attribute is multi-valued
52+
roles = Samly.get_attribute(assertion, "roles")
5053
computed_fullname = Samly.get_attribute(assertion, "fullname")
5154
"""
52-
@spec get_attribute(nil | Assertion.t(), String.t()) :: nil | String.t()
55+
@spec get_attribute(nil | Assertion.t(), Assertion.attr_name_t()) ::
56+
nil | Assertion.attr_value_t()
5357
def get_attribute(nil, _name), do: nil
5458

5559
def get_attribute(%Assertion{} = assertion, name) do
56-
computed = assertion.computed
57-
attributes = assertion.attributes
58-
Map.get(computed, name) || Map.get(attributes, name)
60+
Map.get(assertion.computed, name) || Map.get(assertion.attributes, name)
5961
end
6062
end

lib/samly/assertion.ex

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ defmodule Samly.Assertion do
1515
require Samly.Esaml
1616
alias Samly.{Esaml, Subject}
1717

18+
@type attr_name_t :: String.t()
19+
@type attr_value_t :: String.t() | [String.t()]
20+
1821
defstruct version: "2.0",
1922
issue_instant: "",
2023
recipient: "",
@@ -33,9 +36,9 @@ defmodule Samly.Assertion do
3336
issuer: String.t(),
3437
subject: Subject.t(),
3538
conditions: map,
36-
attributes: map,
39+
attributes: %{required(attr_name_t()) => attr_value_t()},
3740
authn: map,
38-
computed: map,
41+
computed: %{required(attr_name_t()) => attr_value_t()},
3942
idp_id: String.t()
4043
}
4144

@@ -65,6 +68,17 @@ defmodule Samly.Assertion do
6568
end
6669

6770
defp stringize(proplist) do
68-
proplist |> Enum.map(fn {k, v} -> {to_string(k), List.to_string(v)} end) |> Enum.into(%{})
71+
proplist
72+
|> Enum.map(fn
73+
{k, []} ->
74+
{to_string(k), ""}
75+
76+
{k, values} when is_list(values) and is_list(hd(values)) ->
77+
{to_string(k), Enum.map(values, fn v -> List.to_string(v) end)}
78+
79+
{k, v} when is_list(v) ->
80+
{to_string(k), List.to_string(v)}
81+
end)
82+
|> Enum.into(%{})
6983
end
7084
end

0 commit comments

Comments
 (0)