Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Commit 4be3fad

Browse files
committed
Merge branch 'satchinjoshi-authetication' into develop
2 parents 69b504b + 748416e commit 4be3fad

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/Folklore/GraphQL/Support/Field.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public function authorize($root, $args)
1717
return true;
1818
}
1919

20+
/**
21+
* Override this in your queries or mutations
22+
* to authenticate per query or mutation
23+
*/
24+
public function authenticated($root, $args, $context)
25+
{
26+
return true;
27+
}
28+
2029
public function attributes()
2130
{
2231
return [];
@@ -39,11 +48,17 @@ protected function getResolver()
3948
}
4049

4150
$resolver = array($this, 'resolve');
51+
$authenticate = [$this, 'authenticated'];
4252
$authorize = [$this, 'authorize'];
4353

44-
return function () use ($resolver, $authorize) {
54+
return function () use ($resolver, $authorize, $authenticate) {
4555
$args = func_get_args();
4656

57+
// Authenticated
58+
if (call_user_func_array($authenticate, $args) !== true) {
59+
throw new AuthorizationError('Unauthenticated');
60+
}
61+
4762
// Authorize
4863
if (call_user_func_array($authorize, $args) !== true) {
4964
throw new AuthorizationError('Unauthorized');

tests/GraphQLQueryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public function testQueryAndReturnResultWithAuthorize()
114114
$this->assertEquals('Unauthorized', $result['errors'][0]['message']);
115115
}
116116

117+
/**
118+
* Test query with authorize
119+
*
120+
* @test
121+
*/
122+
public function testQueryAndReturnResultWithAuthenticated()
123+
{
124+
$result = GraphQL::query($this->queries['examplesWithAuthenticated']);
125+
$this->assertNull($result['data']['examplesAuthenticated']);
126+
$this->assertEquals('Unauthenticated', $result['errors'][0]['message']);
127+
}
128+
117129
/**
118130
* Test query with schema
119131
*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use GraphQL\Type\Definition\Type;
4+
use Folklore\GraphQL\Support\Query;
5+
6+
class ExamplesAuthenticatedQuery extends ExamplesQuery
7+
{
8+
protected $attributes = [
9+
'name' => 'Examples authenticate query'
10+
];
11+
12+
public function authenticated($root, $args, $context)
13+
{
14+
return false;
15+
}
16+
}

tests/Objects/queries.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
}
5050
",
5151

52+
'examplesWithAuthenticated' => "
53+
query QueryExamplesAuthenticated {
54+
examplesAuthenticated {
55+
test
56+
}
57+
}
58+
",
59+
5260
'examplesWithRoot' => "
5361
query QueryExamplesRoot {
5462
examplesRoot {

tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ protected function getEnvironmentSetUp($app)
2525
'examples' => ExamplesQuery::class,
2626
'examplesContext' => ExamplesContextQuery::class,
2727
'examplesRoot' => ExamplesRootQuery::class,
28-
'examplesAuthorize' => ExamplesAuthorizeQuery::class
28+
'examplesAuthorize' => ExamplesAuthorizeQuery::class,
29+
'examplesAuthenticated' => ExamplesAuthenticatedQuery::class,
2930
],
3031
'mutation' => [
3132
'updateExample' => UpdateExampleMutation::class

0 commit comments

Comments
 (0)