Completed
Push — master ( a8d9bf...b4cf55 )
by Lukas
23:32 queued 12:39
created

SessionStorage::setSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\Security\CSRF\TokenStorage;
24
25
use OCP\ISession;
26
27
/**
28
 * Class SessionStorage provides the session storage
29
 *
30
 * @package OC\Security\CSRF\TokenStorage
31
 */
32
class SessionStorage {
33
	/** @var ISession */
34
	private $session;
35
36
	/**
37
	 * @param ISession $session
38
	 */
39
	public function __construct(ISession $session) {
40
		$this->session = $session;
41
	}
42
43
	/**
44
	 * @param ISession $session
45
	 */
46
	public function setSession(ISession $session) {
47
		$this->session = $session;
48
	}
49
50
	/**
51
	 * Returns the current token or throws an exception if none is found.
52
	 *
53
	 * @return string
54
	 * @throws \Exception
55
	 */
56
	public function getToken() {
57
		$token = $this->session->get('requesttoken');
58
		if(empty($token)) {
59
			throw new \Exception('Session does not contain a requesttoken');
60
		}
61
62
		return $token;
63
	}
64
65
	/**
66
	 * Set the valid current token to $value.
67
	 *
68
	 * @param string $value
69
	 */
70
	public function setToken($value) {
71
		$this->session->set('requesttoken', $value);
72
	}
73
74
	/**
75
	 * Removes the current token.
76
	 */
77
	public function removeToken() {
78
		$this->session->remove('requesttoken');
79
	}
80
	/**
81
	 * Whether the storage has a storage.
82
	 *
83
	 * @return bool
84
	 */
85
	public function hasToken() {
86
		return $this->session->exists('requesttoken');
87
	}
88
}
89