Skip to content

Commit 420cd6c

Browse files
committed
Update PHP SDK to v7.0.0
1 parent d26e581 commit 420cd6c

File tree

9 files changed

+73
-122
lines changed

9 files changed

+73
-122
lines changed

.phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
PHPCompatibility sniffs to check for PHP cross-version incompatible code.
2121
https://github.com/PHPCompatibility/PHPCompatibility
2222
-->
23-
<config name="testVersion" value="5.5-"/>
23+
<config name="testVersion" value="7.1-"/>
2424
<rule ref="PHPCompatibility"/>
2525

2626
</ruleset>

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"name": "auth0/login",
33
"description": "Laravel plugin that helps authenticate with the auth0 service",
44
"license": "MIT",
5+
"prefer-stable": true,
56
"require": {
6-
"php": ">=5.5.0",
7+
"php": "^7.1",
8+
"auth0/auth0-php": "^7.0",
79
"illuminate/support": "5.* | ^6.0",
8-
"auth0/auth0-php": "^5.6.0",
910
"illuminate/contracts": "5.* | ^6.0"
1011
},
1112
"require-dev": {
12-
"phpunit/phpunit": "^4 | ^7",
13+
"phpunit/phpunit": "^7",
1314
"squizlabs/php_codesniffer": "^3.2",
1415
"phpcompatibility/php-compatibility": "^8.1",
1516
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
2+
<phpunit bootstrap="tests/bootstrap.php"
33
backupGlobals="false"
44
backupStaticAttributes="false"
55
colors="true"

src/Auth0/Login/Auth0Service.php

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace Auth0\Login;
44

5-
use Auth0\SDK\API\Helpers\State\SessionStateHandler;
6-
use Auth0\SDK\API\Helpers\State\StateHandler;
75
use Auth0\SDK\Auth0;
8-
use Auth0\SDK\Helpers\Cache\CacheHandler;
9-
use Auth0\SDK\JWTVerifier;
106
use Auth0\SDK\Store\StoreInterface;
11-
use Illuminate\Contracts\Container\BindingResolutionException;
7+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
128
use Illuminate\Http\RedirectResponse;
9+
use Psr\SimpleCache\CacheInterface;
1310

1411
/**
1512
* Service that provides access to the Auth0 SDK.
@@ -28,28 +25,35 @@ class Auth0Service
2825
/**
2926
* Auth0Service constructor.
3027
*
31-
* @param array $auth0Config
28+
* @param array|null $auth0Config
3229
* @param StoreInterface|null $store
33-
* @param StateHandler|null $stateHandler
30+
* @param CacheInterface|null $cache
31+
*
32+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
3433
*/
3534
public function __construct(
3635
array $auth0Config,
3736
StoreInterface $store = null,
38-
StateHandler $stateHandler = null
37+
CacheInterface $cache = null
3938
)
4039
{
41-
$store = isset( $auth0Config['store'] ) ? $auth0Config['store'] : $store;
40+
41+
if (!$auth0Config instanceof ConfigRepository && !is_array($auth0Config)) {
42+
$auth0Config = config('laravel-auth0');
43+
}
44+
45+
$store = $auth0Config['store'] ?? $store;
4246
if (false !== $store && !$store instanceof StoreInterface) {
4347
$store = new LaravelSessionStore();
4448
}
49+
$auth0Config['store'] = $store;
4550

46-
$stateHandler = isset( $auth0Config['state_handler'] ) ? $auth0Config['state_handler'] : $stateHandler;
47-
if (false !== $stateHandler && !$stateHandler instanceof StateHandler) {
48-
$stateHandler = new SessionStateHandler($store);
51+
$cache = $auth0Config['cache_handler'] ?? $cache;
52+
if (!($cache instanceof CacheInterface)) {
53+
$cache = app()->make('cache.store');
4954
}
55+
$auth0Config['cache_handler'] = $cache;
5056

51-
$auth0Config['store'] = $store;
52-
$auth0Config['state_handler'] = $stateHandler;
5357
$this->auth0 = new Auth0($auth0Config);
5458
}
5559

@@ -156,35 +160,14 @@ public function rememberUser($value = null)
156160

157161
/**
158162
* @param $encUser
163+
* @param array $verifierOptions
159164
*
160-
* @return mixed
165+
* @return object
166+
* @throws \Auth0\SDK\Exception\InvalidTokenException
161167
*/
162-
public function decodeJWT($encUser)
168+
public function decodeJWT($encUser, array $verifierOptions = [])
163169
{
164-
try {
165-
$cache = \App::make(CacheHandler::class);
166-
} catch (BindingResolutionException $e) {
167-
$cache = null;
168-
}
169-
170-
$secret_base64_encoded = config('laravel-auth0.secret_base64_encoded');
171-
172-
if (is_null($secret_base64_encoded)) {
173-
$secret_base64_encoded = true;
174-
}
175-
176-
$verifier = new JWTVerifier([
177-
'valid_audiences' => [config('laravel-auth0.client_id'), config('laravel-auth0.api_identifier')],
178-
'supported_algs' => config('laravel-auth0.supported_algs', ['HS256']),
179-
'client_secret' => config('laravel-auth0.client_secret'),
180-
'authorized_iss' => config('laravel-auth0.authorized_issuers'),
181-
'secret_base64_encoded' => $secret_base64_encoded,
182-
'cache' => $cache,
183-
'guzzle_options' => config('laravel-auth0.guzzle_options'),
184-
]);
185-
186-
$this->apiuser = $verifier->verifyAndDecode($encUser);
187-
170+
$this->apiuser = (object) $this->auth0->decodeIdToken($encUser, $verifierOptions);
188171
return $this->apiuser;
189172
}
190173

src/Auth0/Login/LaravelCacheWrapper.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Auth0/Login/LaravelSessionStore.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Auth0\Login;
44

5-
use Session;
65
use Auth0\SDK\Store\StoreInterface;
76

87
class LaravelSessionStore implements StoreInterface
@@ -12,16 +11,14 @@ class LaravelSessionStore implements StoreInterface
1211
/**
1312
* Persists $value on $_SESSION, identified by $key.
1413
*
15-
* @see Auth0SDK\BaseAuth0
16-
*
1714
* @param string $key
1815
* @param mixed $value
1916
*/
20-
public function set($key, $value)
17+
public function set(string $key, $value)
2118
{
2219
$key_name = $this->getSessionKeyName($key);
2320

24-
Session::put($key_name, $value);
21+
\session([$key_name, $value]);
2522
}
2623

2724
/**
@@ -30,11 +27,11 @@ public function set($key, $value)
3027
*
3128
* @return mixed
3229
*/
33-
public function get($key, $default = null)
30+
public function get(string $key, $default = null)
3431
{
3532
$key_name = $this->getSessionKeyName($key);
3633

37-
return Session::get($key_name, $default);
34+
return \session($key_name, $default);
3835
}
3936

4037
/**
@@ -44,11 +41,11 @@ public function get($key, $default = null)
4441
*
4542
* @param string $key
4643
*/
47-
public function delete($key)
44+
public function delete(string $key)
4845
{
4946
$key_name = $this->getSessionKeyName($key);
5047

51-
Session::forget($key_name);
48+
\session([$key_name, null]);
5249
}
5350

5451
/**

src/Auth0/Login/LoginServiceProvider.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Auth0\SDK\API\Helpers\ApiClient;
66
use Auth0\SDK\API\Helpers\InformationHeaders;
7-
use Auth0\SDK\API\Helpers\State\StateHandler;
8-
use Auth0\SDK\API\Helpers\State\SessionStateHandler;
97
use Auth0\SDK\Store\StoreInterface;
108
use Illuminate\Support\ServiceProvider;
119

@@ -50,16 +48,12 @@ public function register()
5048
return new LaravelSessionStore();
5149
});
5250

53-
$this->app->bind(StateHandler::class, function ($app) {
54-
return new SessionStateHandler($app->make(LaravelSessionStore::class));
55-
});
56-
5751
// Bind the auth0 name to a singleton instance of the Auth0 Service
5852
$this->app->singleton(Auth0Service::class, function ($app) {
5953
return new Auth0Service(
6054
$app->make('config')->get('laravel-auth0'),
6155
$app->make(StoreInterface::class),
62-
$app->make(StateHandler::class)
56+
$app->make('cache.store')
6357
);
6458
});
6559
$this->app->singleton('auth0', function () {

tests/Auth0ServiceTest.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
use Auth0\Login\Auth0Service;
55
use Auth0\Login\Facade\Auth0 as Auth0Facade;
6-
use Auth0\Login\LaravelSessionStore;
76
use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider;
8-
use Auth0\SDK\API\Helpers\State\DummyStateHandler;
9-
use Auth0\SDK\Store\EmptyStore;
7+
use Auth0\SDK\Exception\InvalidTokenException;
108
use Auth0\SDK\Store\SessionStore;
9+
use Illuminate\Http\RedirectResponse;
10+
use Illuminate\Support\Facades\Cache;
1111
use Orchestra\Testbench\TestCase as OrchestraTestCase;
12-
use Session;
1312

1413
class Auth0ServiceTest extends OrchestraTestCase
1514
{
@@ -23,13 +22,19 @@ public static function setUpBeforeClass()
2322
'client_id' => '__test_client_id__',
2423
'client_secret' => '__test_client_secret__',
2524
'redirect_uri' => 'https://example.com/callback',
25+
'transient_store' => new SessionStore(),
2626
];
2727
}
2828

29+
public function tearDown() : void
30+
{
31+
Cache::flush();
32+
}
33+
2934
public function testThatServiceUsesSessionStoreByDefault()
3035
{
31-
Session::put('auth0__user', '__test_user__');
32-
$service = new Auth0Service(self::$defaultConfig, new LaravelSessionStore(), new DummyStateHandler());
36+
session(['auth0__user' => '__test_user__']);
37+
$service = new Auth0Service(self::$defaultConfig);
3338
$user = $service->getUser();
3439

3540
$this->assertArrayHasKey('profile', $user);
@@ -38,12 +43,9 @@ public function testThatServiceUsesSessionStoreByDefault()
3843

3944
public function testThatServiceSetsEmptyStoreFromConfigAndConstructor()
4045
{
41-
Session::put('auth0__user', '__test_user__');
46+
session(['auth0__user' => '__test_user__']);
4247

43-
$service = new Auth0Service(self::$defaultConfig + ['store' => false, 'state_handler' => false]);
44-
$this->assertNull($service->getUser());
45-
46-
$service = new Auth0Service(self::$defaultConfig, new EmptyStore(), new DummyStateHandler());
48+
$service = new Auth0Service(self::$defaultConfig + ['store' => false]);
4749
$this->assertNull($service->getUser());
4850

4951
$service = new Auth0Service(self::$defaultConfig);
@@ -52,11 +54,10 @@ public function testThatServiceSetsEmptyStoreFromConfigAndConstructor()
5254

5355
public function testThatServiceLoginReturnsRedirect()
5456
{
55-
5657
$service = new Auth0Service(self::$defaultConfig);
5758
$redirect = $service->login();
5859

59-
$this->assertInstanceOf( \Illuminate\Http\RedirectResponse::class, $redirect );
60+
$this->assertInstanceOf( RedirectResponse::class, $redirect );
6061

6162
$targetUrl = parse_url($redirect->getTargetUrl());
6263

@@ -68,6 +69,22 @@ public function testThatServiceLoginReturnsRedirect()
6869
$this->assertContains('client_id=__test_client_id__', $targetUrlQuery);
6970
}
7071

72+
/**
73+
* @throws InvalidTokenException
74+
*/
75+
public function testThatServiceCanUseLaravelCache()
76+
{
77+
$cache_key = md5('https://__invalid_domain__/.well-known/jwks.json');
78+
cache([$cache_key => [uniqid()]], 10);
79+
session(['auth0__nonce' => uniqid()]);
80+
81+
$service = new Auth0Service(['domain' => '__invalid_domain__'] + self::$defaultConfig);
82+
83+
// Without the cache set above, would expect a cURL error for a bad domain.
84+
$this->expectException(InvalidTokenException::class);
85+
$service->decodeJWT(uniqid());
86+
}
87+
7188
/*
7289
* Test suite helpers
7390
*/

tests/bootstrap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$tests_dir = dirname(__FILE__).'/';
3+
4+
require_once $tests_dir.'../vendor/autoload.php';
5+
6+
ini_set('session.use_cookies', false);
7+
ini_set('session.cache_limiter', false);

0 commit comments

Comments
 (0)