Skip to content

Commit

Permalink
also support saving the kerberos tickets in database
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 21, 2023
1 parent b5abd91 commit 1491368
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
45 changes: 44 additions & 1 deletion .github/workflows/smb-kerberos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ jobs:
with:
repository: nextcloud/user_saml
path: apps/user_saml
ref: event-dispatcher
- name: Pull images
run: |
docker pull ghcr.io/icewind1991/samba-krb-test-dc
Expand All @@ -100,3 +99,47 @@ jobs:
FILEPATH=$(docker exec --user 33 apache ./occ log:file | grep "Log file:" | cut -d' ' -f3)
echo "$FILEPATH:"
docker exec --user 33 apache cat $FILEPATH
smb-kerberos-database-tests:
runs-on: ubuntu-latest

if: ${{ github.repository_owner != 'nextcloud-gmbh' }}

name: smb-kerberos-sso

steps:
- name: Checkout server
uses: actions/checkout@v3
with:
submodules: true
- name: Checkout user_saml
uses: actions/checkout@v3
with:
repository: nextcloud/user_saml
path: apps/user_saml
ref: event-dispatcher
- name: Pull images
run: |
docker pull ghcr.io/icewind1991/samba-krb-test-dc
docker pull ghcr.io/icewind1991/samba-krb-test-apache
docker pull ghcr.io/icewind1991/samba-krb-test-client
docker tag ghcr.io/icewind1991/samba-krb-test-dc icewind1991/samba-krb-test-dc
docker tag ghcr.io/icewind1991/samba-krb-test-apache icewind1991/samba-krb-test-apache
docker tag ghcr.io/icewind1991/samba-krb-test-client icewind1991/samba-krb-test-client
- name: Setup AD-DC
run: |
DC_IP=$(apps/files_external/tests/sso-setup/start-dc.sh)
apps/files_external/tests/sso-setup/start-apache.sh $DC_IP $PWD -v $PWD/apps/files_external/tests/sso-setup/apache-session.conf:/etc/apache2/sites-enabled/000-default.conf
echo "DC_IP=$DC_IP" >> $GITHUB_ENV
- name: Set up Nextcloud
run: |
apps/files_external/tests/sso-setup/setup-sso-nc.sh smb::kerberos_sso_database
- name: Test SSO
run: |
apps/files_external/tests/sso-setup/test-sso-smb-session.sh ${{ env.DC_IP }}
- name: Show logs
if: failure()
run: |
FILEPATH=$(docker exec --user 33 apache ./occ log:file | grep "Log file:" | cut -d' ' -f3)
echo "$FILEPATH:"
docker exec --user 33 apache cat $FILEPATH
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSAPrivateKey' => $baseDir . '/../lib/Lib/Auth/PublicKey/RSAPrivateKey.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosApacheAuth' => $baseDir . '/../lib/Lib/Auth/SMB/KerberosApacheAuth.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosAuth' => $baseDir . '/../lib/Lib/Auth/SMB/KerberosAuth.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosSsoDatabase' => $baseDir . '/../lib/Lib/Auth/SMB/KerberosSsoDatabase.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosSsoSession' => $baseDir . '/../lib/Lib/Auth/SMB/KerberosSsoSession.php',
'OCA\\Files_External\\Lib\\Backend\\AmazonS3' => $baseDir . '/../lib/Lib/Backend/AmazonS3.php',
'OCA\\Files_External\\Lib\\Backend\\Backend' => $baseDir . '/../lib/Lib/Backend/Backend.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files_external/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ComposerStaticInitFiles_External
'OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSAPrivateKey' => __DIR__ . '/..' . '/../lib/Lib/Auth/PublicKey/RSAPrivateKey.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosApacheAuth' => __DIR__ . '/..' . '/../lib/Lib/Auth/SMB/KerberosApacheAuth.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosAuth' => __DIR__ . '/..' . '/../lib/Lib/Auth/SMB/KerberosAuth.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosSsoDatabase' => __DIR__ . '/..' . '/../lib/Lib/Auth/SMB/KerberosSsoDatabase.php',
'OCA\\Files_External\\Lib\\Auth\\SMB\\KerberosSsoSession' => __DIR__ . '/..' . '/../lib/Lib/Auth/SMB/KerberosSsoSession.php',
'OCA\\Files_External\\Lib\\Backend\\AmazonS3' => __DIR__ . '/..' . '/../lib/Lib/Backend/AmazonS3.php',
'OCA\\Files_External\\Lib\\Backend\\Backend' => __DIR__ . '/..' . '/../lib/Lib/Backend/Backend.php',
Expand Down
73 changes: 73 additions & 0 deletions apps/files_external/lib/Lib/Auth/SMB/KerberosSsoDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @author Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Files_External\Lib\Auth\SMB;

use Icewind\SMB\KerberosTicket;
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\Security\ICredentialsManager;

