Skip to content
Closed
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
43 changes: 41 additions & 2 deletions lib/SitesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace OCA\External;

use DateTime;
use OCA\External\Exceptions\GroupNotFoundException;
use OCA\External\Exceptions\IconNotFoundException;
use OCA\External\Exceptions\InvalidDeviceException;
Expand Down Expand Up @@ -106,6 +107,41 @@ public function getSiteById($id) {
throw new SiteNotFoundException();
}

/**
* @param string $data
* @return string
*/
public static function base64UrlEncode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

/**
* @param array $payload
* @return string
*/
public function buildJwt($payload) {
$secret = $this->config->getSystemValue('external_jwt_secret');

if ($secret === '') {
return '';
}

$now = (new DateTime())->getTimestamp();
$payload = array_merge($payload, ['iat' => $now]);

$headers = ['alg' => 'HS256', 'typ' => 'JWT'];
$headersEncoded = self::base64UrlEncode(json_encode($headers));

$payloadEncoded = self::base64UrlEncode(json_encode($payload));

$sig = hash_hmac('SHA256', "$headersEncoded.$payloadEncoded", $secret, true);
$sigEncoded = self::base64UrlEncode($sig);

$jwt = "$headersEncoded.$payloadEncoded.$sigEncoded";

return $jwt;
}

/**
* @return array[]
*/
Expand All @@ -125,6 +161,9 @@ public function getSitesToDisplay() {
$uid = $user instanceof IUser ? $user->getUID() : '';
$displayName = $user instanceof IUser ? $user->getDisplayName() : '';

$payload = ['email' => $email, 'uid' => $uid, 'displayName' => $displayName];
$jwt = $this->buildJwt($payload);

$langSites = [];
foreach ($sites as $id => $site) {
if ($site['lang'] !== '' && $site['lang'] !== $lang) {
Expand All @@ -140,8 +179,8 @@ public function getSitesToDisplay() {
}

$site['url'] = str_replace(
['{email}', '{uid}', '{displayname}'],
array_map('urlencode', [$email, $uid, $displayName]),
['{email}', '{uid}', '{displayname}', '{jwt}'],
array_map('urlencode', [$email, $uid, $displayName, $jwt]),
$site['url']
);

Expand Down
1 change: 1 addition & 0 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<h2><?php p($l->t('External sites'));?></h2>
<p class="settings-hint"><?php p($l->t('Add a website directly to the app list in the top bar. This will be visible for all users and is useful to quickly reach other internally used web apps or important sites.')); ?></p>
<p class="settings-hint"><?php p($l->t('The placeholders {email}, {uid} and {displayname} can be used and are filled with the user´s values to customize the links.')); ?></p>
<p class="settings-hint"><?php p($l->t('A JSON Web Token containing user´s email, uid and display name in its payload can be embedded into the link using the {jwt} placeholder. The HS256 secret used for signing the JWT should be defined in the "external_jwt_secret" configuration parameter.')); ?></p>

<div id="loading_sites" class="icon-loading-small"></div>

Expand Down