6
6
use OneLogin_Saml2_Error ;
7
7
use OneLogin_Saml2_Utils ;
8
8
9
+ use Log ;
10
+ use Psr \Log \InvalidArgumentException ;
11
+
9
12
class Saml2Auth
10
13
{
11
14
12
15
/**
13
16
* @var \OneLogin_Saml2_Auth
14
17
*/
15
18
protected $ auth ;
16
- protected $ uid_key ;
19
+
20
+ protected $ samlAssertion ;
21
+
22
+ protected $ redirectUrl ; //not used right now. Handled in Laravel
23
+
17
24
18
25
function __construct ($ config )
19
26
{
20
- // session_start();
21
27
$ this ->auth = new OneLogin_Saml2_Auth ($ config );
22
- // $this->uid_key = $uid_key;
23
- // $this->id_key = $config[]
24
28
}
25
29
30
+ /**
31
+ * @return bool if a valid user was fetched from the saml assertion this request.
32
+ */
26
33
function isAuthenticated ()
27
34
{
28
- return isset ($ _SESSION ['samlUserdata ' ]);
29
- }
35
+ $ auth = $ this ->auth ;
30
36
31
- function getUserId ()
32
- {
33
- $ attributes = $ this ->getAttributes ();
34
- return $ attributes [$ this ->uid_key ][0 ];
37
+ return $ auth ->isAuthenticated ();
35
38
}
36
39
37
- function getAttributes ()
38
- {
39
- $ attributes = $ _SESSION ['samlUserdata ' ];
40
- return $ attributes ;
40
+ /**
41
+ * The user info from the assertion
42
+ * @return Saml2User
43
+ */
44
+ function getSaml2User (){
45
+
46
+ return new Saml2User ($ this ->auth );
41
47
}
42
48
43
- function getRawSamlAssertion ()
49
+ /**
50
+ * Initiate a saml2 login flow. It will redirect! Before calling this, check if user is
51
+ * authenticated (here in saml2). That would be true when the assertion was received this request.
52
+ */
53
+ function login ($ returnTo = null )
44
54
{
45
- return isset ($ _SESSION ['SAMLAssertion ' ]) ? $ _SESSION ['SAMLAssertion ' ] : null ;
55
+ $ auth = $ this ->auth ;
56
+
57
+ $ auth ->login ($ returnTo );
46
58
}
47
59
48
- function login ()
60
+ /**
61
+ * Initiate a saml2 logout flow. It will close session on all other SSO services. You should close
62
+ * local session if applicable.
63
+ */
64
+ function logout ()
49
65
{
50
- $ this ->auth ->login ();
66
+ $ auth = $ this ->auth ;
67
+
68
+ $ auth ->logout ();
51
69
}
52
70
71
+ /**
72
+ * Porcess a Saml response (assertion consumer service)
73
+ * @throws \Exception when errors are encountered. This sould not happen in a normal flow.
74
+ */
53
75
function acs ()
54
76
{
55
77
@@ -58,43 +80,49 @@ function acs()
58
80
59
81
$ auth ->processResponse ();
60
82
61
-
62
83
$ errors = $ auth ->getErrors ();
63
84
64
85
if (!empty ($ errors )) {
65
- print_r ( ' <p> ' . implode ( ' , ' , $ errors) . ' </p> ' );
66
- exit ( );
86
+ Log:: error ( " Invalid saml response " , $ errors );
87
+ throw new \ Exception ( " The saml assertion is not valid, please check the logs. " );
67
88
}
68
89
69
90
if (!$ auth ->isAuthenticated ()) {
70
- echo " <p>Not authenticated</p> " ;
71
- exit ( );
91
+ Log:: error ( " Could not authenticate with the saml response. Something happened " ) ;
92
+ throw new \ Exception ( " The saml assertion is not valid, please check the logs. " );
72
93
}
73
94
74
- $ _SESSION ['samlUserdata ' ] = $ auth ->getAttributes ();
75
-
76
- $ _SESSION ['SAMLAssertion ' ] = $ _POST ['SAMLResponse ' ]; //se lo robo al saml
77
95
78
96
if (isset ($ _POST ['RelayState ' ]) && OneLogin_Saml2_Utils::getSelfURL () != $ _POST ['RelayState ' ]) {
79
- $ auth -> redirectTo ( $ _POST ['RelayState ' ]) ;
97
+ $ this -> redirectUrl = $ _POST ['RelayState ' ];
80
98
}
81
99
}
82
100
101
+ /**
102
+ * Porcess a Saml response (assertion consumer service)
103
+ * @throws \Exception
104
+ */
83
105
function sls ()
84
106
{
85
107
$ auth = $ this ->auth ;
86
108
87
- $ auth ->processSLO ();
109
+ $ keep_local_session = true ; //we don't touch session here
110
+ $ auth ->processSLO ($ keep_local_session );
88
111
89
112
$ errors = $ auth ->getErrors ();
90
113
91
- if (empty ($ errors )) {
92
- print_r ('Sucessfully logged out ' );
93
- } else {
94
- print_r (implode (', ' , $ errors ));
114
+ if (!empty ($ errors )) {
115
+ Log::error ("Could not log out " , $ errors );
116
+ throw new \Exception ("Could not log out " );
95
117
}
118
+
96
119
}
97
120
121
+ /**
122
+ * Show metadata about the local sp. Use this to configure your saml2 IDP
123
+ * @return mixed xml string representing metadata
124
+ * @throws \InvalidArgumentException if metadata is not correctly set
125
+ */
98
126
function getMetadata ()
99
127
{
100
128
$ auth = $ this ->auth ;
@@ -105,11 +133,10 @@ function getMetadata()
105
133
106
134
if (empty ($ errors )) {
107
135
return $ metadata ;
108
- // header('Content-Type: text/xml');
109
- // echo $metadata;
136
+
110
137
} else {
111
138
112
- throw new OneLogin_Saml2_Error (
139
+ throw new InvalidArgumentException (
113
140
'Invalid SP metadata: ' . implode (', ' , $ errors ),
114
141
OneLogin_Saml2_Error::METADATA_SP_INVALID
115
142
);
0 commit comments