Skip to content

Commit

Permalink
Merge pull request #40 from humhub/enh/39-well-known-files
Browse files Browse the repository at this point in the history
Add possibility to profile `.well-known` files
  • Loading branch information
luke- authored Jun 24, 2024
2 parents 94ef419 + 921d0cb commit 343eef6
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 17 deletions.
40 changes: 40 additions & 0 deletions components/UrlRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\fcmPush\components;

use humhub\modules\fcmPush\services\WellKnownService;
use yii\base\Component;
use yii\web\UrlRuleInterface;

class UrlRule extends Component implements UrlRuleInterface
{
/**
* @inheritdoc
*/
public function createUrl($manager, $route, $params)
{
if ($route === trim(WellKnownService::URL_ROUTE, '/') && isset($params['file'])) {
return WellKnownService::URL_PREFIX . $params['file'];
}

return false;
}

/**
* @inheritdoc
*/
public function parseRequest($manager, $request)
{
$path = $request->getPathInfo();
if (str_starts_with($path, WellKnownService::URL_PREFIX)) {
return WellKnownService::instance($path)->getRuleRoute() ?? false;
}

return false;
}
}
6 changes: 4 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
//[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']]
],
'consoleControllerMap' => [
'firebase' => 'humhub\modules\fcmPush\commands\SendController'
'firebase' => 'humhub\modules\fcmPush\commands\SendController',
],
'urlManagerRules' => [
['class' => 'humhub\modules\fcmPush\components\UrlRule'],
],
];
?>
19 changes: 19 additions & 0 deletions controllers/WellKnownController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\fcmPush\controllers;

use humhub\components\Controller;
use humhub\modules\fcmPush\services\WellKnownService;

class WellKnownController extends Controller
{
public function actionIndex(string $file)
{
return WellKnownService::instance($file)->renderFile();
}
}
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.0.3 (Unreleased)
-----------------------
- Enh: Endpoint to test push status
- Enh: Add possibility to profile `.well-known` files

2.0.2 (May 28, 2024)
-----------------------
Expand Down
41 changes: 34 additions & 7 deletions models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace humhub\modules\fcmPush\models;

use humhub\modules\fcmPush\Module;
use humhub\modules\fcmPush\services\WellKnownService;
use humhub\widgets\Link;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\Model;
Expand All @@ -24,6 +26,10 @@ class ConfigureForm extends Model

public $disableAuthChoicesIos;

public $fileAssetLinks;

public $fileAppleAssociation;

/**
* Validate JSON field params
*
Expand All @@ -39,8 +45,7 @@ private function validateJsonParams($arrayPattern, $arrayCheck)
if (isset($arrayCheck[$key])) {
if (empty($arrayCheck[$key])) {
$errors["empty"] .= $errors["empty"] == "" ? "\"$key\"" : ", \"$key\"";
}
else {
} else {
$condition = false;
switch ($value['type']) {
case "string":
Expand Down Expand Up @@ -85,6 +90,7 @@ public function rules()
[['enableEmailGoService', 'disableAuthChoicesIos'], 'boolean'],
[['senderId'], 'number'],
[['serverKey', 'json', 'humhubApiKey'], 'safe'],
[['fileAssetLinks', 'fileAppleAssociation'], 'string'],
['json', function ($attribute, $params, $validator) {
if (empty($this->$attribute)) {
return;
Expand Down Expand Up @@ -141,15 +147,33 @@ public function attributeLabels()
'json' => Yii::t('FcmPushModule.base', 'Service Account (JSON file)'),
'serverKey' => Yii::t('FcmPushModule.base', 'Cloud Messaging API (Legacy)'),
'disableAuthChoicesIos' => Yii::t('FcmPushModule.base', 'Disable AuthChoices on iOS App'),
'fileAssetLinks' => Yii::t('FcmPushModule.base', 'Well-known file {fileName}', [
'fileName' => '"' . WellKnownService::getFileName('fileAssetLinks') . '"',
]),
'fileAppleAssociation' => Yii::t('FcmPushModule.base', 'Well-known file {fileName}', [
'fileName' => '"' . WellKnownService::getFileName('fileAppleAssociation') . '"',
]),
];
}

public function attributeHints()
{
return [
'humhubInstallId' => 'Use this ID to register your API Key.',
'serverKey' => 'Please switch to the new "Firebase Cloud Messaging API (V1)" and enter a JSON file in the field above. The old legacy API is only temporarily available for existing installations and is no longer supported or maintained. ',
'json' => 'Paste the content of the service account JSON files here. You can find more information in the module instructions.'
'humhubInstallId' => Yii::t('FcmPushModule.base', 'Use this ID to register your API Key.'),
'serverKey' => Yii::t('FcmPushModule.base', 'Please switch to the new "Firebase Cloud Messaging API (V1)" and enter a JSON file in the field above. The old legacy API is only temporarily available for existing installations and is no longer supported or maintained.'),
'json' => Yii::t('FcmPushModule.base', 'Paste the content of the service account JSON files here. You can find more information in the module instructions.'),
'fileAssetLinks' => Yii::t('FcmPushModule.base', 'URL to the file {fileNameLink}', [
'fileNameLink' => Link::to(
WellKnownService::getFileName('fileAssetLinks'),
WellKnownService::getFileRoute('fileAssetLinks'),
)->target('_blank'),
]),
'fileAppleAssociation' => Yii::t('FcmPushModule.base', 'URL to the file {fileNameLink}', [
'fileNameLink' => Link::to(
WellKnownService::getFileName('fileAppleAssociation'),
WellKnownService::getFileRoute('fileAppleAssociation'),
)->target('_blank'),
]),
];
}

Expand All @@ -170,7 +194,8 @@ public function loadSettings()
$this->serverKey = $settings->get('serverKey');
$this->humhubApiKey = $settings->get('humhubApiKey');
$this->disableAuthChoicesIos = $settings->get('disableAuthChoicesIos');

$this->fileAssetLinks = $settings->get('fileAssetLinks');
$this->fileAppleAssociation = $settings->get('fileAppleAssociation');

return true;
}
Expand All @@ -186,6 +211,8 @@ public function saveSettings()
$module->settings->set('serverKey', $this->serverKey);
$module->settings->set('humhubApiKey', $this->humhubApiKey);
$module->settings->set('disableAuthChoicesIos', $this->disableAuthChoicesIos);
$module->settings->set('fileAssetLinks', $this->fileAssetLinks);
$module->settings->set('fileAppleAssociation', $this->fileAppleAssociation);

return true;
}
Expand All @@ -197,7 +224,7 @@ public function getJsonAsArray()

public static function getInstance()
{
$config = new static;
$config = new static();
$config->loadSettings();

return $config;
Expand Down
83 changes: 83 additions & 0 deletions services/WellKnownService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\fcmPush\services;

use Exception;
use humhub\modules\fcmPush\Module;
use Yii;
use yii\helpers\Json;
use yii\web\Response;

class WellKnownService
{
public const URL_ROUTE = '/fcm-push/well-known';
public const URL_PREFIX = '.well-known/';
public const ALLOWED_FILES = [
'fileAssetLinks' => 'assetlinks.json',
'fileAppleAssociation' => 'apple-app-site-association',
];

private ?string $file;

public function __construct(string $path)
{
$this->file = preg_replace('#^' . preg_quote(self::URL_PREFIX) . '#', '', $path);
}

public static function instance(string $path): self
{
return new self($path);
}

public static function getFileName(string $settingName): string
{
return self::ALLOWED_FILES[$settingName] ?? '';
}

public static function getFileRoute(string $settingName): array
{
return [self::URL_ROUTE, 'file' => self::getFileName($settingName)];
}

public function isAllowed(): bool
{
return in_array($this->file, self::ALLOWED_FILES);
}

public function getRuleRoute(): ?array
{
return $this->isAllowed() ? [self::URL_ROUTE, ['file' => $this->file]] : null;
}

public function getFileContent(): string
{
$settingName = array_search($this->file, self::ALLOWED_FILES);
if ($settingName === false) {
return '';
}

/* @var Module $module */
$module = Yii::$app->getModule('fcm-push');

