This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
AuthFrontend.php
159 lines (128 loc) · 4.56 KB
/
AuthFrontend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
namespace Pydio\Core\Http\Wopi;
use JWT;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ApiKeysService;
use Pydio\Auth\Frontend\Core\AbstractAuthFrontend;
use Pydio\Conf\Sql\SqlConfDriver;
use Zend\Diactoros\UploadedFile;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Class Pydio\Core\Http\Wopi\WopiJWTAuthFrontend;
*/
class AuthFrontend extends AbstractAuthFrontend
{
const NOT_FOUND = "";
/**
* @var SqlConfDriver $storage
*/
var $storage;
/**
* @param $httpVars
* @param $varName
* @return string
*/
function detectVar(&$httpVars, $varName)
{
if (isSet($httpVars[$varName])) return $httpVars[$varName];
if (isSet($_SERVER["HTTP_PYDIO_" . strtoupper($varName)])) return $_SERVER["HTTP_" . strtoupper($varName)];
return "";
}
function retrieveParams(ServerRequestInterface &$request, ResponseInterface &$response) {
/** @var ContextInterface $context */
$action = $request->getAttribute("action");
$httpVars = $request->getParsedBody();
$jwt = $this->detectVar($httpVars, "access_token");
if (empty($jwt)) {
return false;
}
// We have an access token - decode
$payload = JWT::decode($jwt);
$httpVars["auth_token"] = $payload->token;
$httpVars["auth_hash"] = $payload->hash;
// NOT GREAT - WE REMOVE /contents from the uri to ensure that the auth_hash works fine
$uri = $request->getUri();
$path = str_replace("/contents", "", $uri->getPath());
$uri = $uri->withPath($path);
$_SERVER["REQUEST_URI"] = $uri->getPath() . '?' . $uri->getQuery();
// Handle upload case
if ($action == "upload") {
$stream = $request->getBody();
$uploadedFile = new UploadedFile(
$stream,
(int)$request->getHeader("content-length"),
0,
basename($path)
);
$request = $request->withUploadedFiles(["userfile_0" => $uploadedFile]);
}
$request = $request
->withUri($uri)
->withParsedBody($httpVars);
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param bool $isLast
* @return bool
*/
function tryToLogUser(ServerRequestInterface &$request, ResponseInterface &$response, $isLast = false) {
// This plugin is depending on other authfront having found the current user
/** @var ContextInterface $context */
$context = $request->getAttribute("ctx");
if (!$context->hasUser()) {
return false;
}
$currentUser = $context->getUser();
$httpVars = $request->getParsedBody();
$jwt = $this->detectVar($httpVars, "access_token");
if (empty($jwt)) {
return false;
}
// We have an access token - decode
$payload = JWT::decode($jwt);
if (!isset($payload->token) || !isset($payload->task)) {
return false;
}
// We have a token - retrieve private signature
$token = $payload->token;
$task = $payload->task;
$key = ApiKeysService::findPairForAdminTask($task, $currentUser->getId());
if ($key["t"] !== $token) {
return false;
}
$signature = $key["p"];
if ($signature == self::NOT_FOUND) {
return false;
}
// We have a signature - verify the payload
try {
JWT::decode($jwt, $signature, ['HS256']);
} catch (\Exception $e) {
return false;
}
// We're through
return true;
}
}