class KerberosSsoDatabase extends AuthMechanism {

Check notice

Code scanning / Psalm

PropertyNotSetInConstructor Note

Property OCA\Files_External\Lib\Auth\SMB\KerberosSsoDatabase::$scheme is not defined in constructor of OCA\Files_External\Lib\Auth\SMB\KerberosSsoDatabase or in any private or final methods called in the constructor
private ICredentialsManager $credentialsManager;

public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
$realm = new DefinitionParameter('default_realm', 'Default realm');
$realm
->setType(DefinitionParameter::VALUE_TEXT)
->setFlag(DefinitionParameter::FLAG_OPTIONAL)
->setTooltip($l->t('Kerberos default realm, defaults to "WORKGROUP"'));
$this
->setIdentifier('smb::kerberos_sso_database')
->setScheme(self::SCHEME_SMB)
->setText($l->t('Kerberos ticket SSO, save in database'))
->addParameter($realm);
$this->credentialsManager = $credentialsManager;
}

public function getTicket(?IUser $user): KerberosTicket {
if (!isset($user)) {
throw new InsufficientDataForMeaningfulAnswerException('No kerberos ticket saved');
}
try {
$envTicket = KerberosTicket::fromEnv();
} catch (\Exception $e) {
$envTicket = null;
}
if ($envTicket) {
$this->credentialsManager->store($user->getUID(), 'kerberos_ticket', base64_encode($envTicket->save()));
return $envTicket;
}

$savedTicket = $this->credentialsManager->retrieve($user->getUID(), 'kerberos_ticket');
if (!$savedTicket) {
throw new InsufficientDataForMeaningfulAnswerException('No kerberos ticket saved');
}
return KerberosTicket::load(base64_decode($savedTicket));
}
}
8 changes: 8 additions & 0 deletions apps/files_external/lib/Lib/Backend/SMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\Auth\Password\Password;
use OCA\Files_External\Lib\Auth\SMB\KerberosApacheAuth as KerberosApacheAuthMechanism;
use OCA\Files_External\Lib\Auth\SMB\KerberosSsoDatabase;
use OCA\Files_External\Lib\Auth\SMB\KerberosSsoSession;
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
Expand Down Expand Up @@ -91,6 +92,13 @@ public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = n
case 'smb::kerberos':
$smbAuth = new KerberosAuth();
break;
case 'smb::kerberos_sso_database':
if (!$auth instanceof KerberosSsoDatabase) {
throw new \InvalidArgumentException('invalid authentication backend');
}
$smbAuth = new KerberosAuth();
$smbAuth->setTicket($auth->getTicket($user));
break;
case 'smb::kerberos_sso_session':
if (!$auth instanceof KerberosSsoSession) {
throw new \InvalidArgumentException('invalid authentication backend');
Expand Down
40 changes: 30 additions & 10 deletions apps/files_external/lib/Lib/TicketSaveMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,67 @@

use Icewind\SMB\KerberosTicket;
use OCA\Files_External\Controller\UserGlobalStoragesController;
use OCA\Files_External\Lib\Auth\SMB\KerberosSsoDatabase;
use OCA\Files_External\Lib\Auth\SMB\KerberosSsoSession;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\ICredentialsManager;

class TicketSaveMiddleware extends Middleware {
const SAVE_SESSION = 1;
const SAVE_DB = 2;

private ISession $session;
private IUserSession $userSession;
private UserGlobalStoragesService $storagesService;
private ICredentialsManager $credentialsManager;

public function __construct(
ISession $session,
ICredentialsManager $credentialsManager,
IUserSession $userSession,
UserGlobalStoragesService $storagesService
) {
$this->session = $session;
$this->credentialsManager = $credentialsManager;
$this->userSession = $userSession;
$this->storagesService = $storagesService;
}

public function afterController($controller, $methodName, Response $response) {
$user = $this->userSession->getUser();
if (!$user) {
return $response;
}
$ticket = KerberosTicket::fromEnv();
if ($ticket && $ticket->isValid() && $this->needToSaveTicket()) {
$this->session->set('kerberos_ticket', base64_encode($ticket->save()));
if ($ticket && $ticket->isValid()) {
$save = $this->needToSaveTicket($user);
if ($save & self::SAVE_SESSION) {
$this->session->set('kerberos_ticket', base64_encode($ticket->save()));
}
if ($save & self::SAVE_DB) {
$this->credentialsManager->store($user->getUID(), 'kerberos_ticket', base64_encode($ticket->save()));
}
}
return $response;
}

private function needToSaveTicket(): bool {
$user = $this->userSession->getUser();
if (!$user) {
return false;
}
private function needToSaveTicket(IUser $user): int {
$save = 0;
$storages = $this->storagesService->getAllStoragesForUser($user);
foreach ($storages as $storage) {
if ($storage->getAuthMechanism() instanceof KerberosSsoSession) {
return true;
$auth = $storage->getAuthMechanism();
if ($auth instanceof KerberosSsoSession) {
$save = $save | self::SAVE_SESSION;
}
if ($auth instanceof KerberosSsoDatabase) {
$save = $save | self::SAVE_DB;
}
}
return false;
return $save;
}
}

0 comments on commit 1491368

Please sign in to comment.