Skip to content

Commit

Permalink
add cas auth provider strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
chownces committed Oct 29, 2022
1 parent 03f657c commit 2e20c1c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/cadet/auth/providers/cas.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Cadet.Auth.Providers.CAS do
@moduledoc """
Provides identity using CAS Protocol.
https://apereo.github.io/cas/6.5.x/protocol/CAS-Protocol.html
"""

alias Cadet.Auth.Provider

@behaviour Provider

@type config :: %{service_validate_endpoint: String.t(), modules: %{}}

@spec authorise(config(), Provider.code(), Provider.client_id(), Provider.redirect_uri()) ::
{:ok, %{token: Provider.token(), username: String.t()}}
| {:error, Provider.error(), String.t()}
def authorise(config, code, _client_id, redirect_uri) do
params = %{
ticket: code,
service: redirect_uri
}

with {:validate, {:ok, %{body: body, status_code: 200}}} <-
{:validate, HTTPoison.get(config.service_validate_endpoint, [], params: params)},
{:validation_response, data} <- {:validation_response, Jason.decode!(body)},
{:extract_username, %{"name" => username}} <- {:extract_username, data} do
IO.inspect(data)
{:ok, %{token: data, username: username}}
else
{:validate, {:ok, %{body: body, status_code: status}}} ->
{:error, :upstream, "Status code #{status} from CAS: #{body}"}
end
end

@spec get_name(config(), Provider.token()) ::
{:ok, String.t()} | {:error, Provider.error(), String.t()}
def get_name(_config, token) do
%{"name" => name} = token
{:ok, name}
rescue
_ ->
{:error, :invalid_credentials, "Failed to retrieve user's name"}
end
end

0 comments on commit 2e20c1c

Please sign in to comment.