Skip to content

Commit

Permalink
feat: Allow logging channel configuration for webauthn (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
iv-craig authored Sep 4, 2023
1 parent 270e384 commit 17bd9b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
14 changes: 14 additions & 0 deletions config/webauthn.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@
'register' => 'webauthn::register',
],

/*
|--------------------------------------------------------------------------
| Webauthn logging
|--------------------------------------------------------------------------
|
| Here you may specify the channel to which Webauthn will log messages.
| This value should correspond with one of your loggers that is already
| present in your "logging" configuration file. If left as null, it will
| use the default logger for the application.
|
*/

'log' => null,

/*
|--------------------------------------------------------------------------
| Session name
Expand Down
3 changes: 1 addition & 2 deletions src/Services/Webauthn/CredentialAssertionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Auth\Authenticatable as User;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use LaravelWebauthn\Exceptions\ResponseMismatchException;
use Psr\Http\Message\ServerRequestInterface;
use Webauthn\AuthenticatorAssertionResponse;
Expand Down Expand Up @@ -76,7 +75,7 @@ protected function pullPublicKey(User $user): PublicKeyCredentialRequestOptions
try {
return PublicKeyCredentialRequestOptions::createFromArray($this->cache->pull($this->cacheKey($user)));
} catch (\Exception $e) {
Log::debug('Webauthn publickKey deserialize error', ['exception' => $e]);
app('webauthn.log')->debug('Webauthn publickKey deserialize error', ['exception' => $e]);
abort(404);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Services/Webauthn/CredentialAttestationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Contracts\Auth\Authenticatable as User;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use LaravelWebauthn\Exceptions\ResponseMismatchException;
use Psr\Http\Message\ServerRequestInterface;
use Webauthn\AuthenticatorAttestationResponse;
Expand Down Expand Up @@ -73,7 +72,7 @@ protected function pullPublicKey(User $user): PublicKeyCredentialCreationOptions
try {
return PublicKeyCredentialCreationOptions::createFromArray($this->cache->pull($this->cacheKey($user)));
} catch (\Exception $e) {
Log::debug('Webauthn publicKey deserialize error', ['exception' => $e]);
app('webauthn.log')->debug('Webauthn publicKey deserialize error', ['exception' => $e]);
abort(404);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/WebauthnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use LaravelWebauthn\Auth\EloquentWebAuthnProvider;
use LaravelWebauthn\Contracts\DestroyResponse as DestroyResponseContract;
Expand Down Expand Up @@ -93,6 +92,7 @@ public function register()
);

$this->app->bind(StatefulGuard::class, fn () => Auth::guard(config('webauthn.guard', null)));
$this->app->bind('webauthn.log', fn ($app) => $app['log']->channel(config('webauthn.log', config('logging.default'))));
}

/**
Expand Down Expand Up @@ -184,13 +184,13 @@ protected function bindWebAuthnPackage(): void
fn ($app) => tap(new AttestationObjectLoader(
$app[AttestationStatementSupportManager::class]
), function ($loader) use ($app) {
$loader->setLogger($app['log']);
$loader->setLogger($app['webauthn.log']);
})
);

$this->app->bind(
CounterChecker::class,
fn ($app) => new ThrowExceptionIfInvalid($app['log'])
fn ($app) => new ThrowExceptionIfInvalid($app['webauthn.log'])
);

$this->app->bind(
Expand All @@ -201,7 +201,7 @@ protected function bindWebAuthnPackage(): void
null,
$app[ExtensionOutputCheckerHandler::class]
), function ($responseValidator) use ($app) {
$responseValidator->setLogger($app['log']);
$responseValidator->setLogger($app['webauthn.log']);
})
);
$this->app->bind(
Expand All @@ -213,7 +213,7 @@ protected function bindWebAuthnPackage(): void
$app[CoseAlgorithmManager::class]
), function ($responseValidator) use ($app) {
$responseValidator->setCounterChecker($app[CounterChecker::class])
->setLogger($app['log']);
->setLogger($app['webauthn.log']);
})
);
$this->app->bind(
Expand Down Expand Up @@ -242,7 +242,7 @@ protected function bindWebAuthnPackage(): void
fn ($app) => tap(new PublicKeyCredentialLoader(
$app[AttestationObjectLoader::class]
), function ($loader) use ($app) {
$loader->setLogger($app['log']);
$loader->setLogger($app['webauthn.log']);
})
);

Expand Down Expand Up @@ -292,7 +292,7 @@ protected function bindPsrInterfaces(): void
return Psr18ClientDiscovery::find();
// @codeCoverageIgnoreStart
} catch (NotFoundException $e) {
Log::error('Could not find PSR-18 Client Factory.', ['exception' => $e]);
app('webauthn.log')->error('Could not find PSR-18 Client Factory.', ['exception' => $e]);
throw new BindingResolutionException('Unable to resolve PSR-18 Client Factory. Please install a psr/http-client-implementation implementation like \'guzzlehttp/guzzle\'.');
}
}
Expand All @@ -307,7 +307,7 @@ protected function bindPsrInterfaces(): void
return Psr17FactoryDiscovery::findRequestFactory();
// @codeCoverageIgnoreStart
} catch (NotFoundException $e) {
Log::error('Could not find PSR-17 Request Factory.', ['exception' => $e]);
app('webauthn.log')->error('Could not find PSR-17 Request Factory.', ['exception' => $e]);
throw new BindingResolutionException('Unable to resolve PSR-17 Request Factory. Please install psr/http-factory-implementation implementation like \'guzzlehttp/psr7\'.');
}
}
Expand Down Expand Up @@ -350,7 +350,7 @@ protected function bindPsrInterfaces(): void
return new PsrHttpFactory($serverRequestFactory, $streamFactory, $uploadFileFactory, $responseFactory);
// @codeCoverageIgnoreStart
} catch (NotFoundException $e) {
Log::error('Could not find PSR-17 Factory.', ['exception' => $e]);
app('webauthn.log')->error('Could not find PSR-17 Factory.', ['exception' => $e]);
}
}

Expand Down

0 comments on commit 17bd9b3

Please sign in to comment.