Tiqr_UserSecretStorage_File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 4
crap 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 Ivo Jansch <[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
/**
23
 * This user storage implementation implements a simple user's secret storage using json files.
24
 * This is mostly for demonstration and development purposes. In a production environment
25
 * please supply your own implementation that hosts the data in your user database OR
26
 * in a secure (e.g. hardware encrypted) storage.
27
 * @author ivo
28
 *
29
 * @see Tiqr_UserSecretStorage::getSecretStorage()
30
 * @see Tiqr_UserSecretStorage_Interface
31
 *
32
 * Supported options:
33
 * path : Path to the directory where the user data is stored
34
 *
35
36
 */
37
class Tiqr_UserSecretStorage_File  extends Tiqr_UserSecretStorage_Abstract
38
{
39
    use FileTrait;
40
41
    private $path;
42
43 10
    public function __construct(
44
        Tiqr_UserSecretStorage_Encryption_Interface $encryption,
45
        string $path,
46
        LoggerInterface $logger,
47
        array $decryption = array()
48
    ) {
49 10
        parent::__construct($logger, $encryption, $decryption);
50
51
        // See FileTrait
52 10
        $this->path = $path;
53
    }
54
55
    /**
56
     * Get the user's secret
57
     *
58
     * @param String $userId
59
     *
60
     * @return String The user's secret
61
     * @throws Exception
62
     */
63 3
    protected function getUserSecret(string $userId): string
64
    {
65 3
        if ($data = $this->_loadUser($userId)) {
66 3
            if (isset($data["secret"])) {
67 3
                return $data["secret"];
68
            }
69
        }
70
        $this->logger->error(sprintf('User or user secret not found in secret storage (file) for user "%s"', $userId));
71
        throw new RuntimeException('User or user secret not found in secret storage (File)');
72
    }
73
74
    /**
75
     * Store a secret for a user
76
     *
77
     * @param String $userId
78
     * @param String $secret
79
     * @throws Exception
80
     */
81 3
    protected function setUserSecret(string $userId, string $secret): void
82
    {
83 3
        $data=array();
84 3
        if ($this->_userExists($userId)) {
85 1
            $data = $this->_loadUser($userId);
86
        }
87 3
        $data["secret"] = $secret;
88 3
        $this->_saveUser($userId, $data);
89
    }
90
}
91