Skip to content

Commit

Permalink
Add helper for accessing the context in grahpql
Browse files Browse the repository at this point in the history
  • Loading branch information
riddler7 committed Dec 5, 2018
1 parent b2e6ed8 commit 98569bd
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
85 changes: 85 additions & 0 deletions src/Helpers/OauthContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Riddler7\Oauth2GraphQL\Helpers;

use AdvancedLearning\Oauth2Server\Models\Client;

trait OauthContext
{
/**
* Name of the oauth client id in the graphql context.
*
* @var string
*/
protected static $oauthClientKey = 'oauthClientIdentifier';

/**
* Name of the oauth scopes in the graphql context.
*
* @var string
*/
protected static $oauthScopesKey = 'oauthScopes';

/**
* Determine if the scope has a valid client.
*
* @param array $context
*
* @return bool
*/
public function hasOauthClient(array $context)
{
return !empty($context[self::$oauthClientKey]);
}

/**
* Return the model for the Oauth Client.
*
* @param array $context
*
* @return null|\SilverStripe\ORM\DataObject
*/
public function getOauthClient(array $context)
{
if (!$this->hasOauthClient($context)) {
return null;
}

return Client::get()->filter(['Identifier' => $context[self::$oauthClientKey]])->first();
}

/**
* Determine whether the a scope has been granted.
*
* @param array $context
* @param string $scope
*
* @return bool
*/
public function hasScope(array $context, string $scope)
{
return !empty($context[self::$oauthScopesKey]) && in_array($scope, $context[self::$oauthScopesKey]);
}

/**
* Determnie whether all the scopes have been granted.
*
* @param array $context
* @param array $scopes
*
* @return bool
*/
public function hasScopes(array $context, array $scopes)
{
$has = true;

foreach ($scopes as $scope) {
// stop once we get the first false result
if (!$this->hasScope($context, $scope)) {
return false;
}
}

return $has;
}
}
37 changes: 35 additions & 2 deletions tests/GraphqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Grant\PasswordGrant;
use Riddler7\Oauth2GraphQL\Controller;
use Riddler7\Oauth2GraphQL\Helpers\OauthContext;
use Riddler7\Oauth2GraphQL\Tests\BlankMutation;
use Riddler7\Oauth2GraphQL\Tests\BlankQuery;
use Riddler7\Oauth2GraphQL\Tests\BlankType;
Expand All @@ -34,7 +35,7 @@

class GraphqlTest extends SapphireTest
{
use CryptTrait;
use CryptTrait, OauthContext;

protected static $fixture_file = 'tests/OAuthFixture.yml';

Expand Down Expand Up @@ -176,7 +177,7 @@ public function testGraphQLContexts()
$manager = new Manager('myschema');

// extract the context
$manager->addMiddleware(new GraphQLSchemaExtractor(function($currentContext) use (&$context) {
$manager->addMiddleware(new GraphQLSchemaExtractor(function ($currentContext) use (&$context) {
$context = $currentContext;
}));

Expand All @@ -186,6 +187,38 @@ public function testGraphQLContexts()
$this->assertEquals($client->Identifier, $context['oauthClientIdentifier']);
$this->assertEquals(1, count($context['oauthScopes']));
$this->assertEquals('members', $context['oauthScopes'][0]);

// test the context helper
$this->assertEquals(
true,
$this->hasOauthClient($context),
'Context should contain a client'
);
$this->assertEquals(
true,
$this->hasScope($context, 'members'),
'Context should have a \'members\' scope'
);
$this->assertEquals(
false,
$this->hasScope($context, 'admin'),
'Context should not have an \'admin\' scope'
);
$this->assertEquals(
true,
$this->hasScopes($context,
['members'])
);
$this->assertEquals(
false,
$this->hasScopes($context,
['admin'])
);
$this->assertEquals(
$client->ID,
$this->getOauthClient($context)->ID,
'The ids for the Oauth Client should match'
);
}

/**
Expand Down

0 comments on commit 98569bd

Please sign in to comment.