Passed
Pull Request — develop (#27)
by Michiel
06:19
created

Tiqr_UserStorage_Abstract   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 20
c 2
b 0
f 0
dl 0
loc 82
ccs 22
cts 25
cp 0.88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdditionalAttributes() 0 3 1
A getSecret() 0 4 1
A _setEncryptedSecret() 0 3 1
A __construct() 0 11 4
A setSecret() 0 4 1
A _getEncryption() 0 3 1
A _getEncryptedSecret() 0 3 1
1
<?php 
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Peter Verhage <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2012 SURFnet BV
18
 */
19
20
use Psr\Log\LoggerInterface;
21
22 1
require_once 'Tiqr/UserStorage/Interface.php';
23 1
require_once 'Tiqr/UserStorage/Encryption.php';
24 1
require_once 'Tiqr/UserSecretStorage.php';
25
26
/**
27
 * Abstract base implementation for user storage. 
28
 *
29
 * Adds built-in support for encryption of the user secret.
30
 * 
31
 * @author peter
32
 */
33
abstract class Tiqr_UserStorage_Abstract implements Tiqr_UserStorage_Interface
34
{
35
    protected $_encryption;
36
37
    protected $_userSecretStorage;
38
39
    protected $logger;
40
41 3
    public function __construct($config, LoggerInterface $logger, $secretconfig = array())
42
    {
43 3
        $this->logger = $logger;
44 3
        $type = isset($config['encryption']['type']) ? $config['encryption']['type'] : 'dummy';
45 3
        $options = isset($config['encryption']) ? $config['encryption'] : array();
46 3
        $this->_encryption = Tiqr_UserStorage_Encryption::getEncryption($logger, $type, $options);
47
48 3
        if (count($secretconfig)) {
49 3
            $this->_userSecretStorage = Tiqr_UserSecretStorage::getSecretStorage($secretconfig['type'], $logger, $secretconfig);
50
        } else {
51
            $this->_userSecretStorage = Tiqr_UserSecretStorage::getSecretStorage($config['type'], $logger, $config);
52
        }
53 3
    }
54
55
    /**
56
     * Returns the encryption instance.
57
     */
58 3
    protected function _getEncryption()
59
    {
60 3
        return $this->_encryption;
61
    }
62
63
    /**
64
     * Get the user's secret
65
     * @param String $userId
66
     * @return String The user's secret
67
     */
68 3
    protected function _getEncryptedSecret($userId)
69
    {
70 3
        return $this->_userSecretStorage->getUserSecret($userId);
71
    }
72
73
    /**
74
     * Store a secret for a user.
75
     * @param String $userId
76
     * @param String $secret
77
     */
78 3
    protected function _setEncryptedSecret($userId, $secret)
79
    {
80 3
        $this->_userSecretStorage->setUserSecret($userId, $secret);
81 3
    }
82
83
    /**
84
     * Get the user's secret
85
     * @param String $userId
86
     * @return String The user's secret
87
     */
88 3
    public final function getSecret($userId)
89
    {
90 3
        $encryptedSecret = $this->_getEncryptedSecret($userId);
91 3
        return $this->_getEncryption()->decrypt($encryptedSecret);
92
    }
93
94
    /**
95
     * Store a secret for a user.
96
     * @param String $userId
97
     * @param String $secret
98
     */
99 3
    public final function setSecret($userId, $secret)
100
    {
101 3
        $encryptedSecret = $this->_getEncryption()->encrypt($secret);
102 3
        $this->_setEncryptedSecret($userId, $encryptedSecret);
103 3
    }
104
105
    /**
106
     * Returns additional attributes for the user.
107
     *
108
     * @param string $userId User identifier.
109
     *
110
     * @return array additional user attributes
111
     */
112
    public function getAdditionalAttributes($userId) 
113
    {
114
        return array();
115
    }
116
}
117