Skip to content

Commit

Permalink
feat: add optional debug logging (#68)
Browse files Browse the repository at this point in the history
* chore: switch workflows to 1.x branch

* feat: oauth debug toggle

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 27, 2023
1 parent 2d1c8dc commit ec71db2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@1.x
with:
enable_backend_testing: false
enable_phpstan: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@main
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@1.x
with:
enable_bundlewatch: false
enable_prettier: true
Expand Down
3 changes: 2 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
->default('fof-oauth.fullscreenPopup', true)
->serializeToForum('fof-oauth.popupWidth', 'fof-oauth.popupWidth', 'intval')
->serializeToForum('fof-oauth.popupHeight', 'fof-oauth.popupHeight', 'intval')
->serializeToForum('fof-oauth.fullscreenPopup', 'fof-oauth.fullscreenPopup', 'boolVal'),
->serializeToForum('fof-oauth.fullscreenPopup', 'fof-oauth.fullscreenPopup', 'boolVal')
->default('fof-oauth.log-oauth-errors', false),

(new Extend\Event())
->listen(OAuthLoginSuccessful::class, Listeners\UpdateEmailFromProvider::class)
Expand Down
12 changes: 12 additions & 0 deletions js/src/admin/components/AuthSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ export default class AuthSettingsPage extends ExtensionPage {

{this.providerSettingsItems().toArray()}

<hr />

<div className="AuthSettingsPage--advanced">
<h4>{app.translator.trans('fof-oauth.admin.settings.advanced.heading')}</h4>
{this.buildSettingComponent({
type: 'boolean',
setting: 'fof-oauth.log-oauth-errors',
label: app.translator.trans('fof-oauth.admin.settings.advanced.log-oauth-errors-label'),
help: app.translator.trans('fof-oauth.admin.settings.advanced.log-oauth-errors-help'),
})}
</div>

{this.submitButton()}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ fof-oauth:
settings_accessibility_label: "{name} settings"

settings:
advanced:
heading: Advanced
log-oauth-errors-label: Log OAuth errors
log-oauth-errors-help: If enabled, OAuth errors will be logged to the Flarum log. This may help with debugging OAuth issues, but may also contain sensitive information.
only_icons_label: Only show the Log In Button icons (alternative layout)
update_email_from_provider_label: Update email address from provider
update_email_from_provider_help: If enabled, the user's email address will be updated to match the one provided by the OAuth provider on each login to the forum. Not all providers provide the updated email, in which case this setting will not have any effect with those providers.
Expand Down
39 changes: 39 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,38 @@
namespace FoF\OAuth;

use Exception;
use Flarum\Forum\Auth\ResponseFactory;
use Flarum\Http\Exception\RouteNotFoundException;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\Extend\Controllers\AbstractOAuthController;
use FoF\OAuth\Errors\AuthenticationException;
use Illuminate\Contracts\Cache\Store as CacheStore;
use Illuminate\Contracts\Events\Dispatcher;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

abstract class Controller extends AbstractOAuthController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

public function __construct(
ResponseFactory $response,
SettingsRepositoryInterface $settings,
UrlGenerator $url,
Dispatcher $events,
CacheStore $cache,
) {
parent::__construct($response, $settings, $url, $events, $cache);

$this->settings = $settings;
}

protected function getRouteName(): string
{
return 'auth.'.$this->getProviderName();
Expand All @@ -47,6 +70,22 @@ public function handle(ServerRequestInterface $request): ResponseInterface
try {
return parent::handle($request);
} catch (Exception $e) {
if ((bool) $this->settings->get('fof-oauth.log-oauth-errors')) {
/** @var LoggerInterface $logger */
$logger = resolve('log');
$detail = json_encode([
'server_params' => $request->getServerParams(),
'request_attrs' => $request->getAttributes(),
'cookie_params' => $request->getCookieParams(),
'query_params' => $request->getQueryParams(),
'parsed_body' => $request->getParsedBody(),
'code' => $e->getCode(),
'trace' => $e->getTraceAsString(),
], JSON_PRETTY_PRINT);

$logger->error("[OAuth][{$this->getProviderName()}] {$e->getMessage()}: {$detail}");
}

if ($e->getMessage() === 'Invalid state' || $e instanceof IdentityProviderException) {
throw new AuthenticationException($e->getMessage());
}
Expand Down

0 comments on commit ec71db2

Please sign in to comment.