-
Notifications
You must be signed in to change notification settings - Fork 11
Initial controller #11
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
Merged
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SimpleSAML\Module\discopower\Controller; | ||
|
|
||
| use Exception; | ||
| use SimpleSAML\Configuration; | ||
| use SimpleSAML\Error; | ||
| use SimpleSAML\HTTP\RunnableResponse; | ||
| use SimpleSAML\Module\discopower\PowerIdPDisco; | ||
| use SimpleSAML\Session; | ||
| use SimpleSAML\XHTML\Template; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
|
||
| class DiscoPower | ||
| { | ||
| /** | ||
| * @param \Symfony\Component\HttpFoundation\Request $request The current request. | ||
| * @return \SimpleSAML\HTTP\RunnableResponse | ||
| */ | ||
| public function main(Request $request): RunnableResponse | ||
| { | ||
| try { | ||
| $discoHandler = new PowerIdPDisco( | ||
| ['saml20-idp-remote'], | ||
| 'poweridpdisco' | ||
| ); | ||
| } catch (Exception $exception) { | ||
| // An error here should be caused by invalid query parameters | ||
| throw new Error\Error('DISCOPARAMS', $exception); | ||
| } | ||
|
|
||
| try { | ||
| return new RunnableResponse([$discoHandler, 'handleRequest'], []); | ||
| } catch (Exception $exception) { | ||
| // An error here should be caused by metadata | ||
| throw new Error\Error('METADATA', $exception); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An AJAX handler to retrieve a list of disco tabs from the session. | ||
| * This allows us to dynamically update the tab list without inline javascript. | ||
| * | ||
| * @param \Symfony\Component\HttpFoundation\Request $request The current request. | ||
| */ | ||
| public function tablist(Request $request): Response | ||
| { | ||
| $session = Session::getSessionFromRequest(); | ||
| $tabs = $session->getData('discopower:tabList', 'tabs'); | ||
| $faventry = $session->getData('discopower:tabList', 'faventry'); | ||
| $defaulttab = $session->getData('discopower:tabList', 'defaulttab'); | ||
|
|
||
| if (!is_array($tabs)) { | ||
| throw new Error\Exception('Could not get tab list from session'); | ||
| } | ||
|
|
||
| $response = new JsonResponse(); | ||
|
|
||
| // handle JSONP requests | ||
| if ($request->query->has('callback')) { | ||
| $callback = $request->query->get('callback'); | ||
| if (!preg_match('/^[a-z0-9_]+$/i', $callback)) { | ||
| throw new Error\Exception('Unsafe JSONP callback function name ' . var_export($callback, true)); | ||
| } | ||
| $response->setCallback($callback); | ||
| } | ||
|
|
||
| $response->setData( | ||
| [ | ||
| 'faventry' => $faventry, | ||
| 'default' => $defaulttab, | ||
| 'tabs' => $tabs, | ||
| ] | ||
| ); | ||
| return $response; | ||
| } | ||
| } |
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,6 @@ | ||
| discopower-main: | ||
| path: /disco.php | ||
| defaults: { _controller: 'SimpleSAML\Module\discopower\Controller\DiscoPower::main' } | ||
| discopower-tablist: | ||
| path: /tablist | ||
| defaults: { _controller: 'SimpleSAML\Module\discopower\Controller\DiscoPower::tablist' } |
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,121 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SimpleSAML\Test\Module\discopower\Controller; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use SimpleSAML\Configuration; | ||
| use SimpleSAML\Error; | ||
| use SimpleSAML\Module\discopower\Controller; | ||
| use SimpleSAML\Session; | ||
| use SimpleSAML\TestUtils\ClearStateTestCase; | ||
| use SimpleSAML\XHTML\Template; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| /** | ||
| * Set of tests for the controllers in the "discopwer" module. | ||
| * | ||
| * @covers \SimpleSAML\Module\discopower\Controller\DiscoPower | ||
| */ | ||
| class DiscoPowerTest extends ClearStateTestCase | ||
| { | ||
| /** | ||
| * Set up for each test. | ||
| */ | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->config = Configuration::loadFromArray( | ||
| [ | ||
| 'module.enable' => ['discopower' => true], | ||
| ], | ||
| '[ARRAY]', | ||
| 'simplesaml' | ||
| ); | ||
|
|
||
| Configuration::setPreLoadedConfig($this->config, 'config.php'); | ||
| } | ||
|
|
||
| public function testDiscoPowerNoDiscoParams(): void | ||
| { | ||
| $request = Request::create( | ||
| '/disco.php', | ||
| 'GET' | ||
| ); | ||
|
|
||
| $c = new Controller\DiscoPower(); | ||
|
|
||
| $this->expectException(Error\Error::class); | ||
| $this->expectExceptionMessage("DISCOPARAMS"); | ||
| $r = $c->main($request); | ||
| } | ||
|
|
||
| public function testTablistJson(): void | ||
| { | ||
| $session = Session::getSessionFromRequest(); | ||
| $session->setData('discopower:tabList', 'faventry', 'http://example.org/idp'); | ||
| $session->setData('discopower:tabList', 'tabs', ['Frankrijk', 'Nederland', 'Duitsland']); | ||
| $session->setData('discopower:tabList', 'defaulttab', 'Nederland'); | ||
|
|
||
| $request = Request::create( | ||
| '/tablist', | ||
| 'GET' | ||
| ); | ||
|
|
||
| $c = new Controller\DiscoPower(); | ||
|
|
||
| $r = $c->tablist($request); | ||
| $this->assertTrue($r->isSuccessful()); | ||
| $this->assertEquals('application/json', $r->headers->get('Content-Type')); | ||
| $this->assertEquals('{"faventry":"http:\/\/example.org\/idp","default":"Nederland","tabs":["Frankrijk","Nederland","Duitsland"]}', $r->getContent()); | ||
|
|
||
| $request = Request::create( | ||
| '/tablist', | ||
| 'GET', | ||
| ['callback' => 'aapnoot'], | ||
| ); | ||
|
|
||
| $c = new Controller\DiscoPower(); | ||
|
|
||
| $r = $c->tablist($request); | ||
| $this->assertTrue($r->isSuccessful()); | ||
| $this->assertEquals('text/javascript', $r->headers->get('Content-Type')); | ||
| $this->assertEquals('/**/aapnoot({"faventry":"http:\/\/example.org\/idp","default":"Nederland","tabs":["Frankrijk","Nederland","Duitsland"]});', $r->getContent()); | ||
| } | ||
|
|
||
| public function testTablistJsonNoSession(): void | ||
| { | ||
| $request = Request::create( | ||
| '/tablist', | ||
| 'GET', | ||
| ); | ||
|
|
||
| $c = new Controller\DiscoPower(); | ||
|
|
||
| $this->expectException(Error\Exception::class); | ||
| $this->expectExceptionMessage("Could not get tab list from session"); | ||
| $r = $c->tablist($request); | ||
| } | ||
|
|
||
| public function testTablistJsonUnsafeCallback(): void | ||
| { | ||
| $session = Session::getSessionFromRequest(); | ||
| $session->setData('discopower:tabList', 'faventry', 'http://example.org/idp'); | ||
| $session->setData('discopower:tabList', 'tabs', ['Frankrijk', 'Nederland', 'Duitsland']); | ||
| $session->setData('discopower:tabList', 'defaulttab', 'Nederland'); | ||
|
|
||
| $request = Request::create( | ||
| '/tablist', | ||
| 'GET', | ||
| ['callback' => 'alert("hallo")'], | ||
| ); | ||
|
|
||
| $c = new Controller\DiscoPower(); | ||
|
|
||
| $this->expectException(Error\Exception::class); | ||
| $this->expectExceptionMessage("Unsafe JSONP callback"); | ||
| $r = $c->tablist($request); | ||
| } | ||
| } | ||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.