Skip to content
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

[SDK-3669] Add StoreInterface bridge for Laravel Sessions #307

Merged
merged 8 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
max-parallel: 10
matrix:
php: ["7.4", "8.0", "8.1"]
php: "8.0", "8.1", "8.2"]

if: (github.actor != 'dependabot[bot]')
steps:
Expand Down
55 changes: 5 additions & 50 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
max-parallel: 10
matrix:
php: ["7.4", "8.0", "8.1"]
php: ["8.0", "8.1", "8.2"]

steps:
- name: Checkout code
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
max-parallel: 10
matrix:
php: ["7.4", "8.0", "8.1"]
php: ["8.0", "8.1", "8.2"]

steps:
- name: Set up PHP ${{ matrix.php }}
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
fail-fast: true
max-parallel: 10
matrix:
php: ["7.4", "8.0", "8.1"]
php: ["8.0", "8.1", "8.2"]

steps:
- name: Set up PHP ${{ matrix.php }}
Expand Down Expand Up @@ -132,48 +132,6 @@ jobs:
- name: Execute PHPStan
run: vendor/bin/phpstan analyze --no-progress

# psalm:
# name: Psalm
# runs-on: ubuntu-latest
# needs: ["dependencies"]

# strategy:
# fail-fast: true
# max-parallel: 10
# matrix:
# php: ["7.4", "8.0", "8.1"]

# steps:
# - name: Set up PHP ${{ matrix.php }}
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php }}
# coverage: none
# extensions: mbstring
# env:
# update: true
# COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# - name: Checkout code
# uses: actions/checkout@v3

# - name: Get composer cache directory
# id: composer-cache
# run: echo "::set-output name=dir::$(composer config cache-files-dir)"

# - name: Cache dependencies
# uses: actions/cache@v2
# with:
# path: ${{ steps.composer-cache.outputs.dir }}
# key: ${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}
# restore-keys: ${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}

# - name: Install dependencies
# run: composer install --prefer-dist

# - name: Execute Psalm
# run: vendor/bin/psalm --no-progress

pint:
name: Laravel Pint
runs-on: ubuntu-latest
Expand All @@ -183,7 +141,7 @@ jobs:
fail-fast: true
max-parallel: 10
matrix:
php: ["8.0", "8.1"]
php: ["8.0", "8.1", "8.2"]

steps:
- name: Set up PHP ${{ matrix.php }}
Expand Down Expand Up @@ -213,9 +171,6 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist

- name: Install Laravel Pint
run: composer require laravel/pint:* --dev --with-all-dependencies

- name: Execute Laravel Pint
run: vendor/bin/pint --test

Expand All @@ -228,7 +183,7 @@ jobs:
fail-fast: true
max-parallel: 10
matrix:
php: ["7.4", "8.0", "8.1"]
php: ["8.0", "8.1", "8.2"]

steps:
- name: Set up PHP ${{ matrix.php }}
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
],
"homepage": "https://github.com/auth0/laravel-auth0",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"ext-filter": "*",
"ext-json": "*",
"ext-mbstring": "*",
Expand All @@ -42,15 +42,17 @@
},
"require-dev": {
"laravel/laravel": "^8.4.4 || ^9.0",
"laravel/pint": "^1.2",
"nunomaduro/larastan": "^1.0",
"nyholm/psr7": "^1.4",
"orchestra/testbench": "6.25.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.2",
"phpstan/phpstan": "^1.7",
"phpstan/phpstan-strict-rules": "^1.3",
"phpstan/phpstan-strict-rules": "1.4.3",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.13.6",
"symplify/easy-coding-standard": "^11.1",
"wikimedia/composer-merge-plugin": "^2.0"
},
"autoload": {
Expand Down
89 changes: 89 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff;
use PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer;
use PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer;
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer;
use Symplify\CodingStandard\Fixer\LineLength\DocBlockLineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->sets([SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE]);

$ecsConfig->paths([
__DIR__ . '/bin',
__DIR__ . '/src',
__DIR__ . '/packages',
__DIR__ . '/packages-tests',
__DIR__ . '/rules',
__DIR__ . '/rules-tests',
__DIR__ . '/tests',
__DIR__ . '/utils',
__DIR__ . '/config',
__DIR__ . '/ecs.php',
__DIR__ . '/easy-ci.php',
__DIR__ . '/rector.php',
__DIR__ . '/scoper.php',
__DIR__ . '/build/build-preload.php',
]);

$ecsConfig->ruleWithConfiguration(NoSuperfluousPhpdocTagsFixer::class, [
'allow_mixed' => true,
]);

$ecsConfig->ruleWithConfiguration(GeneralPhpdocAnnotationRemoveFixer::class, [
'annotations' => [
'throw',
'throws',
'author',
'authors',
'package',
'group',
'required',
'phpstan-ignore-line',
'phpstan-ignore-next-line',
],
]);

$ecsConfig->rule(StaticLambdaFixer::class);

$ecsConfig->skip([
'*/Source/*',
'*/Fixture/*',
'*/Expected/*',

// buggy - @todo fix on Symplify master
DocBlockLineLengthFixer::class,

PhpdocTypesFixer::class => [
// double to Double false positive
__DIR__ . '/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php',
// skip for enum types
__DIR__ . '/rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php',
],

// breaking and handled better by Rector PHPUnit code quality set, removed in symplify dev-main
PhpUnitStrictFixer::class,

// skip add space on &$variable
FunctionTypehintSpaceFixer::class => [
__DIR__ . '/src/PhpParser/Printer/BetterStandardPrinter.php',
__DIR__ . '/src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php',
__DIR__ . '/rules/Php70/EregToPcreTransformer.php',
],

AssignmentInConditionSniff::class . '.FoundInWhileCondition',

// null on purpose as no change
PhpdocNoEmptyReturnFixer::class => [
__DIR__ . '/rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php',
],
]);
};
12 changes: 0 additions & 12 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ parameters:
- tests/constants.php

