-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SAML SP metadata endpoints and flow (#990)
* Add samly dependency * Set up samly * Fix incorrect documentation * Make code param optional in auth controller * Create test SAML provider * Update example config to configure SAML IdP * Fallback to connection for code Hacky fallback because SAML does not require a code to be sent in the authentication flow. * Replace FIXME comment with TODO FIXME was causing `mix credo` to fail. * Fix typing * Test student saml config * Fix rebase conflict * Add assertion extractor * Restructure auth provider directory * Update SAML provider authorise * Add SAML redirect flow * Refactor providers with param map * Update tests * Update swagger * Restructure test providers folder * Fix dialyzer warnings * Add SAML provider tests * Add auth_controller tests for SAML redirect endpoint * Ran format * Fix sigil warning --------- Co-authored-by: En Rong <[email protected]>
- Loading branch information
1 parent
423a597
commit 060355c
Showing
33 changed files
with
478 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/cadet/auth/providers/saml/nusstf_assertion_extractor.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Cadet.Auth.Providers.NusstfAssertionExtractor do | ||
@moduledoc """ | ||
Extracts fields from NUS Staff IdP SAML assertions. | ||
""" | ||
|
||
@behaviour Cadet.Auth.Providers.AssertionExtractor | ||
|
||
def get_username(assertion) do | ||
Map.get(assertion.attributes, "SamAccountName") | ||
end | ||
|
||
def get_name(assertion) do | ||
Map.get(assertion.attributes, "DisplayName") | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/cadet/auth/providers/saml/nusstu_assertion_extractor.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Cadet.Auth.Providers.NusstuAssertionExtractor do | ||
@moduledoc """ | ||
Extracts fields from NUS Student IdP SAML assertions. | ||
""" | ||
|
||
@behaviour Cadet.Auth.Providers.AssertionExtractor | ||
|
||
def get_username(assertion) do | ||
Map.get(assertion.attributes, "samaccountname") | ||
end | ||
|
||
def get_name(assertion) do | ||
Map.get(assertion.attributes, "samaccountname") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Cadet.Auth.Providers.SAML do | ||
@moduledoc """ | ||
Provides identity using SAML. | ||
""" | ||
alias Cadet.Auth.Provider | ||
|
||
@behaviour Provider | ||
|
||
@type config :: %{assertion_extractor: module()} | ||
|
||
@spec authorise(config(), Provider.authorise_params()) :: | ||
{:ok, %{token: Provider.token(), username: String.t()}} | ||
| {:error, Provider.error(), String.t()} | ||
def authorise(config, %{ | ||
conn: conn | ||
}) do | ||
%{assertion_extractor: assertion_extractor} = config | ||
|
||
with {:assertion, assertion} when not is_nil(assertion) <- | ||
{:assertion, Samly.get_active_assertion(conn)}, | ||
{:name, name} when not is_nil(name) <- {:name, assertion_extractor.get_name(assertion)}, | ||
{:username, username} when not is_nil(username) <- | ||
{:username, assertion_extractor.get_username(assertion)} do | ||
{:ok, | ||
%{ | ||
token: Jason.encode!(%{name: name}), | ||
username: username | ||
}} | ||
else | ||
{:assertion, nil} -> {:error, :invalid_credentials, "Missing SAML assertion!"} | ||
{:name, nil} -> {:error, :invalid_credentials, "Missing name attribute!"} | ||
{:username, nil} -> {:error, :invalid_credentials, "Missing username attribute!"} | ||
end | ||
end | ||
|
||
@spec get_name(any(), Provider.token()) :: | ||
{:ok, String.t()} | {:error, Provider.error(), String.t()} | ||
def get_name(_config, token) do | ||
{:ok, Jason.decode!(token)["name"]} | ||
end | ||
end | ||
|
||
defmodule Cadet.Auth.Providers.AssertionExtractor do | ||
@moduledoc """ | ||
A behaviour for modules that extract fields from SAML assertions. | ||
""" | ||
@callback get_username(Samly.Assertion) :: String.t() | nil | ||
@callback get_name(Samly.Assertion) :: String.t() | nil | ||
end |
Oops, something went wrong.