File tree 6 files changed +51
-17
lines changed
6 files changed +51
-17
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace RexGraphQL \Controller ;
4
+
5
+ use GraphQL \Service \Auth \AuthService ;
6
+ use TheCodingMachine \GraphQLite \Annotations \Query ;
7
+
8
+ class AuthController
9
+ {
10
+ private AuthService $ service ;
11
+ public function __construct ()
12
+ {
13
+ $ this ->service = new AuthService ();
14
+ }
15
+
16
+ #[Query]
17
+ public function isRedaxoLoggedIn (): bool
18
+ {
19
+ return $ this ->service ->isRedaxoLoggedIn ();
20
+ }
21
+
22
+ }
Original file line number Diff line number Diff line change 3
3
namespace GraphQL ;
4
4
5
5
use GraphQL \Error \DebugFlag ;
6
+ use GraphQL \Service \Auth \AuthService ;
6
7
use GraphQL \Validator \DocumentValidator ;
7
8
use GraphQL \Validator \Rules \QueryDepth ;
8
9
use rex ;
9
10
use rex_extension ;
10
11
use rex_extension_point ;
11
12
use rex_response ;
12
13
use rex_var ;
13
- use RexGraphQL \Auth \AuthService ;
14
14
use Symfony \Component \DependencyInjection \Container ;
15
15
use TheCodingMachine \GraphQLite \Context \Context ;
16
16
use TheCodingMachine \GraphQLite \Exceptions \WebonyxErrorHandler ;
17
17
use TheCodingMachine \GraphQLite \Schema ;
18
18
use TheCodingMachine \GraphQLite \SchemaFactory ;
19
19
use Yiisoft \Cache \ArrayCache ;
20
-
21
20
use function is_array ;
22
21
23
22
class Endpoint
Original file line number Diff line number Diff line change 2
2
3
3
namespace GraphQL ;
4
4
5
- use RexGraphQL \Auth \AuthService ;
6
- use RexGraphQL \Auth \JwtService ;
5
+ use GraphQL \ Service \Auth \AuthService ;
6
+ use GraphQL \ Service \Auth \JwtService ;
7
7
use RexGraphQL \Connector \Connector ;
8
8
use RexGraphQL \RexGraphQL ;
9
9
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace RexGraphQL \Auth ;
3
+ namespace GraphQL \ Service \Auth ;
4
4
use TheCodingMachine \GraphQLite \Security \AuthenticationServiceInterface ;
5
5
6
+
6
7
class AuthService implements AuthenticationServiceInterface
7
8
{
8
9
@@ -19,11 +20,11 @@ public function __construct()
19
20
public function isLogged (): bool
20
21
{
21
22
$ secret = \rex_addon::get ('graphql ' )->getConfig (self ::SHARED_SECRET_CONFIG_KEY );
22
- $ bearerToken = static ::parseBearerToken ();
23
+ $ bearerToken = static ::getBearerToken ();
23
24
if (!$ bearerToken && $ secret ) {
24
25
return false ;
25
26
}
26
- if (( $ bearerToken && $ this ->jwtService -> validateToken ( $ bearerToken ) ) || !$ secret ) {
27
+ if ($ this ->isRedaxoLoggedIn ( ) || !$ secret ) {
27
28
return true ;
28
29
}
29
30
return $ bearerToken === $ secret ;
@@ -34,7 +35,13 @@ public function getUser(): ?object
34
35
return null ;
35
36
}
36
37
37
- private static function parseBearerToken (): ?string
38
+ public function isRedaxoLoggedIn (): bool
39
+ {
40
+ $ bearerToken = self ::getBearerToken ();
41
+ return $ bearerToken && $ this ->jwtService ->validateToken ($ bearerToken );
42
+ }
43
+
44
+ public static function getBearerToken (): ?string
38
45
{
39
46
$ authHeader = $ _SERVER ['HTTP_AUTHORIZATION ' ] ?? null ;
40
47
if (null === $ authHeader ) {
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace RexGraphQL \Auth ;
3
+ namespace GraphQL \ Service \Auth ;
4
4
5
5
use Firebase \JWT \JWT ;
6
6
use Firebase \JWT \Key ;
7
+ use PHPMailer \PHPMailer \Exception ;
7
8
8
9
class JwtService
9
10
{
@@ -16,12 +17,17 @@ public function validateToken(string $token): bool
16
17
return false ;
17
18
}
18
19
$ key = new Key ($ key , self ::KEY_ALGORITHM );
19
- $ token = JWT ::decode ($ token , $ key );
20
- if ($ token ->userId > 0 ) {
21
- $ user = \rex_user::get ($ token ->userId );
22
- \rex::setProperty ('user ' , $ user );
23
- return true ;
20
+ try {
21
+ $ token = JWT ::decode ($ token , $ key );
22
+ if ($ token ->userId > 0 ) {
23
+ $ user = \rex_user::get ($ token ->userId );
24
+ \rex::setProperty ('user ' , $ user );
25
+ return true ;
26
+ }
27
+ } catch (\DomainException |\UnexpectedValueException $ exception ) {
28
+ return false ;
24
29
}
30
+
25
31
return false ;
26
32
}
27
33
@@ -35,7 +41,7 @@ public function generateToken(): ?string {
35
41
$ key = $ this ->getKey ();
36
42
if ($ user && $ key ) {
37
43
$ payload = [
38
- 'userId ' => $ user ->getId ()
44
+ 'userId ' => $ user ->getId (),
39
45
];
40
46
return JWT ::encode ($ payload , $ key , self ::KEY_ALGORITHM );
41
47
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- use RexGraphQL \Auth \AuthService ;
4
- use RexGraphQL \Auth \JwtService ;
3
+ use GraphQL \ Service \Auth \AuthService ;
4
+ use GraphQL \ Service \Auth \JwtService ;
5
5
use RexGraphQL \RexGraphQL ;
6
6
7
7
echo rex_view::title ($ this ->getProperty ('page ' )['title ' ]);
You can’t perform that action at this time.
0 commit comments