|
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
|
1 |
|
require_once 'Tiqr/UserStorage/Interface.php'; |
|
21
|
1 |
|
require_once 'Tiqr/UserStorage/Encryption.php'; |
|
22
|
1 |
|
require_once 'Tiqr/UserSecretStorage.php'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Abstract base implementation for user storage. |
|
26
|
|
|
* |
|
27
|
|
|
* Adds built-in support for encryption of the user secret. |
|
28
|
|
|
* |
|
29
|
|
|
* @author peter |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class Tiqr_UserStorage_Abstract implements Tiqr_UserStorage_Interface |
|
32
|
|
|
{ |
|
33
|
|
|
protected $_encryption; |
|
34
|
|
|
|
|
35
|
|
|
protected $_userSecretStorage; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct($config, $secretconfig = array()) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$type = isset($config['encryption']['type']) ? $config['encryption']['type'] : 'dummy'; |
|
43
|
1 |
|
$options = isset($config['encryption']) ? $config['encryption'] : array(); |
|
44
|
1 |
|
$this->_encryption = Tiqr_UserStorage_Encryption::getEncryption($type, $options); |
|
45
|
|
|
|
|
46
|
1 |
|
if (count($secretconfig)) { |
|
47
|
1 |
|
$this->_userSecretStorage = Tiqr_UserSecretStorage::getSecretStorage($secretconfig['type'], $secretconfig); |
|
48
|
|
|
} else { |
|
49
|
|
|
$this->_userSecretStorage = Tiqr_UserSecretStorage::getSecretStorage($config['type'], $config); |
|
50
|
|
|
} |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the encryption instance. |
|
55
|
|
|
*/ |
|
56
|
1 |
|
protected function _getEncryption() |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->_encryption; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the user's secret |
|
63
|
|
|
* @param String $userId |
|
64
|
|
|
* @return String The user's secret |
|
65
|
|
|
*/ |
|
66
|
1 |
|
protected function _getEncryptedSecret($userId) |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->_userSecretStorage->getUserSecret($userId); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Store a secret for a user. |
|
73
|
|
|
* @param String $userId |
|
74
|
|
|
* @param String $secret |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function _setEncryptedSecret($userId, $secret) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->_userSecretStorage->setUserSecret($userId, $secret); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the user's secret |
|
83
|
|
|
* @param String $userId |
|
84
|
|
|
* @return String The user's secret |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public final function getSecret($userId) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$encryptedSecret = $this->_getEncryptedSecret($userId); |
|
89
|
1 |
|
return $this->_getEncryption()->decrypt($encryptedSecret); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Store a secret for a user. |
|
94
|
|
|
* @param String $userId |
|
95
|
|
|
* @param String $secret |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public final function setSecret($userId, $secret) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$encryptedSecret = $this->_getEncryption()->encrypt($secret); |
|
100
|
1 |
|
$this->_setEncryptedSecret($userId, $encryptedSecret); |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns additional attributes for the user. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $userId User identifier. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array additional user attributes |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getAdditionalAttributes($userId) |
|
111
|
|
|
{ |
|
112
|
|
|
return array(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|