Completed
Push — master ( a82218...06e071 )
by Thomas
11:03
created

TokenAuthModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
23
namespace OC\User;
24
25
26
use OC\Authentication\Exceptions\InvalidTokenException;
27
use OC\Authentication\Exceptions\PasswordlessTokenException;
28
use OC\Authentication\Token\IProvider;
29
use OCP\Authentication\IAuthModule;
30
use OCP\IRequest;
31
use OCP\ISession;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\Session\Exceptions\SessionNotAvailableException;
35
36
class TokenAuthModule implements IAuthModule {
37
38
	/** @var ISession */
39
	private $session;
40
41
	/** @var IProvider */
42
	private $tokenProvider;
43
44
	/** @var IUserManager */
45
	private $manager;
46
47
	/** @var string */
48
	private $password = '';
49
50
	public function __construct(ISession $session, IProvider $tokenProvider, IUserManager $manager) {
51
		$this->session = $session;
52
		$this->tokenProvider = $tokenProvider;
53
		$this->manager = $manager;
54
	}
55
56
	/**
57
	 * @inheritdoc
58
	 */
59
	public function auth(IRequest $request) {
60
		$authHeader = $request->getHeader('Authorization');
61 View Code Duplication
		if ($authHeader === null || strpos($authHeader, 'token ') === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
			// No auth header, let's try session id
63
			try {
64
				$token = $this->session->getId();
65
			} catch (SessionNotAvailableException $ex) {
66
				return null;
67
			}
68
		} else {
69
			$token = substr($authHeader, 6);
70
		}
71
72
		try {
73
			$dbToken = $this->tokenProvider->getToken($token);
74
		} catch (InvalidTokenException $ex) {
75
			return null;
76
		}
77
		$uid = $dbToken->getUID();
78
79
		// When logging in with token, the password must be decrypted first before passing to login hook
80
		try {
81
			$this->password = $this->tokenProvider->getPassword($dbToken, $token);
82
		} catch (PasswordlessTokenException $ex) {
83
			// Ignore and use empty string instead
84
		}
85
86
		return $this->manager->get($uid);
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	public function getUserPassword(IRequest $request) {
93
		return $this->password;
94
	}
95
}
96