ignoreErrors:
- '#Function (app|auth|event|redirect) not found.#'
- '#Constructor in (.*) has parameter (.*) with default value.#'
- '#Constructor in (.*) has parameter (.*) with null as default value.#'
- '#Method (.*) has parameter (.*) with a nullable type declaration.#'
- '#Method (.*) has parameter (.*) with null as default value.#'
- '#Method (.*) has a nullable return type declaration.#'
- '#Language construct isset\(\) should not be used.#'
- '#Method (.*) has parameter (.*) with no value type specified in iterable type array.#'
- '#not allowed to extend#'
- '#Cannot call method (.*) on mixed#'
- '#no value type specified in iterable type array.#'
- '#is not final, but since the containing class is abstract, it should be.#'
- '#Class "Auth0\\Login\\Exception\\(.*)" is not allowed to extend "Exception".#'
- '#Class "Auth0\\Laravel\\Exception\\(.*)" is not allowed to extend "Exception".#'
- '#Call to an undefined method Illuminate\\(.*).#'
- '#\$hash is never read, only written.#'

reportUnmatchedIgnoredErrors: false
58 changes: 36 additions & 22 deletions src/Auth/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function __construct(
/**
* @inheritdoc
*/
public function login(
\Illuminate\Contracts\Auth\Authenticatable $user
): self {
$this->getState()->setUser($user);
public function login(\Illuminate\Contracts\Auth\Authenticatable $user): self
{
$this->getState()
->setUser($user);
return $this;
}

Expand All @@ -42,7 +42,8 @@ public function login(
*/
public function logout(): self
{
$this->getState()->setUser(null);
$this->getState()
->setUser(null);
app(\Auth0\Laravel\Auth0::class)->getSdk()->clear();
return $this;
}
Expand All @@ -68,7 +69,8 @@ public function guest(): bool
*/
public function user(): ?\Illuminate\Contracts\Auth\Authenticatable
{
return $this->getState()->getUser() ?? $this->getUserFromToken() ?? $this->getUserFromSession() ?? null;
return $this->getState()
->getUser() ?? $this->getUserFromToken() ?? $this->getUserFromSession() ?? null;
}

/**
Expand All @@ -79,7 +81,8 @@ public function id()
$response = null;

if ($this->user() !== null) {
$id = $this->user()->getAuthIdentifier();
$id = $this->user()
->getAuthIdentifier();

if (is_string($id) || is_int($id)) {
$response = $id;
Expand All @@ -94,19 +97,18 @@ public function id()
*
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter
*/
public function validate(
array $credentials = []
): bool {
public function validate(array $credentials = []): bool
{
return false;
}

/**
* @inheritdoc
*/
public function setUser(
\Illuminate\Contracts\Auth\Authenticatable $user
): self {
$user = $this->getState()->setUser($user);
public function setUser(\Illuminate\Contracts\Auth\Authenticatable $user): self
{
$user = $this->getState()
->setUser($user);
return $this;
}

Expand All @@ -115,19 +117,18 @@ public function setUser(
*/
public function hasUser(): bool
{
return ! is_null($this->getState()->getUser());
return $this->getState()->getUser() !== null;
}

/**
* @inheritdoc
*/
public function hasScope(
string $scope
): bool {
public function hasScope(string $scope): bool
{
$state = $this->getState();
return in_array($scope, $state->getAccessTokenScope() ?? [], true);
}

/**
* Always returns false to keep third-party apps happy
*/
Expand All @@ -151,14 +152,25 @@ private function getUserFromToken(): ?\Illuminate\Contracts\Auth\Authenticatable

try {
// Attempt to decode the bearer token.
$decoded = app(\Auth0\Laravel\Auth0::class)->getSdk()->decode($token, null, null, null, null, null, null, \Auth0\SDK\Token::TYPE_TOKEN)->toArray();
$decoded = app(\Auth0\Laravel\Auth0::class)->getSdk()->decode(
$token,
null,
null,
null,
null,
null,
null,
\Auth0\SDK\Token::TYPE_TOKEN
)->toArray();
} catch (\Auth0\SDK\Exception\InvalidTokenException $invalidToken) {
// Invalid bearer token.
return null;
}

// Query the UserProvider to retrieve tue user for the token.
$user = $this->getProvider()->getRepository()->fromAccessToken($decoded);
$user = $this->getProvider()
->getRepository()
->fromAccessToken($decoded);

// Was a user retrieved successfully?
if ($user !== null) {
Expand Down Expand Up @@ -195,7 +207,9 @@ private function getUserFromSession(): ?\Illuminate\Contracts\Auth\Authenticatab
}

// Query the UserProvider to retrieve tue user for the session.
$user = $this->getProvider()->getRepository()->fromSession($session->user);
$user = $this->getProvider()
->getRepository()
->fromSession($session->user);

// Was a user retrieved successfully?
if ($user !== null) {
Expand Down
Loading