return $module->settings->get($settingName, '');
}

public function renderFile(): Response
{
Yii::$app->response->format = Response::FORMAT_JSON;

try {
Yii::$app->response->data = Json::decode($this->getFileContent());
} catch (Exception $ex) {
Yii::$app->response->data = '';
Yii::error('Wrong file format "' . $this->file . '". Error: ' . $ex->getMessage(), 'fcm-push');
}

return Yii::$app->response;
}
}
24 changes: 16 additions & 8 deletions views/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/* @var $this \humhub\modules\ui\view\components\View */

/* @var $model \humhub\modules\fcmPush\models\ConfigureForm */

use humhub\modules\fcmPush\models\ConfigureForm;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\widgets\Button;
use humhub\modules\ui\form\widgets\ActiveForm;
use yii\helpers\Html;

/* @var $model ConfigureForm */
?>
<div class="panel panel-default">
<div class="panel-heading"><?= Yii::t('FcmPushModule.base', '<strong>FireBase Messaging</strong> Configuration'); ?></div>
Expand All @@ -16,7 +16,7 @@
<h3><?= Yii::t('FcmPushModule.base', 'Link Redirection Service') ?></h3>
<?= $form->field($model, 'enableEmailGoService')->checkbox()
->label(Yii::t('FcmPushModule.base', 'Enable Link Redirection Service. In order for links to open in the app on mobile devices, rather than in the mobile browser, all links (e.g. notification emails) need to be routed through the HumHub proxy server. (Experimental Features // <a href="{url}">Privacy Policy</a>)', [
'url' => 'https://www.humhub.com/en/privacy/'
'url' => 'https://www.humhub.com/en/privacy/',
])) ?>

<hr>
Expand Down Expand Up @@ -49,19 +49,27 @@
<?= $form->beginCollapsibleFields('Advanced Settings'); ?>
<?= $form->field($model, 'disableAuthChoicesIos')->checkbox()
->label(Yii::t('FcmPushModule.base', 'Hide third-party login options for app users with iOS.')) ?>

<?php if (!Yii::$app->urlManager->enablePrettyUrl) : ?>
<div class="alert alert-warning">
<?= Icon::get('warning') ?>
<?= Yii::t('FcmPushModule.base', 'Please enable <a {attrs}>Pretty URLs</a> for proper working of the well-known files.', [
'attrs' => 'href="https://docs.humhub.org/docs/admin/installation/#pretty-urls" target="_blank"',
]) ?>
</div>
<?php endif; ?>

<?= $form->field($model, 'fileAssetLinks')->textarea(['rows' => 10]) ?>
<?= $form->field($model, 'fileAppleAssociation')->textarea(['rows' => 10]) ?>
<?= $form->endCollapsibleFields(); ?>
<br/>

<div class="form-group">
<?= Html::submitButton(Yii::t('base', 'Save'), ['class' => 'btn btn-primary', 'data-ui-loader' => '']) ?>
</div>


<?php ActiveForm::end(); ?>




<?= Html::a('Mobile App Debug', ['/fcm-push/mobile-app'], ['class' => 'pull-right']); ?>
</div>
</div>

0 comments on commit 343eef6

Please sign in to comment.