Skip to content

Commit

Permalink
formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
handnot2 committed Nov 5, 2017
1 parent 6cdbce0 commit 423ecb5
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 391 deletions.
11 changes: 11 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
inputs: [
"mix.exs",
"{config,lib,test}/**/*.{ex,exs}"
],

locals_without_parens: [
plug: 1,
plug: 2
]
]
6 changes: 4 additions & 2 deletions lib/samly.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ defmodule Samly do
- conn: Plug connection
"""
@spec get_active_assertion(Conn.t) :: Assertion.t
@spec get_active_assertion(Conn.t()) :: Assertion.t()
def get_active_assertion(conn) do
nameid = conn |> Conn.get_session("samly_nameid")

case State.get_by_nameid(nameid) do
{^nameid, saml_assertion} -> saml_assertion
_ -> nil
Expand All @@ -37,8 +38,9 @@ defmodule Samly do
- assertion: SAML assertion obtained by calling `get_active_assertion/1`
- name: Attribute name
"""
@spec get_attribute(nil | Assertion.t, String.t) :: nil | String.t
@spec get_attribute(nil | Assertion.t(), String.t()) :: nil | String.t()
def get_attribute(nil, _name), do: nil

def get_attribute(%Assertion{} = assertion, name) do
computed = assertion.computed
attributes = assertion.attributes
Expand Down
47 changes: 22 additions & 25 deletions lib/samly/assertion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,31 @@ defmodule Samly.Assertion do
"""

require Samly.Esaml
alias Samly.Esaml
alias Samly.Subject
alias Samly.{Esaml, Subject}

defstruct [
version: "2.0",
issue_instant: "",
recipient: "",
issuer: "",
subject: %Subject{},
conditions: %{},
attributes: %{},
authn: %{},
computed: %{},
idp_id: ""
]
defstruct version: "2.0",
issue_instant: "",
recipient: "",
issuer: "",
subject: %Subject{},
conditions: %{},
attributes: %{},
authn: %{},
computed: %{},
idp_id: ""

@type t :: %__MODULE__{
version: String.t,
issue_instant: String.t,
recipient: String.t,
issuer: String.t,
subject: Subject.t,
conditions: map,
attributes: map,
authn: map,
computed: map,
idp_id: String.t
}
version: String.t(),
issue_instant: String.t(),
recipient: String.t(),
issuer: String.t(),
subject: Subject.t(),
conditions: map,
attributes: map,
authn: map,
computed: map,
idp_id: String.t()
}

@doc false
def from_rec(assertion_rec) do
Expand Down
99 changes: 54 additions & 45 deletions lib/samly/auth_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ defmodule Samly.AuthHandler do
"""

def valid_referer?(conn) do
referer = case conn |> get_req_header("referer") do
[uri] -> URI.parse(uri)
_ -> %URI{}
end
referer =
case conn |> get_req_header("referer") do
[uri] -> URI.parse(uri)
_ -> %URI{}
end

[request_authority] = conn |> get_req_header("host")
request_authority == referer.authority && referer.scheme == Atom.to_string(conn.scheme)
Expand All @@ -38,68 +39,71 @@ defmodule Samly.AuthHandler do
def initiate_sso_req(conn) do
import Plug.CSRFProtection, only: [get_csrf_token: 0]

with true <- valid_referer?(conn),
target_url = conn.params["target_url"],
target_url = (if target_url, do: URI.decode_www_form(target_url), else: nil)
do
with true <- valid_referer?(conn), target_url = conn.params["target_url"] do
target_url = if target_url, do: URI.decode_www_form(target_url), else: nil

opts = [
action: conn.request_path,
target_url: (if target_url, do: URI.encode_www_form(target_url), else: nil),
target_url: target_url,
csrf_token: get_csrf_token()
]

conn
|> put_resp_header("Content-Type", "text/html")
|> send_resp(200, EEx.eval_string(@sso_init_resp_template, opts))
|> put_resp_header("Content-Type", "text/html")
|> send_resp(200, EEx.eval_string(@sso_init_resp_template, opts))
else
_ -> conn |> send_resp(403, "invalid_request")
end
# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")

# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")
end

def send_signin_req(conn) do
%IdpData{id: idp_id} = idp = conn.private[:samly_idp]
%IdpData{esaml_idp_rec: idp_rec, esaml_sp_rec: sp_rec} = idp
sp = ensure_sp_uris_set(sp_rec, conn)

target_url = conn.params["target_url"] || "/"
|> URI.decode_www_form()

target_url = (conn.params["target_url"] || "/") |> URI.decode_www_form()
nameid = get_session(conn, "samly_nameid")

case State.get_by_nameid(nameid) do
{^nameid, %Assertion{idp_id: ^idp_id}} ->
conn
|> redirect(302, target_url)
conn |> redirect(302, target_url)

_ ->
relay_state = State.gen_id()
{idp_signin_url, req_xml_frag} = Helper.gen_idp_signin_req(sp, idp_rec)

conn
|> configure_session(renew: true)
|> put_session("relay_state", relay_state)
|> put_session("idp_id", idp_id)
|> put_session("target_url", target_url)
|> send_saml_request(idp_signin_url, idp.use_redirect_for_req,
req_xml_frag, relay_state |> URI.encode_www_form())
|> configure_session(renew: true)
|> put_session("relay_state", relay_state)
|> put_session("idp_id", idp_id)
|> put_session("target_url", target_url)
|> send_saml_request(
idp_signin_url,
idp.use_redirect_for_req,
req_xml_frag,
relay_state |> URI.encode_www_form()
)
end
# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")

# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")
end

def send_signout_req(conn) do
%IdpData{id: idp_id} = idp = conn.private[:samly_idp]
%IdpData{esaml_idp_rec: idp_rec, esaml_sp_rec: sp_rec} = idp
sp = ensure_sp_uris_set(sp_rec, conn)

target_url = conn.params["target_url"] || "/"
|> URI.decode_www_form()

target_url = (conn.params["target_url"] || "/") |> URI.decode_www_form()
nameid = get_session(conn, "samly_nameid")

case State.get_by_nameid(nameid) do
{^nameid, %Assertion{idp_id: ^idp_id}} ->
{idp_signout_url, req_xml_frag} = Helper.gen_idp_signout_req(sp, idp_rec, nameid)
Expand All @@ -108,19 +112,24 @@ defmodule Samly.AuthHandler do
relay_state = State.gen_id()

conn
|> put_session("target_url", target_url)
|> put_session("relay_state", relay_state)
|> put_session("idp_id", idp_id)
|> delete_session("samly_nameid")
|> send_saml_request(idp_signout_url, idp.use_redirect_for_req,
req_xml_frag, relay_state |> URI.encode_www_form())
|> put_session("target_url", target_url)
|> put_session("relay_state", relay_state)
|> put_session("idp_id", idp_id)
|> delete_session("samly_nameid")
|> send_saml_request(
idp_signout_url,
idp.use_redirect_for_req,
req_xml_frag,
relay_state |> URI.encode_www_form()
)

_ ->
conn
|> send_resp(403, "access_denied")
conn |> send_resp(403, "access_denied")
end
# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")

# rescue
# error ->
# Logger.error("#{inspect error}")
# conn |> send_resp(500, "request_failed")
end
end
12 changes: 4 additions & 8 deletions lib/samly/auth_router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@ defmodule Samly.AuthRouter do
plug :dispatch

get "/signin/*idp_id_seg" do
conn
|> Samly.AuthHandler.initiate_sso_req()
conn |> Samly.AuthHandler.initiate_sso_req()
end

post "/signin/*idp_id_seg" do
conn
|> Samly.AuthHandler.send_signin_req()
conn |> Samly.AuthHandler.send_signin_req()
end

get "/signout/*idp_id_seg" do
conn
|> Samly.AuthHandler.initiate_sso_req()
conn |> Samly.AuthHandler.initiate_sso_req()
end

post "/signout/*idp_id_seg" do
conn
|> Samly.AuthHandler.send_signout_req()
conn |> Samly.AuthHandler.send_signout_req()
end

match _ do
Expand Down
4 changes: 2 additions & 2 deletions lib/samly/config_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule Samly.ConfigError do

defexception [:message]

@spec exception(map) :: Exception.t
@spec exception(map) :: Exception.t()
def exception(data) when is_map(data) do
%__MODULE__{message: "invalid_config: #{inspect data}"}
%__MODULE__{message: "invalid_config: #{inspect(data)}"}
end
end
33 changes: 11 additions & 22 deletions lib/samly/esaml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,15 @@ defmodule Samly.Esaml do
import Record, only: [defrecord: 2, extract: 2]

@esaml_hrl "esaml/include/esaml.hrl"
defrecord :esaml_org,
extract(:esaml_org, from_lib: @esaml_hrl)
defrecord :esaml_contact,
extract(:esaml_contact, from_lib: @esaml_hrl)
defrecord :esaml_sp_metadata,
extract(:esaml_sp_metadata, from_lib: @esaml_hrl)
defrecord :esaml_idp_metadata,
extract(:esaml_idp_metadata, from_lib: @esaml_hrl)
defrecord :esaml_authnreq,
extract(:esaml_authnreq, from_lib: @esaml_hrl)
defrecord :esaml_subject,
extract(:esaml_subject, from_lib: @esaml_hrl)
defrecord :esaml_assertion,
extract(:esaml_assertion, from_lib: @esaml_hrl)
defrecord :esaml_logoutreq,
extract(:esaml_logoutreq, from_lib: @esaml_hrl)
defrecord :esaml_logoutresp,
extract(:esaml_logoutresp, from_lib: @esaml_hrl)
defrecord :esaml_response,
extract(:esaml_response, from_lib: @esaml_hrl)
defrecord :esaml_sp,
extract(:esaml_sp, from_lib: @esaml_hrl)
defrecord :esaml_org, extract(:esaml_org, from_lib: @esaml_hrl)
defrecord :esaml_contact, extract(:esaml_contact, from_lib: @esaml_hrl)
defrecord :esaml_sp_metadata, extract(:esaml_sp_metadata, from_lib: @esaml_hrl)
defrecord :esaml_idp_metadata, extract(:esaml_idp_metadata, from_lib: @esaml_hrl)
defrecord :esaml_authnreq, extract(:esaml_authnreq, from_lib: @esaml_hrl)
defrecord :esaml_subject, extract(:esaml_subject, from_lib: @esaml_hrl)
defrecord :esaml_assertion, extract(:esaml_assertion, from_lib: @esaml_hrl)
defrecord :esaml_logoutreq, extract(:esaml_logoutreq, from_lib: @esaml_hrl)
defrecord :esaml_logoutresp, extract(:esaml_logoutresp, from_lib: @esaml_hrl)
defrecord :esaml_response, extract(:esaml_response, from_lib: @esaml_hrl)
defrecord :esaml_sp, extract(:esaml_sp, from_lib: @esaml_hrl)
end
Loading

0 comments on commit 423ecb5

Please sign in to comment.