From 8277b1dd36e31f07dd6f235cbe631e9764eec3d5 Mon Sep 17 00:00:00 2001 From: Italo Date: Wed, 27 Jul 2022 14:22:05 -0400 Subject: [PATCH] Fixes static analysis --- .../Creator/Pipes/MayRetrieveCredentialsIdForUser.php | 2 +- src/Attestation/AuthenticatorData.php | 8 ++++++++ src/Attestation/Creator/Pipes/AddUserDescriptor.php | 1 + .../Creator/Pipes/MayPreventDuplicateCredentials.php | 1 + src/Auth/WebAuthnUserProvider.php | 1 + src/ByteBuffer.php | 10 +++++----- src/CborDecoder.php | 1 + src/Challenge.php | 2 +- src/Contracts/WebAuthnAuthenticatable.php | 2 +- src/Exceptions/AssertionException.php | 2 +- src/Exceptions/AttestationException.php | 2 +- src/Http/Requests/AssertedRequest.php | 3 ++- src/Http/Requests/AssertionRequest.php | 2 ++ src/Models/WebAuthnCredential.php | 10 ++++++---- src/WebAuthnServiceProvider.php | 1 + 15 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Assertion/Creator/Pipes/MayRetrieveCredentialsIdForUser.php b/src/Assertion/Creator/Pipes/MayRetrieveCredentialsIdForUser.php index 77d0824..42977da 100644 --- a/src/Assertion/Creator/Pipes/MayRetrieveCredentialsIdForUser.php +++ b/src/Assertion/Creator/Pipes/MayRetrieveCredentialsIdForUser.php @@ -36,7 +36,7 @@ class MayRetrieveCredentialsIdForUser * Adapt all credentials into an `allowCredentials` digestible array. * * @param \Illuminate\Database\Eloquent\Collection $credentials - * @return Illuminate\Support\Collection}> + * @return \Illuminate\Support\Collection}> */ protected function parseCredentials(EloquentCollection $credentials): Collection { diff --git a/src/Attestation/AuthenticatorData.php b/src/Attestation/AuthenticatorData.php index ea26191..ccea326 100644 --- a/src/Attestation/AuthenticatorData.php +++ b/src/Attestation/AuthenticatorData.php @@ -366,13 +366,21 @@ class AuthenticatorData protected static function readFlags(string $binFlag): object { $flags = (object) [ + // @phpstan-ignore-next-line 'bit_0' => (bool) ($binFlag & 1), + // @phpstan-ignore-next-line 'bit_1' => (bool) ($binFlag & 2), + // @phpstan-ignore-next-line 'bit_2' => (bool) ($binFlag & 4), + // @phpstan-ignore-next-line 'bit_3' => (bool) ($binFlag & 8), + // @phpstan-ignore-next-line 'bit_4' => (bool) ($binFlag & 16), + // @phpstan-ignore-next-line 'bit_5' => (bool) ($binFlag & 32), + // @phpstan-ignore-next-line 'bit_6' => (bool) ($binFlag & 64), + // @phpstan-ignore-next-line 'bit_7' => (bool) ($binFlag & 128), 'userPresent' => false, 'userVerified' => false, diff --git a/src/Attestation/Creator/Pipes/AddUserDescriptor.php b/src/Attestation/Creator/Pipes/AddUserDescriptor.php index e3f0852..d1ba4d3 100644 --- a/src/Attestation/Creator/Pipes/AddUserDescriptor.php +++ b/src/Attestation/Creator/Pipes/AddUserDescriptor.php @@ -23,6 +23,7 @@ class AddUserDescriptor $config = $attestable->user->webAuthnData(); // Create a new User UUID if it doesn't existe already in the credentials. + // @phpstan-ignore-next-line $config['id'] = $attestable->user->webAuthnCredentials()->value('user_id') ?: Str::uuid()->getHex()->toString(); diff --git a/src/Attestation/Creator/Pipes/MayPreventDuplicateCredentials.php b/src/Attestation/Creator/Pipes/MayPreventDuplicateCredentials.php index 71ee893..4f90068 100644 --- a/src/Attestation/Creator/Pipes/MayPreventDuplicateCredentials.php +++ b/src/Attestation/Creator/Pipes/MayPreventDuplicateCredentials.php @@ -39,6 +39,7 @@ class MayPreventDuplicateCredentials return $user ->webAuthnCredentials() ->get(['id', 'transports']) + // @phpstan-ignore-next-line ->map(static function (WebAuthnCredential $credential): array { return array_filter([ 'id'=> $credential->getKey(), diff --git a/src/Auth/WebAuthnUserProvider.php b/src/Auth/WebAuthnUserProvider.php index dd4e7c0..9a2b68d 100644 --- a/src/Auth/WebAuthnUserProvider.php +++ b/src/Auth/WebAuthnUserProvider.php @@ -50,6 +50,7 @@ class WebAuthnUserProvider extends EloquentUserProvider /** @noinspection PhpIncompatibleReturnTypeInspection */ return $this->newModelQuery() ->whereHas('webAuthnCredentials', static function (Builder $query) use ($credentials): void { + // @phpstan-ignore-next-line $query->whereKey($credentials['id'])->whereEnabled(); }) ->first(); diff --git a/src/ByteBuffer.php b/src/ByteBuffer.php index f386ede..f1db6fc 100644 --- a/src/ByteBuffer.php +++ b/src/ByteBuffer.php @@ -86,7 +86,7 @@ class ByteBuffer implements JsonSerializable, Jsonable, Stringable * @param string $binaryData * @param int $dataLength */ - public function __construct(protected string $binaryData, protected int $dataLength = 0) + final public function __construct(protected string $binaryData, protected int $dataLength = 0) { $this->dataLength = strlen($binaryData); } @@ -194,7 +194,7 @@ class ByteBuffer implements JsonSerializable, Jsonable, Stringable * Returns the value of a single unsigned 16-bit integer. * * @param int $offset - * @return mixed + * @return int */ public function getUint16Val(int $offset = 0): int { @@ -209,7 +209,7 @@ class ByteBuffer implements JsonSerializable, Jsonable, Stringable * Returns the value of a single unsigned 32-bit integer. * * @param int $offset - * @return mixed + * @return int */ public function getUint32Val(int $offset = 0): int { @@ -404,7 +404,7 @@ class ByteBuffer implements JsonSerializable, Jsonable, Stringable throw new InvalidArgumentException('ByteBuffer: Invalid base64 url string'); } - return new ByteBuffer($bin); + return new static($bin); } /** @@ -419,7 +419,7 @@ class ByteBuffer implements JsonSerializable, Jsonable, Stringable throw new InvalidArgumentException('ByteBuffer: Invalid base64 string'); } - return new ByteBuffer($bin); + return new static($bin); } /** diff --git a/src/CborDecoder.php b/src/CborDecoder.php index 6e4459c..db752e0 100644 --- a/src/CborDecoder.php +++ b/src/CborDecoder.php @@ -140,6 +140,7 @@ class CborDecoder try { return static::parseItemData($type, $val, $buf, $offset); + // @phpstan-ingnore-next-line } catch (InvalidArgumentException $e) { throw new DataException($e->getMessage()); } diff --git a/src/Challenge.php b/src/Challenge.php index 92e899c..ebdfd49 100644 --- a/src/Challenge.php +++ b/src/Challenge.php @@ -17,7 +17,7 @@ class Challenge * @param bool $verify * @param array $properties */ - public function __construct( + final public function __construct( public ByteBuffer $data, public int $timeout, public bool $verify = true, diff --git a/src/Contracts/WebAuthnAuthenticatable.php b/src/Contracts/WebAuthnAuthenticatable.php index ec61d43..dccaa1a 100644 --- a/src/Contracts/WebAuthnAuthenticatable.php +++ b/src/Contracts/WebAuthnAuthenticatable.php @@ -41,7 +41,7 @@ interface WebAuthnAuthenticatable /** * Returns a queryable relationship for its WebAuthn Credentials. * - * @return \Illuminate\Database\Eloquent\Relations\MorphMany&\Laragear\WebAuthn\Models\WebAuthnCredential + * @return \Illuminate\Database\Eloquent\Relations\MorphMany|\Laragear\WebAuthn\Models\WebAuthnCredential */ public function webAuthnCredentials(): MorphMany; } diff --git a/src/Exceptions/AssertionException.php b/src/Exceptions/AssertionException.php index afbb5f2..8536b12 100644 --- a/src/Exceptions/AssertionException.php +++ b/src/Exceptions/AssertionException.php @@ -11,7 +11,7 @@ class AssertionException extends ValidationException implements WebAuthnExceptio * Create a new Assertion Exception with the error message. * * @param string $message - * @return \Laragear\WebAuthn\Exceptions\AssertionException + * @return static */ public static function make(string $message): static { diff --git a/src/Exceptions/AttestationException.php b/src/Exceptions/AttestationException.php index 3853532..14e7abc 100644 --- a/src/Exceptions/AttestationException.php +++ b/src/Exceptions/AttestationException.php @@ -11,7 +11,7 @@ class AttestationException extends ValidationException implements WebAuthnExcept * Create a new Attestation Exception with the error message. * * @param string $message - * @return \Laragear\WebAuthn\Exceptions\AttestationException + * @return static */ public static function make(string $message): static { diff --git a/src/Http/Requests/AssertedRequest.php b/src/Http/Requests/AssertedRequest.php index 59c88ba..a912890 100644 --- a/src/Http/Requests/AssertedRequest.php +++ b/src/Http/Requests/AssertedRequest.php @@ -48,10 +48,11 @@ class AssertedRequest extends FormRequest * Logs in the user for this assertion request. * * @param string|null $guard - * @return \Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable&\Illuminate\Contracts\Auth\Authenticatable|null + * @return \Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable|\Illuminate\Contracts\Auth\Authenticatable|null */ public function login(string $guard = null, bool $remember = null, bool $destroySession = false): ?WebAuthnAuthenticatable { + /** @var \Illuminate\Contracts\Auth\StatefulGuard $guard */ $auth = Auth::guard($guard); if ($auth->attempt($this->validated(), $remember ?? $this->hasRemember())) { diff --git a/src/Http/Requests/AssertionRequest.php b/src/Http/Requests/AssertionRequest.php index 81c5256..c6c750d 100644 --- a/src/Http/Requests/AssertionRequest.php +++ b/src/Http/Requests/AssertionRequest.php @@ -133,7 +133,9 @@ class AssertionRequest extends FormRequest // retrieve by its ID, otherwise we will fall back to credentials. Once done, we // will check it uses WebAuthn if is not null, otherwise we'll fail miserably. $user = is_string($credentials) || is_int($credentials) + // @phpstan-ignore-next-line ? Auth::guard($this->guard)->getProvider()->retrieveById($credentials) + // @phpstan-ignore-next-line : Auth::guard($this->guard)->getProvider()->retrieveByCredentials($credentials); if ($user && ! $user instanceof WebAuthnAuthenticatable) { diff --git a/src/Models/WebAuthnCredential.php b/src/Models/WebAuthnCredential.php index 48c8f98..c5796bb 100644 --- a/src/Models/WebAuthnCredential.php +++ b/src/Models/WebAuthnCredential.php @@ -9,7 +9,7 @@ use Laragear\WebAuthn\Events\CredentialDisabled; use Laragear\WebAuthn\Events\CredentialEnabled; /** - * @mixin \Illuminate\Database\Eloquent\Builder<\Laragear\WebAuthn\Models\WebAuthnCredential> + * @mixin \Illuminate\Database\Eloquent\Builder * * @method static \Illuminate\Database\Eloquent\Builder|static query() * @method \Illuminate\Database\Eloquent\Builder|static newQuery() @@ -22,10 +22,10 @@ use Laragear\WebAuthn\Events\CredentialEnabled; * @method \Laragear\WebAuthn\Models\WebAuthnCredential firstOr($columns = ['*'], \Closure $callback = null) * @method \Laragear\WebAuthn\Models\WebAuthnCredential firstWhere($column, $operator = null, $value = null, $boolean = 'and') * @method \Laragear\WebAuthn\Models\WebAuthnCredential updateOrCreate(array $attributes, array $values = []) - * @method static|null first($columns = ['*']) + * @method ?static first($columns = ['*']) * @method static static findOrFail($id, $columns = ['*']) * @method static static findOrNew($id, $columns = ['*']) - * @method static static|null find($id, $columns = ['*']) + * @method static ?null find($id, $columns = ['*']) * * @property-read string $id * @@ -98,7 +98,7 @@ class WebAuthnCredential extends Model protected $visible = ['id', 'origin', 'alias', 'aaguid', 'attestation_format', 'disabled_at', 'is_enabled']; /** - * @return \Illuminate\Database\Eloquent\Relations\MorphTo&\Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable + * @return \Illuminate\Database\Eloquent\Relations\MorphTo|\Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable */ public function authenticatable(): MorphTo { @@ -113,6 +113,7 @@ class WebAuthnCredential extends Model */ protected function scopeWhereEnabled(Builder $query): Builder { + // @phpstan-ignore-next-line return $query->whereNull('disabled_at'); } /** @@ -123,6 +124,7 @@ class WebAuthnCredential extends Model */ protected function scopeWhereDisabled(Builder $query): Builder { + // @phpstan-ignore-next-line return $query->whereNotNull('disabled_at'); } diff --git a/src/WebAuthnServiceProvider.php b/src/WebAuthnServiceProvider.php index 4fc7984..7ca9ed4 100644 --- a/src/WebAuthnServiceProvider.php +++ b/src/WebAuthnServiceProvider.php @@ -45,6 +45,7 @@ class WebAuthnServiceProvider extends ServiceProvider if ($this->app->runningInConsole()) { $this->publishesMigrations(static::MIGRATIONS); $this->publishes([static::ROUTES => $this->app->basePath('routes/webauthn.php')], 'routes'); + // @phpstan-ignore-next-line $this->publishes([static::CONTROLLERS => $this->app->path('Http/Controllers/WebAuthn')], 'controllers'); $this->publishes([static::JS => $this->app->resourcePath('js/vendor/webauthn')], 'js'); }