Skip to content

Commit

Permalink
migrate 'password::sessioncredentials' extern storage auth to credent…
Browse files Browse the repository at this point in the history
…ial store

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Jan 2, 2017
1 parent 113fb2e commit f21c2db
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions apps/files_external/lib/Lib/Auth/Password/SessionCredentials.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand All @@ -23,16 +24,17 @@

namespace OCA\Files_External\Lib\Auth\Password;

use \OCP\IUser;
use \OCP\IL10N;
use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Lib\StorageConfig;
use \OCP\ISession;
use \OCP\Security\ICrypto;
use \OCP\Files\Storage;
use \OCA\Files_External\Lib\SessionStorageWrapper;
use \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\SessionStorageWrapper;
use OCA\Files_External\Lib\StorageConfig;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
use OCP\Files\Storage;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\Security\ICrypto;

/**
* Username and password from login credentials, saved in session
Expand All @@ -45,39 +47,29 @@ class SessionCredentials extends AuthMechanism {
/** @var ICrypto */
protected $crypto;

public function __construct(IL10N $l, ISession $session, ICrypto $crypto) {
/** @var CredentialsStore */
private $credentialsStore;

public function __construct(IL10N $l, ISession $session, ICrypto $crypto, CredentialsStore $credentialsStore) {
$this->session = $session;
$this->crypto = $crypto;
$this->credentialsStore = $credentialsStore;

$this
->setIdentifier('password::sessioncredentials')
$this->setIdentifier('password::sessioncredentials')
->setScheme(self::SCHEME_PASSWORD)
->setText($l->t('Log-in credentials, save in session'))
->addParameters([
])
;

\OCP\Util::connectHook('OC_User', 'post_login', $this, 'authenticate');
}

/**
* Hook listener on post login
*
* @param array $params
*/
public function authenticate(array $params) {
$this->session->set('password::sessioncredentials/credentials', $this->crypto->encrypt(json_encode($params)));
->addParameters([]);
}

public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
$encrypted = $this->session->get('password::sessioncredentials/credentials');
if (!isset($encrypted)) {
try {
$credentials = $this->credentialsStore->getLoginCredentials();
} catch (CredentialsUnavailableException $e) {
throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
}

$credentials = json_decode($this->crypto->decrypt($encrypted), true);
$storage->setBackendOption('user', $this->session->get('loginname'));
$storage->setBackendOption('password', $credentials['password']);
$storage->setBackendOption('user', $credentials->getLoginName());
$storage->setBackendOption('password', $credentials->getPassword());
}

public function wrapStorage(Storage $storage) {
Expand Down

0 comments on commit f21c2db

Please sign in to comment.