Repository navigation
Add guide for adding Web eID for Mobile support to an existing integration #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aarmam
wants to merge
5
commits into
web-eid-mobile
Choose a base branch
from
NFC-183
base: web-eid-mobile
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d7ff34
Add guide for adding Web eID for Mobile support to an existing integr…
aarmam ffe9b9d
Fix CSRF validation for mobile authentication initialization
aarmam e97dc40
Fix base URL
aarmam 83d1d95
Update README MobileAuth example to use assertCsrf()
aarmam 15ad796
Post raw authentication token in mobile login like desktop login
aarmam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,9 +34,7 @@ public function init(): void | |
| { | ||
| header("Content-Type: application/json; charset=utf-8"); | ||
|
|
||
| if (!isset($_SESSION["csrf-token"])) { | ||
| $_SESSION["csrf-token"] = bin2hex(random_bytes(32)); | ||
| } | ||
| $this->ctx->assertCsrf(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. README MobileAuth code example still contains the old |
||
|
|
||
| $challenge = $this->ctx->nonceGenerator()->generateAndStoreNonce(); | ||
|
|
||
|
|
@@ -60,18 +58,13 @@ public function login(): void | |
| $this->ctx->assertCsrf(); | ||
| $this->ctx->assertJsonContentType(); | ||
|
|
||
| $json = json_decode(file_get_contents("php://input"), true); | ||
| if (!isset($json["authToken"])) { | ||
| http_response_code(400); | ||
| echo json_encode(["error" => "Missing authToken"]); | ||
| return; | ||
| } | ||
| $authToken = file_get_contents("php://input"); | ||
|
|
||
| try { | ||
| $nonce = (new ChallengeNonceStore())->getAndRemove(); | ||
|
|
||
| $authResult = $this->ctx->authenticate( | ||
| json_encode($json["authToken"]), | ||
| $authToken, | ||
| $nonce->getBase64EncodedNonce() | ||
| ); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace web_eid\web_eid_authtoken_validation_php\example; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use web_eid\web_eid_authtoken_validation_php\challenge\ChallengeNonce; | ||
|
|
||
| final class MobileAuthInitTest extends TestCase | ||
| { | ||
| public static function csrfRequests(): array | ||
| { | ||
| return [ | ||
| 'missing header' => [[], ['csrf-token' => 'expected'], false], | ||
| 'incorrect token' => [['X-CSRF-TOKEN' => 'wrong'], ['csrf-token' => 'expected'], false], | ||
| 'missing session token' => [['X-CSRF-TOKEN' => 'expected'], [], false], | ||
| 'matching token' => [['X-CSRF-TOKEN' => 'expected'], ['csrf-token' => 'expected'], true], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('csrfRequests')] | ||
| public function testInitializationRequiresCsrfToken(array $headers, array $session, bool $accepted): void | ||
| { | ||
| // A rejected request must not replace an outstanding authentication challenge. | ||
| $session['web-eid-challenge-nonce'] = 'existing challenge'; | ||
| $process = proc_open( | ||
| [PHP_BINARY, __DIR__ . '/fixtures/mobile-auth-init.php'], | ||
| [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], | ||
| $pipes | ||
| ); | ||
| self::assertIsResource($process); | ||
| fwrite($pipes[0], json_encode(['headers' => $headers, 'session' => $session], JSON_THROW_ON_ERROR)); | ||
| fclose($pipes[0]); | ||
| $output = stream_get_contents($pipes[1]); | ||
| $errors = stream_get_contents($pipes[2]); | ||
| fclose($pipes[1]); | ||
| fclose($pipes[2]); | ||
| self::assertSame(0, proc_close($process), $errors); | ||
| self::assertSame('', $errors); | ||
| $result = json_decode($output, true, 512, JSON_THROW_ON_ERROR); | ||
| $body = json_decode($result['body'], true, 512, JSON_THROW_ON_ERROR); | ||
|
|
||
| if (!$accepted) { | ||
| self::assertSame(405, $result['status']); | ||
| self::assertSame(['error' => 'CSRF token missing or invalid'], $body); | ||
| self::assertSame($session, $result['session']); | ||
| return; | ||
| } | ||
|
|
||
| self::assertSame(200, $result['status']); | ||
| self::assertSame($session['csrf-token'], $result['session']['csrf-token']); | ||
| self::assertStringStartsWith('https://id.eesti.ee/auth#', $body['authUri']); | ||
| $payload = json_decode(base64_decode(explode('#', $body['authUri'], 2)[1]), true, 512, JSON_THROW_ON_ERROR); | ||
| self::assertSame('https://example.com/auth/mobile/login', $payload['loginUri']); | ||
| self::assertFalse($payload['getSigningCertificate']); | ||
| $nonce = unserialize($result['session']['web-eid-challenge-nonce'], [ | ||
| 'allowed_classes' => [ChallengeNonce::class, \DateTime::class], | ||
| ]); | ||
| self::assertInstanceOf(ChallengeNonce::class, $nonce); | ||
| self::assertSame($nonce->getBase64EncodedNonce(), $payload['challenge']); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| namespace web_eid\web_eid_authtoken_validation_php\example; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| require_once __DIR__ . '/../../example/src/Template.php'; | ||
| require_once __DIR__ . '/../../example/src/Pages.php'; | ||
|
|
||
| final class PagesCsrfTest extends TestCase | ||
| { | ||
| public function testOpeningAnotherPageKeepsTheFirstPagesCsrfTokenValid(): void | ||
| { | ||
| $previousSession = $_SESSION ?? null; | ||
| $_SESSION = []; | ||
| try { | ||
| $firstToken = $this->renderPageToken('login'); | ||
| self::assertMatchesRegularExpression('/^[a-f0-9]{64}$/', $firstToken); | ||
| self::assertSame($firstToken, $_SESSION['csrf-token']); | ||
|
|
||
| // Another tab or the mobile callback must not invalidate the first page. | ||
| self::assertSame($firstToken, $this->renderPageToken('login')); | ||
| self::assertSame($firstToken, $this->renderPageToken('mobileLoginView')); | ||
| self::assertSame($firstToken, $_SESSION['csrf-token']); | ||
| } finally { | ||
| if ($previousSession === null) { | ||
| unset($_SESSION); | ||
| } else { | ||
| $_SESSION = $previousSession; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function renderPageToken(string $action): string | ||
| { | ||
| ob_start(); | ||
| try { | ||
| $page = new \Pages(); | ||
| $page->$action(); | ||
| unset($page); // Pages renders the outer template in its destructor. | ||
| $html = ob_get_contents(); | ||
| } finally { | ||
| ob_end_clean(); | ||
| } | ||
| self::assertSame(1, preg_match('/<meta id="csrftoken" name="csrftoken" content="([^"]*)"/', $html, $matches)); | ||
| return $matches[1]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| // Run the real handler in a subprocess because assertCsrf() terminates rejected requests. | ||
| require __DIR__ . '/../../../vendor/autoload.php'; | ||
| require __DIR__ . '/../../../example/src/Config.php'; | ||
| require __DIR__ . '/../../../example/src/AuthContext.php'; | ||
| require __DIR__ . '/../../../example/src/MobileAuth.php'; | ||
|
|
||
| $request = json_decode(stream_get_contents(STDIN), true, 512, JSON_THROW_ON_ERROR); | ||
| $_SESSION = $request['session']; | ||
|
|
||
| function getallheaders(): array | ||
| { | ||
| global $request; | ||
| return $request['headers']; | ||
| } | ||
|
|
||
| http_response_code(200); | ||
| ob_start(); | ||
| register_shutdown_function(function (): void { | ||
| $body = ob_get_clean(); | ||
| echo json_encode([ | ||
| 'status' => http_response_code(), | ||
| 'body' => $body, | ||
| 'session' => $_SESSION, | ||
| ], JSON_THROW_ON_ERROR); | ||
| }); | ||
|
|
||
| $config = Config::fromArray([ | ||
| 'origin_url' => 'https://example.com', | ||
| 'mobile_base_url' => 'https://id.eesti.ee', | ||
| 'mobile_request_signing_cert' => false, | ||
| ]); | ||
| (new MobileAuth(new AuthContext($config)))->init(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/validateaccepts the raw authentication token directly, but mobile/auth/mobile/loginexpects{"authToken": ...}structure, this should perhaps be clarified?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aligned with the Java example: the mobile login page now posts the token object directly as the request body, same as /validate, so both