1010use SimpleSAML \Module ;
1111use SimpleSAML \Utils ;
1212use Symfony \Component \HttpFoundation \Request ;
13+ use Symfony \Component \HttpFoundation \Response ;
1314use Symfony \Component \HttpFoundation \Session \Session as SymfonySession ;
1415
1516/**
@@ -66,12 +67,12 @@ private function getUser(): ?array
6667 * stored in the users PHP session, but this could be replaced
6768 * with anything.
6869 */
69- $ session = new SymfonySession ();
70- if (! $ session -> getId ()) {
71- $ session -> start ();
70+ if (! session_id ()) {
71+ // session_start not called before. Do it here
72+ @ session_start ();
7273 }
7374
74- if (!$ session -> has ( 'uid ' )) {
75+ if (!isset ( $ _SESSION [ 'uid ' ] )) {
7576 // The user isn't authenticated
7677 return null ;
7778 }
@@ -82,14 +83,14 @@ private function getUser(): ?array
8283 * to store them as arrays.
8384 */
8485 $ attributes = [
85- 'uid ' => [$ session -> get ( 'uid ' ) ],
86- 'displayName ' => [$ session -> get ( 'name ' ) ],
87- 'mail ' => [$ session -> get ( 'mail ' ) ],
86+ 'uid ' => [$ _SESSION [ 'uid ' ] ],
87+ 'displayName ' => [$ _SESSION [ 'name ' ] ],
88+ 'mail ' => [$ _SESSION [ 'mail ' ] ],
8889 ];
8990
9091 // Here we generate a multivalued attribute based on the account type
9192 $ attributes ['eduPersonAffiliation ' ] = [
92- $ session -> get ( 'type ' ) , /* In this example, either 'student' or 'employee'. */
93+ $ _SESSION [ 'type ' ] , /* In this example, either 'student' or 'employee'. */
9394 'member ' ,
9495 ];
9596
@@ -100,9 +101,11 @@ private function getUser(): ?array
100101 /**
101102 * Log in using an external authentication helper.
102103 *
104+ * @param \Symfony\Component\HttpFoundation\Request The current request
103105 * @param array &$state Information about the current authentication.
106+ * @return \Symfony\Component\HttpFoundation\Response
104107 */
105- public function authenticate (array &$ state ): void
108+ public function authenticate (Request $ request , array &$ state ): ? Response
106109 {
107110 $ attributes = $ this ->getUser ();
108111 if ($ attributes !== null ) {
@@ -113,7 +116,7 @@ public function authenticate(array &$state): void
113116 * to the authentication process.
114117 */
115118 $ state ['Attributes ' ] = $ attributes ;
116- return ;
119+ return null ;
117120 }
118121
119122 /*
@@ -148,7 +151,7 @@ public function authenticate(array &$state): void
148151 * option to return the user to a specific page afterwards.
149152 */
150153 $ returnTo = Module::getModuleURL ('exampleauth/resume ' , [
151- 'State ' => $ stateId ,
154+ 'AuthState ' => $ stateId ,
152155 ]);
153156
154157 /*
@@ -167,14 +170,9 @@ public function authenticate(array &$state): void
167170 * the real name of the parameter for the login page.
168171 */
169172 $ httpUtils = new Utils \HTTP ();
170- $ httpUtils ->redirectTrustedURL ($ authPage , [
173+ return $ httpUtils ->redirectTrustedURL ($ authPage , [
171174 'ReturnTo ' => $ returnTo ,
172175 ]);
173-
174- /*
175- * The redirect function never returns, so we never get this far.
176- */
177- Assert::true (false );
178176 }
179177
180178
@@ -185,25 +183,27 @@ public function authenticate(array &$state): void
185183 * entered his or her credentials.
186184 *
187185 * @param \Symfony\Component\HttpFoundation\Request $request
186+ * @param \SimpleSAML\Auth\State $authState
187+ * @return \Symfony\Component\HttpFoundation\Response
188188 *
189189 * @throws \SimpleSAML\Error\BadRequest
190190 * @throws \SimpleSAML\Error\Exception
191191 */
192- public static function resume (Request $ request ): void
192+ public static function resume (Request $ request, Auth \ State $ authState ): Response
193193 {
194194 /*
195195 * First we need to restore the $state-array. We should have the identifier for
196196 * it in the 'State' request parameter.
197197 */
198- if (!$ request ->query ->has ('State ' )) {
198+ if (!$ request ->query ->has ('AuthState ' )) {
199199 throw new Error \BadRequest ('Missing "State" parameter. ' );
200200 }
201201
202202 /*
203203 * Once again, note the second parameter to the loadState function. This must
204204 * match the string we used in the saveState-call above.
205205 */
206- $ state = Auth \State ::loadState ($ request ->query ->get ('State ' ), 'exampleauth:External ' );
206+ $ state = $ authState ::loadState ($ request ->query ->get ('AuthState ' ), 'exampleauth:External ' );
207207
208208 /*
209209 * Now we have the $state-array, and can use it to locate the authentication
@@ -249,12 +249,7 @@ public static function resume(Request $request): void
249249 */
250250
251251 $ state ['Attributes ' ] = $ attributes ;
252- Auth \Source::completeAuth ($ state );
253-
254- /*
255- * The completeAuth-function never returns, so we never get this far.
256- */
257- Assert::true (false );
252+ return parent ::completeAuth ($ state );
258253 }
259254
260255
@@ -264,18 +259,22 @@ public static function resume(Request $request): void
264259 *
265260 * @param array &$state The logout state array.
266261 */
267- public function logout (array &$ state ): void
262+ public function logout (array &$ state ): null
268263 {
269- $ session = new SymfonySession ();
270- if (! $ session -> getId ()) {
271- $ session -> start ();
264+ if (! session_id ()) {
265+ // session_start not called before. Do it here
266+ @ session_start ();
272267 }
273268
274- $ session ->clear ();
269+ /**
270+ * In this example we simply remove the 'uid' from the session
271+ */
272+ unset($ _SESSION ['uid ' ]);
275273
276- /*
274+ /**
277275 * If we need to do a redirect to a different page, we could do this
278276 * here, but in this example we don't need to do this.
279277 */
278+ return null ;
280279 }
281280}
0 commit comments