11<?php
22
3- use web_eid \web_eid_authtoken_validation_php \authtoken \WebEidAuthToken ;
3+ /*
4+ * Copyright (c) 2025-2025 Estonian Information System Authority
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
425use web_eid \web_eid_authtoken_validation_php \challenge \ChallengeNonceStore ;
526
627final class MobileAuth
@@ -20,20 +41,25 @@ public function init(): void
2041 $ payload = [
2142 "challenge " => $ challenge ->getBase64EncodedNonce (),
2243 "login_uri " => $ this ->ctx ->originUrl () . "/auth/mobile/login " ,
23- "get_signing_certificate " => true
44+ "get_signing_certificate " => $ this -> ctx -> mobileRequestSigningCert ()
2445 ];
2546
26- $ authUri =
27- $ this ->ctx ->mobileBaseUrl () .
28- "//auth# " .
29- base64_encode (json_encode ($ payload ));
47+ $ baseUrl = $ this ->ctx ->mobileBaseUrl ();
48+ $ encodedPayload = base64_encode (json_encode ($ payload ));
49+
50+ if (str_starts_with ($ baseUrl , 'http ' )) {
51+ $ authUri = rtrim ($ baseUrl , '/ ' ) . '/auth# ' . $ encodedPayload ;
52+ } else {
53+ $ authUri = rtrim ($ baseUrl , '/ ' ) . '//auth# ' . $ encodedPayload ;
54+ }
3055
3156 echo json_encode (["auth_uri " => $ authUri ]);
3257 }
3358
3459 public function login (): void
3560 {
3661 $ this ->ctx ->assertCsrf ();
62+ $ this ->ctx ->assertJsonContentType ();
3763
3864 $ json = json_decode (file_get_contents ("php://input " ), true );
3965 if (!isset ($ json ["auth_token " ])) {
@@ -50,10 +76,16 @@ public function login(): void
5076 $ nonce ->getBase64EncodedNonce ()
5177 );
5278
79+ session_regenerate_id ();
80+
5381 echo json_encode (["redirect " => "/welcome " ]);
54- } catch (Exception $ e ) {
55- http_response_code (400 );
56- echo json_encode (["error " => "Validation failed " ]);
82+ } catch (Throwable $ e ) {
83+ error_log ("Authentication failed: " . $ e ->getMessage ());
84+
85+ unset($ _SESSION ["auth-user " ]);
86+
87+ http_response_code (401 );
88+ echo json_encode (["error " => "Authentication failed " ]);
5789 }
5890 }
5991}
0 commit comments