Skip to content

Commit

Permalink
Merge pull request #73 from oat-sa/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ekkinox authored Dec 1, 2020
2 parents 7795151 + 8e45d86 commit 2f2fb18
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.3.0
-----

* Added OidcTestingTrait to ease OIDC based testing flows

3.2.2
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@
use OAT\Library\Lti1p3Core\Security\Nonce\NonceRepositoryInterface;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcAuthenticator;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcInitiator;
use OAT\Library\Lti1p3Core\Tests\Traits\DomainTestingTrait;
use OAT\Library\Lti1p3Core\Tests\Traits\NetworkTestingTrait;
use OAT\Library\Lti1p3Core\Tests\Traits\OidcTestingTrait;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class ToolLaunchValidatorTest extends TestCase
{
use DomainTestingTrait;
use NetworkTestingTrait;
use OidcTestingTrait;

/** @var RegistrationRepositoryInterface */
private $registrationRepository;
Expand Down Expand Up @@ -109,7 +107,7 @@ public function testValidatePlatformOriginatingLaunchForLtiResourceLinkSuccess()
]
);

$result = $this->subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result = $this->subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));

$this->assertInstanceOf(LaunchValidationResult::class, $result);
$this->assertFalse($result->hasError());
Expand Down Expand Up @@ -152,7 +150,7 @@ public function testValidatePlatformOriginatingLaunchForDeepLinkingSuccess(): vo
]
);

$result = $this->subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result = $this->subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));

$this->assertInstanceOf(LaunchValidationResult::class, $result);
$this->assertFalse($result->hasError());
Expand Down Expand Up @@ -201,7 +199,7 @@ public function testValidatePlatformOriginatingLaunchForStartProctoringSuccess()
]
);

$result = $this->subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result = $this->subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));

$this->assertInstanceOf(LaunchValidationResult::class, $result);
$this->assertFalse($result->hasError());
Expand Down Expand Up @@ -261,7 +259,7 @@ public function testValidatePlatformOriginatingLaunchFallbackOnJwks(): void
]
);

$result =$subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result =$subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));

$this->assertInstanceOf(LaunchValidationResult::class, $result);
$this->assertFalse($result->hasError());
Expand All @@ -288,7 +286,7 @@ public function testValidatePlatformOriginatingLaunchFailureOnMissingToolKey():
]
);

$result =$subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result =$subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));

$this->assertInstanceOf(LaunchValidationResult::class, $result);
$this->assertTrue($result->hasError());
Expand All @@ -305,7 +303,7 @@ public function testValidatePlatformOriginatingLaunchFailureWithExpiredPayload()
);

Carbon::setTestNow(Carbon::now()->subSeconds(MessagePayloadInterface::TTL + 1));
$result = $this->subject->validatePlatformOriginatingLaunch($this->performOidcFlow($message));
$result = $this->subject->validatePlatformOriginatingLaunch($this->buildOidcFlowRequest($message));
Carbon::setTestNow();

$this->assertInstanceOf(LaunchValidationResult::class, $result);
Expand Down Expand Up @@ -698,12 +696,8 @@ public function provideValidationFailureContexts(): array
];
}

private function performOidcFlow(LtiMessageInterface $message): ServerRequestInterface
private function buildOidcFlowRequest(LtiMessageInterface $message): ServerRequestInterface
{
$initMessage = $this->oidcInitiator->initiate($this->createServerRequest('GET', $message->toUrl()));

$authMessage = $this->oidcAuthenticator->authenticate($this->createServerRequest('GET', $initMessage->toUrl()));

return $this->createServerRequest('GET', $authMessage->toUrl());
return $this->createServerRequest('GET', $this->performOidcFlow($message)->toUrl());
}
}
51 changes: 51 additions & 0 deletions tests/Traits/OidcTestingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace OAT\Library\Lti1p3Core\Tests\Traits;

use OAT\Library\Lti1p3Core\Message\LtiMessageInterface;
use OAT\Library\Lti1p3Core\Registration\RegistrationRepositoryInterface;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcAuthenticator;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcInitiator;
use OAT\Library\Lti1p3Core\Security\User\UserAuthenticatorInterface;

trait OidcTestingTrait
{
use DomainTestingTrait;
use NetworkTestingTrait;

private function performOidcFlow(
LtiMessageInterface $message,
RegistrationRepositoryInterface $repository = null,
UserAuthenticatorInterface $authenticator = null
): LtiMessageInterface {
$repository = $repository ?? $this->createTestRegistrationRepository();
$authenticator = $authenticator ?? $this->createTestUserAuthenticator();

$oidcInitiator = new OidcInitiator($repository);
$oidcAuthenticator = new OidcAuthenticator($repository, $authenticator);

$oidcInitMessage = $oidcInitiator->initiate($this->createServerRequest('GET', $message->toUrl()));

return $oidcAuthenticator->authenticate($this->createServerRequest('GET', $oidcInitMessage->toUrl()));
}
}

0 comments on commit 2f2fb18

Please sign in to comment.