@@ -4,6 +4,8 @@ defmodule Cadet.Auth.Providers.CAS do
4
4
https://apereo.github.io/cas/6.5.x/protocol/CAS-Protocol.html
5
5
"""
6
6
7
+ import SweetXml
8
+
7
9
alias Cadet.Auth.Provider
8
10
9
11
@ behaviour Provider
@@ -20,24 +22,37 @@ defmodule Cadet.Auth.Providers.CAS do
20
22
}
21
23
22
24
with { :validate , { :ok , % { body: body , status_code: 200 } } } <-
23
- { :validate , HTTPoison . get ( config . service_validate_endpoint , [ ] , params: params ) } do
24
- # {:validation_response, data} <- {:validation_response, Jason.decode!(body)},
25
- # {:extract_username, %{"name" => username}} <- {:extract_username, data} do
26
- IO . inspect ( body )
27
- { :ok , % { token: body , username: "placeholder" } }
25
+ { :validate , HTTPoison . get ( config . service_validate_endpoint , [ ] , params: params ) } ,
26
+ { :authentication_success , success_xml } when not is_nil ( success_xml ) <-
27
+ { :authentication_success , authentication_success ( body ) } ,
28
+ { :extract_username , username } <- { :extract_username , get_username ( success_xml ) } do
29
+ { :ok , % { token: success_xml , username: username } }
28
30
else
29
31
{ :validate , { :ok , % { body: body , status_code: status } } } ->
30
32
{ :error , :upstream , "Status code #{ status } from CAS: #{ body } " }
33
+
34
+ { :authentication_success , nil } ->
35
+ { :error , :upstream , "Authentication failure from CAS" }
31
36
end
32
37
end
33
38
34
39
@ spec get_name ( config ( ) , Provider . token ( ) ) ::
35
40
{ :ok , String . t ( ) } | { :error , Provider . error ( ) , String . t ( ) }
36
41
def get_name ( _config , token ) do
37
- % { " name" => name } = token
42
+ name = get_username ( token )
38
43
{ :ok , name }
39
44
rescue
40
45
_ ->
41
46
{ :error , :invalid_credentials , "Failed to retrieve user's name" }
42
47
end
48
+
49
+ defp authentication_success ( xml ) do
50
+ xml
51
+ |> xpath ( ~x" //cas:serviceResponse/cas:authenticationSuccess" e )
52
+ end
53
+
54
+ defp get_username ( xml ) do
55
+ xml
56
+ |> xpath ( ~x" //cas:user/text()" s )
57
+ end
43
58
end
0 commit comments