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 Patrick Honing <[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
|
|
|
* Create SQL table (MySQL): |
20
|
|
|
* CREATE TABLE `tiqrusersecret` (`userid` varchar(10) PRIMARY KEY, `secret` varchar(100)) |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
1 |
|
require_once 'Tiqr/UserStorage/Pdo.php'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This user storage implementation implements a user secret storage using PDO. |
28
|
|
|
* It is usable for any database with a PDO driver |
29
|
|
|
* |
30
|
|
|
* @author Patrick Honing <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Tiqr_UserSecretStorage_Pdo extends Tiqr_UserStorage_Pdo implements Tiqr_UserSecretStorage_Interface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Construct a user class |
36
|
|
|
* |
37
|
|
|
* @param array $config The configuration that a specific user class may use. |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct($config, $secretconfig = array()) |
40
|
|
|
{ |
41
|
2 |
|
$this->tablename = isset($config['table']) ? $config['table'] : 'tiqrusersecret'; |
42
|
|
|
try { |
43
|
2 |
|
$this->handle = new PDO($config['dsn'],$config['username'],$config['password']); |
44
|
|
|
} catch (PDOException $e) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the user's secret |
51
|
|
|
* |
52
|
|
|
* @param String $userId |
53
|
|
|
* |
54
|
|
|
* @return String The user's secret |
55
|
|
|
*/ |
56
|
2 |
|
public function getUserSecret($userId) |
57
|
|
|
{ |
58
|
2 |
|
$sth = $this->handle->prepare("SELECT secret FROM ".$this->tablename." WHERE userid = ?"); |
59
|
2 |
|
$sth->execute(array($userId)); |
60
|
2 |
|
return $sth->fetchColumn(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Store a secret for a user. |
65
|
|
|
* |
66
|
|
|
* @param String $userId |
67
|
|
|
* @param String $secret |
68
|
|
|
*/ |
69
|
2 |
|
public function setUserSecret($userId, $secret) |
70
|
|
|
{ |
71
|
2 |
|
if ($this->userExists($userId)) { |
72
|
1 |
|
$sth = $this->handle->prepare("UPDATE ".$this->tablename." SET secret = ? WHERE userid = ?"); |
73
|
|
|
} else { |
74
|
1 |
|
$sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (secret,userid) VALUES (?,?)"); |
75
|
|
|
} |
76
|
2 |
|
$sth->execute(array($secret,$userId)); |
77
|
2 |
|
} |
78
|
|
|
} |
79
|
|
|
|