-
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.
- Loading branch information
Showing
6 changed files
with
143 additions
and
61 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
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,48 @@ | ||
defmodule Cadet.Auth.Providers.SAML do | ||
@moduledoc """ | ||
Provides identity using SAML. | ||
""" | ||
alias Cadet.Auth.Provider | ||
alias Samly.Assertion | ||
|
||
@behaviour Provider | ||
|
||
@type config :: %{assertion_extractor: module()} | ||
|
||
@spec authorise( | ||
any(), | ||
Provider.code() | Plug.Conn.t(), | ||
Provider.client_id(), | ||
Provider.redirect_uri() | ||
) :: | ||
{:ok, %{token: Provider.token(), username: String.t()}} | ||
| {:error, Provider.error(), String.t()} | ||
def authorise(config, conn, _client_id, _redirect_uri) do | ||
%{assertion_extractor: assertion_extractor} = config | ||
|
||
with {:assertion, assertion} when not is_nil(assertion) <- | ||
{:assertion, Samly.get_attribute(conn)} do | ||
{:ok, | ||
%{ | ||
token: Jason.encode!(%{name: assertion_extractor.get_name(assertion)}), | ||
username: assertion_extractor.get_username(assertion) | ||
}} | ||
else | ||
{:assertion, nil} -> {:error, :invalid_credentials, "Missing SAML assertion!"} | ||
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 |
This file was deleted.
Oops, something went wrong.